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 | |
|
rkc
2014/10/28 04:12:45
.
Charlie
2014/10/28 22:51:51
Done.
| |
| 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& token) { | |
| 66 auth_token_ = token; | |
| 56 } | 67 } |
| 57 | 68 |
| 58 void CopresenceService::set_manager_for_testing( | 69 void CopresenceService::set_manager_for_testing( |
| 59 scoped_ptr<copresence::CopresenceManager> manager) { | 70 scoped_ptr<copresence::CopresenceManager> manager) { |
| 60 manager_ = manager.Pass(); | 71 manager_ = manager.Pass(); |
| 61 } | 72 } |
| 62 | 73 |
| 63 // static | 74 // static |
| 64 BrowserContextKeyedAPIFactory<CopresenceService>* | 75 BrowserContextKeyedAPIFactory<CopresenceService>* |
| 65 CopresenceService::GetFactoryInstance() { | 76 CopresenceService::GetFactoryInstance() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 } | 116 } |
| 106 | 117 |
| 107 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { | 118 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { |
| 108 return browser_context_->GetRequestContext(); | 119 return browser_context_->GetRequestContext(); |
| 109 } | 120 } |
| 110 | 121 |
| 111 const std::string CopresenceService::GetPlatformVersionString() const { | 122 const std::string CopresenceService::GetPlatformVersionString() const { |
| 112 return chrome::VersionInfo().CreateVersionString(); | 123 return chrome::VersionInfo().CreateVersionString(); |
| 113 } | 124 } |
| 114 | 125 |
| 115 const std::string CopresenceService::GetAPIKey() const { | 126 const std::string CopresenceService::GetAPIKey(const std::string& app_id) |
| 116 return api_key_; | 127 const { |
| 128 // This won't be const if we use map[] | |
| 129 const auto& key = api_keys_by_app_.find(app_id); | |
| 130 return key == api_keys_by_app_.end() ? std::string() : key->second; | |
| 131 } | |
| 132 | |
| 133 const std::string CopresenceService::GetAuthToken() const { | |
| 134 return auth_token_; | |
| 117 } | 135 } |
| 118 | 136 |
| 119 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | 137 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { |
| 120 return whispernet_client(); | 138 return whispernet_client(); |
| 121 } | 139 } |
| 122 | 140 |
| 123 template <> | 141 template <> |
| 124 void | 142 void |
| 125 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { | 143 BrowserContextKeyedAPIFactory<CopresenceService>::DeclareFactoryDependencies() { |
| 126 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 144 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 127 } | 145 } |
| 128 | 146 |
| 129 // CopresenceExecuteFunction implementation: | 147 // CopresenceExecuteFunction implementation |
| 130 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { | 148 ExtensionFunction::ResponseAction CopresenceExecuteFunction::Run() { |
| 131 scoped_ptr<api::copresence::Execute::Params> params( | 149 scoped_ptr<api::copresence::Execute::Params> params( |
| 132 api::copresence::Execute::Params::Create(*args_)); | 150 api::copresence::Execute::Params::Create(*args_)); |
| 133 EXTENSION_FUNCTION_VALIDATE(params.get()); | 151 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 134 | 152 |
| 135 CopresenceService* service = | 153 CopresenceService* service = |
| 136 CopresenceService::GetFactoryInstance()->Get(browser_context()); | 154 CopresenceService::GetFactoryInstance()->Get(browser_context()); |
| 137 | 155 |
| 138 // This can only happen if we're shutting down. In all other cases, if we | 156 // 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. | 157 // don't have a manager, we'll create one. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 157 } | 175 } |
| 158 | 176 |
| 159 void CopresenceExecuteFunction::SendResult( | 177 void CopresenceExecuteFunction::SendResult( |
| 160 copresence::CopresenceStatus status) { | 178 copresence::CopresenceStatus status) { |
| 161 api::copresence::ExecuteStatus api_status = | 179 api::copresence::ExecuteStatus api_status = |
| 162 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS | 180 (status == copresence::SUCCESS) ? api::copresence::EXECUTE_STATUS_SUCCESS |
| 163 : api::copresence::EXECUTE_STATUS_FAILED; | 181 : api::copresence::EXECUTE_STATUS_FAILED; |
| 164 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | 182 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); |
| 165 } | 183 } |
| 166 | 184 |
| 167 // CopresenceSetApiKeyFunction implementation: | 185 // CopresenceSetApiKeyFunction implementation |
|
rkc
2014/10/28 04:12:45
.
Charlie
2014/10/28 22:51:51
Done.
| |
| 168 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | 186 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { |
| 169 scoped_ptr<api::copresence::SetApiKey::Params> params( | 187 scoped_ptr<api::copresence::SetApiKey::Params> params( |
| 170 api::copresence::SetApiKey::Params::Create(*args_)); | 188 api::copresence::SetApiKey::Params::Create(*args_)); |
| 171 EXTENSION_FUNCTION_VALIDATE(params.get()); | 189 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 172 | 190 |
| 173 // The api key may be set to empty, to clear it. | 191 // The api key may be set to empty, to clear it. |
| 174 CopresenceService::GetFactoryInstance()->Get(browser_context()) | 192 CopresenceService::GetFactoryInstance()->Get(browser_context()) |
| 175 ->set_api_key(params->api_key); | 193 ->set_api_key(extension_id(), params->api_key); |
| 176 return RespondNow(NoArguments()); | 194 return RespondNow(NoArguments()); |
| 177 } | 195 } |
| 178 | 196 |
| 197 // CopresenceSetAuthTokenFunction implementation | |
| 198 ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() { | |
| 199 scoped_ptr<api::copresence::SetAuthToken::Params> params( | |
| 200 api::copresence::SetAuthToken::Params::Create(*args_)); | |
| 201 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 202 | |
| 203 // The token may be set to empty, to clear it. | |
| 204 CopresenceService::GetFactoryInstance()->Get(browser_context()) | |
| 205 ->set_auth_token(params->token); | |
|
rkc
2014/10/28 04:12:45
Add a TODO here with a link to a bug tracking that
Charlie
2014/10/28 22:51:51
Done.
| |
| 206 return RespondNow(NoArguments()); | |
| 207 } | |
| 208 | |
| 179 } // namespace extensions | 209 } // namespace extensions |
| OLD | NEW |