com.google.ipc.invalidation.util.Bytes Java Examples

The following examples show how to use com.google.ipc.invalidation.util.Bytes. 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: PersistenceUtils.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Deserializes a Ticl state blob. Returns either the parsed state or {@code null}
 * if it could not be parsed.
 */
public static PersistentTiclState deserializeState(Logger logger, byte[] stateBlobBytes,
    DigestFunction digestFn) {
  PersistentStateBlob stateBlob;
  try {
    // Try parsing the envelope protocol buffer.
    stateBlob = PersistentStateBlob.parseFrom(stateBlobBytes);
  } catch (ValidationException exception) {
    logger.severe("Failed deserializing Ticl state: %s", exception.getMessage());
    return null;
  }

  // Check the mac in the envelope against the recomputed mac from the state.
  PersistentTiclState ticlState = stateBlob.getTiclState();
  Bytes mac = generateMac(ticlState, digestFn);
  if (!TypedUtil.<Bytes>equals(mac, stateBlob.getAuthenticationCode())) {
    logger.warning("Ticl state failed MAC check: computed %s vs %s", mac,
        stateBlob.getAuthenticationCode());
    return null;
  }
  return ticlState;
}
 
Example #2
Source File: JavaClient.java    From 365browser with Apache License 2.0 6 votes vote down vote up
static InvalidationClientState fromMessageNano(com.google.protos.ipc.invalidation.NanoJavaClient.InvalidationClientState message) {
  if (message == null) { return null; }
  return new InvalidationClientState(com.google.ipc.invalidation.ticl.proto.Client.RunStateP.fromMessageNano(message.runState),
      Bytes.fromByteArray(message.clientToken),
      Bytes.fromByteArray(message.nonce),
      message.shouldSendRegistrations,
      message.lastMessageSendTimeMs,
      message.isOnline,
      com.google.ipc.invalidation.ticl.proto.JavaClient.ProtocolHandlerState.fromMessageNano(message.protocolHandlerState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.RegistrationManagerStateP.fromMessageNano(message.registrationManagerState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState.fromMessageNano(message.acquireTokenTaskState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState.fromMessageNano(message.regSyncHeartbeatTaskState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState.fromMessageNano(message.persistentWriteTaskState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState.fromMessageNano(message.heartbeatTaskState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState.fromMessageNano(message.batchingTaskState),
      com.google.ipc.invalidation.ticl.proto.Client.PersistentTiclState.fromMessageNano(message.lastWrittenState),
      com.google.ipc.invalidation.ticl.proto.JavaClient.StatisticsState.fromMessageNano(message.statisticsState));
}
 
Example #3
Source File: AndroidListenerProtocol.java    From 365browser with Apache License 2.0 6 votes vote down vote up
static AndroidListenerState fromMessageNano(com.google.protos.ipc.invalidation.NanoAndroidListenerProtocol.AndroidListenerState message) {
  if (message == null) { return null; }
  List<com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP> registration = new ArrayList<com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP>(message.registration.length);
  for (int i = 0; i < message.registration.length; i++) {
    registration.add(com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP.fromMessageNano(message.registration[i]));
  }
  List<com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.RetryRegistrationState> retryRegistrationState = new ArrayList<com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.RetryRegistrationState>(message.retryRegistrationState.length);
  for (int i = 0; i < message.retryRegistrationState.length; i++) {
    retryRegistrationState.add(com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.RetryRegistrationState.fromMessageNano(message.retryRegistrationState[i]));
  }
  List<com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.ScheduledRegistrationRetry> registrationRetry = new ArrayList<com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.ScheduledRegistrationRetry>(message.registrationRetry.length);
  for (int i = 0; i < message.registrationRetry.length; i++) {
    registrationRetry.add(com.google.ipc.invalidation.ticl.proto.AndroidListenerProtocol.AndroidListenerState.ScheduledRegistrationRetry.fromMessageNano(message.registrationRetry[i]));
  }
  return new AndroidListenerState(registration,
      retryRegistrationState,
      Bytes.fromByteArray(message.clientId),
      message.requestCodeSeqNum,
      registrationRetry);
}
 
Example #4
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private ServerHeader(com.google.ipc.invalidation.ticl.proto.ClientProtocol.ProtocolVersion protocolVersion,
    Bytes clientToken,
    com.google.ipc.invalidation.ticl.proto.ClientProtocol.RegistrationSummary registrationSummary,
    Long serverTimeMs,
    String messageId) throws ValidationArgumentException {
  int hazzerBits = 0;
  required("protocol_version", protocolVersion);
  this.protocolVersion = protocolVersion;
  required("client_token", clientToken);
  nonEmpty("client_token", clientToken);
  this.clientToken = clientToken;
  this.registrationSummary = registrationSummary;
  required("server_time_ms", serverTimeMs);
  nonNegative("server_time_ms", serverTimeMs);
  this.serverTimeMs = serverTimeMs;
  if (messageId != null) {
    hazzerBits |= 0x1;
    nonEmpty("message_id", messageId);
    this.messageId = messageId;
  } else {
    this.messageId = "";
  }
  this.__hazzerBits = hazzerBits;
}
 
Example #5
Source File: Client.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private PersistentStateBlob(com.google.ipc.invalidation.ticl.proto.Client.PersistentTiclState ticlState,
    Bytes authenticationCode) {
  int hazzerBits = 0;
  if (ticlState != null) {
    hazzerBits |= 0x1;
    this.ticlState = ticlState;
  } else {
    this.ticlState = com.google.ipc.invalidation.ticl.proto.Client.PersistentTiclState.DEFAULT_INSTANCE;
  }
  if (authenticationCode != null) {
    hazzerBits |= 0x2;
    this.authenticationCode = authenticationCode;
  } else {
    this.authenticationCode = Bytes.EMPTY_BYTES;
  }
  this.__hazzerBits = hazzerBits;
}
 
Example #6
Source File: ProtocolHandler.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a message to the server to request a client token.
 *
 * @param applicationClientId application-specific client id
 * @param nonce nonce for the request
 * @param debugString information to identify the caller
 */
void sendInitializeMessage(ApplicationClientIdP applicationClientId, Bytes nonce,
    BatchingTask batchingTask, String debugString) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  if (applicationClientId.getClientType() != clientType) {
    // This condition is not fatal, but it probably represents a bug somewhere if it occurs.
    logger.warning(
        "Client type in application id does not match constructor-provided type: %s vs %s",
        applicationClientId, clientType);
  }

  // Simply store the message in pendingInitializeMessage and send it when the batching task runs.
  InitializeMessage initializeMsg = InitializeMessage.create(clientType, nonce,
      applicationClientId, DigestSerializationType.BYTE_BASED);
  batcher.setInitializeMessage(initializeMsg);
  logger.info("Batching initialize message for client: %s, %s", debugString, initializeMsg);
  batchingTask.ensureScheduled(debugString);
}
 
Example #7
Source File: JavaClient.java    From 365browser with Apache License 2.0 6 votes vote down vote up
public static InvalidationClientState create(com.google.ipc.invalidation.ticl.proto.Client.RunStateP runState,
    Bytes clientToken,
    Bytes nonce,
    Boolean shouldSendRegistrations,
    Long lastMessageSendTimeMs,
    Boolean isOnline,
    com.google.ipc.invalidation.ticl.proto.JavaClient.ProtocolHandlerState protocolHandlerState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.RegistrationManagerStateP registrationManagerState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState acquireTokenTaskState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState regSyncHeartbeatTaskState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState persistentWriteTaskState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState heartbeatTaskState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.RecurringTaskState batchingTaskState,
    com.google.ipc.invalidation.ticl.proto.Client.PersistentTiclState lastWrittenState,
    com.google.ipc.invalidation.ticl.proto.JavaClient.StatisticsState statisticsState) {
  return new InvalidationClientState(runState, clientToken, nonce, shouldSendRegistrations, lastMessageSendTimeMs, isOnline, protocolHandlerState, registrationManagerState, acquireTokenTaskState, regSyncHeartbeatTaskState, persistentWriteTaskState, heartbeatTaskState, batchingTaskState, lastWrittenState, statisticsState);
}
 
Example #8
Source File: AndroidChannel.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private AddressedAndroidMessage(String clientKey,
    Bytes message) {
  int hazzerBits = 0;
  if (clientKey != null) {
    hazzerBits |= 0x1;
    this.clientKey = clientKey;
  } else {
    this.clientKey = "";
  }
  if (message != null) {
    hazzerBits |= 0x2;
    this.message = message;
  } else {
    this.message = Bytes.EMPTY_BYTES;
  }
  this.__hazzerBits = hazzerBits;
}
 
Example #9
Source File: ProtocolIntents.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public static Intent newInvalidateUnknownIntent(ObjectIdP object, AckHandleP ackHandle) {
  Intent intent = new Intent();
  InvalidateUpcall invUpcall =
      InvalidateUpcall.createWithInvalidateUnknown(new Bytes(ackHandle.toByteArray()), object);
  intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithInvalidate(
      ANDROID_PROTOCOL_VERSION_VALUE, invUpcall).toByteArray());
  return intent;
}
 
Example #10
Source File: AndroidService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private ReissueRegistrationsUpcall(Bytes prefix,
    Integer length) throws ValidationArgumentException {
  required("prefix", prefix);
  this.prefix = prefix;
  required("length", length);
  this.length = length;
}
 
Example #11
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void toCompactString(TextBuilder builder) {
  builder.append("Storage state: ");
  for (Map.Entry<String, byte[]> entry : ticlPersistentState.entrySet()) {
    builder.appendFormat("<%s, %s>, ", entry.getKey(), Bytes.toString(entry.getValue()));
  }
}
 
Example #12
Source File: AndroidListener.java    From 365browser with Apache License 2.0 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, Bytes.fromByteArray(clientId),
      objectIds, isRegister);
}
 
Example #13
Source File: AndroidService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private AndroidNetworkSendRequest(com.google.ipc.invalidation.ticl.proto.ClientProtocol.Version version,
    Bytes message) throws ValidationArgumentException {
  required("version", version);
  this.version = version;
  required("message", message);
  this.message = message;
}
 
Example #14
Source File: TiclStateManager.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link AndroidTiclStateWithDigest} containing {@code ticlState} and its computed
 * digest.
 */
private static AndroidTiclStateWithDigest createDigestedState(
    AndroidInvalidationClientImpl ticl) {
  Sha1DigestFunction digester = new Sha1DigestFunction();
  ApplicationClientIdP ticlAppId = ticl.getApplicationClientIdP();
  Metadata metadata = Metadata.create(ticlAppId.getClientType(), ticlAppId.getClientName(),
      ticl.getSchedulingId(), ticl.getConfig());
  AndroidTiclState state = AndroidTiclState.create(ProtocolIntents.ANDROID_PROTOCOL_VERSION_VALUE,
      ticl.marshal(), metadata,
      ((AndroidInternalScheduler) ticl.getResources().getInternalScheduler()).marshal());
  digester.update(state.toByteArray());
  AndroidTiclStateWithDigest verifiedState =
      AndroidTiclStateWithDigest.create(state, new Bytes(digester.getDigest()));
  return verifiedState;
}
 
Example #15
Source File: InvalidationClientCore.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override  // InvalidationClient
public void acknowledge(final AckHandle acknowledgeHandle) {
  Preconditions.checkNotNull(acknowledgeHandle);
  Preconditions.checkState(internalScheduler.isRunningOnThread(),
      "Not running on internal thread");

  // Parse and validate the ack handle first.
  AckHandleP ackHandle;
  try {
    ackHandle = AckHandleP.parseFrom(acknowledgeHandle.getHandleData());
  } catch (ValidationException exception) {
    logger.warning("Bad ack handle : %s",
        Bytes.toLazyCompactString(acknowledgeHandle.getHandleData()));
    statistics.recordError(ClientErrorType.ACKNOWLEDGE_HANDLE_FAILURE);
    return;
  }

  // Currently, only invalidations have non-trivial ack handle.
  InvalidationP invalidation = ackHandle.getNullableInvalidation();
  if (invalidation == null) {
    logger.warning("Ack handle without invalidation : %s",
        Bytes.toLazyCompactString(acknowledgeHandle.getHandleData()));
    statistics.recordError(ClientErrorType.ACKNOWLEDGE_HANDLE_FAILURE);
    return;
  }

  // Don't send the payload back.
  if (invalidation.hasPayload()) {
    InvalidationP.Builder builder = invalidation.toBuilder();
    builder.payload = null;
    invalidation = builder.build();
  }
  statistics.recordIncomingOperation(IncomingOperationType.ACKNOWLEDGE);
  protocolHandler.sendInvalidationAck(invalidation, batchingTask);

  // Record that the invalidation has been acknowledged to potentially avoid unnecessary delivery
  // of earlier invalidations for the same object.
  ackCache.recordAck(invalidation);
}
 
Example #16
Source File: CommonProtos.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a network endpoint id for an Android client with the given {@code registrationId},
 * {@code clientKey}, and {@code packageName}.
 */
public static NetworkEndpointId newAndroidEndpointId(String registrationId, String clientKey,
    String packageName, Version channelVersion) {
  Preconditions.checkNotNull(registrationId, "Null registration id");
  Preconditions.checkNotNull(clientKey, "Null client key");
  Preconditions.checkNotNull(packageName, "Null package name");
  Preconditions.checkNotNull(channelVersion, "Null channel version");

  AndroidEndpointId endpoint = AndroidEndpointId.create(registrationId, clientKey,
      /* senderId */ null, channelVersion, packageName);
  return NetworkEndpointId.create(NetworkAddress.ANDROID, new Bytes(endpoint.toByteArray()),
      null);
}
 
Example #17
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private ApplicationClientIdP(Integer clientType,
    Bytes clientName) throws ValidationArgumentException {
  int hazzerBits = 0;
  if (clientType != null) {
    hazzerBits |= 0x1;
    this.clientType = clientType;
  } else {
    this.clientType = 0;
  }
  required("client_name", clientName);
  nonEmpty("client_name", clientName);
  this.clientName = clientName;
  this.__hazzerBits = hazzerBits;
}
 
Example #18
Source File: ProtocolIntents.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public static Intent newReissueRegistrationsIntent(byte[] prefix, int length) {
  Intent intent = new Intent();
  ReissueRegistrationsUpcall reissueRegistrations =
      ReissueRegistrationsUpcall.create(new Bytes(prefix), length);
  intent.putExtra(LISTENER_UPCALL_KEY, ListenerUpcall.createWithReissueRegistrations(
      ANDROID_PROTOCOL_VERSION_VALUE, reissueRegistrations).toByteArray());
  return intent;
}
 
Example #19
Source File: CommonProtoStrings2.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** See spec in implementation notes. */
public static Object toLazyCompactString(final ByteString byteString) {
  if (byteString == null) {
    return null;
  }
  return new Object() {
    @Override
    public String toString() {
      return Bytes.toString(byteString.toByteArray());
    }
  };
}
 
Example #20
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public static InvalidationP create(com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP objectId,
    boolean isKnownVersion,
    long version,
    Bytes payload,
    Boolean isTrickleRestart) {
  return new InvalidationP(objectId, isKnownVersion, version, payload, isTrickleRestart);
}
 
Example #21
Source File: ObjectIdDigestUtils.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the digest of {@code objectIdDigests} using {@code digestFn}.
 * <p>
 * REQUIRES: {@code objectIdDigests} iterate in sorted order.
 */

public static Bytes getDigest(Iterable<Bytes> objectIdDigests, DigestFunction digestFn) {
  digestFn.reset();
  for (Bytes objectIdDigest : objectIdDigests) {
    digestFn.update(objectIdDigest.getByteArray());
  }
  return new Bytes(digestFn.getDigest());
}
 
Example #22
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 5 votes vote down vote up
static InvalidationP fromMessageNano(com.google.protos.ipc.invalidation.nano.NanoClientProtocol.InvalidationP message) {
  if (message == null) { return null; }
  return new InvalidationP(com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP.fromMessageNano(message.objectId),
      message.isKnownVersion,
      message.version,
      Bytes.fromByteArray(message.payload),
      message.isTrickleRestart);
}
 
Example #23
Source File: AndroidListener.java    From 365browser with Apache License 2.0 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, Bytes.fromByteArray(clientId),
      objectIds, isRegister);
}
 
