Skip to content

Commit

Permalink
Support getting and deleting token for Instance ID.
Browse files Browse the repository at this point in the history
GCM's registration and unregistration request have been refactored to
support token requests for Instance ID.

BUG=477084
TEST=new tests

Review URL: https://codereview.chromium.org/1137463003

Cr-Commit-Position: refs/heads/master@{#331475}
  • Loading branch information
jianli-chromium authored and Commit bot committed May 26, 2015
1 parent 45ab39a commit 7a0c9b6
Show file tree
Hide file tree
Showing 59 changed files with 2,463 additions and 673 deletions.
3 changes: 3 additions & 0 deletions chrome/browser/extensions/api/instance_id/instance_id_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace {
// Error messages.
const char kInvalidParameter[] = "Function was called with invalid parameters.";
const char kDisabled[] = "Instance ID is currently disabled.";
const char kAsyncOperationPending[] = "Asynchronous operation is pending.";
const char kNetworkError[] = "Network error occurred.";
const char kServerError[] = "Server error occurred.";
const char kUnknownError[] = "Unknown error occurred.";
Expand All @@ -29,6 +30,8 @@ const char* InstanceIDResultToError(instance_id::InstanceID::Result result) {
return kInvalidParameter;
case instance_id::InstanceID::DISABLED:
return kDisabled;
case instance_id::InstanceID::ASYNC_OPERATION_PENDING:
return kAsyncOperationPending;
case instance_id::InstanceID::NETWORK_ERROR:
return kNetworkError;
case instance_id::InstanceID::SERVER_ERROR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class InstanceIDGetTokenFunction : public InstanceIDApiFunction {

class InstanceIDDeleteTokenFunction : public InstanceIDApiFunction {
public:
DECLARE_EXTENSION_FUNCTION("instanceID.DeleteToken", INSTANCEID_DELETETOKEN);
DECLARE_EXTENSION_FUNCTION("instanceID.deleteToken", INSTANCEID_DELETETOKEN);

InstanceIDDeleteTokenFunction();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,51 @@ function deleteTokenAfterGetToken() {
);
}

var oldToken;
function getTokenDeleteTokeAndGetToken() {
chrome.instanceID.getToken(
{"authorizedEntity": "1", "scope": "GCM"},
function(token) {
if (chrome.runtime.lastError || !token) {
chrome.test.fail(
"chrome.runtime.lastError was set or token was empty.");
return;
}
oldToken = token;
chrome.instanceID.deleteToken(
{"authorizedEntity": "1", "scope": "GCM"},
function() {
if (chrome.runtime.lastError) {
chrome.test.fail("chrome.runtime.lastError: " +
chrome.runtime.lastError.message);
return;
}

chrome.instanceID.getToken(
{"authorizedEntity": "1", "scope": "GCM"},
function(token) {
if (!token || token == oldToken) {
chrome.test.fail(
"Different token should be returned after deleteToken.");
return;
}
chrome.test.succeed();
}
);
}
);
}
);
}

chrome.test.runTests([
deleteTokenWithoutParameters,
deleteTokenWithoutCallback,
deleteTokenWithoutAuthorizedEntity,
deleteTokenWithInvalidAuthorizedEntity,
deleteTokenWithoutScope,
deleteTokenWithInvalidScope,
// TODO(jianli): To be enabled when deleteToken is implemented.
//deleteTokenBeforeGetToken,
//deleteTokenAfterGetToken,
//getTokenDeleteTokeAndGetToken,
deleteTokenBeforeGetToken,
deleteTokenAfterGetToken,
getTokenDeleteTokeAndGetToken,
]);
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ chrome.test.runTests([
getTokenWithoutScope,
getTokenWithInvalidScope,
getTokenWithInvalidOptionValue,
// TODO(jianli): To be enabled when GetToken is implemented.
//getTokenWithoutOptions,
//getTokenWithValidOptions,
getTokenWithoutOptions,
getTokenWithValidOptions,
]);
2 changes: 2 additions & 0 deletions components/gcm_driver.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
'gcm_driver/gcm_driver_desktop.h',
'gcm_driver/gcm_stats_recorder_impl.cc',
'gcm_driver/gcm_stats_recorder_impl.h',
'gcm_driver/registration_info.cc',
'gcm_driver/registration_info.h',
'gcm_driver/system_encryptor.cc',
'gcm_driver/system_encryptor.h',
],
Expand Down
2 changes: 2 additions & 0 deletions components/gcm_driver/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static_library("gcm_driver") {
"gcm_driver_desktop.h",
"gcm_stats_recorder_impl.cc",
"gcm_stats_recorder_impl.h",
"registration_info.cc",
"registration_info.h",
"system_encryptor.cc",
"system_encryptor.h",
]
Expand Down
40 changes: 26 additions & 14 deletions components/gcm_driver/fake_gcm_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,33 @@ void FakeGCMClient::Stop() {
delegate_->OnDisconnected();
}

void FakeGCMClient::Register(const std::string& app_id,
const std::vector<std::string>& sender_ids) {
void FakeGCMClient::Register(
const linked_ptr<RegistrationInfo>& registration_info) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());

std::string registration_id = GetRegistrationIdFromSenderIds(sender_ids);
GCMRegistrationInfo* gcm_registration_info =
GCMRegistrationInfo::FromRegistrationInfo(registration_info.get());
DCHECK(gcm_registration_info);

std::string registration_id = GetRegistrationIdFromSenderIds(
gcm_registration_info->sender_ids);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&FakeGCMClient::RegisterFinished,
weak_ptr_factory_.GetWeakPtr(),
app_id,
registration_info,
registration_id));
}

