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

Side by Side Diff: chrome/browser/services/gcm/gcm_account_tracker.h

Issue 631343002: [GCM] Fetching OAuth2 tokens periodically in account tracker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mapper-in-driver
Patch Set: Created 6 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/memory/scoped_vector.h" 11 #include "base/memory/scoped_vector.h"
12 #include "components/gcm_driver/gcm_client.h" 12 #include "components/gcm_driver/gcm_client.h"
13 #include "components/gcm_driver/gcm_connection_observer.h" 13 #include "components/gcm_driver/gcm_connection_observer.h"
14 #include "google_apis/gaia/account_tracker.h" 14 #include "google_apis/gaia/account_tracker.h"
15 #include "google_apis/gaia/oauth2_token_service.h" 15 #include "google_apis/gaia/oauth2_token_service.h"
16 16
17 class PrefService;
18
19 namespace base {
20 class Time;
21 }
22
23 namespace user_prefs {
24 class PrefRegistrySyncable;
25 }
26
17 namespace gcm { 27 namespace gcm {
18 28
19 class GCMDriver; 29 class GCMDriver;
20 30
21 // Class for reporting back which accounts are signed into. It is only meant to 31 // Class for reporting back which accounts are signed into. It is only meant to
22 // be used when the user is signed into sync. 32 // be used when the user is signed into sync.
23 class GCMAccountTracker : public gaia::AccountTracker::Observer, 33 class GCMAccountTracker : public gaia::AccountTracker::Observer,
Nicolas Zea 2014/10/08 00:45:29 FYI I think it would be good to explain _why_ we'r
fgorski 2014/11/06 01:16:14 Done.
24 public OAuth2TokenService::Consumer, 34 public OAuth2TokenService::Consumer,
25 public GCMConnectionObserver { 35 public GCMConnectionObserver {
26 public: 36 public:
27 // State of the account. 37 // State of the account.
28 // Allowed transitions: 38 // Allowed transitions:
29 // TOKEN_NEEDED - account info was created. 39 // TOKEN_NEEDED - account info was created.
30 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested. 40 // TOKEN_NEEDED -> GETTING_TOKEN - access token was requested.
31 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed. 41 // GETTING_TOKEN -> TOKEN_NEEDED - access token fetching failed.
32 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded. 42 // GETTING_TOKEN -> TOKEN_PRESENT - access token fetching succeeded.
33 // GETTING_TOKEN -> ACCOUNT_REMOVED - account was removed. 43 // GETTING_TOKEN -> ACCOUNT_REMOVED - account was removed.
(...skipping 15 matching lines...) Expand all
49 // Email address of the tracked account. 59 // Email address of the tracked account.
50 std::string email; 60 std::string email;
51 // OAuth2 access token, when |state| is TOKEN_PRESENT. 61 // OAuth2 access token, when |state| is TOKEN_PRESENT.
52 std::string access_token; 62 std::string access_token;
53 // Expiration time of the access tokens. 63 // Expiration time of the access tokens.
54 base::Time expiration_time; 64 base::Time expiration_time;
55 // Status of the token fetching. 65 // Status of the token fetching.
56 AccountState state; 66 AccountState state;
57 }; 67 };
58 68
69 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
70
59 // Creates an instance of GCMAccountTracker. |account_tracker| is used to 71 // Creates an instance of GCMAccountTracker. |account_tracker| is used to
60 // deliver information about the accounts present in the browser context to 72 // deliver information about the accounts present in the browser context to
61 // |driver|. 73 // |driver|.
62 GCMAccountTracker(scoped_ptr<gaia::AccountTracker> account_tracker, 74 GCMAccountTracker(scoped_ptr<gaia::AccountTracker> account_tracker,
63 GCMDriver* driver); 75 GCMDriver* driver,
76 PrefService* prefs);
64 virtual ~GCMAccountTracker(); 77 virtual ~GCMAccountTracker();
65 78
66 // Shuts down the tracker ensuring a proper clean up. After Shutdown() is 79 // Shuts down the tracker ensuring a proper clean up. After Shutdown() is
67 // called Start() and Stop() should no longer be used. Must be called before 80 // called Start() and Stop() should no longer be used. Must be called before
68 // destruction. 81 // destruction.
69 void Shutdown(); 82 void Shutdown();
70 83
71 // Starts tracking accounts. 84 // Starts tracking accounts.
72 void Start(); 85 void Start();
73 86
(...skipping 17 matching lines...) Expand all
91 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 104 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
92 const std::string& access_token, 105 const std::string& access_token,
93 const base::Time& expiration_time) override; 106 const base::Time& expiration_time) override;
94 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 107 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
95 const GoogleServiceAuthError& error) override; 108 const GoogleServiceAuthError& error) override;
96 109
97 // GCMConnectionObserver overrides. 110 // GCMConnectionObserver overrides.
98 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE; 111 virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE;
99 virtual void OnDisconnected() OVERRIDE; 112 virtual void OnDisconnected() OVERRIDE;
100 113
114 // Schedules fetching tokens at the time when they are needed to be reported.
115 void ScheduleFetchingTokens();
101 // Report the list of accounts with OAuth2 tokens back using the |callback_| 116 // Report the list of accounts with OAuth2 tokens back using the |callback_|
102 // function. If there are token requests in progress, do nothing. 117 // function. If there are token requests in progress, do nothing.
103 void CompleteCollectingTokens(); 118 void ReportTokens();
104 // Verify that all of the tokens are ready to be passed down to the GCM 119 // Verify that all of the tokens are ready to be passed down to the GCM
105 // Driver, e.g. none of them has expired or is missing. Returns true if not 120 // Driver, e.g. none of them has expired or is missing. Returns true if not
106 // all tokens are valid and a fetching yet more tokens is required. 121 // all tokens are valid and a fetching yet more tokens is required.
107 bool SanitizeTokens(); 122 bool SanitizeTokens();
123 // Indicates whether token reporting is required, either because it is due, or
124 // some of the accounts were removed.
125 bool IsTokenReportingRequired();
126 // Indicates whether there are tokens that still need fetching.
127 bool IsTokenFetchingRequired();
128 // Gets the time until next token fetching.
129 base::TimeDelta GetTimeToNextTokenFetching();
108 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or 130 // Deletes a token request. Should be called from OnGetTokenSuccess(..) or
109 // OnGetTokenFailure(..). 131 // OnGetTokenFailure(..).
110 void DeleteTokenRequest(const OAuth2TokenService::Request* request); 132 void DeleteTokenRequest(const OAuth2TokenService::Request* request);
111 // Checks on all known accounts, and calls GetToken(..) for those with 133 // Checks on all known accounts, and calls GetToken(..) for those with
112 // |state == TOKEN_NEEDED|. 134 // |state == TOKEN_NEEDED|.
113 void GetAllNeededTokens(); 135 void GetAllNeededTokens();
114 // Starts fetching the OAuth2 token for the GCM group scope. 136 // Starts fetching the OAuth2 token for the GCM group scope.
115 void GetToken(AccountInfos::iterator& account_iter); 137 void GetToken(AccountInfos::iterator& account_iter);
116 138
117 // Handling of actual sign in and sign out for accounts. 139 // Handling of actual sign in and sign out for accounts.
118 void OnAccountSignedIn(const gaia::AccountIds& ids); 140 void OnAccountSignedIn(const gaia::AccountIds& ids);
119 void OnAccountSignedOut(const gaia::AccountIds& ids); 141 void OnAccountSignedOut(const gaia::AccountIds& ids);
120 142
121 OAuth2TokenService* GetTokenService(); 143 OAuth2TokenService* GetTokenService();
122 144
123 // Account tracker. 145 // Account tracker.
124 scoped_ptr<gaia::AccountTracker> account_tracker_; 146 scoped_ptr<gaia::AccountTracker> account_tracker_;
125 147
126 // GCM Driver. Not owned. 148 // GCM Driver. Not owned.
127 GCMDriver* driver_; 149 GCMDriver* driver_;
128 150
129 // State of the account. 151 // State of the account.
130 AccountInfos account_infos_; 152 AccountInfos account_infos_;
131 153
132 // Indicates whether shutdown has been called. 154 // Indicates whether shutdown has been called.
133 bool shutdown_called_; 155 bool shutdown_called_;
134 156
157 // Pref service for storing timestamp of last token fetching. Not owned.
158 PrefService* prefs_;
159
160 base::Time last_token_fetch_time_;
Nicolas Zea 2014/10/08 00:45:29 nit: comment about what this is for
fgorski 2014/11/06 01:16:14 Done.
161
135 ScopedVector<OAuth2TokenService::Request> pending_token_requests_; 162 ScopedVector<OAuth2TokenService::Request> pending_token_requests_;
136 163
164 base::WeakPtrFactory<GCMAccountTracker> weak_ptr_factory_;
165
137 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker); 166 DISALLOW_COPY_AND_ASSIGN(GCMAccountTracker);
138 }; 167 };
139 168
140 } // namespace gcm 169 } // namespace gcm
141 170
142 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_ 171 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_ACCOUNT_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698