summaryrefslogtreecommitdiffstats
path: root/chrome/browser/services/gcm/gcm_account_tracker.cc
blob: 05216887c65a04b14848cf1994f295e8bed7e3b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/services/gcm/gcm_account_tracker.h"

#include <algorithm>
#include <vector>

#include "base/time/time.h"
#include "components/gcm_driver/gcm_driver.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/ip_endpoint.h"

namespace gcm {

namespace {

// Scopes needed by the OAuth2 access tokens.
const char kGCMGroupServerScope[] = "https://www.googleapis.com/auth/gcm";
const char kGCMCheckinServerScope[] =
    "https://www.googleapis.com/auth/android_checkin";
// Name of the GCM account tracker for the OAuth2TokenService.
const char kGCMAccountTrackerName[] = "gcm_account_tracker";
// Minimum token validity when sending to GCM groups server.
const int64 kMinimumTokenValidityMs = 500;
// Token reporting interval, when no account changes are detected.
const int64 kTokenReportingIntervalMs = 12 * 60 * 60 * 1000;  // 12 hours in ms.

}  // namespace

GCMAccountTracker::AccountInfo::AccountInfo(const std::string& email,
                                            AccountState state)
    : email(email), state(state) {
}

GCMAccountTracker::AccountInfo::~AccountInfo() {
}

GCMAccountTracker::GCMAccountTracker(
    scoped_ptr<gaia::AccountTracker> account_tracker,
    GCMDriver* driver)
    : OAuth2TokenService::Consumer(kGCMAccountTrackerName),
      account_tracker_(account_tracker.release()),
      driver_(driver),
      shutdown_called_(false),
      reporting_weak_ptr_factory_(this) {
}

GCMAccountTracker::~GCMAccountTracker() {
  DCHECK(shutdown_called_);
}

void GCMAccountTracker::Shutdown() {
  shutdown_called_ = true;
  driver_->RemoveConnectionObserver(this);
  account_tracker_->RemoveObserver(this);
  account_tracker_->Shutdown();
}

void GCMAccountTracker::Start() {
  DCHECK(!shutdown_called_);
  account_tracker_->AddObserver(this);
  driver_->AddConnectionObserver(this);

  std::vector<gaia::AccountIds> accounts = account_tracker_->GetAccounts();
  for (std::vector<gaia::AccountIds>::const_iterator iter = accounts.begin();
       iter != accounts.end();
       ++iter) {
    if (!iter->email.empty()) {
      account_infos_.insert(std::make_pair(
          iter->account_key, AccountInfo(iter->email, TOKEN_NEEDED)));
    }
  }

  if (IsTokenReportingRequired())
    ReportTokens();
  else
    ScheduleReportTokens();
}

void GCMAccountTracker::ScheduleReportTokens() {
  DVLOG(1) << "Deferring the token reporting for: "
           << GetTimeToNextTokenReporting().InSeconds() << " seconds.";

  reporting_weak_ptr_factory_.InvalidateWeakPtrs();
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&GCMAccountTracker::ReportTokens,
                 reporting_weak_ptr_factory_.GetWeakPtr()),
      GetTimeToNextTokenReporting());
}

void GCMAccountTracker::OnAccountAdded(const gaia::AccountIds& ids) {
  DVLOG(1) << "Account added: " << ids.email;
  // We listen for the account signing in, which happens after account is added.
}

void GCMAccountTracker::OnAccountRemoved(const gaia::AccountIds& ids) {
  DVLOG(1) << "Account removed: " << ids.email;
  // We listen for the account signing out, which happens before account is
  // removed.
}

void GCMAccountTracker::OnAccountSignInChanged(const gaia::AccountIds& ids,
                                               bool is_signed_in) {
  if (is_signed_in)
    OnAccountSignedIn(ids);
  else
    OnAccountSignedOut(ids);
}

