Upstream version 7.35.144.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_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"
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   registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
76       policy_client.get(),
77       GetRegistrationType()));
78   registration_helper_->StartRegistration(
79       oauth2_token_service_,
80       username,
81       base::Bind(&UserPolicySigninService::CallPolicyRegistrationCallback,
82                  base::Unretained(this),
83                  base::Passed(&policy_client),
84                  [callback copy]));
85 }
86
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(
94       username,
95       dm_token,
96       client_id,
97       profile_request_context,
98       base::Bind(&UserPolicySigninService::CallPolicyFetchCallback,
99                  base::Unretained(this),
100                  [callback copy]));
101 }
102
103 void UserPolicySigninService::CallPolicyRegistrationCallback(
104     scoped_ptr<CloudPolicyClient> client,
105     PolicyRegistrationBlockCallback callback) {
106   registration_helper_.reset();
107   callback(client->dm_token(), client->client_id());
108   [callback release];
109 }
110
111 void UserPolicySigninService::CallPolicyFetchCallback(
112     PolicyFetchBlockCallback callback,
113     bool succeeded) {
114   callback(succeeded);
115   [callback release];
116 }
117
118 void UserPolicySigninService::Shutdown() {
119   CancelPendingRegistration();
120   registration_helper_.reset();
121   UserPolicySigninServiceBase::Shutdown();
122 }
123
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.
132 }
133
134 void UserPolicySigninService::CancelPendingRegistration() {
135   weak_factory_.InvalidateWeakPtrs();
136 }
137
138 }  // namespace policy