Chromium Code Reviews| 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/common/chrome_version_info.h" | 10 #include "chrome/common/chrome_version_info.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 24 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService>> | 24 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresenceService>> |
| 25 g_factory = LAZY_INSTANCE_INITIALIZER; | 25 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 26 | 26 |
| 27 const char kInvalidOperationsMessage[] = | 27 const char kInvalidOperationsMessage[] = |
| 28 "Invalid operation in operations array."; | 28 "Invalid operation in operations array."; |
| 29 const char kShuttingDownMessage[] = "Shutting down."; | 29 const char kShuttingDownMessage[] = "Shutting down."; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 // CopresenceService implementation: | 33 |
| 34 // CopresenceService implementation | |
| 34 | 35 |
| 35 CopresenceService::CopresenceService(content::BrowserContext* context) | 36 CopresenceService::CopresenceService(content::BrowserContext* context) |
| 36 : is_shutting_down_(false), browser_context_(context) {} | 37 : is_shutting_down_(false), browser_context_(context) {} |
| 37 | 38 |
| 38 CopresenceService::~CopresenceService() {} | 39 CopresenceService::~CopresenceService() {} |
| 39 | 40 |
| 41 void CopresenceService::Shutdown() { | |
| 42 is_shutting_down_ = true; | |
| 43 manager_.reset(); | |
| 44 whispernet_client_.reset(); | |
| 45 } | |
| 46 | |
| 40 copresence::CopresenceManager* CopresenceService::manager() { | 47 copresence::CopresenceManager* CopresenceService::manager() { |
| 41 if (!manager_ && !is_shutting_down_) | 48 if (!manager_ && !is_shutting_down_) |
| 42 manager_ = copresence::CopresenceManager::Create(this); | 49 manager_ = copresence::CopresenceManager::Create(this); |
| 43 return manager_.get(); | 50 return manager_.get(); |
| 44 } | 51 } |
| 45 | 52 |
| 46 copresence::WhispernetClient* CopresenceService::whispernet_client() { | 53 copresence::WhispernetClient* CopresenceService::whispernet_client() { |
| 47 if (!whispernet_client_ && !is_shutting_down_) | 54 if (!whispernet_client_ && !is_shutting_down_) |
| 48 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); | 55 whispernet_client_.reset(new ChromeWhispernetClient(browser_context_)); |
| 49 return whispernet_client_.get(); | 56 return whispernet_client_.get(); |
| 50 } | 57 } |
| 51 | 58 |
| 52 void CopresenceService::Shutdown() { | 59 void CopresenceService::set_api_key(const std::string& app_id, |
| 53 is_shutting_down_ = true; | 60 const std::string& api_key) { |
| 54 manager_.reset(); | 61 DCHECK(!app_id.empty()); |
| 55 whispernet_client_.reset(); | 62 api_keys_by_app_[app_id] = api_key; |
| 63 } | |
| 64 | |
| 65 void CopresenceService::set_auth_token(const std::string& app_id, | |
| 66 const std::string& token) { | |
| 67 DCHECK(!app_id.empty()); | |
| 68 auth_tokens_by_app_[app_id] = token; | |
| 56 } | 69 } |
| 57 | 70 |
| 58 void CopresenceService::set_manager_for_testing( | 71 void CopresenceService::set_manager_for_testing( |
| 59 scoped_ptr<copresence::CopresenceManager> manager) { | 72 scoped_ptr<copresence::CopresenceManager> manager) { |
| 60 manager_ = manager.Pass(); | 73 manager_ = manager.Pass(); |
| 61 } | 74 } |
| 62 | 75 |
| 63 // static | 76 // static |
| 64 BrowserContextKeyedAPIFactory<CopresenceService>* | 77 BrowserContextKeyedAPIFactory<CopresenceService>* |
| 65 CopresenceService::GetFactoryInstance() { | 78 CopresenceService::GetFactoryInstance() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 } | 118 } |
| 106 | 119 |
| 107 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { | 120 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { |
| 108 return browser_context_->GetRequestContext(); | 121 return browser_context_->GetRequestContext(); |
| 109 } | 122 } |
| 110 | 123 |
| 111 const std::string CopresenceService::GetPlatformVersionString() const { | 124 const std::string CopresenceService::GetPlatformVersionString() const { |
| 112 return chrome::VersionInfo().CreateVersionString(); | 125 return chrome::VersionInfo().CreateVersionString(); |
| 113 } | 126 } |
| 114 | 127 |
| 115 const std::string CopresenceService::GetAPIKey() const { | 128 const std::string CopresenceService::GetAPIKey(const std::string& app_id) |
| 116 return api_key_; | 129 const { |
| 130 // This won't be const if we use map[] | |
| 131 auto key = api_keys_by_app_.find(app_id); | |
|
rkc
2014/10/21 22:48:38
const auto& ?
Charlie
2014/10/21 23:19:44
Done.
| |
| 132 return key == api_keys_by_app_.end() ? "" : key->second; | |
| 133 } | |
| 134 | |
| 135 const std::string CopresenceService::GetAuthToken(const std::string& app_id) | |
| 136 const { | |
| 137 auto token = auth_tokens_by_app_.find(app_id); | |
| 138 return token == auth_tokens_by_app_.end() ? "" : token->second; | |
| 117 } | 139 } |
| 118 | 140 |
| 119 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | 141 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { |
| 120 return whispernet_client(); | 142 return whispernet_client(); |
| 121 } | 143 } |
| 122 | 144 |
| 123 template <> | 145 template <> |
| 124 void | 146 void |
| 125 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { | 147 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { |
| 126 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 148 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 127 } | 149 } |
| 128 | 150 |
| 129 // CopresenceExecuteFunction implementation: | 151 // CopresenceExecuteFunction implementation |
| 130 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { | 152 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { |
| 131 scoped_ptr<api::copresence::Execute::Params> params( | 153 scoped_ptr<api::copresence::Execute::Params> params( |
| 132 api::copresence::Execute::Params::Create(*args_)); | 154 api::copresence::Execute::Params::Create(*args_)); |
| 133 EXTENSION_FUNCTION_VALIDATE(params.get()); | 155 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 134 | 156 |
| 135 CopresenceService* service = | 157 CopresenceService* service = |
| 136 CopresenceService::GetFactoryInstance()->Get(browser_context()); | 158 CopresenceService::GetFactoryInstance()->Get(browser_context()); |
| 137 | 159 |
| 138 // This can only happen if we're shutting down. In all other cases, if we | 160 // This can only happen if we're shutting down. In all other cases, if we |
| 139 // don't have a manager, we'll create one. | 161 // don't have a manager, we'll create one. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 157 } | 179 } |
| 158 | 180 |
| 159 void CopresenceExecuteFunction::SendResult( | 181 void CopresenceExecuteFunction::SendResult( |
| 160 copresence::CopresenceStatus status) { | 182 copresence::CopresenceStatus status) { |
| 161 api::copresence::ExecuteStatus api_status = | 183 api::copresence::ExecuteStatus api_status = |
| 162 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | 184 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS |
| 163 : api::copresence::EXECUTE_STATUS_FAILED; | 185 : api::copresence::EXECUTE_STATUS_FAILED; |
| 164 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | 186 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); |
| 165 } | 187 } |
| 166 | 188 |
| 167 // CopresenceSetApiKeyFunction implementation: | 189 // CopresenceSetApiKeyFunction implementation |
| 168 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | 190 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { |
| 169 scoped_ptr<api::copresence::SetApiKey::Params> params( | 191 scoped_ptr<api::copresence::SetApiKey::Params> params( |
| 170 api::copresence::SetApiKey::Params::Create(*args_)); | 192 api::copresence::SetApiKey::Params::Create(*args_)); |
| 171 EXTENSION_FUNCTION_VALIDATE(params.get()); | 193 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 172 | 194 |
| 173 // The api key may be set to empty, to clear it. | 195 // The api key may be set to empty, to clear it. |
| 174 CopresenceService::GetFactoryInstance()->Get(browser_context()) | 196 CopresenceService::GetFactoryInstance()->Get(browser_context()) |
| 175 ->set_api_key(params->api_key); | 197 ->set_api_key(extension_id(), params->api_key); |
| 176 return RespondNow(NoArguments()); | 198 return RespondNow(NoArguments()); |
| 177 } | 199 } |
| 178 | 200 |
| 201 // CopresenceSetAuthTokenFunction implementation | |
| 202 ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() { | |
| 203 scoped_ptr<api::copresence::SetAuthToken::Params> params( | |
| 204 api::copresence::SetAuthToken::Params::Create(*args_)); | |
| 205 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 206 | |
| 207 // The token may be set to empty, to clear it. | |
| 208 CopresenceService::GetFactoryInstance()->Get(browser_context()) | |
| 209 ->set_auth_token(extension_id(), params->token); | |
| 210 return RespondNow(NoArguments()); | |
| 211 } | |
| 212 | |
| 179 } // namespace extensions | 213 } // namespace extensions |
| OLD | NEW |