Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Unified Diff: components/gcm_driver/gcm_driver_desktop.cc

Issue 1137463003: Support getting and deleting token for Instance ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add new files Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/gcm_driver/gcm_driver_desktop.cc
diff --git a/components/gcm_driver/gcm_driver_desktop.cc b/components/gcm_driver/gcm_driver_desktop.cc
index a711999f6bde014abee597a6d819b1232ab25865..b96ae20f26ce2136bdfdf2f2d3c4c6986f0e76ee 100644
--- a/components/gcm_driver/gcm_driver_desktop.cc
+++ b/components/gcm_driver/gcm_driver_desktop.cc
@@ -20,6 +20,7 @@
#include "components/gcm_driver/gcm_channel_status_syncer.h"
#include "components/gcm_driver/gcm_client_factory.h"
#include "components/gcm_driver/gcm_delayed_task_controller.h"
+#include "components/gcm_driver/instance_id/instance_id_impl.h"
#include "components/gcm_driver/system_encryptor.h"
#include "google_apis/gcm/engine/account_mapping.h"
#include "net/base/ip_endpoint.h"
@@ -40,11 +41,12 @@ class GCMDriverDesktop::IOWorker : public GCMClient::Delegate {
// Overridden from GCMClient::Delegate:
// Called on IO thread.
- void OnRegisterFinished(const std::string& app_id,
+ void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info,
const std::string& registration_id,
GCMClient::Result result) override;
- void OnUnregisterFinished(const std::string& app_id,
- GCMClient::Result result) override;
+ void OnUnregisterFinished(
+ const linked_ptr<RegistrationInfo>& registration_info,
+ GCMClient::Result result) override;
void OnSendFinished(const std::string& app_id,
const std::string& message_id,
GCMClient::Result result) override;
@@ -91,6 +93,13 @@ class GCMDriverDesktop::IOWorker : public GCMClient::Delegate {
const std::string& instance_id_data);
void RemoveInstanceIDData(const std::string& app_id);
void GetInstanceIDData(const std::string& app_id);
+ void GetToken(const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const std::map<std::string, std::string>& options);
+ void DeleteToken(const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope);
// For testing purpose. Can be called from UI thread. Use with care.
GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); }
@@ -141,26 +150,47 @@ void GCMDriverDesktop::IOWorker::Initialize(
}
void GCMDriverDesktop::IOWorker::OnRegisterFinished(
- const std::string& app_id,
+ const linked_ptr<RegistrationInfo>& registration_info,
const std::string& registration_id,
GCMClient::Result result) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());
- ui_thread_->PostTask(
+ const GCMRegistrationInfo* gcm_registration_info =
+ GCMRegistrationInfo::FromRegistrationInfo(registration_info.get());
+ if (gcm_registration_info) {
+ ui_thread_->PostTask(
+ FROM_HERE,
+ base::Bind(&GCMDriverDesktop::RegisterFinished,
+ service_,
+ gcm_registration_info->app_id,
+ registration_id,
+ result));
+ }
+
+ const InstanceIDTokenInfo* instance_id_token_info =
+ InstanceIDTokenInfo::FromRegistrationInfo(registration_info.get());
+ if (instance_id_token_info) {
+ ui_thread_->PostTask(
FROM_HERE,
- base::Bind(&GCMDriverDesktop::RegisterFinished, service_, app_id,
- registration_id, result));
+ base::Bind(&GCMDriverDesktop::GetTokenFinished,
+ service_,
+ instance_id_token_info->app_id,
+ instance_id_token_info->authorized_entity,
+ instance_id_token_info->scope,
+ registration_id,
+ result));
+ }
}
void GCMDriverDesktop::IOWorker::OnUnregisterFinished(
- const std::string& app_id,
+ const linked_ptr<RegistrationInfo>& registration_info,
GCMClient::Result result) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());
ui_thread_->PostTask(FROM_HERE,
base::Bind(&GCMDriverDesktop::UnregisterFinished,
service_,
- app_id,
+ registration_info->app_id,
result));
}
@@ -268,13 +298,19 @@ void GCMDriverDesktop::IOWorker::Register(
const std::vector<std::string>& sender_ids) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());
- gcm_client_->Register(app_id, sender_ids);
+ scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
+ gcm_info->app_id = app_id;
+ gcm_info->sender_ids = sender_ids;
+ gcm_client_->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release()));
}
void GCMDriverDesktop::IOWorker::Unregister(const std::string& app_id) {
DCHECK(io_thread_->RunsTasksOnCurrentThread());
- gcm_client_->Unregister(app_id);
+ scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo);
+ gcm_info->app_id = app_id;
+ gcm_client_->Unregister(
+ make_linked_ptr<RegistrationInfo>(gcm_info.release()));
}
void GCMDriverDesktop::IOWorker::Send(
@@ -378,6 +414,36 @@ void GCMDriverDesktop::IOWorker::GetInstanceIDData(
service_, app_id, instance_id_data));
}
+void GCMDriverDesktop::IOWorker::GetToken(
+ const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const std::map<std::string, std::string>& options) {
+ DCHECK(io_thread_->RunsTasksOnCurrentThread());
+
+ scoped_ptr<InstanceIDTokenInfo> instance_id_token_info(
+ new InstanceIDTokenInfo);
+ instance_id_token_info->app_id = app_id;
+ instance_id_token_info->authorized_entity = authorized_entity;
+ instance_id_token_info->scope = scope;
+ instance_id_token_info->options = options;
+ gcm_client_->Register(
+ make_linked_ptr<RegistrationInfo>(instance_id_token_info.release()));
+}
+
+void GCMDriverDesktop::IOWorker::DeleteToken(
+ const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope) {
+ scoped_ptr<InstanceIDTokenInfo> instance_id_token_info(
+ new InstanceIDTokenInfo);
+ instance_id_token_info->app_id = app_id;
+ instance_id_token_info->authorized_entity = authorized_entity;
+ instance_id_token_info->scope = scope;
+ gcm_client_->Unregister(
+ make_linked_ptr<RegistrationInfo>(instance_id_token_info.release()));
+}
+
void GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat(bool wake) {
#if defined(OS_CHROMEOS)
DCHECK(io_thread_->RunsTasksOnCurrentThread());
@@ -694,10 +760,80 @@ void GCMDriverDesktop::SetLastTokenFetchTime(const base::Time& time) {
time));
}
-InstanceIDStore* GCMDriverDesktop::GetInstanceIDStore() {
+InstanceIDHandler* GCMDriverDesktop::GetInstanceIDHandler() {
return this;
}
+void GCMDriverDesktop::GetToken(
+ const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const std::map<std::string, std::string>& options,
+ const GetTokenCallback& callback) {
+ DCHECK(!app_id.empty());
+ DCHECK(!authorized_entity.empty());
+ DCHECK(!scope.empty());
+ DCHECK(!callback.is_null());
+
+ GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START);
+ if (result != GCMClient::SUCCESS) {
+ callback.Run(std::string(), result);
+ return;
+ }
+
+ // If previous GetToken operation is still in progress, bail out.
+ TokenTuple tuple_key(app_id, authorized_entity, scope);
+ if (get_token_callbacks_.find(tuple_key) != get_token_callbacks_.end()) {
+ callback.Run(std::string(), GCMClient::ASYNC_OPERATION_PENDING);
+ return;
+ }
+
+ get_token_callbacks_[tuple_key] = callback;
+
+ io_thread_->PostTask(
+ FROM_HERE,
+ base::Bind(&GCMDriverDesktop::IOWorker::GetToken,
+ base::Unretained(io_worker_.get()),
+ app_id,
+ authorized_entity,
+ scope,
+ options));
+}
+
+void GCMDriverDesktop::DeleteToken(const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const DeleteTokenCallback& callback) {
+ DCHECK(!app_id.empty());
+ DCHECK(!authorized_entity.empty());
+ DCHECK(!scope.empty());
+ DCHECK(!callback.is_null());
+
+ GCMClient::Result result = EnsureStarted(GCMClient::IMMEDIATE_START);
+ if (result != GCMClient::SUCCESS) {
+ callback.Run(result);
+ return;
+ }
+
+ // If previous GetToken operation is still in progress, bail out.
+ TokenTuple tuple_key(app_id, authorized_entity, scope);
+ if (delete_token_callbacks_.find(tuple_key) !=
+ delete_token_callbacks_.end()) {
+ callback.Run(GCMClient::ASYNC_OPERATION_PENDING);
+ return;
+ }
+
+ delete_token_callbacks_[tuple_key] = callback;
+
+ io_thread_->PostTask(
+ FROM_HERE,
+ base::Bind(&GCMDriverDesktop::IOWorker::DeleteToken,
+ base::Unretained(io_worker_.get()),
+ app_id,
+ authorized_entity,
+ scope));
+}
+
void GCMDriverDesktop::AddInstanceIDData(
const std::string& app_id,
const std::string& instance_id_data) {
@@ -741,6 +877,39 @@ void GCMDriverDesktop::GetInstanceIDDataFinished(
get_instance_id_data_callbacks_.erase(app_id);
}
+void GCMDriverDesktop::GetTokenFinished(const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ const std::string& registration_id,
fgorski 2015/05/13 18:32:39 nit: we could just call it token
jianli 2015/05/13 22:42:56 Done.
+ GCMClient::Result result) {
+ TokenTuple tuple_key(app_id, authorized_entity, scope);
+ auto callback_iter = get_token_callbacks_.find(tuple_key);
+ if (callback_iter == get_token_callbacks_.end()) {
+ // The callback could have been removed when the app is uninstalled.
+ return;
+ }
+
+ GetTokenCallback callback = callback_iter->second;
+ get_token_callbacks_.erase(callback_iter);
+ callback.Run(registration_id, result);
+}
+
+void GCMDriverDesktop::DeleteTokenFinished(const std::string& app_id,
+ const std::string& authorized_entity,
+ const std::string& scope,
+ GCMClient::Result result) {
+ TokenTuple tuple_key(app_id, authorized_entity, scope);
+ auto callback_iter = delete_token_callbacks_.find(tuple_key);
+ if (callback_iter == delete_token_callbacks_.end()) {
+ // The callback could have been removed when the app is uninstalled.
+ return;
+ }
+
+ DeleteTokenCallback callback = callback_iter->second;
+ delete_token_callbacks_.erase(callback_iter);
+ callback.Run(result);
+}
+
void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) {
DCHECK(ui_thread_->RunsTasksOnCurrentThread());
@@ -929,4 +1098,19 @@ void GCMDriverDesktop::GetGCMStatisticsFinished(
LOG(WARNING) << "request_gcm_statistics_callback_ is NULL.";
}
+bool GCMDriverDesktop::TokenTupleComparer::operator()(
+ const TokenTuple& a, const TokenTuple& b) const {
fgorski 2015/05/13 18:32:39 pretty cool
jianli 2015/05/13 22:42:56 Acknowledged.
+ if (get<0>(a) < get<0>(b))
+ return true;
+ if (get<0>(a) > get<0>(b))
+ return false;
+
+ if (get<1>(a) < get<1>(b))
+ return true;
+ if (get<1>(a) > get<1>(b))
+ return false;
+
+ return get<2>(a) < get<2>(b);
+}
+
} // namespace gcm

Powered by Google App Engine
This is Rietveld 408576698