| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/event_names.h" | 9 #include "chrome/browser/extensions/event_names.h" |
| 10 #include "chrome/browser/extensions/event_router.h" | 10 #include "chrome/browser/extensions/event_router.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/signin/token_service.h" |
| 13 #include "chrome/browser/signin/token_service_factory.h" |
| 12 #include "chrome/common/extensions/api/experimental_push_messaging.h" | 14 #include "chrome/common/extensions/api/experimental_push_messaging.h" |
| 13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 14 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetche
r.h" |
| 18 |
| 19 using content::BrowserThread; |
| 15 | 20 |
| 16 namespace extensions { | 21 namespace extensions { |
| 17 | 22 |
| 18 namespace glue = extensions::api::experimental_push_messaging; | 23 namespace glue = extensions::api::experimental_push_messaging; |
| 19 | 24 |
| 20 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) | 25 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) |
| 21 : profile_(profile) { | 26 : profile_(profile) { |
| 22 } | 27 } |
| 23 | 28 |
| 24 PushMessagingEventRouter::~PushMessagingEventRouter() {} | 29 PushMessagingEventRouter::~PushMessagingEventRouter() {} |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 | 41 |
| 37 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); | 42 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); |
| 38 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 43 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 39 extension_id, | 44 extension_id, |
| 40 event_names::kOnPushMessage, | 45 event_names::kOnPushMessage, |
| 41 args.Pass(), | 46 args.Pass(), |
| 42 profile_, | 47 profile_, |
| 43 GURL()); | 48 GURL()); |
| 44 } | 49 } |
| 45 | 50 |
| 51 PushMessagingGetChannelIdFunction::PushMessagingGetChannelIdFunction() {} |
| 52 |
| 53 PushMessagingGetChannelIdFunction::~PushMessagingGetChannelIdFunction() {} |
| 54 |
| 55 bool PushMessagingGetChannelIdFunction::RunImpl() { |
| 56 // Start the async fetch of the GAIA ID. |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 net::URLRequestContextGetter* context = profile()->GetRequestContext(); |
| 59 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
| 60 const std::string& refresh_token = |
| 61 token_service->GetOAuth2LoginRefreshToken(); |
| 62 fetcher_.reset(new ObfuscatedGaiaIdFetcher(context, this, refresh_token)); |
| 63 |
| 64 // Balanced in ReportResult() |
| 65 AddRef(); |
| 66 |
| 67 fetcher_->Start(); |
| 68 |
| 69 // Will finish asynchronously. |
| 70 return true; |
| 71 } |
| 72 |
| 73 void PushMessagingGetChannelIdFunction::ReportResult( |
| 74 const std::string& gaia_id, long status) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 76 // Unpack the status and GaiaId parameters, and use it to build the |
| 77 // channel ID here. |
| 78 std::string channel_id(gaia_id); |
| 79 if (!gaia_id.empty()) { |
| 80 channel_id += "."; |
| 81 channel_id += extension_id(); |
| 82 } else if (status == 0) { |
| 83 // If somehow we don't have an error set, but there is no GAIA ID, |
| 84 // return an error now. |
| 85 DCHECK(status != 0); |
| 86 status = GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS; |
| 87 } |
| 88 |
| 89 // TODO(petewil): It may be a good idea to further |
| 90 // obfuscate the channel ID to prevent the user's obfuscated GAIA ID |
| 91 // from being readily obtained. Security review will tell us if we need to. |
| 92 |
| 93 // Create a ChannelId results object and set the fields. |
| 94 glue::ChannelIdResult result; |
| 95 result.channel_id = channel_id; |
| 96 result.status = status; |
| 97 results_ = glue::GetChannelId::Results::Create(result); |
| 98 SendResponse(true); |
| 99 |
| 100 // Balanced in RunImpl |
| 101 Release(); |
| 102 } |
| 103 |
| 104 void PushMessagingGetChannelIdFunction::OnObfuscatedGaiaIdFetchSuccess( |
| 105 const std::string& gaia_id) { |
| 106 ReportResult(gaia_id, 0); |
| 107 } |
| 108 |
| 109 void PushMessagingGetChannelIdFunction::OnObfuscatedGaiaIdFetchFailure( |
| 110 const GoogleServiceAuthError& error) { |
| 111 ReportResult(std::string(), error.state()); |
| 112 } |
| 113 |
| 46 } // namespace extensions | 114 } // namespace extensions |
| OLD | NEW |