Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / user_policy_signin_service_ios.mm
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.
4
5 #include "chrome/browser/policy/cloud/user_policy_signin_service_ios.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::IOS_BROWSER;
36 }
37
38 }  // namespace
39
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()) {}
57
58 UserPolicySigninService::~UserPolicySigninService() {}
59
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(
65       username);
66   if (!policy_client) {
67     callback(std::string(), std::string());
68     return;
69   }
70
71   CancelPendingRegistration();
72
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 copy]));
87 }
88
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(
96       username,
97       dm_token,
98       client_id,
99       profile_request_context,
100       base::Bind(&UserPolicySigninService::CallPolicyFetchCallback,
101                  base::Unretained(this),
102                  [callback copy]));
103 }
104
105 void UserPolicySigninService::CallPolicyRegistrationCallback(
106     scoped_ptr<CloudPolicyClient> client,
107     PolicyRegistrationBlockCallback callback) {
108   registration_helper_.reset();
109   callback(client->dm_token(), client->client_id());
110   [callback release];
111 }
112
113 void UserPolicySigninService::CallPolicyFetchCallback(
114     PolicyFetchBlockCallback callback,
115     bool succeeded) {
116   callback(succeeded);
117   [callback release];
118 }
119
120 void UserPolicySigninService::Shutdown() {
121   CancelPendingRegistration();
122   registration_helper_.reset();
123   UserPolicySigninServiceBase::Shutdown();
124 }
125
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.
134 }
135
136 void UserPolicySigninService::CancelPendingRegistration() {
137   weak_factory_.InvalidateWeakPtrs();
138 }
139
140 }  // namespace policy