diff options
author | jianli <jianli@chromium.org> | 2015-04-29 14:55:34 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-29 21:57:23 +0000 |
commit | 408900326adb348e1c6b464439d1e2c3631cf21e (patch) | |
tree | 303205a83a2ca4166b833fcc32f761b690b0d933 | |
parent | 04ae4f5ca488a6066efef98fd60d58f7f71040aa (diff) | |
download | chromium_src-408900326adb348e1c6b464439d1e2c3631cf21e.zip chromium_src-408900326adb348e1c6b464439d1e2c3631cf21e.tar.gz chromium_src-408900326adb348e1c6b464439d1e2c3631cf21e.tar.bz2 |
Add InstanceIDDriver and InstanceID.
They are used for Instance ID service. Not implemented yet.
BUG=477084
TEST=none
Review URL: https://codereview.chromium.org/1102963004
Cr-Commit-Position: refs/heads/master@{#327589}
-rw-r--r-- | chrome/chrome_browser.gypi | 1 | ||||
-rw-r--r-- | components/gcm_driver.gypi | 27 | ||||
-rw-r--r-- | components/gcm_driver/BUILD.gn | 7 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/BUILD.gn | 32 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id.cc | 25 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id.h | 110 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id_android.cc | 51 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id_android.h | 42 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id_driver.cc | 28 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id_driver.h | 39 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id_impl.cc | 51 | ||||
-rw-r--r-- | components/gcm_driver/instance_id/instance_id_impl.h | 42 |
12 files changed, 455 insertions, 0 deletions
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 38e0ce6..bab2a5a 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -3009,6 +3009,7 @@ '../components/components.gyp:history_core_browser', '../components/components.gyp:history_core_common', '../components/components.gyp:infobars_core', + '../components/components.gyp:instance_id_driver', '../components/components.gyp:invalidation', '../components/components.gyp:metrics', '../components/components.gyp:metrics_gpu', diff --git a/components/gcm_driver.gypi b/components/gcm_driver.gypi index e783ba9..d850b37 100644 --- a/components/gcm_driver.gypi +++ b/components/gcm_driver.gypi @@ -135,6 +135,33 @@ }], ], }, + { + # GN version: //components/gcm_driver/instance_id:instance_id_driver + 'target_name': 'instance_id_driver', + 'type': 'static_library', + 'include_dirs': [ + '..', + ], + 'sources': [ + # Note: file list duplicated in GN build. + 'gcm_driver/instance_id/instance_id.cc', + 'gcm_driver/instance_id/instance_id.h', + 'gcm_driver/instance_id/instance_id_android.cc', + 'gcm_driver/instance_id/instance_id_android.h', + 'gcm_driver/instance_id/instance_id_driver.cc', + 'gcm_driver/instance_id/instance_id_driver.h', + 'gcm_driver/instance_id/instance_id_impl.cc', + 'gcm_driver/instance_id/instance_id_impl.h', + ], + 'conditions': [ + ['OS == "android"', { + 'sources!': [ + 'gcm_driver/instance_id/instance_id_impl.cc', + 'gcm_driver/instance_id/instance_id_impl.h', + ], + }], + ], + }, ], 'conditions': [ ['OS == "android"', { diff --git a/components/gcm_driver/BUILD.gn b/components/gcm_driver/BUILD.gn index e8c9a17..7ac85b0 100644 --- a/components/gcm_driver/BUILD.gn +++ b/components/gcm_driver/BUILD.gn @@ -2,6 +2,13 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +group("gn_instance_id_driver") { + testonly = true + deps = [ + "//components/gcm_driver/instance_id:instance_id_driver", + ] +} + # GYP version: components/gcm_driver.gypi:gcm_driver static_library("gcm_driver") { sources = [ diff --git a/components/gcm_driver/instance_id/BUILD.gn b/components/gcm_driver/instance_id/BUILD.gn new file mode 100644 index 0000000..efb6400 --- /dev/null +++ b/components/gcm_driver/instance_id/BUILD.gn @@ -0,0 +1,32 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# GYP version: components/gcm_driver.gypi:instance_id_driver +static_library("instance_id_driver") { + sources = [ + "instance_id.cc", + "instance_id.h", + "instance_id_driver.cc", + "instance_id_driver.h", + "instance_id_impl.cc", + "instance_id_impl.h", + ] + + deps = [ + "//base", + "//google_apis/gcm", + "//net", + ] + + if (is_android) { + sources -= [ + "instance_id_impl.cc", + "instance_id_impl.h", + ] + sources += [ + "instance_id_android.cc", + "instance_id_android.h", + ] + } +} diff --git a/components/gcm_driver/instance_id/instance_id.cc b/components/gcm_driver/instance_id/instance_id.cc new file mode 100644 index 0000000..b83b15f --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id.cc @@ -0,0 +1,25 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/gcm_driver/instance_id/instance_id.h" + +namespace instance_id { + +InstanceID::InstanceID(const std::string& app_id) + : app_id_(app_id) { +} + +InstanceID::~InstanceID() { +} + +void InstanceID::SetTokenRefreshCallback(const TokenRefreshCallback& callback) { + token_refresh_callback_ = callback; +} + +void InstanceID::NotifyTokenRefresh(bool update_id) { + if (!token_refresh_callback_.is_null()) + token_refresh_callback_.Run(this, update_id); +} + +} // namespace instance_id diff --git a/components/gcm_driver/instance_id/instance_id.h b/components/gcm_driver/instance_id/instance_id.h new file mode 100644 index 0000000..bb0ca99 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id.h @@ -0,0 +1,110 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_ +#define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_ + +#include <map> +#include <string> + +#include "base/callback.h" +#include "base/macros.h" +#include "base/time/time.h" + +namespace instance_id { + +// Encapsulates Instance ID functionalities that need to be implemented for +// different platform. One instance is created per application. Life of +// Instance ID is managed by the InstanceIdDriver. +class InstanceID { + public: + enum Result { + // Successful operation. + SUCCESS, + // Invalid parameter. + INVALID_PARAMETER, + // Instance ID is disabled. + DISABLED, + // Network socket error. + NETWORK_ERROR, + // Problem at the server. + SERVER_ERROR, + // Other errors. + UNKNOWN_ERROR + }; + + // Asynchronous callbacks. + typedef base::Callback<void(InstanceID* instance_id, + bool update_id)> TokenRefreshCallback; + typedef base::Callback<void(InstanceID* instance_id, + const std::string& token, + Result result)> GetTokenCallback; + typedef base::Callback<void(InstanceID* instance_id, + Result result)> DeleteTokenCallback; + typedef base::Callback<void(InstanceID* instance_id, + Result result)> DeleteIDCallback; + + // Creator. + // |app_id|: identifies the application that uses the Instance ID. + // |callback|: to be called when the token refresh is needed. + static InstanceID* Create(const std::string& app_id); + + virtual ~InstanceID(); + + // Sets the callback that will be invoked when the token refresh event needs + // to be triggered. + void SetTokenRefreshCallback(const TokenRefreshCallback& callback); + + // Returns the Instance ID. + virtual std::string GetID() = 0; + + // Returns the time when the InstanceID has been generated. + virtual base::Time GetCreationTime() = 0; + + // Retrieves a token that allows the identified "audience" to access the + // service defined as "scope". + // |audience|: identifies the entity that is authorized to access resources + // associated with this Instance ID. It can be another Instance + // ID or a project ID. + // |scope|: identifies authorized actions that the authorized entity can take. + // E.g. for sending GCM messages, "GCM" scope should be used. + // |options|: allows including a small number of string key/value pairs that + // will be associated with the token and may be used in processing + // the request. + // |callback|: to be called once the asynchronous operation is done. + virtual void GetToken(const std::string& audience, + const std::string& scope, + const std::map<std::string, std::string>& options, + const GetTokenCallback& callback) = 0; + + // Revokes a granted token. + // |audience|: the audience that is passed for obtaining a token. + // |scope|: the scope that is passed for obtaining a token. + // |callback|: to be called once the asynchronous operation is done. + virtual void DeleteToken(const std::string& audience, + const std::string& scope, + const DeleteTokenCallback& callback) = 0; + + // Resets the app instance identifier and revokes all tokens associated with + // it. + // |callback|: to be called once the asynchronous operation is done. + virtual void DeleteID(const DeleteIDCallback& callback) = 0; + + std::string app_id() const { return app_id_; } + + protected: + explicit InstanceID(const std::string& app_id); + + void NotifyTokenRefresh(bool update_id); + + private: + std::string app_id_; + TokenRefreshCallback token_refresh_callback_; + + DISALLOW_COPY_AND_ASSIGN(InstanceID); +}; + +} // namespace instance_id + +#endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_H_ diff --git a/components/gcm_driver/instance_id/instance_id_android.cc b/components/gcm_driver/instance_id/instance_id_android.cc new file mode 100644 index 0000000..2d696e0 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id_android.cc @@ -0,0 +1,51 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/gcm_driver/instance_id/instance_id_android.h" + +#include "base/logging.h" + +namespace instance_id { + +// static +InstanceID* InstanceID::Create(const std::string& app_id) { + return new InstanceIDAndroid(app_id); +} + +InstanceIDAndroid::InstanceIDAndroid(const std::string& app_id) + : InstanceID(app_id) { +} + +InstanceIDAndroid::~InstanceIDAndroid() { +} + +std::string InstanceIDAndroid::GetID() { + NOTIMPLEMENTED(); + return std::string(); +} + +base::Time InstanceIDAndroid::GetCreationTime() { + NOTIMPLEMENTED(); + return base::Time(); +} + +void InstanceIDAndroid::GetToken( + const std::string& audience, + const std::string& scope, + const std::map<std::string, std::string>& options, + const GetTokenCallback& callback) { + NOTIMPLEMENTED(); +} + +void InstanceIDAndroid::DeleteToken(const std::string& audience, + const std::string& scope, + const DeleteTokenCallback& callback) { + NOTIMPLEMENTED(); +} + +void InstanceIDAndroid::DeleteID(const DeleteIDCallback& callback) { + NOTIMPLEMENTED(); +} + +} // namespace instance_id diff --git a/components/gcm_driver/instance_id/instance_id_android.h b/components/gcm_driver/instance_id/instance_id_android.h new file mode 100644 index 0000000..3089901 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id_android.h @@ -0,0 +1,42 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_ANDROID_H_ +#define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_ANDROID_H_ + +#include <map> +#include <string> + +#include "base/callback.h" +#include "base/macros.h" +#include "base/time/time.h" +#include "components/gcm_driver/instance_id/instance_id.h" + +namespace instance_id { + +// InstanceID implementation for Android. +class InstanceIDAndroid : public InstanceID { + public: + explicit InstanceIDAndroid(const std::string& app_id); + ~InstanceIDAndroid() override; + + // InstanceID: + std::string GetID() override; + base::Time GetCreationTime() override; + void GetToken(const std::string& audience, + const std::string& scope, + const std::map<std::string, std::string>& options, + const GetTokenCallback& callback) override; + void DeleteToken(const std::string& audience, + const std::string& scope, + const DeleteTokenCallback& callback) override; + void DeleteID(const DeleteIDCallback& callback) override; + + private: + DISALLOW_COPY_AND_ASSIGN(InstanceIDAndroid); +}; + +} // namespace instance_id + +#endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_ANDROID_H_ diff --git a/components/gcm_driver/instance_id/instance_id_driver.cc b/components/gcm_driver/instance_id/instance_id_driver.cc new file mode 100644 index 0000000..ba0eab0 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id_driver.cc @@ -0,0 +1,28 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/gcm_driver/instance_id/instance_id_driver.h" + +#include "components/gcm_driver/instance_id/instance_id.h" + +namespace instance_id { + +InstanceIDDriver::InstanceIDDriver() + : instance_id_map_deleter_(&instance_id_map_) { +} + +InstanceIDDriver::~InstanceIDDriver() { +} + +InstanceID* InstanceIDDriver::GetInstanceID(const std::string& app_id) { + auto iter = instance_id_map_.find(app_id); + if (iter != instance_id_map_.end()) + return iter->second; + + InstanceID* instance_id = InstanceID::Create(app_id); + instance_id_map_[app_id] = instance_id; + return instance_id; +} + +} // namespace instance_id diff --git a/components/gcm_driver/instance_id/instance_id_driver.h b/components/gcm_driver/instance_id/instance_id_driver.h new file mode 100644 index 0000000..73d05b0 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id_driver.h @@ -0,0 +1,39 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_DRIVER_H_ +#define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_DRIVER_H_ + +#include <map> +#include <string> + +#include "base/macros.h" +#include "base/stl_util.h" + +namespace instance_id { + +class InstanceID; + +// Bridge between Instance ID users in Chrome and the platform-specific +// implementation. +class InstanceIDDriver { + public: + InstanceIDDriver(); + ~InstanceIDDriver(); + + // Returns the InstanceID that provides the Instance ID service for the given + // application. The lifetime of InstanceID will be managed by this class. + InstanceID* GetInstanceID(const std::string& app_id); + + private: + typedef std::map<std::string, InstanceID*> InstanceIDMap; + InstanceIDMap instance_id_map_; + STLValueDeleter<InstanceIDMap> instance_id_map_deleter_; + + DISALLOW_COPY_AND_ASSIGN(InstanceIDDriver); +}; + +} // namespace instance_id + +#endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_DRIVER_H_ diff --git a/components/gcm_driver/instance_id/instance_id_impl.cc b/components/gcm_driver/instance_id/instance_id_impl.cc new file mode 100644 index 0000000..4d76ad2 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id_impl.cc @@ -0,0 +1,51 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/gcm_driver/instance_id/instance_id_impl.h" + +#include "base/logging.h" + +namespace instance_id { + +// static +InstanceID* InstanceID::Create(const std::string& app_id) { + return new InstanceIDImpl(app_id); +} + +InstanceIDImpl::InstanceIDImpl(const std::string& app_id) + : InstanceID(app_id) { +} + +InstanceIDImpl::~InstanceIDImpl() { +} + +std::string InstanceIDImpl::GetID() { + NOTIMPLEMENTED(); + return std::string(); +} + +base::Time InstanceIDImpl::GetCreationTime() { + NOTIMPLEMENTED(); + return base::Time(); +} + +void InstanceIDImpl::GetToken( + const std::string& audience, + const std::string& scope, + const std::map<std::string, std::string>& options, + const GetTokenCallback& callback) { + NOTIMPLEMENTED(); +} + +void InstanceIDImpl::DeleteToken(const std::string& audience, + const std::string& scope, + const DeleteTokenCallback& callback) { + NOTIMPLEMENTED(); +} + +void InstanceIDImpl::DeleteID(const DeleteIDCallback& callback) { + NOTIMPLEMENTED(); +} + +} // namespace instance_id diff --git a/components/gcm_driver/instance_id/instance_id_impl.h b/components/gcm_driver/instance_id/instance_id_impl.h new file mode 100644 index 0000000..9855500 --- /dev/null +++ b/components/gcm_driver/instance_id/instance_id_impl.h @@ -0,0 +1,42 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_IMPL_H_ +#define COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_IMPL_H_ + +#include <map> +#include <string> + +#include "base/callback.h" +#include "base/macros.h" +#include "base/time/time.h" +#include "components/gcm_driver/instance_id/instance_id.h" + +namespace instance_id { + +// InstanceID implementation for desktop and iOS. +class InstanceIDImpl : public InstanceID { + public: + explicit InstanceIDImpl(const std::string& app_id); + ~InstanceIDImpl() override; + + // InstanceID: + std::string GetID() override; + base::Time GetCreationTime() override; + void GetToken(const std::string& audience, + const std::string& scope, + const std::map<std::string, std::string>& options, + const GetTokenCallback& callback) override; + void DeleteToken(const std::string& audience, + const std::string& scope, + const DeleteTokenCallback& callback) override; + void DeleteID(const DeleteIDCallback& callback) override; + + private: + DISALLOW_COPY_AND_ASSIGN(InstanceIDImpl); +}; + +} // namespace instance_id + +#endif // COMPONENTS_GCM_DRIVER_INSTANCE_ID_INSTANCE_ID_IMPL_H_ |