Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/signin/easy_unlock_service_regular.cc

Issue 577683002: Introduce EasyUnlockService class for signin profile (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/signin/easy_unlock_service_regular.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/values.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/signin/easy_unlock_toggle_flow.h"
15 #include "chrome/browser/signin/screenlock_bridge.h"
16 #include "chrome/browser/ui/extensions/application_launch.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "extensions/browser/extension_system.h"
21
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/profiles/profile_helper.h"
24 #include "components/user_manager/user_manager.h"
25 #endif
26
27 namespace {
28
29 // Key name of the local device permit record dictonary in kEasyUnlockPairing.
30 const char kKeyPermitAccess[] = "permitAccess";
31
32 // Key name of the remote device list in kEasyUnlockPairing.
33 const char kKeyDevices[] = "devices";
34
35 // Key name of the phone public key in a device dictionary.
36 const char kKeyPhoneId[] = "permitRecord.id";
37
38 } // namespace
39
40 EasyUnlockServiceRegular::EasyUnlockServiceRegular(Profile* profile)
41 : EasyUnlockService(profile) {
42 }
43
44 EasyUnlockServiceRegular::~EasyUnlockServiceRegular() {
45 }
46
47 EasyUnlockService::Type EasyUnlockServiceRegular::GetType() const {
48 return EasyUnlockService::TYPE_REGULAR;
49 }
50
51 std::string EasyUnlockServiceRegular::GetUserEmail() const {
52 return ScreenlockBridge::GetAuthenticatedUserEmail(profile_);
53 }
54
55 void EasyUnlockServiceRegular::LaunchSetup() {
56 ExtensionService* service =
57 extensions::ExtensionSystem::Get(profile_)->extension_service();
58 const extensions::Extension* extension =
59 service->GetExtensionById(extension_misc::kEasyUnlockAppId, false);
60
61 OpenApplication(AppLaunchParams(
62 profile_, extension, extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW));
63 }
64
65 const base::DictionaryValue* EasyUnlockServiceRegular::GetPermitAccess() const {
66 const base::DictionaryValue* pairing_dict =
67 profile_->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing);
68 const base::DictionaryValue* permit_dict = NULL;
69 if (pairing_dict &&
70 pairing_dict->GetDictionary(kKeyPermitAccess, &permit_dict))
71 return permit_dict;
72
73 return NULL;
74 }
75
76 void EasyUnlockServiceRegular::SetPermitAccess(
77 const base::DictionaryValue& permit) {
78 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(),
79 prefs::kEasyUnlockPairing);
80 pairing_update->SetWithoutPathExpansion(kKeyPermitAccess, permit.DeepCopy());
81 }
82
83 void EasyUnlockServiceRegular::ClearPermitAccess() {
84 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(),
85 prefs::kEasyUnlockPairing);
86 pairing_update->RemoveWithoutPathExpansion(kKeyPermitAccess, NULL);
87 }
88
89 const base::ListValue* EasyUnlockServiceRegular::GetRemoteDevices() const {
90 const base::DictionaryValue* pairing_dict =
91 profile_->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing);
92 const base::ListValue* devices = NULL;
93 if (pairing_dict && pairing_dict->GetList(kKeyDevices, &devices))
94 return devices;
95
96 return NULL;
97 }
98
99 void EasyUnlockServiceRegular::SetRemoteDevices(
100 const base::ListValue& devices) {
101 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(),
102 prefs::kEasyUnlockPairing);
103 pairing_update->SetWithoutPathExpansion(kKeyDevices, devices.DeepCopy());
104 }
105
106 void EasyUnlockServiceRegular::ClearRemoteDevices() {
107 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(),
108 prefs::kEasyUnlockPairing);
109 pairing_update->RemoveWithoutPathExpansion(kKeyDevices, NULL);
110 }
111
112 void EasyUnlockServiceRegular::RunTurnOffFlow() {
113 if (turn_off_flow_status_ == PENDING)
114 return;
115
116 SetTurnOffFlowStatus(PENDING);
117
118 // Currently there should only be one registered phone.
119 // TODO(xiyuan): Revisit this when server supports toggle for all or
120 // there are multiple phones.
121 const base::DictionaryValue* pairing_dict =
122 profile_->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing);
123 const base::ListValue* devices_list = NULL;
124 const base::DictionaryValue* first_device = NULL;
125 std::string phone_public_key;
126 if (!pairing_dict || !pairing_dict->GetList(kKeyDevices, &devices_list) ||
127 !devices_list || !devices_list->GetDictionary(0, &first_device) ||
128 !first_device ||
129 !first_device->GetString(kKeyPhoneId, &phone_public_key)) {
130 LOG(WARNING) << "Bad easy unlock pairing data, wiping out local data";
131 OnTurnOffFlowFinished(true);
132 return;
133 }
134
135 turn_off_flow_.reset(new EasyUnlockToggleFlow(
136 profile_,
137 phone_public_key,
138 false,
139 base::Bind(&EasyUnlockServiceRegular::OnTurnOffFlowFinished,
140 base::Unretained(this))));
141 turn_off_flow_->Start();
142 }
143
144 void EasyUnlockServiceRegular::ResetTurnOffFlow() {
145 turn_off_flow_.reset();
146 SetTurnOffFlowStatus(IDLE);
147 }
148
149 void EasyUnlockServiceRegular::InitializeInternal() {
150 registrar_.Init(profile_->GetPrefs());
151 registrar_.Add(
152 prefs::kEasyUnlockAllowed,
153 base::Bind(&EasyUnlockServiceRegular::OnPrefsChanged,
154 base::Unretained(this)));
155 OnPrefsChanged();
156 }
157
158 bool EasyUnlockServiceRegular::IsAllowedInternal() {
159 #if defined(OS_CHROMEOS)
160 if (!user_manager::UserManager::Get()->IsLoggedInAsRegularUser())
161 return false;
162
163 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_))
164 return false;
165
166 return true;
167 #else
168 // TODO(xiyuan): Revisit when non-chromeos platforms are supported.
169 return false;
170 #endif
171 }
172
173 void EasyUnlockServiceRegular::OnPrefsChanged() {
174 UpdateAppState();
175 }
176
177 void EasyUnlockServiceRegular::SetTurnOffFlowStatus(TurnOffFlowStatus status) {
178 turn_off_flow_status_ = status;
179 NotifyTurnOffOperationStatusChanged();
180 }
181
182 void EasyUnlockServiceRegular::OnTurnOffFlowFinished(bool success) {
183 turn_off_flow_.reset();
184
185 if (!success) {
186 SetTurnOffFlowStatus(FAIL);
187 return;
188 }
189
190 ClearRemoteDevices();
191 SetTurnOffFlowStatus(IDLE);
192 ReloadApp();
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698