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.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"
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 const bool force_load_policy = false;
76 registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
79 GetRegistrationType()));
80 registration_helper_->StartRegistration(
81 oauth2_token_service_,
83 base::Bind(&UserPolicySigninService::CallPolicyRegistrationCallback,
84 base::Unretained(this),
85 base::Passed(&policy_client),
89 void UserPolicySigninService::FetchPolicy(
90 const std::string& username,
91 const std::string& dm_token,
92 const std::string& client_id,
93 scoped_refptr<net::URLRequestContextGetter> profile_request_context,
94 PolicyFetchBlockCallback callback) {
95 FetchPolicyForSignedInUser(
99 profile_request_context,
100 base::Bind(&UserPolicySigninService::CallPolicyFetchCallback,
101 base::Unretained(this),
105 void UserPolicySigninService::CallPolicyRegistrationCallback(
106 scoped_ptr<CloudPolicyClient> client,
107 PolicyRegistrationBlockCallback callback) {
108 registration_helper_.reset();
109 callback(client->dm_token(), client->client_id());
113 void UserPolicySigninService::CallPolicyFetchCallback(
114 PolicyFetchBlockCallback callback,
120 void UserPolicySigninService::Shutdown() {
121 CancelPendingRegistration();
122 registration_helper_.reset();
123 UserPolicySigninServiceBase::Shutdown();
126 void UserPolicySigninService::OnInitializationCompleted(
127 CloudPolicyService* service) {
128 UserCloudPolicyManager* manager = policy_manager();
129 DCHECK_EQ(service, manager->core()->service());
130 DCHECK(service->IsInitializationComplete());
131 // Note: we don't register the cloud policy client on iOS if it's not
132 // registered at this stage. If there was no policy at sign-in time then
133 // there won't be policy later either.
136 void UserPolicySigninService::CancelPendingRegistration() {
137 weak_factory_.InvalidateWeakPtrs();
140 } // namespace policy