summaryrefslogtreecommitdiff
path: root/chromium/components/signin/core/browser/child_account_info_fetcher_impl.cc
blob: faee2aec332c08a7f6af80b26b5cbbedb443ae40 (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
// Copyright 2015 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 "components/signin/core/browser/child_account_info_fetcher_impl.h"

#include "base/strings/string_split.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "components/invalidation/public/invalidation_service.h"
#include "components/invalidation/public/object_id_invalidation_map.h"
#include "components/signin/core/browser/account_fetcher_service.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/signin_client.h"
#include "google/cacheinvalidation/types.pb.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
#include "google_apis/gaia/gaia_constants.h"

// TODO(maroun): Remove this file.

namespace {

const char kFetcherId[] = "ChildAccountInfoFetcherImpl";

// Exponential backoff policy on service flag fetching failure.
const net::BackoffEntry::Policy kBackoffPolicy = {
  0,  // Number of initial errors to ignore without backoff.
  2000,  // Initial delay for backoff in ms.
  2,  // Factor to multiply waiting time by.
  0.2,  // Fuzzing percentage. 20% will spread requests randomly between
        // 80-100% of the calculated time.
  1000 * 60 * 60* 4,  // Maximum time to delay requests by (4 hours).
  -1,  // Don't discard entry even if unused.
  false,  // Don't use the initial delay unless the last request was an error.
};

// The invalidation object ID used for child account graduation event.
// The syntax is:
// 'U' -> This is a user specific invalidation.
// 'CA' -> Namespace used for all ChildAccount invalidations.
// 'GRAD' -> Indicates the actual event i.e. child account graduation.
const char kChildAccountGraduationId[] = "UCAGRAD";

}  // namespace

ChildAccountInfoFetcherImpl::ChildAccountInfoFetcherImpl(
    const std::string& account_id,
    AccountFetcherService* fetcher_service,
    OAuth2TokenService* token_service,
    net::URLRequestContextGetter* request_context_getter,
    invalidation::InvalidationService* invalidation_service)
    : OAuth2TokenService::Consumer(kFetcherId),
      token_service_(token_service),
      request_context_getter_(request_context_getter),
      fetcher_service_(fetcher_service),
      invalidation_service_(invalidation_service),
      account_id_(account_id),
      backoff_(&kBackoffPolicy),
      fetch_in_progress_(false) {
  TRACE_EVENT_ASYNC_BEGIN1("AccountFetcherService", kFetcherId, this,
                           "account_id", account_id);
  // Invalidation service may not be available in tests.
  if (invalidation_service_) {
    invalidation_service_->RegisterInvalidationHandler(this);
    syncer::ObjectIdSet ids;
    ids.insert(invalidation::ObjectId(
        ipc::invalidation::ObjectSource::CHROME_COMPONENTS,
        kChildAccountGraduationId));
    bool insert_success =
        invalidation_service_->UpdateRegisteredInvalidationIds(this, ids);
    DCHECK(insert_success);
  }
  FetchIfNotInProgress();
}

ChildAccountInfoFetcherImpl::~ChildAccountInfoFetcherImpl() {
  TRACE_EVENT_ASYNC_END0("AccountFetcherService", kFetcherId, this);
  if (invalidation_service_)
    UnregisterInvalidationHandler();
}

void ChildAccountInfoFetcherImpl::FetchIfNotInProgress() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (fetch_in_progress_)
    return;
  fetch_in_progress_ = true;
  OAuth2TokenService::ScopeSet scopes;
  scopes.insert(GaiaConstants::kOAuth1LoginScope);
  login_token_request_ =
      token_service_->StartRequest(account_id_, scopes, this);
}

void ChildAccountInfoFetcherImpl::OnGetTokenSuccess(
    const OAuth2TokenService::Request* request,
    const std::string& access_token,
    const base::Time& expiration_time) {
  TRACE_EVENT_ASYNC_STEP_PAST0("AccountFetcherService", kFetcherId, this,
                               "OnGetTokenSuccess");
  DCHECK_EQ(request, login_token_request_.get());

  gaia_auth_fetcher_ = fetcher_service_->signin_client_->CreateGaiaAuthFetcher(
      this, GaiaConstants::kChromeSource, request_context_getter_);
  gaia_auth_fetcher_->StartOAuthLogin(access_token,
                                      GaiaConstants::kGaiaService);
}

void ChildAccountInfoFetcherImpl::OnGetTokenFailure(
    const OAuth2TokenService::Request* request,
    const GoogleServiceAuthError& error) {
  HandleFailure();
}

void ChildAccountInfoFetcherImpl::OnClientLoginSuccess(
    const ClientLoginResult& result) {
  gaia_auth_fetcher_->StartGetUserInfo(result.lsid);
}

void ChildAccountInfoFetcherImpl::OnClientLoginFailure(
    const GoogleServiceAuthError& error) {
  HandleFailure();
}

void ChildAccountInfoFetcherImpl::OnGetUserInfoSuccess(
    const UserInfoMap& data) {
  UserInfoMap::const_iterator services_iter = data.find("allServices");
  if (services_iter != data.end()) {
    std::vector<std::string> service_flags = base::SplitString(
        services_iter->second, ",", base::TRIM_WHITESPACE,
        base::SPLIT_WANT_ALL);
    bool is_child_account =
        std::find(service_flags.begin(), service_flags.end(),
                  AccountTrackerService::kChildAccountServiceFlag) !=
        service_flags.end();
    if (!is_child_account && invalidation_service_) {
      // Don't bother listening for invalidations as a non-child account can't
      // become a child account.
      bool insert_success =
          invalidation_service_->UpdateRegisteredInvalidationIds(
              this, syncer::ObjectIdSet());
      DCHECK(insert_success);
      UnregisterInvalidationHandler();
    }
    fetcher_service_->SetIsChildAccount(account_id_, is_child_account);
  } else {
    DLOG(ERROR) << "ChildAccountInfoFetcherImpl::OnGetUserInfoSuccess: "
                << "GetUserInfo response didn't include allServices field.";
  }
  fetch_in_progress_ = false;
}

void ChildAccountInfoFetcherImpl::OnGetUserInfoFailure(
    const GoogleServiceAuthError& error) {
  HandleFailure();
}

void ChildAccountInfoFetcherImpl::HandleFailure() {
  fetch_in_progress_ = false;
  backoff_.InformOfRequest(false);
  timer_.Start(FROM_HERE, backoff_.GetTimeUntilRelease(), this,
               &ChildAccountInfoFetcherImpl::FetchIfNotInProgress);
}

void ChildAccountInfoFetcherImpl::UnregisterInvalidationHandler() {
  invalidation_service_->UnregisterInvalidationHandler(this);
  invalidation_service_ = nullptr;
}

void ChildAccountInfoFetcherImpl::OnInvalidatorStateChange(
    syncer::InvalidatorState state) {
  if (state == syncer::INVALIDATOR_SHUTTING_DOWN)
    UnregisterInvalidationHandler();
}

void ChildAccountInfoFetcherImpl::OnIncomingInvalidation(
    const syncer::ObjectIdInvalidationMap& invalidation_map) {
  FetchIfNotInProgress();
  invalidation_map.AcknowledgeAll();
}

std::string ChildAccountInfoFetcherImpl::GetOwnerName() const {
  return std::string(kFetcherId);
}