Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: chrome/browser/services/gcm/gcm_profile_service.cc

Issue 378643002: [GCM] Check-in with signed in accounts associates device to user (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update to the code to prevent test crashes. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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/services/gcm/gcm_profile_service.h" 5 #include "chrome/browser/services/gcm/gcm_profile_service.h"
6 6
7 #include <map>
8
7 #include "base/logging.h" 9 #include "base/logging.h"
8 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/pref_names.h" 12 #include "chrome/common/pref_names.h"
11 #include "components/pref_registry/pref_registry_syncable.h" 13 #include "components/pref_registry/pref_registry_syncable.h"
12 14
13 #if defined(OS_ANDROID) 15 #if defined(OS_ANDROID)
14 #include "components/gcm_driver/gcm_driver_android.h" 16 #include "components/gcm_driver/gcm_driver_android.h"
15 #else 17 #else
18 #include "base/bind.h"
16 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
20 #include "base/memory/weak_ptr.h"
21 #include "chrome/browser/services/gcm/gcm_account_tracker.h"
17 #include "chrome/browser/services/gcm/gcm_desktop_utils.h" 22 #include "chrome/browser/services/gcm/gcm_desktop_utils.h"
18 #include "chrome/browser/signin/profile_identity_provider.h" 23 #include "chrome/browser/signin/profile_identity_provider.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
20 #include "chrome/browser/signin/signin_manager_factory.h" 25 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" 26 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
22 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
23 #include "components/gcm_driver/gcm_client_factory.h" 28 #include "components/gcm_driver/gcm_client_factory.h"
29 #include "components/gcm_driver/gcm_driver_desktop.h"
24 #include "components/signin/core/browser/signin_manager.h" 30 #include "components/signin/core/browser/signin_manager.h"
31 #include "google_apis/gaia/account_tracker.h"
25 #include "google_apis/gaia/identity_provider.h" 32 #include "google_apis/gaia/identity_provider.h"
26 #include "net/url_request/url_request_context_getter.h" 33 #include "net/url_request/url_request_context_getter.h"
27 #endif 34 #endif
28 35
29 namespace gcm { 36 namespace gcm {
30 37
31 #if !defined(OS_ANDROID) 38 #if !defined(OS_ANDROID)
39 // Identity observer only has actual work to do when the user is actually signed
40 // in. It ensures that account tracker is taking
32 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer { 41 class GCMProfileService::IdentityObserver : public IdentityProvider::Observer {
33 public: 42 public:
34 IdentityObserver(Profile* profile, GCMDriver* driver); 43 IdentityObserver(Profile* profile, GCMDriverDesktop* driver);
35 virtual ~IdentityObserver(); 44 virtual ~IdentityObserver();
36 45
37 // IdentityProvider::Observer: 46 // IdentityProvider::Observer:
38 virtual void OnActiveAccountLogin() OVERRIDE; 47 virtual void OnActiveAccountLogin() OVERRIDE;
39 virtual void OnActiveAccountLogout() OVERRIDE; 48 virtual void OnActiveAccountLogout() OVERRIDE;
40 49
41 std::string SignedInUserName() const; 50 std::string SignedInUserName() const;
42 51
52 // Called to inform IdentityObserver that a list of accounts was updated.
53 // |account_tokens| maps email addresses to OAuth2 access tokens.
54 void AccountsUpdated(
55 const std::map<std::string, std::string>& account_tokens);
56
43 private: 57 private:
44 GCMDriver* driver_; 58 Profile* profile_;
59 GCMDriverDesktop* driver_;
45 scoped_ptr<IdentityProvider> identity_provider_; 60 scoped_ptr<IdentityProvider> identity_provider_;
61 scoped_ptr<GCMAccountTracker> gcm_account_tracker_;
46 62
47 // The account ID that this service is responsible for. Empty when the service 63 // The account ID that this service is responsible for. Empty when the service
48 // is not running. 64 // is not running.
49 std::string account_id_; 65 std::string account_id_;
50 66
67 base::WeakPtrFactory<GCMProfileService::IdentityObserver> weak_ptr_factory_;
68
51 DISALLOW_COPY_AND_ASSIGN(IdentityObserver); 69 DISALLOW_COPY_AND_ASSIGN(IdentityObserver);
52 }; 70 };
53 71
54 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile, 72 GCMProfileService::IdentityObserver::IdentityObserver(Profile* profile,
55 GCMDriver* driver) 73 GCMDriverDesktop* driver)
56 : driver_(driver) { 74 : profile_(profile), driver_(driver), weak_ptr_factory_(this) {
57 identity_provider_.reset(new ProfileIdentityProvider( 75 identity_provider_.reset(new ProfileIdentityProvider(
58 SigninManagerFactory::GetForProfile(profile), 76 SigninManagerFactory::GetForProfile(profile),
59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile), 77 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
60 LoginUIServiceFactory::GetForProfile(profile))); 78 LoginUIServiceFactory::GetForProfile(profile)));
61 identity_provider_->AddObserver(this); 79 identity_provider_->AddObserver(this);
62 80
63 OnActiveAccountLogin(); 81 OnActiveAccountLogin();
64 } 82 }
65 83
66 GCMProfileService::IdentityObserver::~IdentityObserver() { 84 GCMProfileService::IdentityObserver::~IdentityObserver() {
85 if (gcm_account_tracker_)
86 gcm_account_tracker_->Shutdown();
67 identity_provider_->RemoveObserver(this); 87 identity_provider_->RemoveObserver(this);
68 } 88 }
69 89
70 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() { 90 void GCMProfileService::IdentityObserver::OnActiveAccountLogin() {
71 // This might be called multiple times when the password changes. 91 // This might be called multiple times when the password changes.
72 const std::string account_id = identity_provider_->GetActiveAccountId(); 92 const std::string account_id = identity_provider_->GetActiveAccountId();
73 if (account_id == account_id_) 93 if (account_id == account_id_)
74 return; 94 return;
75 account_id_ = account_id; 95 account_id_ = account_id;
76 96
77 driver_->OnSignedIn(); 97 driver_->OnSignedIn();
98
99 if (!gcm_account_tracker_) {
100 scoped_ptr<gaia::AccountTracker> gaia_account_tracker(
101 new gaia::AccountTracker(identity_provider_.get(),
102 profile_->GetRequestContext()));
103
104 gcm_account_tracker_.reset(new GCMAccountTracker(
105 gaia_account_tracker.Pass(),
106 base::Bind(&GCMProfileService::IdentityObserver::AccountsUpdated,
107 weak_ptr_factory_.GetWeakPtr())));
108 }
109
110 gcm_account_tracker_->Start();
78 } 111 }
79 112
80 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() { 113 void GCMProfileService::IdentityObserver::OnActiveAccountLogout() {
114 // Check is necessary to not crash browser_tests.
115 if (gcm_account_tracker_)
116 gcm_account_tracker_->Stop();
117 // TODO(fgorski): If we purge here, what should happen when we get
118 // OnActiveAccountLogin() right after that?
81 driver_->Purge(); 119 driver_->Purge();
82 } 120 }
83 121
84 std::string GCMProfileService::IdentityObserver::SignedInUserName() const { 122 std::string GCMProfileService::IdentityObserver::SignedInUserName() const {
85 return driver_->IsStarted() ? account_id_ : std::string(); 123 return driver_->IsStarted() ? account_id_ : std::string();
86 } 124 }
125
126 void GCMProfileService::IdentityObserver::AccountsUpdated(
127 const std::map<std::string, std::string>& account_tokens) {
128 driver_->SetAccountsForCheckin(account_tokens);
129 }
87 #endif // !defined(OS_ANDROID) 130 #endif // !defined(OS_ANDROID)
88 131
89 // static 132 // static
90 bool GCMProfileService::IsGCMEnabled(Profile* profile) { 133 bool GCMProfileService::IsGCMEnabled(Profile* profile) {
91 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled); 134 return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled);
92 } 135 }
93 136
94 // static 137 // static
95 void GCMProfileService::RegisterProfilePrefs( 138 void GCMProfileService::RegisterProfilePrefs(
96 user_prefs::PrefRegistrySyncable* registry) { 139 user_prefs::PrefRegistrySyncable* registry) {
(...skipping 18 matching lines...) Expand all
115 scoped_ptr<GCMClientFactory> gcm_client_factory) 158 scoped_ptr<GCMClientFactory> gcm_client_factory)
116 : profile_(profile), 159 : profile_(profile),
117 push_messaging_service_(this, profile) { 160 push_messaging_service_(this, profile) {
118 DCHECK(!profile->IsOffTheRecord()); 161 DCHECK(!profile->IsOffTheRecord());
119 162
120 driver_ = CreateGCMDriverDesktop( 163 driver_ = CreateGCMDriverDesktop(
121 gcm_client_factory.Pass(), 164 gcm_client_factory.Pass(),
122 profile_->GetPath().Append(chrome::kGCMStoreDirname), 165 profile_->GetPath().Append(chrome::kGCMStoreDirname),
123 profile_->GetRequestContext()); 166 profile_->GetRequestContext());
124 167
125 identity_observer_.reset(new IdentityObserver(profile, driver_.get())); 168 identity_observer_.reset(new IdentityObserver(
169 profile, static_cast<gcm::GCMDriverDesktop*>(driver_.get())));
126 } 170 }
127 #endif // defined(OS_ANDROID) 171 #endif // defined(OS_ANDROID)
128 172
129 GCMProfileService::GCMProfileService() 173 GCMProfileService::GCMProfileService()
130 : profile_(NULL), 174 : profile_(NULL),
131 push_messaging_service_(this, NULL) { 175 push_messaging_service_(this, NULL) {
132 } 176 }
133 177
134 GCMProfileService::~GCMProfileService() { 178 GCMProfileService::~GCMProfileService() {
135 } 179 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 return identity_observer_ ? identity_observer_->SignedInUserName() 214 return identity_observer_ ? identity_observer_->SignedInUserName()
171 : std::string(); 215 : std::string();
172 #endif // defined(OS_ANDROID) 216 #endif // defined(OS_ANDROID)
173 } 217 }
174 218
175 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) { 219 void GCMProfileService::SetDriverForTesting(GCMDriver* driver) {
176 driver_.reset(driver); 220 driver_.reset(driver);
177 } 221 }
178 222
179 } // namespace gcm 223 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/gcm_account_tracker_unittest.cc ('k') | components/gcm_driver/fake_gcm_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698