| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/push_messaging/push_messaging_api.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" | 14 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
| 15 #include "chrome/browser/extensions/event_names.h" | 15 #include "chrome/browser/extensions/event_names.h" |
| 16 #include "chrome/browser/extensions/event_router.h" | 16 #include "chrome/browser/extensions/event_router.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
| 19 #include "chrome/browser/extensions/extension_system_factory.h" | 19 #include "chrome/browser/extensions/extension_system_factory.h" |
| 20 #include "chrome/browser/extensions/token_cache/token_cache_service.h" |
| 21 #include "chrome/browser/extensions/token_cache/token_cache_service_factory.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/signin/token_service.h" | 23 #include "chrome/browser/signin/token_service.h" |
| 22 #include "chrome/browser/signin/token_service_factory.h" | 24 #include "chrome/browser/signin/token_service_factory.h" |
| 23 #include "chrome/browser/sync/profile_sync_service.h" | 25 #include "chrome/browser/sync/profile_sync_service.h" |
| 24 #include "chrome/browser/sync/profile_sync_service_factory.h" | 26 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 25 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | 27 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | 28 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/extensions/api/push_messaging.h" | 29 #include "chrome/common/extensions/api/push_messaging.h" |
| 28 #include "chrome/common/extensions/extension.h" | 30 #include "chrome/common/extensions/extension.h" |
| 29 #include "chrome/common/extensions/extension_set.h" | 31 #include "chrome/common/extensions/extension_set.h" |
| 30 #include "chrome/common/extensions/permissions/api_permission.h" | 32 #include "chrome/common/extensions/permissions/api_permission.h" |
| 31 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/notification_details.h" | 34 #include "content/public/browser/notification_details.h" |
| 33 #include "content/public/browser/notification_source.h" | 35 #include "content/public/browser/notification_source.h" |
| 34 #include "google_apis/gaia/gaia_constants.h" | 36 #include "google_apis/gaia/gaia_constants.h" |
| 35 #include "googleurl/src/gurl.h" | 37 #include "googleurl/src/gurl.h" |
| 36 | 38 |
| 37 using content::BrowserThread; | 39 using content::BrowserThread; |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 const char kChannelIdSeparator[] = "/"; | 42 const char kChannelIdSeparator[] = "/"; |
| 41 const char kUserNotSignedIn[] = "The user is not signed in."; | 43 const char kUserNotSignedIn[] = "The user is not signed in."; |
| 42 const char kTokenServiceNotAvailable[] = "Failed to get token service."; | 44 const char kTokenServiceNotAvailable[] = "Failed to get token service."; |
| 45 const int kObfuscatedGaiaIdTimeoutInDays = 30; |
| 43 } | 46 } |
| 44 | 47 |
| 45 namespace extensions { | 48 namespace extensions { |
| 46 | 49 |
| 47 namespace glue = api::push_messaging; | 50 namespace glue = api::push_messaging; |
| 48 | 51 |
| 49 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) | 52 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) |
| 50 : profile_(profile) { | 53 : profile_(profile) { |
| 51 } | 54 } |
| 52 | 55 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 net::URLRequestContextGetter* context = profile()->GetRequestContext(); | 126 net::URLRequestContextGetter* context = profile()->GetRequestContext(); |
| 124 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 127 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
| 125 if (!token_service) { | 128 if (!token_service) { |
| 126 ReportResult(std::string(), std::string(kTokenServiceNotAvailable)); | 129 ReportResult(std::string(), std::string(kTokenServiceNotAvailable)); |
| 127 return false; | 130 return false; |
| 128 } | 131 } |
| 129 const std::string& refresh_token = | 132 const std::string& refresh_token = |
| 130 token_service->GetOAuth2LoginRefreshToken(); | 133 token_service->GetOAuth2LoginRefreshToken(); |
| 131 fetcher_.reset(new ObfuscatedGaiaIdFetcher(context, this, refresh_token)); | 134 fetcher_.reset(new ObfuscatedGaiaIdFetcher(context, this, refresh_token)); |
| 132 | 135 |
| 136 // Get the token cache and see if we have already cached a Gaia Id. |
| 137 TokenCacheService* token_cache = |
| 138 TokenCacheServiceFactory::GetForProfile(profile()); |
| 139 |
| 133 // Check the cache, if we already have a gaia ID, use it instead of | 140 // Check the cache, if we already have a gaia ID, use it instead of |
| 134 // fetching the ID over the network. | 141 // fetching the ID over the network. |
| 135 const std::string& gaia_id = | 142 const std::string& gaia_id = |
| 136 token_service->GetTokenForService(GaiaConstants::kObfuscatedGaiaId); | 143 token_cache->RetrieveToken(GaiaConstants::kObfuscatedGaiaId); |
| 137 if (!gaia_id.empty()) { | 144 if (!gaia_id.empty()) { |
| 138 ReportResult(gaia_id, std::string()); | 145 ReportResult(gaia_id, std::string()); |
| 139 return true; | 146 return true; |
| 140 } | 147 } |
| 141 | 148 |
| 142 fetcher_->Start(); | 149 fetcher_->Start(); |
| 143 | 150 |
| 144 // Will finish asynchronously. | 151 // Will finish asynchronously. |
| 145 return true; | 152 return true; |
| 146 } | 153 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 171 | 178 |
| 172 void PushMessagingGetChannelIdFunction::ReportResult( | 179 void PushMessagingGetChannelIdFunction::ReportResult( |
| 173 const std::string& gaia_id, const std::string& error_string) { | 180 const std::string& gaia_id, const std::string& error_string) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 175 | 182 |
| 176 BuildAndSendResult(gaia_id, error_string); | 183 BuildAndSendResult(gaia_id, error_string); |
| 177 | 184 |
| 178 // Cache the obfuscated ID locally. It never changes for this user, | 185 // Cache the obfuscated ID locally. It never changes for this user, |
| 179 // and if we call the web API too often, we get errors due to rate limiting. | 186 // and if we call the web API too often, we get errors due to rate limiting. |
| 180 if (!gaia_id.empty()) { | 187 if (!gaia_id.empty()) { |
| 181 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 188 base::TimeDelta timeout = |
| 182 if (token_service) { | 189 base::TimeDelta::FromDays(kObfuscatedGaiaIdTimeoutInDays); |
| 183 token_service->AddAuthTokenManually(GaiaConstants::kObfuscatedGaiaId, | 190 TokenCacheService* token_cache = |
| 184 gaia_id); | 191 TokenCacheServiceFactory::GetForProfile(profile()); |
| 185 } | 192 token_cache->StoreToken(GaiaConstants::kObfuscatedGaiaId, gaia_id, |
| 193 timeout); |
| 186 } | 194 } |
| 187 | 195 |
| 188 // Balanced in RunImpl. | 196 // Balanced in RunImpl. |
| 189 Release(); | 197 Release(); |
| 190 } | 198 } |
| 191 | 199 |
| 192 void PushMessagingGetChannelIdFunction::BuildAndSendResult( | 200 void PushMessagingGetChannelIdFunction::BuildAndSendResult( |
| 193 const std::string& gaia_id, const std::string& error_message) { | 201 const std::string& gaia_id, const std::string& error_message) { |
| 194 std::string channel_id; | 202 std::string channel_id; |
| 195 if (!gaia_id.empty()) { | 203 if (!gaia_id.empty()) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 handler_ = mapper.Pass(); | 308 handler_ = mapper.Pass(); |
| 301 } | 309 } |
| 302 | 310 |
| 303 template <> | 311 template <> |
| 304 void ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 312 void ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
| 305 DependsOn(ExtensionSystemFactory::GetInstance()); | 313 DependsOn(ExtensionSystemFactory::GetInstance()); |
| 306 DependsOn(ProfileSyncServiceFactory::GetInstance()); | 314 DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| 307 } | 315 } |
| 308 | 316 |
| 309 } // namespace extensions | 317 } // namespace extensions |
| OLD | NEW |