com.google.ipc.invalidation.external.client.types.ObjectId Java Examples

The following examples show how to use com.google.ipc.invalidation.external.client.types.ObjectId. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidListenerIntents.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Issues a registration retry with delay. */
static void issueDelayedRegistrationIntent(Context context, AndroidClock clock,
    ByteString clientId, ObjectId objectId, boolean isRegister, int delayMs, int requestCode) {
  RegistrationCommand command = isRegister ?
      AndroidListenerProtos.newDelayedRegisterCommand(clientId, objectId) :
          AndroidListenerProtos.newDelayedUnregisterCommand(clientId, objectId);
  Intent intent = new Intent()
      .putExtra(EXTRA_REGISTRATION, command.toByteArray())
      .setClass(context, AlarmReceiver.class);

  // Create a pending intent that will cause the AlarmManager to fire the above intent.
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent,
      PendingIntent.FLAG_ONE_SHOT);

  // Schedule the pending intent after the appropriate delay.
  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  long executeMs = clock.nowMs() + delayMs;
  alarmManager.set(AlarmManager.RTC, executeMs, pendingIntent);
}
 
Example #2
Source File: InvalidationPreferences.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Converts the given object id string stored in preferences to an object id.
 * Returns null if the string does not represent a valid object id.
 */
private ObjectId getObjectId(String objectIdString) {
    int separatorPos = objectIdString.indexOf(':');
    // Ensure that the separator is surrounded by at least one character on each side.
    if (separatorPos < 1 || separatorPos == objectIdString.length() - 1) {
        return null;
    }
    int objectSource;
    try {
        objectSource = Integer.parseInt(objectIdString.substring(0, separatorPos));
    } catch (NumberFormatException e) {
        return null;
    }
    byte[] objectName = objectIdString.substring(separatorPos + 1).getBytes();
    return ObjectId.newInstance(objectSource, objectName);
}
 
Example #3
Source File: ExampleListener.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void informRegistrationStatus(byte[] clientId, ObjectId objectId,
    RegistrationState regState) {
  Log.i(TAG, "informRegistrationStatus");

  List<ObjectId> objectIds = new ArrayList<ObjectId>();
  objectIds.add(objectId);
  if (regState == RegistrationState.REGISTERED) {
    if (!interestingObjects.contains(objectId)) {
      Log.i(TAG, "Unregistering for object we're no longer interested in");
      unregister(clientId, objectIds);
    }
  } else {
    if (interestingObjects.contains(objectId)) {
      Log.i(TAG, "Registering for an object");
      register(clientId, objectIds);
    }
  }
}
 
Example #4
Source File: InvalidationClientCore.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Handles a server {@code header}. */
private void handleIncomingHeader(ServerMessageHeader header) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  Preconditions.checkState(nonce == null,
      "Cannot process server header with non-null nonce (have %s): %s", nonce, header);
  if (header.registrationSummary != null) {
    // We've received a summary from the server, so if we were suppressing
    // registrations, we should now allow them to go to the registrar.
    shouldSendRegistrations = true;

    // Pass the registration summary to the registration manager. If we are now in agreement
    // with the server and we had any pending operations, we can tell the listener that those
    // operations have succeeded.
    Set<ProtoWrapper<RegistrationP>> upcalls =
        registrationManager.informServerRegistrationSummary(header.registrationSummary);
    logger.fine("Receivced new server registration summary (%s); will make %s upcalls",
        header.registrationSummary, upcalls.size());
    for (ProtoWrapper<RegistrationP> upcall : upcalls) {
      RegistrationP registration = upcall.getProto();
      ObjectId objectId = ProtoConverter.convertFromObjectIdProto(registration.getObjectId());
      RegistrationState regState = convertOpTypeToRegState(registration.getOpType());
      listener.informRegistrationStatus(this, objectId, regState);
    }
  }
}
 
Example #5
Source File: InvalidationIntentProtocol.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/** Returns the object ids for which to register contained in the intent. */
public static Set<ObjectId> getRegisteredObjectIds(Intent intent) {
    ArrayList<Integer> objectSources =
            intent.getIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
    ArrayList<String> objectNames =
            intent.getStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES);
    if (objectSources == null || objectNames == null
            || objectSources.size() != objectNames.size()) {
        return null;
    }
    Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.size());
    for (int i = 0; i < objectSources.size(); i++) {
        objectIds.add(
                ObjectId.newInstance(objectSources.get(i), objectNames.get(i).getBytes()));
    }
    return objectIds;
}
 