void GCMAccountTracker::OnGetTokenSuccess(
    const OAuth2TokenService::Request* request,
    const std::string& access_token,
    const base::Time& expiration_time) {
  DCHECK(request);
  DCHECK(!request->GetAccountId().empty());
  DVLOG(1) << "Get token success: " << request->GetAccountId();

  AccountInfos::iterator iter = account_infos_.find(request->GetAccountId());
  DCHECK(iter != account_infos_.end());
  if (iter != account_infos_.end()) {
    DCHECK(iter->second.state == GETTING_TOKEN ||
           iter->second.state == ACCOUNT_REMOVED);
    // If OnAccountSignedOut(..) was called most recently, account is kept in
    // ACCOUNT_REMOVED state.
    if (iter->second.state == GETTING_TOKEN) {
      iter->second.state = TOKEN_PRESENT;
      iter->second.access_token = access_token;
      iter->second.expiration_time = expiration_time;
    }
  }

  DeleteTokenRequest(request);
  ReportTokens();
}

void GCMAccountTracker::OnGetTokenFailure(
    const OAuth2TokenService::Request* request,
    const GoogleServiceAuthError& error) {
  DCHECK(request);
  DCHECK(!request->GetAccountId().empty());
  DVLOG(1) << "Get token failure: " << request->GetAccountId();

  AccountInfos::iterator iter = account_infos_.find(request->GetAccountId());
  DCHECK(iter != account_infos_.end());
  if (iter != account_infos_.end()) {
    DCHECK(iter->second.state == GETTING_TOKEN ||
           iter->second.state == ACCOUNT_REMOVED);
    // If OnAccountSignedOut(..) was called most recently, account is kept in
    // ACCOUNT_REMOVED state.
    if (iter->second.state == GETTING_TOKEN)
      iter->second.state = TOKEN_NEEDED;
  }

  DeleteTokenRequest(request);
  ReportTokens();
}

void GCMAccountTracker::OnConnected(const net::IPEndPoint& ip_endpoint) {
  if (IsTokenReportingRequired())
    ReportTokens();
}

void GCMAccountTracker::OnDisconnected() {
  // We are disconnected, so no point in trying to work with tokens.
}

void GCMAccountTracker::ReportTokens() {
  SanitizeTokens();
  // Make sure all tokens are valid.
  if (IsTokenFetchingRequired()) {
    GetAllNeededTokens();
    return;
  }

  // Wait for gaia::AccountTracker to be done with fetching the user info, as
  // well as all of the pending token requests from GCMAccountTracker to be done
  // before you report the results.
  if (!account_tracker_->IsAllUserInfoFetched() ||
      !pending_token_requests_.empty()) {
    return;
  }

  bool account_removed = false;
  // Stop tracking the accounts, that were removed, as it will be reported to
  // the driver.
  for (AccountInfos::iterator iter = account_infos_.begin();
       iter != account_infos_.end();) {
    if (iter->second.state == ACCOUNT_REMOVED) {
      account_removed = true;
      account_infos_.erase(iter++);
    } else {
      ++iter;
    }
  }

  std::vector<GCMClient::AccountTokenInfo> account_tokens;
  for (AccountInfos::iterator iter = account_infos_.begin();
       iter != account_infos_.end(); ++iter) {
    if (iter->second.state == TOKEN_PRESENT) {
      GCMClient::AccountTokenInfo token_info;
      token_info.account_id = iter->first;
      token_info.email = iter->second.email;
      token_info.access_token = iter->second.access_token;
      account_tokens.push_back(token_info);
    } else {
      // This should not happen, as we are making a check that there are no
      // pending requests above, stopping tracking of removed accounts, or start
      // fetching tokens.
      NOTREACHED();
    }
  }

  // Make sure that there is something to report, otherwise bail out.
  if (!account_tokens.empty() || account_removed) {
    DVLOG(1) << "Reporting the tokens to driver: " << account_tokens.size();
    driver_->SetAccountTokens(account_tokens);
    driver_->SetLastTokenFetchTime(base::Time::Now());
    ScheduleReportTokens();
  } else {
    DVLOG(1) << "No tokens and nothing removed. Skipping callback.";
  }
}

void GCMAccountTracker::SanitizeTokens() {
  for (AccountInfos::iterator iter = account_infos_.begin();
       iter != account_infos_.end();
       ++iter) {
    if (iter->second.state == TOKEN_PRESENT &&
        iter->second.expiration_time <
            base::Time::Now() +
                base::TimeDelta::FromMilliseconds(kMinimumTokenValidityMs)) {
      iter->second.access_token.clear();
      iter->second.state = TOKEN_NEEDED;
      iter->second.expiration_time = base::Time();
    }
  }
}

