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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/wildcard_login_checker.h"
8 #include "base/bind_helpers.h"
9 #include "base/metrics/histogram.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
12 #include "components/policy/core/browser/browser_policy_connector.h"
13 #include "net/url_request/url_request_context_getter.h"
19 // Presence of this key in the userinfo response indicates whether the user is
20 // on a hosted domain.
21 const char kHostedDomainKey
[] = "hd";
23 // UMA histogram names.
24 const char kUMADelayPolicyTokenFetch
[] =
25 "Enterprise.WildcardLoginCheck.DelayPolicyTokenFetch";
26 const char kUMADelayUserInfoFetch
[] =
27 "Enterprise.WildcardLoginCheck.DelayUserInfoFetch";
28 const char kUMADelayTotal
[] =
29 "Enterprise.WildcardLoginCheck.DelayTotal";
33 WildcardLoginChecker::WildcardLoginChecker() {}
35 WildcardLoginChecker::~WildcardLoginChecker() {}
37 void WildcardLoginChecker::StartWithSigninContext(
38 scoped_refptr
<net::URLRequestContextGetter
> signin_context
,
39 const StatusCallback
& callback
) {
40 CHECK(!token_fetcher_
);
41 CHECK(!user_info_fetcher_
);
43 start_timestamp_
= base::Time::Now();
46 token_fetcher_
.reset(new PolicyOAuth2TokenFetcher());
47 token_fetcher_
->StartWithSigninContext(
48 signin_context
.get(), g_browser_process
->system_request_context(),
49 base::Bind(&WildcardLoginChecker::OnPolicyTokenFetched
,
50 base::Unretained(this)));
53 void WildcardLoginChecker::StartWithRefreshToken(
54 const std::string
& refresh_token
,
55 const StatusCallback
& callback
) {
56 CHECK(!token_fetcher_
);
57 CHECK(!user_info_fetcher_
);
59 start_timestamp_
= base::Time::Now();
62 token_fetcher_
.reset(new PolicyOAuth2TokenFetcher());
63 token_fetcher_
->StartWithRefreshToken(
64 refresh_token
, g_browser_process
->system_request_context(),
65 base::Bind(&WildcardLoginChecker::OnPolicyTokenFetched
,
66 base::Unretained(this)));
69 void WildcardLoginChecker::StartWithAccessToken(
70 const std::string
& access_token
,
71 const StatusCallback
& callback
) {
72 CHECK(!token_fetcher_
);
73 CHECK(!user_info_fetcher_
);
75 start_timestamp_
= base::Time::Now();
78 StartUserInfoFetcher(access_token
);
81 void WildcardLoginChecker::OnGetUserInfoSuccess(
82 const base::DictionaryValue
* response
) {
83 if (!start_timestamp_
.is_null()) {
84 base::Time now
= base::Time::Now();
85 UMA_HISTOGRAM_MEDIUM_TIMES(kUMADelayUserInfoFetch
,
86 now
- token_available_timestamp_
);
87 UMA_HISTOGRAM_MEDIUM_TIMES(kUMADelayTotal
,
88 now
- start_timestamp_
);
91 OnCheckCompleted(response
->HasKey(kHostedDomainKey
) ? RESULT_ALLOWED
95 void WildcardLoginChecker::OnGetUserInfoFailure(
96 const GoogleServiceAuthError
& error
) {
97 LOG(ERROR
) << "Failed to fetch user info " << error
.ToString();
98 OnCheckCompleted(RESULT_FAILED
);
101 void WildcardLoginChecker::OnPolicyTokenFetched(
102 const std::string
& access_token
,
103 const GoogleServiceAuthError
& error
) {
104 if (error
.state() != GoogleServiceAuthError::NONE
) {
105 LOG(ERROR
) << "Failed to fetch policy token " << error
.ToString();
106 OnCheckCompleted(RESULT_FAILED
);
110 if (!start_timestamp_
.is_null()) {
111 token_available_timestamp_
= base::Time::Now();
112 UMA_HISTOGRAM_MEDIUM_TIMES(kUMADelayPolicyTokenFetch
,
113 token_available_timestamp_
- start_timestamp_
);
116 token_fetcher_
.reset();
117 StartUserInfoFetcher(access_token
);
120 void WildcardLoginChecker::StartUserInfoFetcher(
121 const std::string
& access_token
) {
122 user_info_fetcher_
.reset(
123 new UserInfoFetcher(this, g_browser_process
->system_request_context()));
124 user_info_fetcher_
->Start(access_token
);
127 void WildcardLoginChecker::OnCheckCompleted(Result result
) {
128 if (!callback_
.is_null())
129 callback_
.Run(result
);
132 } // namespace policy