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. 4 5 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h" 6 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" 26 27 namespace policy { 28 29 namespace { 30 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; 36 } 37 38 } // namespace 39 40 UserPolicySigninService::UserPolicySigninService( 41 Profile* profile, 42 PrefService* local_state, 43 DeviceManagementService* device_management_service, 44 scoped_refptr<net::URLRequestContextGetter> system_request_context, 45 ProfileOAuth2TokenService* token_service) 46 : UserPolicySigninServiceBase(profile, 47 local_state, 48 device_management_service, 49 system_request_context), 50 weak_factory_(this), 51 oauth2_token_service_(token_service) {} 52 53 UserPolicySigninService::~UserPolicySigninService() {} 54 55 void UserPolicySigninService::RegisterForPolicy( 56 const std::string& username, 57 const PolicyRegistrationCallback& callback) { 58 // Create a new CloudPolicyClient for fetching the DMToken. 59 scoped_ptr<CloudPolicyClient> policy_client = CreateClientForRegistrationOnly( 60 username); 61 if (!policy_client) { 62 callback.Run(std::string(), std::string()); 63 return; 64 } 65 66 CancelPendingRegistration(); 67 68 // Fire off the registration process. Callback keeps the CloudPolicyClient 69 // alive for the length of the registration process. 70 const bool force_load_policy = false; 71 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( 72 policy_client.get(), 73 force_load_policy, 74 GetRegistrationType())); 75 registration_helper_->StartRegistration( 76 oauth2_token_service_, 77 username, 78 base::Bind(&UserPolicySigninService::CallPolicyRegistrationCallback, 79 base::Unretained(this), 80 base::Passed(&policy_client), 81 callback)); 82 } 83 84 void UserPolicySigninService::CallPolicyRegistrationCallback( 85 scoped_ptr<CloudPolicyClient> client, 86 PolicyRegistrationCallback callback) { 87 registration_helper_.reset(); 88 callback.Run(client->dm_token(), client->client_id()); 89 } 90 91 void UserPolicySigninService::Shutdown() { 92 CancelPendingRegistration(); 93 registration_helper_.reset(); 94 UserPolicySigninServiceBase::Shutdown(); 95 } 96 97 void UserPolicySigninService::OnInitializationCompleted( 98 CloudPolicyService* service) { 99 UserCloudPolicyManager* manager = GetManager(); 100 DCHECK_EQ(service, manager->core()->service()); 101 DCHECK(service->IsInitializationComplete()); 102 // The service is now initialized - if the client is not yet registered, then 103 // it means that there is no cached policy and so we need to initiate a new 104 // client registration. 105 if (manager->IsClientRegistered()) { 106 DVLOG(1) << "Client already registered - not fetching DMToken"; 107 return; 108 } 109 110 net::NetworkChangeNotifier::ConnectionType connection_type = 111 net::NetworkChangeNotifier::GetConnectionType(); 112 base::TimeDelta retry_delay = base::TimeDelta::FromDays(3); 113 if (connection_type == net::NetworkChangeNotifier::CONNECTION_ETHERNET || 114 connection_type == net::NetworkChangeNotifier::CONNECTION_WIFI) { 115 retry_delay = base::TimeDelta::FromDays(1); 116 } 117 118 base::Time last_check_time = base::Time::FromInternalValue( 119 profile()->GetPrefs()->GetInt64(prefs::kLastPolicyCheckTime)); 120 base::Time now = base::Time::Now(); 121 base::Time next_check_time = last_check_time + retry_delay; 122 123 // Check immediately if no check was ever done before (last_check_time == 0), 124 // or if the last check was in the future (?), or if we're already past the 125 // next check time. Otherwise, delay checking until the next check time. 126 base::TimeDelta try_registration_delay = base::TimeDelta::FromSeconds(5); 127 if (now > last_check_time && now < next_check_time) 128 try_registration_delay = next_check_time - now; 129 130 base::MessageLoop::current()->PostDelayedTask( 131 FROM_HERE, 132 base::Bind(&UserPolicySigninService::RegisterCloudPolicyService, 133 weak_factory_.GetWeakPtr()), 134 try_registration_delay); 135 } 136 137 void UserPolicySigninService::RegisterCloudPolicyService() { 138 // If the user signed-out while this task was waiting then Shutdown() would 139 // have been called, which would have invalidated this task. Since we're here 140 // then the user must still be signed-in. 141 const std::string& username = GetSigninManager()->GetAuthenticatedUsername(); 142 DCHECK(!username.empty()); 143 DCHECK(!GetManager()->IsClientRegistered()); 144 DCHECK(GetManager()->core()->client()); 145 146 // Persist the current time as the last policy registration attempt time. 147 profile()->GetPrefs()->SetInt64(prefs::kLastPolicyCheckTime, 148 base::Time::Now().ToInternalValue()); 149 150 const bool force_load_policy = false; 151 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( 152 GetManager()->core()->client(), 153 force_load_policy, 154 GetRegistrationType())); 155 registration_helper_->StartRegistration( 156 oauth2_token_service_, 157 username, 158 base::Bind(&UserPolicySigninService::OnRegistrationDone, 159 base::Unretained(this))); 160 } 161 162 void UserPolicySigninService::CancelPendingRegistration() { 163 weak_factory_.InvalidateWeakPtrs(); 164 } 165 166 void UserPolicySigninService::OnRegistrationDone() { 167 registration_helper_.reset(); 168 } 169 170 } // namespace policy 171