Example #6
Source File: InvalidationService.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void informRegistrationStatus(
        byte[] clientId, ObjectId objectId, RegistrationState regState) {
    Log.d(TAG, "Registration status for " + objectId + ": " + regState);
    List<ObjectId> objectIdAsList = CollectionUtil.newArrayList(objectId);
    boolean registrationisDesired = readRegistrationsFromPrefs().contains(objectId);
    if (regState == RegistrationState.REGISTERED) {
      if (!registrationisDesired) {
        Log.i(TAG, "Unregistering for object we're no longer interested in");
        unregister(clientId, objectIdAsList);
      }
    } else {
      if (registrationisDesired) {
        Log.i(TAG, "Registering for an object");
        register(clientId, objectIdAsList);
      }
    }
}
 
Example #7
Source File: InvalidationClientService.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void informRegistrationFailure(
        byte[] clientId, ObjectId objectId, boolean isTransient, String errorMessage) {
    Log.w(TAG, "Registration failure on " + objectId + " ; transient = " + isTransient
            + ": " + errorMessage);
    if (isTransient) {
      // Retry immediately on transient failures. The base AndroidListener will handle
      // exponential backoff if there are repeated failures.
        List<ObjectId> objectIdAsList = CollectionUtil.newArrayList(objectId);
        if (readRegistrationsFromPrefs().contains(objectId)) {
            register(clientId, objectIdAsList);
        } else {
            unregister(clientId, objectIdAsList);
        }
    }
}
 
Example #8
Source File: InvalidationClientService.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void informRegistrationStatus(
        byte[] clientId, ObjectId objectId, RegistrationState regState) {
    Log.d(TAG, "Registration status for " + objectId + ": " + regState);
    List<ObjectId> objectIdAsList = CollectionUtil.newArrayList(objectId);
    boolean registrationisDesired = readRegistrationsFromPrefs().contains(objectId);
    if (regState == RegistrationState.REGISTERED) {
        if (!registrationisDesired) {
            Log.i(TAG, "Unregistering for object we're no longer interested in");
            unregister(clientId, objectIdAsList);
        }
    } else {
        if (registrationisDesired) {
            Log.i(TAG, "Registering for an object");
            register(clientId, objectIdAsList);
        }
    }
}
 
Example #9
Source File: AndroidListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** Handles a registration command for this client. */
private void handleRegistrationCommand(RegistrationCommand command) {
  // Make sure the registration is intended for this client. If not, we ignore it (suggests
  // there is a new client now).
  if (!command.getClientId().equals(state.getClientId())) {
    logger.warning("Ignoring registration request for old client. Old ID = %s, New ID = %s",
        command.getClientId(), state.getClientId());
    return;
  }
  boolean isRegister = command.getIsRegister();
  for (ObjectIdP objectIdP : command.getObjectId()) {
    ObjectId objectId = ProtoWrapperConverter.convertFromObjectIdProto(objectIdP);
    // We may need to delay the registration command (if it is not already delayed).
    int delayMs = 0;
    if (!command.getIsDelayed()) {
      delayMs = state.getNextDelay(objectId);
    }
    if (delayMs == 0) {
      issueRegistration(objectId, isRegister);
    } else {
      // Add a scheduled registration retry to listener state. An alarm will be triggered at the
      // end of the onHandleIntent method if needed.
      long executeMs = clock.nowMs() + delayMs;
      state.addScheduledRegistrationRetry(objectId, isRegister, executeMs);
    }
  }
}
 