void FakeGCMClient::Unregister(const std::string& app_id) {
void FakeGCMClient::Unregister(
const linked_ptr<RegistrationInfo>& registration_info) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());

base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&FakeGCMClient::UnregisterFinished,
weak_ptr_factory_.GetWeakPtr(),
app_id));
registration_info));
}

void FakeGCMClient::Send(const std::string& app_id,
Expand Down Expand Up @@ -138,14 +144,16 @@ void FakeGCMClient::UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) {
}

void FakeGCMClient::AddInstanceIDData(const std::string& app_id,
const std::string& instance_id_data) {
const std::string& instance_id,
const std::string& extra_data) {
}

void FakeGCMClient::RemoveInstanceIDData(const std::string& app_id) {
}

std::string FakeGCMClient::GetInstanceIDData(const std::string& app_id) {
return std::string();
void FakeGCMClient::GetInstanceIDData(const std::string& app_id,
std::string* instance_id,
std::string* extra_data) {
}

void FakeGCMClient::AddHeartbeatInterval(const std::string& scope,
Expand Down Expand Up @@ -211,14 +219,18 @@ void FakeGCMClient::Started() {
delegate_->OnConnected(net::IPEndPoint());
}

void FakeGCMClient::RegisterFinished(const std::string& app_id,
const std::string& registrion_id) {
void FakeGCMClient::RegisterFinished(
const linked_ptr<RegistrationInfo>& registration_info,
const std::string& registrion_id) {
delegate_->OnRegisterFinished(
app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
registration_info,
registrion_id,
registrion_id.empty() ? SERVER_ERROR : SUCCESS);
}

void FakeGCMClient::UnregisterFinished(const std::string& app_id) {
delegate_->OnUnregisterFinished(app_id, GCMClient::SUCCESS);
void FakeGCMClient::UnregisterFinished(
const linked_ptr<RegistrationInfo>& registration_info) {
delegate_->OnUnregisterFinished(registration_info, GCMClient::SUCCESS);
}

void FakeGCMClient::SendFinished(const std::string& app_id,
Expand Down
21 changes: 13 additions & 8 deletions components/gcm_driver/fake_gcm_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class FakeGCMClient : public GCMClient {
Delegate* delegate) override;
void Start(StartMode start_mode) override;
void Stop() override;
void Register(const std::string& app_id,
const std::vector<std::string>& sender_ids) override;
void Unregister(const std::string& app_id) override;
void Register(const linked_ptr<RegistrationInfo>& registration_info) override;
void Unregister(
const linked_ptr<RegistrationInfo>& registration_info) override;
void Send(const std::string& app_id,
const std::string& receiver_id,
const OutgoingMessage& message) override;
Expand All @@ -57,9 +57,12 @@ class FakeGCMClient : public GCMClient {
void SetLastTokenFetchTime(const base::Time& time) override;
void UpdateHeartbeatTimer(scoped_ptr<base::Timer> timer) override;
void AddInstanceIDData(const std::string& app_id,
const std::string& instance_id_data) override;
const std::string& instance_id,
const std::string& extra_data) override;
void RemoveInstanceIDData(const std::string& app_id) override;
std::string GetInstanceIDData(const std::string& app_id) override;
void GetInstanceIDData(const std::string& app_id,
std::string* instance_id,
std::string* extra_data) override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;

Expand All @@ -84,9 +87,11 @@ class FakeGCMClient : public GCMClient {
// Called on IO thread.
void DoStart();
void Started();
void RegisterFinished(const std::string& app_id,
const std::string& registrion_id);
void UnregisterFinished(const std::string& app_id);
void RegisterFinished(
const linked_ptr<RegistrationInfo>& registration_info,
const std::string& registrion_id);
void UnregisterFinished(
const linked_ptr<RegistrationInfo>& registration_info);
void SendFinished(const std::string& app_id, const OutgoingMessage& message);
void MessageReceived(const std::string& app_id,
const IncomingMessage& message);
Expand Down
2 changes: 1 addition & 1 deletion components/gcm_driver/fake_gcm_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void FakeGCMDriver::SetLastTokenFetchTime(const base::Time& time) {
void FakeGCMDriver::WakeFromSuspendForHeartbeat(bool wake) {
}

InstanceIDStore* FakeGCMDriver::GetInstanceIDStore() {
InstanceIDHandler* FakeGCMDriver::GetInstanceIDHandler() {
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion components/gcm_driver/fake_gcm_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FakeGCMDriver : public GCMDriver {
base::Time GetLastTokenFetchTime() override;
void SetLastTokenFetchTime(const base::Time& time) override;
void WakeFromSuspendForHeartbeat(bool wake) override;
InstanceIDStore* GetInstanceIDStore() override;
InstanceIDHandler* GetInstanceIDHandler() override;
void AddHeartbeatInterval(const std::string& scope, int interval_ms) override;
void RemoveHeartbeatInterval(const std::string& scope) override;

Expand Down
53 changes: 33 additions & 20 deletions components/gcm_driver/gcm_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <vector>

#include "base/basictypes.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "components/gcm_driver/gcm_activity.h"
#include "components/gcm_driver/registration_info.h"

template <class T> class scoped_refptr;

Expand Down Expand Up @@ -163,18 +165,22 @@ class GCMClient {
class Delegate {
public:
// Called when the registration completed successfully or an error occurs.
// |app_id|: application ID.
// |registration_info|: the specific information required for the
// registration.
// |registration_id|: non-empty if the registration completed successfully.
// |result|: the type of the error if an error occured, success otherwise.
virtual void OnRegisterFinished(const std::string& app_id,
const std::string& registration_id,
Result result) = 0;
virtual void OnRegisterFinished(
const linked_ptr<RegistrationInfo>& registration_info,
const std::string& registration_id,
Result result) = 0;

// Called when the unregistration completed.
// |app_id|: application ID.
// |registration_info|: the specific information required for the
// registration.
// |result|: result of the unregistration.
virtual void OnUnregisterFinished(const std::string& app_id,
GCMClient::Result result) = 0;
virtual void OnUnregisterFinished(
const linked_ptr<RegistrationInfo>& registration_info,
GCMClient::Result result) = 0;

// Called when the message is scheduled to send successfully or an error
// occurs.
Expand Down Expand Up @@ -258,20 +264,24 @@ class GCMClient {
// Stops using the GCM service. This will not erase the persisted data.
virtual void Stop() = 0;

// Registers the application for GCM. Delegate::OnRegisterFinished will be
// called asynchronously upon completion.
// |app_id|: application ID.
// |sender_ids|: list of IDs of the servers that are allowed to send the
// messages to the application. These IDs are assigned by the
// Google API Console.
virtual void Register(const std::string& app_id,
const std::vector<std::string>& sender_ids) = 0;
// Registers with the server to access the provided service.
// Delegate::OnRegisterFinished will be called asynchronously upon completion.
// |registration_info|: the specific information required for the
// registration. For GCM, it will contain app id and
// sender IDs. For InstanceID, it will contain app_id,
// authorized entity and scope.
virtual void Register(
const linked_ptr<RegistrationInfo>& registration_info) = 0;

// Unregisters the application from GCM when it is uninstalled.
// Unregisters from the server to stop accessing the provided service.
// Delegate::OnUnregisterFinished will be called asynchronously upon
// completion.
// |app_id|: application ID.
virtual void Unregister(const std::string& app_id) = 0;
// |registration_info|: the specific information required for the
// registration. For GCM, it will contain app id (sender
// IDs can be ingored). For InstanceID, it will contain
// app id, authorized entity and scope.
virtual void Unregister(
const linked_ptr<RegistrationInfo>& registration_info) = 0;

// Sends a message to a given receiver. Delegate::OnSendFinished will be
// called asynchronously upon completion.
Expand Down Expand Up @@ -312,14 +322,17 @@ class GCMClient {

// Adds the Instance ID data for a specific app to the persistent store.
virtual void AddInstanceIDData(const std::string& app_id,
const std::string& instance_id_data) = 0;
const std::string& instance_id,
const std::string& extra_data) = 0;

// Removes the Instance ID data for a specific app from the persistent store.
virtual void RemoveInstanceIDData(const std::string& app_id) = 0;

// Retrieves the Instance ID data for a specific app from the persistent
// store.
virtual std::string GetInstanceIDData(const std::string& app_id) = 0;
virtual void GetInstanceIDData(const std::string& app_id,
std::string* instance_id,
std::string* extra_data) = 0;

// Gets and sets custom heartbeat interval for the MCS connection.
// |scope| is used to identify the component that requests a custom interval
Expand Down
Loading

0 comments on commit 7a0c9b6

Please sign in to comment.