1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/services/gcm/gcm_profile_service.h"
9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "components/gcm_driver/gcm_driver.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
16 #if defined(OS_ANDROID)
17 #include "base/sequenced_task_runner.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "components/gcm_driver/gcm_driver_android.h"
20 #include "content/public/browser/browser_thread.h"
22 #include "base/bind.h"
23 #include "base/files/file_path.h"
24 #include "base/memory/weak_ptr.h"
25 #include "chrome/browser/services/gcm/gcm_desktop_utils.h"
26 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
27 #include "chrome/browser/signin/signin_manager_factory.h"
28 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
29 #include "components/gcm_driver/gcm_account_tracker.h"
30 #include "components/gcm_driver/gcm_channel_status_syncer.h"
31 #include "components/gcm_driver/gcm_client_factory.h"
32 #include "components/gcm_driver/gcm_driver_desktop.h"
33 #include "components/signin/core/browser/profile_identity_provider.h"
34 #include "components/signin/core/browser/signin_manager.h"
35 #include "google_apis/gaia/account_tracker.h"
36 #include "google_apis/gaia/identity_provider.h"
37 #include "net/url_request/url_request_context_getter.h"
42 #if !defined(OS_ANDROID)
43 // Identity observer only has actual work to do when the user is actually signed
44 // in. It ensures that account tracker is taking
45 class GCMProfileService::IdentityObserver
: public IdentityProvider::Observer
{
47 IdentityObserver(Profile
* profile
, GCMDriver
* driver
);
48 ~IdentityObserver() override
;
50 // IdentityProvider::Observer:
51 void OnActiveAccountLogin() override
;
52 void OnActiveAccountLogout() override
;
55 void StartAccountTracker();
59 scoped_ptr
<IdentityProvider
> identity_provider_
;
60 scoped_ptr
<GCMAccountTracker
> gcm_account_tracker_
;
62 // The account ID that this service is responsible for. Empty when the service
64 std::string account_id_
;
66 base::WeakPtrFactory
<GCMProfileService::IdentityObserver
> weak_ptr_factory_
;
68 DISALLOW_COPY_AND_ASSIGN(IdentityObserver
);
71 GCMProfileService::IdentityObserver::IdentityObserver(Profile
* profile
,
73 : profile_(profile
), driver_(driver
), weak_ptr_factory_(this) {
74 identity_provider_
.reset(new ProfileIdentityProvider(
75 SigninManagerFactory::GetForProfile(profile
),
76 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
77 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(profile
)));
78 identity_provider_
->AddObserver(this);
80 OnActiveAccountLogin();
81 StartAccountTracker();
84 GCMProfileService::IdentityObserver::~IdentityObserver() {
85 if (gcm_account_tracker_
)
86 gcm_account_tracker_
->Shutdown();
87 identity_provider_
->RemoveObserver(this);
90 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() {
91 // This might be called multiple times when the password changes.
92 const std::string account_id
= identity_provider_
->GetActiveAccountId();
93 if (account_id
== account_id_
)
95 account_id_
= account_id
;
97 // Still need to notify GCMDriver for UMA purpose.
98 driver_
->OnSignedIn();
101 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() {
104 // Still need to notify GCMDriver for UMA purpose.
105 driver_
->OnSignedOut();
108 void GCMProfileService::IdentityObserver::StartAccountTracker() {
109 if (gcm_account_tracker_
)
112 scoped_ptr
<gaia::AccountTracker
> gaia_account_tracker(
113 new gaia::AccountTracker(identity_provider_
.get(),
114 profile_
->GetRequestContext()));
116 gcm_account_tracker_
.reset(
117 new GCMAccountTracker(gaia_account_tracker
.Pass(), driver_
));
119 gcm_account_tracker_
->Start();
122 #endif // !defined(OS_ANDROID)
125 bool GCMProfileService::IsGCMEnabled(Profile
* profile
) {
126 #if defined(OS_ANDROID)
129 return profile
->GetPrefs()->GetBoolean(gcm::prefs::kGCMChannelStatus
);
130 #endif // defined(OS_ANDROID)
133 #if defined(OS_ANDROID)
134 static GCMProfileService
* debug_instance
= nullptr;
136 GCMProfileService::GCMProfileService(Profile
* profile
)
137 : profile_(profile
) {
138 CHECK(!profile
->IsOffTheRecord());
140 // TODO(johnme): Remove debug_instance and this logging code once
141 // crbug.com/437827 is fixed.
142 if (debug_instance
!= nullptr) {
143 LOG(FATAL
) << "An instance of GCMProfileService already exists!"
144 << " Old profile: " << debug_instance
->profile_
<< " "
145 << debug_instance
->profile_
->GetDebugName() << " "
146 << debug_instance
->profile_
->GetProfileType() << " "
147 << debug_instance
->profile_
->IsSupervised() << " "
148 << debug_instance
->profile_
->IsNewProfile() << " "
149 << debug_instance
->profile_
->GetStartTime().ToInternalValue()
150 << ", new profile: " << profile
<< " "
151 << profile
->GetDebugName() << " "
152 << profile
->GetProfileType() << " "
153 << profile
->IsSupervised() << " "
154 << profile
->IsNewProfile() << " "
155 << profile
->GetStartTime().ToInternalValue();
157 debug_instance
= this;
159 scoped_refptr
<base::SequencedWorkerPool
> worker_pool(
160 content::BrowserThread::GetBlockingPool());
161 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner(
162 worker_pool
->GetSequencedTaskRunnerWithShutdownBehavior(
163 worker_pool
->GetSequenceToken(),
164 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
166 driver_
.reset(new GCMDriverAndroid(
167 profile_
->GetPath().Append(chrome::kGCMStoreDirname
),
168 blocking_task_runner
));
171 GCMProfileService::GCMProfileService(
173 scoped_ptr
<GCMClientFactory
> gcm_client_factory
)
174 : profile_(profile
) {
175 DCHECK(!profile
->IsOffTheRecord());
177 driver_
= CreateGCMDriverDesktop(
178 gcm_client_factory
.Pass(),
179 profile_
->GetPrefs(),
180 profile_
->GetPath().Append(chrome::kGCMStoreDirname
),
181 profile_
->GetRequestContext());
183 identity_observer_
.reset(new IdentityObserver(profile
, driver_
.get()));
185 #endif // defined(OS_ANDROID)
187 GCMProfileService::GCMProfileService()
191 GCMProfileService::~GCMProfileService() {
192 #if defined(OS_ANDROID)
193 debug_instance
= nullptr;
197 void GCMProfileService::Shutdown() {
198 #if !defined(OS_ANDROID)
199 identity_observer_
.reset();
200 #endif // !defined(OS_ANDROID)
207 void GCMProfileService::SetDriverForTesting(GCMDriver
* driver
) {
208 driver_
.reset(driver
);
209 #if !defined(OS_ANDROID)
210 if (identity_observer_
)
211 identity_observer_
.reset(new IdentityObserver(profile_
, driver
));
212 #endif // !defined(OS_ANDROID)