1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/services/gcm/gcm_profile_service.h"
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <map>
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/logging.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/prefs/pref_service.h"
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/profiles/profile.h"
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/common/pref_names.h"
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if defined(OS_ANDROID)
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/gcm_driver/gcm_driver_android.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#else
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/bind.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_CHROMEOS)
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/services/gcm/chromeos_gcm_connection_observer.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/files/file_path.h"
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/weak_ptr.h"
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/services/gcm/gcm_account_tracker.h"
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/services/gcm/gcm_desktop_utils.h"
265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/signin/profile_identity_provider.h"
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/gcm_driver/gcm_client_factory.h"
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/gcm_driver/gcm_driver_desktop.h"
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/account_tracker.h"
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "google_apis/gaia/identity_provider.h"
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace gcm {
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if !defined(OS_ANDROID)
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Identity observer only has actual work to do when the user is actually signed
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// in. It ensures that account tracker is taking
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class GCMProfileService::IdentityObserver : public IdentityProvider::Observer {
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) public:
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  IdentityObserver(Profile* profile, GCMDriverDesktop* driver);
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual ~IdentityObserver();
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // IdentityProvider::Observer:
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual void OnActiveAccountLogin() OVERRIDE;
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual void OnActiveAccountLogout() OVERRIDE;
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string SignedInUserName() const;
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Called to inform IdentityObserver that a list of accounts was updated.
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |account_tokens| maps email addresses to OAuth2 access tokens.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void AccountsUpdated(
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::map<std::string, std::string>& account_tokens);
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) private:
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Profile* profile_;
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GCMDriverDesktop* driver_;
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_ptr<IdentityProvider> identity_provider_;
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<GCMAccountTracker> gcm_account_tracker_;
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The account ID that this service is responsible for. Empty when the service
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // is not running.
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string account_id_;
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_;
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
72f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(IdentityObserver);
73f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)};
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
75f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile,
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                      GCMDriverDesktop* driver)
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : profile_(profile), driver_(driver), weak_ptr_factory_(this) {
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  identity_provider_.reset(new ProfileIdentityProvider(
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SigninManagerFactory::GetForProfile(profile),
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      LoginUIServiceFactory::GetForProfile(profile)));
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  identity_provider_->AddObserver(this);
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  OnActiveAccountLogin();
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GCMProfileService::IdentityObserver::~IdentityObserver() {
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (gcm_account_tracker_)
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    gcm_account_tracker_->Shutdown();
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  identity_provider_->RemoveObserver(this);
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void GCMProfileService::IdentityObserver::OnActiveAccountLogin() {
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // This might be called multiple times when the password changes.
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const std::string account_id = identity_provider_->GetActiveAccountId();
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (account_id == account_id_)
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return;
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  account_id_ = account_id;
99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  driver_->OnSignedIn();
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!gcm_account_tracker_) {
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_ptr<gaia::AccountTracker> gaia_account_tracker(
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        new gaia::AccountTracker(identity_provider_.get(),
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 profile_->GetRequestContext()));
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    gcm_account_tracker_.reset(new GCMAccountTracker(
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        gaia_account_tracker.Pass(),
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        base::Bind(&GCMProfileService::IdentityObserver::AccountsUpdated,
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                   weak_ptr_factory_.GetWeakPtr())));
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  gcm_account_tracker_->Start();
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
115f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void GCMProfileService::IdentityObserver::OnActiveAccountLogout() {
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  account_id_.clear();
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Check is necessary to not crash browser_tests.
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (gcm_account_tracker_)
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    gcm_account_tracker_->Stop();
1221675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch  // When sign-in enforcement is not dropped, OnSignedOut will also clear all
1231675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch  // the GCM data and a new GCM ID will be retrieved after the user signs in
1241675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch  // again. Otherwise, the user sign-out will not affect the existing GCM
1251675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch  // data.
1261675a649fd7a8b3cb80ffddae2dc181f122353c5Ben Murdoch  driver_->OnSignedOut();
127f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
128f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
129f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)std::string GCMProfileService::IdentityObserver::SignedInUserName() const {
130f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return driver_->IsStarted() ? account_id_ : std::string();
131f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid GCMProfileService::IdentityObserver::AccountsUpdated(
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::map<std::string, std::string>& account_tokens) {
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  driver_->SetAccountsForCheckin(account_tokens);
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
137f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // !defined(OS_ANDROID)
138f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// static
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool GCMProfileService::IsGCMEnabled(Profile* profile) {
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled);
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// static
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void GCMProfileService::RegisterProfilePrefs(
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  registry->RegisterBooleanPref(
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      prefs::kGCMChannelEnabled,
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      true,
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
151f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PushMessagingServiceImpl::RegisterProfilePrefs(registry);
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
154f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_ANDROID)
155f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GCMProfileService::GCMProfileService(Profile* profile)
156f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : profile_(profile),
157f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      push_messaging_service_(this, profile) {
158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK(!profile->IsOffTheRecord());
159f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
160f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  driver_.reset(new GCMDriverAndroid);
161f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)GCMProfileService::GCMProfileService(
164cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Profile* profile,
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_ptr<GCMClientFactory> gcm_client_factory)
166f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : profile_(profile),
167f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      push_messaging_service_(this, profile) {
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(!profile->IsOffTheRecord());
169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
17046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  driver_ = CreateGCMDriverDesktop(
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      gcm_client_factory.Pass(),
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      profile_->GetPrefs(),
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      profile_->GetPath().Append(chrome::kGCMStoreDirname),
17446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      profile_->GetRequestContext());
175f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_CHROMEOS)
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  chromeos_connection_observer_.reset(new gcm::ChromeOSGCMConnectionObserver);
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  driver_->AddConnectionObserver(chromeos_connection_observer_.get());
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  identity_observer_.reset(new IdentityObserver(
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      profile, static_cast<gcm::GCMDriverDesktop*>(driver_.get())));
183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
184f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(OS_ANDROID)
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
186f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)GCMProfileService::GCMProfileService()
187f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : profile_(NULL),
188f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      push_messaging_service_(this, NULL) {
189f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)GCMProfileService::~GCMProfileService() {
192f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
193f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
194cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GCMProfileService::AddAppHandler(const std::string& app_id,
195cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      GCMAppHandler* handler) {
196cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (driver_)
197cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    driver_->AddAppHandler(app_id, handler);
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
200cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GCMProfileService::RemoveAppHandler(const std::string& app_id) {
201cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (driver_)
202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    driver_->RemoveAppHandler(app_id);
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GCMProfileService::Register(const std::string& app_id,
206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 const std::vector<std::string>& sender_ids,
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 const GCMDriver::RegisterCallback& callback) {
208cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (driver_)
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    driver_->Register(app_id, sender_ids, callback);
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GCMProfileService::Shutdown() {
213f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if !defined(OS_ANDROID)
214f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  identity_observer_.reset();
215f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // !defined(OS_ANDROID)
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (driver_) {
2171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_CHROMEOS)
2181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    driver_->RemoveConnectionObserver(chromeos_connection_observer_.get());
2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    chromeos_connection_observer_.reset();
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    driver_->Shutdown();
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    driver_.reset();
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
226f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)std::string GCMProfileService::SignedInUserName() const {
227f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_ANDROID)
228f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return std::string();
229f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return identity_observer_ ? identity_observer_->SignedInUserName()
231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                            : std::string();
232f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif  // defined(OS_ANDROID)
233f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
234f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
235cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GCMProfileService::SetDriverForTesting(GCMDriver* driver) {
236cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  driver_.reset(driver);
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace gcm
240