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

Side by Side Diff: chrome/browser/extensions/api/instance_id/instance_id_api.cc

Issue 1128123003: Implement InstanceID API functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix test 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/instance_id/instance_id_api.h" 5 #include "chrome/browser/extensions/api/instance_id/instance_id_api.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service.h"
10 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service_fa ctory.h"
11 #include "chrome/common/extensions/api/instance_id.h"
12 #include "components/gcm_driver/instance_id/instance_id_driver.h"
8 #include "extensions/common/extension.h" 13 #include "extensions/common/extension.h"
9 14
10 namespace extensions { 15 namespace extensions {
11 16
17 namespace {
18
19 // Error messages.
20 const char kInvalidParameter[] = "Function was called with invalid parameters.";
21 const char kDisabled[] = "Instance ID is currently disabled.";
22 const char kNetworkError[] = "Network error occurred.";
23 const char kServerError[] = "Server error occurred.";
24 const char kUnknownError[] = "Unknown error occurred.";
25
26 const char* InstanceIDResultToError(instance_id::InstanceID::Result result) {
27 switch (result) {
28 case instance_id::InstanceID::INVALID_PARAMETER:
29 return kInvalidParameter;
30 case instance_id::InstanceID::DISABLED:
31 return kDisabled;
32 case instance_id::InstanceID::NETWORK_ERROR:
33 return kNetworkError;
34 case instance_id::InstanceID::SERVER_ERROR:
35 return kServerError;
36 case instance_id::InstanceID::UNKNOWN_ERROR:
37 return kUnknownError;
38 default:
39 NOTREACHED() << "Unexpected value of result cannot be converted: "
40 << result;
41 }
42 return "";
43 }
44
45 } // namespace
46
47 InstanceIDApiFunction::InstanceIDApiFunction() {
48 }
49
50 InstanceIDApiFunction::~InstanceIDApiFunction() {
51 }
52
53 ExtensionFunction::ResponseAction InstanceIDApiFunction::Run() {
54 if (Profile::FromBrowserContext(browser_context())->IsOffTheRecord()) {
55 return RespondNow(Error(
56 "chrome.instanceID not supported in incognito mode"));
57 }
58
59 if (!IsEnabled()) {
60 return RespondNow(Error(
61 InstanceIDResultToError(instance_id::InstanceID::DISABLED)));
62 }
63
64 return DoWork();
65 }
66
67 bool InstanceIDApiFunction::IsEnabled() const {
68 Profile* profile = Profile::FromBrowserContext(browser_context());
69
70 return instance_id::InstanceIDProfileService::IsInstanceIDEnabled(profile);
71 }
72
73 instance_id::InstanceID* InstanceIDApiFunction::GetInstanceID() const {
74 return instance_id::InstanceIDProfileServiceFactory::GetForProfile(
75 Profile::FromBrowserContext(browser_context()))->driver()->
76 GetInstanceID(extension()->id());
77 }
78
12 InstanceIDGetIDFunction::InstanceIDGetIDFunction() {} 79 InstanceIDGetIDFunction::InstanceIDGetIDFunction() {}
13 80
14 InstanceIDGetIDFunction::~InstanceIDGetIDFunction() {} 81 InstanceIDGetIDFunction::~InstanceIDGetIDFunction() {}
15 82
16 ExtensionFunction::ResponseAction InstanceIDGetIDFunction::Run() { 83 ExtensionFunction::ResponseAction InstanceIDGetIDFunction::DoWork() {
17 NOTIMPLEMENTED(); 84 return RespondNow(
18 return RespondLater(); 85 OneArgument(new base::StringValue(GetInstanceID()->GetID())));
19 } 86 }
20 87
21 InstanceIDGetCreationTimeFunction::InstanceIDGetCreationTimeFunction() {} 88 InstanceIDGetCreationTimeFunction::InstanceIDGetCreationTimeFunction() {}
22 89
23 InstanceIDGetCreationTimeFunction::~InstanceIDGetCreationTimeFunction() {} 90 InstanceIDGetCreationTimeFunction::~InstanceIDGetCreationTimeFunction() {}
24 91
25 ExtensionFunction::ResponseAction InstanceIDGetCreationTimeFunction::Run() { 92 ExtensionFunction::ResponseAction InstanceIDGetCreationTimeFunction::DoWork() {
26 NOTIMPLEMENTED(); 93 return RespondNow(OneArgument(
27 return RespondLater(); 94 new base::FundamentalValue(
95 GetInstanceID()->GetCreationTime().ToDoubleT())));
28 } 96 }
29 97
30 InstanceIDGetTokenFunction::InstanceIDGetTokenFunction() {} 98 InstanceIDGetTokenFunction::InstanceIDGetTokenFunction() {}
31 99
32 InstanceIDGetTokenFunction::~InstanceIDGetTokenFunction() {} 100 InstanceIDGetTokenFunction::~InstanceIDGetTokenFunction() {}
33 101
34 ExtensionFunction::ResponseAction InstanceIDGetTokenFunction::Run() { 102 ExtensionFunction::ResponseAction InstanceIDGetTokenFunction::DoWork() {
35 NOTIMPLEMENTED(); 103 scoped_ptr<api::instance_id::GetToken::Params> params =
104 api::instance_id::GetToken::Params::Create(*args_);
105 EXTENSION_FUNCTION_VALIDATE(params.get());
106
107 std::map<std::string, std::string> options;
108 if (params->get_token_params.options.get()) {
109 base::DictionaryValue::Iterator iter(
110 params->get_token_params.options->additional_properties);
111 for (; !iter.IsAtEnd(); iter.Advance()) {
112 const base::StringValue* string_value = NULL;
113 if (!iter.value().GetAsString(&string_value)) {
114 return RespondNow(Error(InstanceIDResultToError(
115 instance_id::InstanceID::INVALID_PARAMETER)));
116 }
117 options[iter.key()] = string_value->GetString();
118 }
119 }
120
121 GetInstanceID()->GetToken(
122 params->get_token_params.authorized_entity,
123 params->get_token_params.scope,
124 options,
125 base::Bind(&InstanceIDGetTokenFunction::GetTokenCompleted, this));
126
36 return RespondLater(); 127 return RespondLater();
37 } 128 }
38 129
130 void InstanceIDGetTokenFunction::GetTokenCompleted(
131 const std::string& token,
132 instance_id::InstanceID::Result result) {
133 if (result == instance_id::InstanceID::SUCCESS)
134 Respond(OneArgument(new base::StringValue(token)));
135 else
136 Respond(Error(InstanceIDResultToError(result)));
137 }
138
39 InstanceIDDeleteTokenFunction::InstanceIDDeleteTokenFunction() {} 139 InstanceIDDeleteTokenFunction::InstanceIDDeleteTokenFunction() {}
40 140
41 InstanceIDDeleteTokenFunction::~InstanceIDDeleteTokenFunction() {} 141 InstanceIDDeleteTokenFunction::~InstanceIDDeleteTokenFunction() {}
42 142
43 ExtensionFunction::ResponseAction InstanceIDDeleteTokenFunction::Run() { 143 ExtensionFunction::ResponseAction InstanceIDDeleteTokenFunction::DoWork() {
44 NOTIMPLEMENTED(); 144 scoped_ptr<api::instance_id::DeleteToken::Params> params =
145 api::instance_id::DeleteToken::Params::Create(*args_);
146 EXTENSION_FUNCTION_VALIDATE(params.get());
147
148 GetInstanceID()->DeleteToken(
149 params->delete_token_params.authorized_entity,
150 params->delete_token_params.scope,
151 base::Bind(&InstanceIDDeleteTokenFunction::DeleteTokenCompleted, this));
152
45 return RespondLater(); 153 return RespondLater();
46 } 154 }
47 155
156 void InstanceIDDeleteTokenFunction::DeleteTokenCompleted(
157 instance_id::InstanceID::Result result) {
158 if (result == instance_id::InstanceID::SUCCESS)
159 Respond(NoArguments());
160 else
161 Respond(Error(InstanceIDResultToError(result)));
162 }
163
48 InstanceIDDeleteIDFunction::InstanceIDDeleteIDFunction() {} 164 InstanceIDDeleteIDFunction::InstanceIDDeleteIDFunction() {}
49 165
50 InstanceIDDeleteIDFunction::~InstanceIDDeleteIDFunction() {} 166 InstanceIDDeleteIDFunction::~InstanceIDDeleteIDFunction() {}
51 167
52 ExtensionFunction::ResponseAction InstanceIDDeleteIDFunction::Run() { 168 ExtensionFunction::ResponseAction InstanceIDDeleteIDFunction::DoWork() {
53 NOTIMPLEMENTED(); 169 GetInstanceID()->DeleteID(
170 base::Bind(&InstanceIDDeleteIDFunction::DeleteIDCompleted, this));
171
54 return RespondLater(); 172 return RespondLater();
55 } 173 }
56 174
175 void InstanceIDDeleteIDFunction::DeleteIDCompleted(
176 instance_id::InstanceID::Result result) {
177 if (result == instance_id::InstanceID::SUCCESS)
178 Respond(NoArguments());
179 else
180 Respond(Error(InstanceIDResultToError(result)));
181 }
182
57 } // namespace extensions 183 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698