| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 net::URLRequestContextGetter* context = profile()->GetRequestContext(); | 121 net::URLRequestContextGetter* context = profile()->GetRequestContext(); |
| 119 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 122 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
| 120 if (!token_service) { | 123 if (!token_service) { |
| 121 ReportResult(std::string(), std::string(kTokenServiceNotAvailable)); | 124 ReportResult(std::string(), std::string(kTokenServiceNotAvailable)); |
| 122 return false; | 125 return false; |
| 123 } | 126 } |
| 124 const std::string& refresh_token = | 127 const std::string& refresh_token = |
| 125 token_service->GetOAuth2LoginRefreshToken(); | 128 token_service->GetOAuth2LoginRefreshToken(); |
| 126 fetcher_.reset(new ObfuscatedGaiaIdFetcher(context, this, refresh_token)); | 129 fetcher_.reset(new ObfuscatedGaiaIdFetcher(context, this, refresh_token)); |
| 127 | 130 |
| 131 // Get the token cache and see if we have already cached a Gaia Id. |
| 132 TokenCacheService* token_cache = |
| 133 TokenCacheServiceFactory::GetForProfile(profile()); |
| 134 |
| 128 // Check the cache, if we already have a gaia ID, use it instead of | 135 // Check the cache, if we already have a gaia ID, use it instead of |
| 129 // fetching the ID over the network. | 136 // fetching the ID over the network. |
| 130 const std::string& gaia_id = | 137 const std::string& gaia_id = |
| 131 token_service->GetTokenForService(GaiaConstants::kObfuscatedGaiaId); | 138 token_cache->RetrieveToken(GaiaConstants::kObfuscatedGaiaId); |
| 132 if (!gaia_id.empty()) { | 139 if (!gaia_id.empty()) { |
| 133 ReportResult(gaia_id, std::string()); | 140 ReportResult(gaia_id, std::string()); |
| 134 return true; | 141 return true; |
| 135 } | 142 } |
| 136 | 143 |
| 137 fetcher_->Start(); | 144 fetcher_->Start(); |
| 138 | 145 |
| 139 // Will finish asynchronously. | 146 // Will finish asynchronously. |
| 140 return true; | 147 return true; |
| 141 } | 148 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 166 | 173 |
| 167 void PushMessagingGetChannelIdFunction::ReportResult( | 174 void PushMessagingGetChannelIdFunction::ReportResult( |
| 168 const std::string& gaia_id, const std::string& error_string) { | 175 const std::string& gaia_id, const std::string& error_string) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 170 | 177 |
| 171 BuildAndSendResult(gaia_id, error_string); | 178 BuildAndSendResult(gaia_id, error_string); |
| 172 | 179 |
| 173 // Cache the obfuscated ID locally. It never changes for this user, | 180 // Cache the obfuscated ID locally. It never changes for this user, |
| 174 // and if we call the web API too often, we get errors due to rate limiting. | 181 // and if we call the web API too often, we get errors due to rate limiting. |
| 175 if (!gaia_id.empty()) { | 182 if (!gaia_id.empty()) { |
| 176 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 183 base::TimeDelta timeout = |
| 177 if (token_service) { | 184 base::TimeDelta::FromDays(kObfuscatedGaiaIdTimeoutInDays); |
| 178 token_service->AddAuthTokenManually(GaiaConstants::kObfuscatedGaiaId, | 185 TokenCacheService* token_cache = |
| 179 gaia_id); | 186 TokenCacheServiceFactory::GetForProfile(profile()); |
| 180 } | 187 token_cache->StoreToken(GaiaConstants::kObfuscatedGaiaId, gaia_id, |
| 188 timeout); |
| 181 } | 189 } |
| 182 | 190 |
| 183 // Balanced in RunImpl. | 191 // Balanced in RunImpl. |
| 184 Release(); | 192 Release(); |
| 185 } | 193 } |
| 186 | 194 |
| 187 void PushMessagingGetChannelIdFunction::BuildAndSendResult( | 195 void PushMessagingGetChannelIdFunction::BuildAndSendResult( |
| 188 const std::string& gaia_id, const std::string& error_message) { | 196 const std::string& gaia_id, const std::string& error_message) { |
| 189 std::string channel_id; | 197 std::string channel_id; |
| 190 if (!gaia_id.empty()) { | 198 if (!gaia_id.empty()) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 handler_ = mapper.Pass(); | 302 handler_ = mapper.Pass(); |
| 295 } | 303 } |
| 296 | 304 |
| 297 template <> | 305 template <> |
| 298 void ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 306 void ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
| 299 DependsOn(ExtensionSystemFactory::GetInstance()); | 307 DependsOn(ExtensionSystemFactory::GetInstance()); |
| 300 DependsOn(ProfileSyncServiceFactory::GetInstance()); | 308 DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| 301 } | 309 } |
| 302 | 310 |
| 303 } // namespace extensions | 311 } // namespace extensions |
| OLD | NEW |