bool GCMAccountTracker::IsTokenReportingRequired() const {
  if (GetTimeToNextTokenReporting() == base::TimeDelta())
    return true;

  bool reporting_required = false;
  for (AccountInfos::const_iterator iter = account_infos_.begin();
       iter != account_infos_.end();
       ++iter) {
    if (iter->second.state == ACCOUNT_REMOVED)
      reporting_required = true;
  }

  return reporting_required;
}

bool GCMAccountTracker::IsTokenFetchingRequired() const {
  bool token_needed = false;
  for (AccountInfos::const_iterator iter = account_infos_.begin();
       iter != account_infos_.end();
       ++iter) {
    if (iter->second.state == TOKEN_NEEDED)
      token_needed = true;
  }

  return token_needed;
}

base::TimeDelta GCMAccountTracker::GetTimeToNextTokenReporting() const {
  base::TimeDelta time_till_next_reporting =
      driver_->GetLastTokenFetchTime() +
      base::TimeDelta::FromMilliseconds(kTokenReportingIntervalMs) -
      base::Time::Now();
  return time_till_next_reporting < base::TimeDelta() ?
             base::TimeDelta() : time_till_next_reporting;
}

void GCMAccountTracker::DeleteTokenRequest(
    const OAuth2TokenService::Request* request) {
  ScopedVector<OAuth2TokenService::Request>::iterator iter = std::find(
      pending_token_requests_.begin(), pending_token_requests_.end(), request);
  if (iter != pending_token_requests_.end())
    pending_token_requests_.erase(iter);
}

void GCMAccountTracker::GetAllNeededTokens() {
  // Only start fetching tokens if driver is running, they have a limited
  // validity time and GCM connection is a good indication of network running.
  // If the GetAllNeededTokens was called as part of periodic schedule, it may
  // not have network. In that case the next network change will trigger token
  // fetching.
  if (!driver_->IsConnected())
    return;

  for (AccountInfos::iterator iter = account_infos_.begin();
       iter != account_infos_.end();
       ++iter) {
    if (iter->second.state == TOKEN_NEEDED)
      GetToken(iter);
  }
}

void GCMAccountTracker::GetToken(AccountInfos::iterator& account_iter) {
  DCHECK(GetTokenService());
  DCHECK_EQ(account_iter->second.state, TOKEN_NEEDED);

  OAuth2TokenService::ScopeSet scopes;
  scopes.insert(kGCMGroupServerScope);
  scopes.insert(kGCMCheckinServerScope);
  scoped_ptr<OAuth2TokenService::Request> request =
      GetTokenService()->StartRequest(account_iter->first, scopes, this);

  pending_token_requests_.push_back(request.release());
  account_iter->second.state = GETTING_TOKEN;
}

void GCMAccountTracker::OnAccountSignedIn(const gaia::AccountIds& ids) {
  DVLOG(1) << "Account signed in: " << ids.email;
  AccountInfos::iterator iter = account_infos_.find(ids.account_key);
  if (iter == account_infos_.end()) {
    DCHECK(!ids.email.empty());
    account_infos_.insert(
        std::make_pair(ids.account_key, AccountInfo(ids.email, TOKEN_NEEDED)));
  } else if (iter->second.state == ACCOUNT_REMOVED) {
    iter->second.state = TOKEN_NEEDED;
  }

  GetAllNeededTokens();
}

void GCMAccountTracker::OnAccountSignedOut(const gaia::AccountIds& ids) {
  DVLOG(1) << "Account signed out: " << ids.email;
  AccountInfos::iterator iter = account_infos_.find(ids.account_key);
  if (iter == account_infos_.end())
    return;

  iter->second.access_token.clear();
  iter->second.state = ACCOUNT_REMOVED;
  ReportTokens();
}

OAuth2TokenService* GCMAccountTracker::GetTokenService() {
  DCHECK(account_tracker_->identity_provider());
  return account_tracker_->identity_provider()->GetTokenService();
}

}  // namespace gcm