| 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 "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "chrome/browser/copresence/chrome_whispernet_client.h" | 10 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { | 178 net::URLRequestContextGetter* CopresenceService::GetRequestContext() const { |
| 179 return browser_context_->GetRequestContext(); | 179 return browser_context_->GetRequestContext(); |
| 180 } | 180 } |
| 181 | 181 |
| 182 const std::string CopresenceService::GetPlatformVersionString() const { | 182 const std::string CopresenceService::GetPlatformVersionString() const { |
| 183 return chrome::VersionInfo().CreateVersionString(); | 183 return chrome::VersionInfo().CreateVersionString(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 const std::string | 186 const std::string |
| 187 CopresenceService::GetAPIKey(const std::string& app_id) const { | 187 CopresenceService::GetAPIKey(const std::string& app_id) const { |
| 188 // This won't be const if we use map[] | 188 // Check first if the app has set its key via the API. |
| 189 const auto& key = api_keys_by_app_.find(app_id); | 189 const auto& key = api_keys_by_app_.find(app_id); |
| 190 return key == api_keys_by_app_.end() ? std::string() : key->second; | 190 if (key != api_keys_by_app_.end()) |
| 191 } | 191 return key->second; |
| 192 | 192 |
| 193 const std::string | 193 // If no key was found, look in the manifest. |
| 194 CopresenceService::GetProjectId(const std::string& app_id) const { | 194 if (!app_id.empty()) { |
| 195 const Extension* extension = ExtensionRegistry::Get(browser_context_) | 195 const Extension* extension = ExtensionRegistry::Get(browser_context_) |
| 196 ->GetExtensionById(app_id, ExtensionRegistry::ENABLED); | 196 ->GetExtensionById(app_id, ExtensionRegistry::ENABLED); |
| 197 DCHECK(extension) << "Invalid extension ID"; | 197 DCHECK(extension) << "Invalid extension ID"; |
| 198 CopresenceManifestData* manifest_data = static_cast<CopresenceManifestData*>( | 198 CopresenceManifestData* manifest_data = |
| 199 extension->GetManifestData(manifest_keys::kCopresence)); | 199 static_cast<CopresenceManifestData*>( |
| 200 return manifest_data ? manifest_data->project_id : std::string(); | 200 extension->GetManifestData(manifest_keys::kCopresence)); |
| 201 if (manifest_data) |
| 202 return manifest_data->api_key; |
| 203 } |
| 204 |
| 205 return std::string(); |
| 201 } | 206 } |
| 202 | 207 |
| 203 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { | 208 copresence::WhispernetClient* CopresenceService::GetWhispernetClient() { |
| 204 return whispernet_client(); | 209 return whispernet_client(); |
| 205 } | 210 } |
| 206 | 211 |
| 207 gcm::GCMDriver* CopresenceService::GetGCMDriver() { | 212 gcm::GCMDriver* CopresenceService::GetGCMDriver() { |
| 208 gcm::GCMProfileService* gcm_service = | 213 gcm::GCMProfileService* gcm_service = |
| 209 gcm::GCMProfileServiceFactory::GetForProfile(browser_context_); | 214 gcm::GCMProfileServiceFactory::GetForProfile(browser_context_); |
| 210 return gcm_service ? gcm_service->driver() : nullptr; | 215 return gcm_service ? gcm_service->driver() : nullptr; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 : api::copresence::EXECUTE_STATUS_FAILED; | 280 : api::copresence::EXECUTE_STATUS_FAILED; |
| 276 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); | 281 Respond(ArgumentList(api::copresence::Execute::Results::Create(api_status))); |
| 277 } | 282 } |
| 278 | 283 |
| 279 // CopresenceSetApiKeyFunction implementation. | 284 // CopresenceSetApiKeyFunction implementation. |
| 280 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { | 285 ExtensionFunction::ResponseAction CopresenceSetApiKeyFunction::Run() { |
| 281 scoped_ptr<api::copresence::SetApiKey::Params> params( | 286 scoped_ptr<api::copresence::SetApiKey::Params> params( |
| 282 api::copresence::SetApiKey::Params::Create(*args_)); | 287 api::copresence::SetApiKey::Params::Create(*args_)); |
| 283 EXTENSION_FUNCTION_VALIDATE(params.get()); | 288 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 284 | 289 |
| 290 LOG(WARNING) << "copresence.setApiKey() is deprecated. " |
| 291 << "Put the key in the manifest at copresence.api_key instead."; |
| 292 |
| 285 // The api key may be set to empty, to clear it. | 293 // The api key may be set to empty, to clear it. |
| 286 CopresenceService::GetFactoryInstance()->Get(browser_context()) | 294 CopresenceService::GetFactoryInstance()->Get(browser_context()) |
| 287 ->set_api_key(extension_id(), params->api_key); | 295 ->set_api_key(extension_id(), params->api_key); |
| 288 return RespondNow(NoArguments()); | 296 return RespondNow(NoArguments()); |
| 289 } | 297 } |
| 290 | 298 |
| 291 // CopresenceSetAuthTokenFunction implementation | 299 // CopresenceSetAuthTokenFunction implementation |
| 292 ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() { | 300 ExtensionFunction::ResponseAction CopresenceSetAuthTokenFunction::Run() { |
| 293 scoped_ptr<api::copresence::SetAuthToken::Params> params( | 301 scoped_ptr<api::copresence::SetAuthToken::Params> params( |
| 294 api::copresence::SetAuthToken::Params::Create(*args_)); | 302 api::copresence::SetAuthToken::Params::Create(*args_)); |
| 295 EXTENSION_FUNCTION_VALIDATE(params.get()); | 303 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 296 | 304 |
| 297 // The token may be set to empty, to clear it. | 305 // The token may be set to empty, to clear it. |
| 298 CopresenceService::GetFactoryInstance()->Get(browser_context()) | 306 CopresenceService::GetFactoryInstance()->Get(browser_context()) |
| 299 ->set_auth_token(extension_id(), params->token); | 307 ->set_auth_token(extension_id(), params->token); |
| 300 return RespondNow(NoArguments()); | 308 return RespondNow(NoArguments()); |
| 301 } | 309 } |
| 302 | 310 |
| 303 } // namespace extensions | 311 } // namespace extensions |
| OLD | NEW |