Example #10
Source File: AndroidListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * See specs for {@link InvalidationClient#register}.
 *
 * @param context the context
 * @param clientId identifier for the client service for which we are registering
 * @param objectIds the object ids being registered
 */
public static Intent createRegisterIntent(Context context, byte[] clientId,
    Iterable<ObjectId> objectIds) {
  Preconditions.checkNotNull(context);
  Preconditions.checkNotNull(clientId);
  Preconditions.checkNotNull(objectIds);

  final boolean isRegister = true;
  return AndroidListenerIntents.createRegistrationIntent(context, clientId, objectIds,
      isRegister);
}
 
Example #11
Source File: InvalidationTestListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void informRegistrationFailure(
    InvalidationClient client, ObjectId objectId, boolean isTransient, String errorMessage) {
  InvalidationListener listener = getListener(client);
  logger.fine("Received INFORM_REGISTRATION_FAILURE for %s: %s", getClientKey(client), listener);
  if (listener != null) {
    listener.informRegistrationFailure(client, objectId, isTransient, errorMessage);
  }
}
 
Example #12
Source File: ProtoConverter.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Converts an invalidation protocol buffer {@code invalidation} to the
 * corresponding external object and returns it
 */
public static Invalidation convertFromInvalidationProto(InvalidationP invalidation) {
  Preconditions.checkNotNull(invalidation);
  ObjectId objectId = convertFromObjectIdProto(invalidation.getObjectId());

  // No bridge arrival time in invalidation.
  return Invalidation.newInstance(objectId, invalidation.getVersion(),
      invalidation.hasPayload() ? invalidation.getPayload().toByteArray() : null,
          invalidation.getIsTrickleRestart());
}
 
Example #13
Source File: Request.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Returns the object ID set on the message or {@code null} if not set */
public Collection<ObjectId> getObjectIds() {
  List<ParcelableObjectId> parcelableObjectIds =
      parameters.getParcelableArrayList(Parameter.OBJECT_ID_LIST);
  if (parcelableObjectIds == null) {
    return null;
  }
  List<ObjectId> objectIds = new ArrayList<ObjectId>(parcelableObjectIds.size());
  for (ParcelableObjectId parcelableObjectId : parcelableObjectIds) {
    objectIds.add(parcelableObjectId.objectId);
  }
  return objectIds;
}
 
Example #14
Source File: AndroidListenerState.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** Initializes state for a new client. */
AndroidListenerState(int initialMaxDelayMs, int maxDelayFactor) {
  desiredRegistrations = new HashSet<ObjectId>();
  clientId = createGloballyUniqueClientId();
  // Assigning a client ID dirties the state because calling the constructor twice produces
  // different results.
  isDirty = true;
  requestCodeSeqNum = 0;
  this.initialMaxDelayMs = initialMaxDelayMs;
  this.maxDelayFactor = maxDelayFactor;
}
 
Example #15
Source File: InvalidationService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void reissueRegistrations(byte[] clientId) {
    Set<ObjectId> desiredRegistrations = readRegistrationsFromPrefs();
    if (!desiredRegistrations.isEmpty()) {
        register(clientId, desiredRegistrations);
    }
}
 
Example #16
Source File: AndroidInvalidationClientImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Unregisters to disable receipt of invalidations on an object.
 *
 * @param objectId object id.
 */
@Override
public void unregister(ObjectId objectId) {
  Request request =
      Request.newBuilder(Action.UNREGISTER).setClientKey(clientKey).setObjectId(objectId).build();
  executeServiceRequest(request);
}
 
Example #17
Source File: AndroidListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * See specs for {@link InvalidationClient#unregister}.
 *
 * @param context the context
 * @param clientId identifier for the client service for which we are unregistering
 * @param objectIds the object ids being unregistered
 */
public static Intent createUnregisterIntent(Context context, byte[] clientId,
    Iterable<ObjectId> objectIds) {
  Preconditions.checkNotNull(context);
  Preconditions.checkNotNull(clientId);
  Preconditions.checkNotNull(objectIds);

  final boolean isRegister = false;
  return AndroidListenerIntents.createRegistrationIntent(context, clientId, objectIds,
      isRegister);
}
 
Example #18
Source File: AndroidListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public final void informRegistrationFailure(final InvalidationClient client,
    final ObjectId objectId, final boolean isTransient, final String errorMessage) {
  state.informRegistrationFailure(objectId, isTransient);
  AndroidListener.this.informRegistrationFailure(state.getClientId().toByteArray(), objectId,
      isTransient, errorMessage);
}
 
Example #19
Source File: CheckingInvalidationListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void invalidateUnknownVersion(final InvalidationClient client, final ObjectId objectId,
    final AckHandle ackHandle) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  Preconditions.checkNotNull(ackHandle);
  listenerScheduler.schedule(NO_DELAY,
      new NamedRunnable("CheckingInvalListener.invalidateUnknownVersion") {
    @Override
    public void run() {
      statistics.recordListenerEvent(ListenerEventType.INVALIDATE_UNKNOWN);
      delegate.invalidateUnknownVersion(client, objectId, ackHandle);
    }
  });
}
 
Example #20
Source File: InvalidationClientImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void unregister(final Collection<ObjectId> objectIds) {
  getResources().getInternalScheduler().schedule(NO_DELAY, new Runnable() {
    @Override
    public void run() {
      InvalidationClientImpl.super.unregister(objectIds);
    }
  });
}
 
Example #21
Source File: AndroidInvalidationService.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void register(Request request, Response.Builder response) {
  String clientKey = request.getClientKey();
  AndroidInvalidationClient client = clientManager.get(clientKey);
  if (setResponseStatus(client, request, response)) {
    ObjectId objectId = request.getObjectId();
    client.register(objectId);
  }
}
 
Example #22
Source File: InvalidationTestListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void invalidateUnknownVersion(
    InvalidationClient client, ObjectId objectId, AckHandle ackHandle) {
  InvalidationListener listener = getListener(client);
  logger.fine("Received INVALIDATE_UNKNOWN_VERSION for %s: %s", getClientKey(client), listener);
  if (listener != null) {
    listener.invalidateUnknownVersion(client, objectId, ackHandle);
  }
}
 
Example #23
Source File: AndroidListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * See specs for {@link InvalidationClient#register}.
 *
 * @param clientId identifier for the client service for which we are registering
 * @param objectIds the object ids being registered
 */
public void register(byte[] clientId, Iterable<ObjectId> objectIds) {
  Preconditions.checkNotNull(clientId);
  Preconditions.checkNotNull(objectIds);

  Context context = getApplicationContext();
  context.startService(createRegisterIntent(context, clientId, objectIds));
}
 
Example #24
Source File: AndroidListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * See specs for {@link InvalidationClient#unregister}.
 *
 * @param clientId identifier for the client service for which we are registering
 * @param objectIds the object ids being unregistered
 */
public void unregister(byte[] clientId, Iterable<ObjectId> objectIds) {
  Preconditions.checkNotNull(clientId);
  Preconditions.checkNotNull(objectIds);

  Context context = getApplicationContext();
  try {
    context.startService(createUnregisterIntent(context, clientId, objectIds));
  } catch (IllegalStateException exception) {
    logger.info("Unable to deliver `unregister` intent: %s", exception);
  }
}
 
Example #25
Source File: AndroidListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * See specs for {@link InvalidationClient#register}.
 *
 * @param clientId identifier for the client service for which we are registering
 * @param objectIds the object ids being registered
 */
public void register(byte[] clientId, Iterable<ObjectId> objectIds) {
  Preconditions.checkNotNull(clientId);
  Preconditions.checkNotNull(objectIds);

  Context context = getApplicationContext();
  try {
    context.startService(createRegisterIntent(context, clientId, objectIds));
  } catch (IllegalStateException exception) {
    logger.info("Unable to deliver `register` intent: %s", exception);
  }
}
 
Example #26
Source File: AndroidListenerProtos.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Creates proto for retry registration state. */
static RetryRegistrationState newRetryRegistrationState(ObjectId objectId,
    TiclExponentialBackoffDelayGenerator delayGenerator) {
  return RetryRegistrationState.newBuilder()
      .setObjectId(ProtoConverter.convertToObjectIdProto(objectId))
      .setExponentialBackoffState(delayGenerator.marshal())
      .build();
}
 
Example #27
Source File: AndroidInvalidationClientImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void informRegistrationStatus(
    InvalidationClient client, ObjectId objectId, RegistrationState regState) {
  Intent intent = ListenerUpcalls.newRegistrationStatusIntent(
      ProtoConverter.convertToObjectIdProto(objectId),
      regState == RegistrationState.REGISTERED);
  issueIntent(context, intent);
}
 
Example #28
Source File: AndroidInvalidationClientImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void invalidateUnknownVersion(InvalidationClient client, ObjectId objectId,
    AckHandle ackHandle) {
  try {
    AckHandleP ackHandleP = AckHandleP.parseFrom(ackHandle.getHandleData());
    issueIntent(context, ListenerUpcalls.newInvalidateUnknownIntent(
        ProtoConverter.convertToObjectIdProto(objectId), ackHandleP));
  } catch (InvalidProtocolBufferException exception) {
    // Log and drop invalid call.
    logBadAckHandle("invalidateUnknownVersion", ackHandle);
  }
}
 
Example #29
Source File: AndroidClientProxy.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void invalidateUnknownVersion(
    InvalidationClient client, ObjectId objectId, AckHandle ackHandle) {
  Event event = Event.newBuilder(Event.Action.INVALIDATE_UNKNOWN)
      .setClientKey(clientKey).setObjectId(objectId).setAckHandle(ackHandle).build();
  sendEvent(event);
}
 
Example #30
Source File: AndroidListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public final void informRegistrationFailure(final InvalidationClient client,
    final ObjectId objectId, final boolean isTransient, final String errorMessage) {
  state.informRegistrationFailure(objectId, isTransient);
  AndroidListener.this.informRegistrationFailure(state.getClientId().getByteArray(), objectId,
      isTransient, errorMessage);
}