Example #24
Source File: ObjectIdDigestUtils.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the digest of {@code objectIdDigests} using {@code digestFn}.
 * <p>
 * REQUIRES: {@code objectIdDigests} iterate in sorted order.
 */

public static Bytes getDigest(Iterable<Bytes> objectIdDigests, DigestFunction digestFn) {
  digestFn.reset();
  for (Bytes objectIdDigest : objectIdDigests) {
    digestFn.update(objectIdDigest.getByteArray());
  }
  return new Bytes(digestFn.getDigest());
}
 
Example #25
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 5 votes vote down vote up
static InitializeMessage fromMessageNano(com.google.protos.ipc.invalidation.nano.NanoClientProtocol.InitializeMessage message) {
  if (message == null) { return null; }
  return new InitializeMessage(message.clientType,
      Bytes.fromByteArray(message.nonce),
      com.google.ipc.invalidation.ticl.proto.ClientProtocol.ApplicationClientIdP.fromMessageNano(message.applicationClientId),
      message.digestSerializationType);
}
 
Example #26
Source File: ClientProtocol.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public static ServerHeader create(com.google.ipc.invalidation.ticl.proto.ClientProtocol.ProtocolVersion protocolVersion,
    Bytes clientToken,
    com.google.ipc.invalidation.ticl.proto.ClientProtocol.RegistrationSummary registrationSummary,
    long serverTimeMs,
    String messageId) {
  return new ServerHeader(protocolVersion, clientToken, registrationSummary, serverTimeMs, messageId);
}
 
