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/policy/cloud/user_policy_signin_service_ios.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_factory.h"
17 #include "chrome/browser/signin/signin_manager.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/policy/core/common/cloud/cloud_policy_client_registration_helper.h"
20 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
21 #include "components/policy/core/common/policy_switches.h"
22 #include "components/signin/core/browser/profile_oauth2_token_service.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"
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::IOS_BROWSER;
40 UserPolicySigninService::UserPolicySigninService(
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,
50 device_management_service,
53 system_request_context),
55 oauth2_token_service_(token_service),
56 profile_prefs_(profile->GetPrefs()) {}
58 UserPolicySigninService::~UserPolicySigninService() {}
60 void UserPolicySigninService::RegisterForPolicy(
61 const std::string& username,
62 PolicyRegistrationBlockCallback callback) {
63 // Create a new CloudPolicyClient for fetching the DMToken.
64 scoped_ptr<CloudPolicyClient> policy_client = CreateClientForRegistrationOnly(
67 callback(std::string(), std::string());
71 CancelPendingRegistration();
73 // Fire off the registration process. Callback keeps the CloudPolicyClient
74 // alive for the length of the registration process.
75 registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
77 GetRegistrationType()));
78 registration_helper_->StartRegistration(
79 oauth2_token_service_,
81 base::Bind(&UserPolicySigninService::CallPolicyRegistrationCallback,
82 base::Unretained(this),
83 base::Passed(&policy_client),
87 void UserPolicySigninService::FetchPolicy(
88 const std::string& username,
89 const std::string& dm_token,
90 const std::string& client_id,
91 scoped_refptr<net::URLRequestContextGetter> profile_request_context,
92 PolicyFetchBlockCallback callback) {
93 FetchPolicyForSignedInUser(
97 profile_request_context,
98 base::Bind(&UserPolicySigninService::CallPolicyFetchCallback,
99 base::Unretained(this),
103 void UserPolicySigninService::CallPolicyRegistrationCallback(
104 scoped_ptr<CloudPolicyClient> client,
105 PolicyRegistrationBlockCallback callback) {
106 registration_helper_.reset();
107 callback(client->dm_token(), client->client_id());
111 void UserPolicySigninService::CallPolicyFetchCallback(
112 PolicyFetchBlockCallback callback,
118 void UserPolicySigninService::Shutdown() {
119 CancelPendingRegistration();
120 registration_helper_.reset();
121 UserPolicySigninServiceBase::Shutdown();
124 void UserPolicySigninService::OnInitializationCompleted(
125 CloudPolicyService* service) {
126 UserCloudPolicyManager* manager = policy_manager();
127 DCHECK_EQ(service, manager->core()->service());
128 DCHECK(service->IsInitializationComplete());
129 // Note: we don't register the cloud policy client on iOS if it's not
130 // registered at this stage. If there was no policy at sign-in time then
131 // there won't be policy later either.
134 void UserPolicySigninService::CancelPendingRegistration() {
135 weak_factory_.InvalidateWeakPtrs();
138 } // namespace policy