Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / policy / cloud / user_policy_signin_service_android.cc
blob402b0104dff86f4d28400dd8af56c70b9f0bcf21
1 // Copyright 2013 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/policy/cloud/user_policy_signin_service_android.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
18 #include "chrome/browser/signin/signin_manager.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/policy/core/common/cloud/cloud_policy_client_registration_helper.h"
21 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
22 #include "components/policy/core/common/policy_switches.h"
23 #include "net/base/network_change_notifier.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "policy/proto/device_management_backend.pb.h"
27 namespace policy {
29 namespace {
31 enterprise_management::DeviceRegisterRequest::Type GetRegistrationType() {
32 CommandLine* command_line = CommandLine::ForCurrentProcess();
33 if (command_line->HasSwitch(switches::kFakeCloudPolicyType))
34 return enterprise_management::DeviceRegisterRequest::BROWSER;
35 return enterprise_management::DeviceRegisterRequest::ANDROID_BROWSER;
38 } // namespace
40 UserPolicySigninService::UserPolicySigninService(
41 Profile* profile,
42 PrefService* local_state,
43 DeviceManagementService* device_management_service,
44 UserCloudPolicyManager* policy_manager,
45 SigninManager* signin_manager,
46 scoped_refptr<net::URLRequestContextGetter> system_request_context,
47 ProfileOAuth2TokenService* token_service)
48 : UserPolicySigninServiceBase(profile,
49 local_state,
50 device_management_service,
51 policy_manager,
52 signin_manager,
53 system_request_context),
54 weak_factory_(this),
55 oauth2_token_service_(token_service),
56 profile_prefs_(profile->GetPrefs()) {}
58 UserPolicySigninService::~UserPolicySigninService() {}
60 void UserPolicySigninService::RegisterForPolicy(
61 const std::string& username,
62 const PolicyRegistrationCallback& callback) {
63 // Create a new CloudPolicyClient for fetching the DMToken.
64 scoped_ptr<CloudPolicyClient> policy_client = CreateClientForRegistrationOnly(
65 username);
66 if (!policy_client) {
67 callback.Run(std::string(), std::string());
68 return;
71 CancelPendingRegistration();
73 // Fire off the registration process. Callback keeps the CloudPolicyClient
74 // alive for the length of the registration process.
75 const bool force_load_policy = false;
76 registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
77 policy_client.get(),
78 force_load_policy,
79 GetRegistrationType()));
80 registration_helper_->StartRegistration(
81 oauth2_token_service_,
82 username,
83 base::Bind(&UserPolicySigninService::CallPolicyRegistrationCallback,
84 base::Unretained(this),
85 base::Passed(&policy_client),
86 callback));
89 void UserPolicySigninService::CallPolicyRegistrationCallback(
90 scoped_ptr<CloudPolicyClient> client,
91 PolicyRegistrationCallback callback) {
92 registration_helper_.reset();
93 callback.Run(client->dm_token(), client->client_id());
96 void UserPolicySigninService::Shutdown() {
97 CancelPendingRegistration();
98 registration_helper_.reset();
99 UserPolicySigninServiceBase::Shutdown();
102 void UserPolicySigninService::OnInitializationCompleted(
103 CloudPolicyService* service) {
104 UserCloudPolicyManager* manager = policy_manager();
105 DCHECK_EQ(service, manager->core()->service());
106 DCHECK(service->IsInitializationComplete());
107 // The service is now initialized - if the client is not yet registered, then
108 // it means that there is no cached policy and so we need to initiate a new
109 // client registration.
110 if (manager->IsClientRegistered()) {
111 DVLOG(1) << "Client already registered - not fetching DMToken";
112 return;
115 net::NetworkChangeNotifier::ConnectionType connection_type =
116 net::NetworkChangeNotifier::GetConnectionType();
117 base::TimeDelta retry_delay = base::TimeDelta::FromDays(3);
118 if (connection_type == net::NetworkChangeNotifier::CONNECTION_ETHERNET ||
119 connection_type == net::NetworkChangeNotifier::CONNECTION_WIFI) {
120 retry_delay = base::TimeDelta::FromDays(1);
123 base::Time last_check_time = base::Time::FromInternalValue(
124 profile_prefs_->GetInt64(prefs::kLastPolicyCheckTime));
125 base::Time now = base::Time::Now();
126 base::Time next_check_time = last_check_time + retry_delay;
128 // Check immediately if no check was ever done before (last_check_time == 0),
129 // or if the last check was in the future (?), or if we're already past the
130 // next check time. Otherwise, delay checking until the next check time.
131 base::TimeDelta try_registration_delay = base::TimeDelta::FromSeconds(5);
132 if (now > last_check_time && now < next_check_time)
133 try_registration_delay = next_check_time - now;
135 base::MessageLoop::current()->PostDelayedTask(
136 FROM_HERE,
137 base::Bind(&UserPolicySigninService::RegisterCloudPolicyService,
138 weak_factory_.GetWeakPtr()),
139 try_registration_delay);
142 void UserPolicySigninService::RegisterCloudPolicyService() {
143 // If the user signed-out while this task was waiting then Shutdown() would
144 // have been called, which would have invalidated this task. Since we're here
145 // then the user must still be signed-in.
146 const std::string& username = signin_manager()->GetAuthenticatedUsername();
147 DCHECK(!username.empty());
148 DCHECK(!policy_manager()->IsClientRegistered());
149 DCHECK(policy_manager()->core()->client());
151 // Persist the current time as the last policy registration attempt time.
152 profile_prefs_->SetInt64(prefs::kLastPolicyCheckTime,
153 base::Time::Now().ToInternalValue());
155 const bool force_load_policy = false;
156 registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
157 policy_manager()->core()->client(),
158 force_load_policy,
159 GetRegistrationType()));
160 registration_helper_->StartRegistration(
161 oauth2_token_service_,
162 username,
163 base::Bind(&UserPolicySigninService::OnRegistrationDone,
164 base::Unretained(this)));
167 void UserPolicySigninService::CancelPendingRegistration() {
168 weak_factory_.InvalidateWeakPtrs();
171 void UserPolicySigninService::OnRegistrationDone() {
172 registration_helper_.reset();
175 } // namespace policy