OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/copresence/copresence_api.h" | 5 #include "chrome/browser/extensions/api/copresence/copresence_api.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
9 #include "chrome/browser/copresence/chrome_whispernet_client.h" | 9 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
10 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 10 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 manager_.reset(new copresence::CopresenceManagerImpl(this)); | 51 manager_.reset(new copresence::CopresenceManagerImpl(this)); |
52 return manager_.get(); | 52 return manager_.get(); |
53 } | 53 } |
54 | 54 |
55 copresence::WhispernetClient* CopresenceService::whispernet_client() { | 55 copresence::WhispernetClient* CopresenceService::whispernet_client() { |
56 if (!whispernet_client_ && !is_shutting_down_) | 56 if (!whispernet_client_ && !is_shutting_down_) |
57 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); | 57 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); |
58 return whispernet_client_.get(); | 58 return whispernet_client_.get(); |
59 } | 59 } |
60 | 60 |
| 61 const std::string CopresenceService::auth_token(const std::string& app_id) |
| 62 const { |
| 63 // This won't be const if we use map[] |
| 64 const auto& key = auth_tokens_by_app_.find(app_id); |
| 65 return key == auth_tokens_by_app_.end() ? std::string() : key->second; |
| 66 } |
| 67 |
61 void CopresenceService::set_api_key(const std::string& app_id, | 68 void CopresenceService::set_api_key(const std::string& app_id, |
62 const std::string& api_key) { | 69 const std::string& api_key) { |
63 DCHECK(!app_id.empty()); | 70 DCHECK(!app_id.empty()); |
64 api_keys_by_app_[app_id] = api_key; | 71 api_keys_by_app_[app_id] = api_key; |
65 } | 72 } |
66 | 73 |
67 void CopresenceService::set_auth_token(const std::string& token) { | 74 void CopresenceService::set_auth_token(const std::string& app_id, |
68 auth_token_ = token; | 75 const std::string& token) { |
| 76 DCHECK(!app_id.empty()); |
| 77 auth_tokens_by_app_[app_id] = token; |
69 } | 78 } |
70 | 79 |
71 void CopresenceService::set_manager_for_testing( | 80 void CopresenceService::set_manager_for_testing( |
72 scoped_ptr<copresence::CopresenceManager> manager) { | 81 scoped_ptr<copresence::CopresenceManager> manager) { |
73 manager_ = manager.Pass(); | 82 manager_ = manager.Pass(); |
74 } | 83 } |
75 | 84 |
76 // static | 85 // static |
77 BrowserContextKeyedAPIFactory<CopresenceService>* | 86 BrowserContextKeyedAPIFactory<CopresenceService>* |
78 CopresenceService::GetFactoryInstance() { | 87 CopresenceService::GetFactoryInstance() { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (!PrepareReportRequestProto(params->operations, | 186 if (!PrepareReportRequestProto(params->operations, |
178 extension_id(), | 187 extension_id(), |
179 &service->apps_by_subscription_id(), | 188 &service->apps_by_subscription_id(), |
180 &request)) { | 189 &request)) { |
181 return RespondNow(Error(kInvalidOperationsMessage)); | 190 return RespondNow(Error(kInvalidOperationsMessage)); |
182 } | 191 } |
183 | 192 |
184 service->manager()->ExecuteReportRequest( | 193 service->manager()->ExecuteReportRequest( |
185 request, | 194 request, |
186 extension_id(), | 195 extension_id(), |
187 service->auth_token(), | 196 service->auth_token(extension_id()), |
188 base::Bind(&CopresenceExecuteFunction::SendResult, this)); | 197 base::Bind(&CopresenceExecuteFunction::SendResult, this)); |
189 return RespondLater(); | 198 return RespondLater(); |
190 } | 199 } |
191 | 200 |
192 void CopresenceExecuteFunction::SendResult( | 201 void CopresenceExecuteFunction::SendResult( |
193 copresence::CopresenceStatus status) { | 202 copresence::CopresenceStatus status) { |
194 api::copresence::ExecuteStatus api_status = | 203 api::copresence::ExecuteStatus api_status = |
195 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | 204 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS |
196 : api::copresence::EXECUTE_STATUS_FAILED; | 205 : api::copresence::EXECUTE_STATUS_FAILED; |
197 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | 206 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); |
(...skipping 11 matching lines...) Expand all Loading... |
209 return RespondNow(NoArguments()); | 218 return RespondNow(NoArguments()); |
210 } | 219 } |
211 | 220 |
212 // CopresenceSetAuthTokenFunction implementation | 221 // CopresenceSetAuthTokenFunction implementation |
213 ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() { | 222 ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() { |
214 scoped_ptr<api::copresence::SetAuthToken::Params> params( | 223 scoped_ptr<api::copresence::SetAuthToken::Params> params( |
215 api::copresence::SetAuthToken::Params::Create(*args_)); | 224 api::copresence::SetAuthToken::Params::Create(*args_)); |
216 EXTENSION_FUNCTION_VALIDATE(params.get()); | 225 EXTENSION_FUNCTION_VALIDATE(params.get()); |
217 | 226 |
218 // The token may be set to empty, to clear it. | 227 // The token may be set to empty, to clear it. |
219 // TODO(ckehoe): Scope the auth token appropriately (crbug/423517). | |
220 CopresenceService::GetFactoryInstance()->Get(browser_context()) | 228 CopresenceService::GetFactoryInstance()->Get(browser_context()) |
221 ->set_auth_token(params->token); | 229 ->set_auth_token(extension_id(), params->token); |
222 return RespondNow(NoArguments()); | 230 return RespondNow(NoArguments()); |
223 } | 231 } |
224 | 232 |
225 } // namespace extensions | 233 } // namespace extensions |
OLD | NEW |