Example #27
Source File: AndroidListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** See specs for {@link InvalidationClient#start}. */
public static Intent createStartIntent(Context context, InvalidationClientConfig config) {
  Preconditions.checkNotNull(context);
  Preconditions.checkNotNull(config);
  Preconditions.checkNotNull(config.clientName);

  return AndroidListenerIntents.createStartIntent(context, config.clientType,
      Bytes.fromByteArray(config.clientName), config.allowSuppression);
}
 
Example #28
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void toCompactString(TextBuilder builder) {
  builder.append("Storage state: ");
  for (Map.Entry<String, byte[]> entry : ticlPersistentState.entrySet()) {
    builder.appendFormat("<%s, %s>, ", entry.getKey(), Bytes.toString(entry.getValue()));
  }
}
 
Example #29
Source File: AndroidListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** See specs for {@link InvalidationClient#start}. */
public static Intent createStartIntent(Context context, int clientType, byte[] clientName) {
  Preconditions.checkNotNull(context);
  Preconditions.checkNotNull(clientName);

  final boolean allowSuppression = true;
  return AndroidListenerIntents.createStartIntent(context, clientType,
      Bytes.fromByteArray(clientName), allowSuppression);
}
 
Example #30
Source File: CommonProtoStrings2.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** See spec in implementation notes. */
public static TextBuilder toCompactString(TextBuilder builder, ByteString byteString) {
  if (byteString == null) {
    return builder;
  }
  return Bytes.toCompactString(builder, byteString.toByteArray());
}