Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / instance_id / instance_id_api.cc
blobf7cdbe2c27031057d18d5e7b8f8f40b2b936696b
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/instance_id/instance_id_api.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service.h"
11 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service_factory.h"
12 #include "chrome/common/extensions/api/instance_id.h"
13 #include "components/gcm_driver/instance_id/instance_id_driver.h"
14 #include "extensions/common/extension.h"
16 namespace extensions {
18 namespace {
20 // Error messages.
21 const char kInvalidParameter[] = "Function was called with invalid parameters.";
22 const char kDisabled[] = "Instance ID is currently disabled.";
23 const char kAsyncOperationPending[] = "Asynchronous operation is pending.";
24 const char kNetworkError[] = "Network error occurred.";
25 const char kServerError[] = "Server error occurred.";
26 const char kUnknownError[] = "Unknown error occurred.";
28 const char* InstanceIDResultToError(instance_id::InstanceID::Result result) {
29 switch (result) {
30 case instance_id::InstanceID::INVALID_PARAMETER:
31 return kInvalidParameter;
32 case instance_id::InstanceID::DISABLED:
33 return kDisabled;
34 case instance_id::InstanceID::ASYNC_OPERATION_PENDING:
35 return kAsyncOperationPending;
36 case instance_id::InstanceID::NETWORK_ERROR:
37 return kNetworkError;
38 case instance_id::InstanceID::SERVER_ERROR:
39 return kServerError;
40 case instance_id::InstanceID::UNKNOWN_ERROR:
41 return kUnknownError;
42 default:
43 NOTREACHED() << "Unexpected value of result cannot be converted: "
44 << result;
46 return "";
49 } // namespace
51 InstanceIDApiFunction::InstanceIDApiFunction() {
54 InstanceIDApiFunction::~InstanceIDApiFunction() {
57 ExtensionFunction::ResponseAction InstanceIDApiFunction::Run() {
58 if (Profile::FromBrowserContext(browser_context())->IsOffTheRecord()) {
59 return RespondNow(Error(
60 "chrome.instanceID not supported in incognito mode"));
63 bool isInstanceIDEnabled = IsEnabled();
64 UMA_HISTOGRAM_BOOLEAN("InstanceID.Enabled", isInstanceIDEnabled);
66 if (!isInstanceIDEnabled) {
67 return RespondNow(Error(
68 InstanceIDResultToError(instance_id::InstanceID::DISABLED)));
71 return DoWork();
74 bool InstanceIDApiFunction::IsEnabled() const {
75 Profile* profile = Profile::FromBrowserContext(browser_context());
77 return instance_id::InstanceIDProfileService::IsInstanceIDEnabled(profile);
80 instance_id::InstanceID* InstanceIDApiFunction::GetInstanceID() const {
81 return instance_id::InstanceIDProfileServiceFactory::GetForProfile(
82 Profile::FromBrowserContext(browser_context()))->driver()->
83 GetInstanceID(extension()->id());
86 InstanceIDGetIDFunction::InstanceIDGetIDFunction() {}
88 InstanceIDGetIDFunction::~InstanceIDGetIDFunction() {}
90 ExtensionFunction::ResponseAction InstanceIDGetIDFunction::DoWork() {
91 GetInstanceID()->GetID(
92 base::Bind(&InstanceIDGetIDFunction::GetIDCompleted, this));
93 return RespondLater();
96 void InstanceIDGetIDFunction::GetIDCompleted(const std::string& id) {
97 Respond(OneArgument(new base::StringValue(id)));
100 InstanceIDGetCreationTimeFunction::InstanceIDGetCreationTimeFunction() {}
102 InstanceIDGetCreationTimeFunction::~InstanceIDGetCreationTimeFunction() {}
104 ExtensionFunction::ResponseAction InstanceIDGetCreationTimeFunction::DoWork() {
105 GetInstanceID()->GetCreationTime(
106 base::Bind(&InstanceIDGetCreationTimeFunction::GetCreationTimeCompleted,
107 this));
108 return RespondLater();
111 void InstanceIDGetCreationTimeFunction::GetCreationTimeCompleted(
112 const base::Time& creation_time) {
113 Respond(OneArgument(new base::FundamentalValue(creation_time.ToDoubleT())));
116 InstanceIDGetTokenFunction::InstanceIDGetTokenFunction() {}
118 InstanceIDGetTokenFunction::~InstanceIDGetTokenFunction() {}
120 ExtensionFunction::ResponseAction InstanceIDGetTokenFunction::DoWork() {
121 scoped_ptr<api::instance_id::GetToken::Params> params =
122 api::instance_id::GetToken::Params::Create(*args_);
123 EXTENSION_FUNCTION_VALIDATE(params.get());
125 std::map<std::string, std::string> options;
126 if (params->get_token_params.options.get())
127 options = params->get_token_params.options->additional_properties;
129 GetInstanceID()->GetToken(
130 params->get_token_params.authorized_entity,
131 params->get_token_params.scope,
132 options,
133 base::Bind(&InstanceIDGetTokenFunction::GetTokenCompleted, this));
135 return RespondLater();
138 void InstanceIDGetTokenFunction::GetTokenCompleted(
139 const std::string& token,
140 instance_id::InstanceID::Result result) {
141 if (result == instance_id::InstanceID::SUCCESS)
142 Respond(OneArgument(new base::StringValue(token)));
143 else
144 Respond(Error(InstanceIDResultToError(result)));
147 InstanceIDDeleteTokenFunction::InstanceIDDeleteTokenFunction() {}
149 InstanceIDDeleteTokenFunction::~InstanceIDDeleteTokenFunction() {}
151 ExtensionFunction::ResponseAction InstanceIDDeleteTokenFunction::DoWork() {
152 scoped_ptr<api::instance_id::DeleteToken::Params> params =
153 api::instance_id::DeleteToken::Params::Create(*args_);
154 EXTENSION_FUNCTION_VALIDATE(params.get());
156 GetInstanceID()->DeleteToken(
157 params->delete_token_params.authorized_entity,
158 params->delete_token_params.scope,
159 base::Bind(&InstanceIDDeleteTokenFunction::DeleteTokenCompleted, this));
161 return RespondLater();
164 void InstanceIDDeleteTokenFunction::DeleteTokenCompleted(
165 instance_id::InstanceID::Result result) {
166 if (result == instance_id::InstanceID::SUCCESS)
167 Respond(NoArguments());
168 else
169 Respond(Error(InstanceIDResultToError(result)));
172 InstanceIDDeleteIDFunction::InstanceIDDeleteIDFunction() {}
174 InstanceIDDeleteIDFunction::~InstanceIDDeleteIDFunction() {}
176 ExtensionFunction::ResponseAction InstanceIDDeleteIDFunction::DoWork() {
177 GetInstanceID()->DeleteID(
178 base::Bind(&InstanceIDDeleteIDFunction::DeleteIDCompleted, this));
180 return RespondLater();
183 void InstanceIDDeleteIDFunction::DeleteIDCompleted(
184 instance_id::InstanceID::Result result) {
185 if (result == instance_id::InstanceID::SUCCESS)
186 Respond(NoArguments());
187 else
188 Respond(Error(InstanceIDResultToError(result)));
191 } // namespace extensions