Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/signin/easy_unlock_service.h" | 5 #include "chrome/browser/signin/easy_unlock_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/prefs/scoped_user_pref_update.h" | |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_ api.h" | |
| 13 #include "chrome/browser/extensions/component_loader.h" | 15 #include "chrome/browser/extensions/component_loader.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/easy_unlock_service_factory.h" | 18 #include "chrome/browser/signin/easy_unlock_service_factory.h" |
| 19 #include "chrome/browser/signin/easy_unlock_service_observer.h" | |
| 20 #include "chrome/browser/signin/easy_unlock_toggle_flow.h" | |
| 17 #include "chrome/browser/ui/extensions/application_launch.h" | 21 #include "chrome/browser/ui/extensions/application_launch.h" |
| 18 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" | 24 #include "components/pref_registry/pref_registry_syncable.h" |
| 21 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
| 22 #include "extensions/common/one_shot_event.h" | 26 #include "extensions/common/one_shot_event.h" |
| 23 #include "grit/browser_resources.h" | 27 #include "grit/browser_resources.h" |
| 24 | 28 |
| 25 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 26 #include "chrome/browser/chromeos/login/users/user_manager.h" | 30 #include "chrome/browser/chromeos/login/users/user_manager.h" |
| 27 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 31 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 28 #endif | 32 #endif |
| 29 | 33 |
| 30 namespace { | 34 namespace { |
| 31 | 35 |
| 36 // Key name of the local device permit record dictonary in kEasyUnlockPairing. | |
| 37 const char kKeyPermitAccess[] = "permitAccess"; | |
| 38 | |
| 39 // Key name of the remote device list in kEasyUnlockPairing. | |
| 40 const char kKeyDevices[] = "devices"; | |
| 41 | |
| 42 // Key name of the phone public key in a device dictionary. | |
| 43 const char kKeyPhoneId[] = "permitRecord.id"; | |
| 44 | |
| 32 extensions::ComponentLoader* GetComponentLoader( | 45 extensions::ComponentLoader* GetComponentLoader( |
| 33 content::BrowserContext* context) { | 46 content::BrowserContext* context) { |
| 34 extensions::ExtensionSystem* extension_system = | 47 extensions::ExtensionSystem* extension_system = |
| 35 extensions::ExtensionSystem::Get(context); | 48 extensions::ExtensionSystem::Get(context); |
| 36 ExtensionService* extension_service = extension_system->extension_service(); | 49 ExtensionService* extension_service = extension_system->extension_service(); |
| 37 return extension_service->component_loader(); | 50 return extension_service->component_loader(); |
| 38 } | 51 } |
| 39 | 52 |
| 40 } // namespace | 53 } // namespace |
| 41 | 54 |
| 42 // static | 55 // static |
| 43 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { | 56 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { |
| 44 return EasyUnlockServiceFactory::GetForProfile(profile); | 57 return EasyUnlockServiceFactory::GetForProfile(profile); |
| 45 } | 58 } |
| 46 | 59 |
| 47 EasyUnlockService::EasyUnlockService(Profile* profile) | 60 EasyUnlockService::EasyUnlockService(Profile* profile) |
| 48 : profile_(profile), weak_ptr_factory_(this) { | 61 : profile_(profile), turn_off_flow_status_(IDLE), weak_ptr_factory_(this) { |
| 49 extensions::ExtensionSystem::Get(profile_)->ready().Post( | 62 extensions::ExtensionSystem::Get(profile_)->ready().Post( |
| 50 FROM_HERE, | 63 FROM_HERE, |
| 51 base::Bind(&EasyUnlockService::Initialize, | 64 base::Bind(&EasyUnlockService::Initialize, |
| 52 weak_ptr_factory_.GetWeakPtr())); | 65 weak_ptr_factory_.GetWeakPtr())); |
| 53 } | 66 } |
| 54 | 67 |
| 55 EasyUnlockService::~EasyUnlockService() { | 68 EasyUnlockService::~EasyUnlockService() { |
| 56 } | 69 } |
| 57 | 70 |
| 58 // static | 71 // static |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 return false; | 111 return false; |
| 99 | 112 |
| 100 // It is only disabled when the trial exists and is in "Disable" group. | 113 // It is only disabled when the trial exists and is in "Disable" group. |
| 101 return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable"; | 114 return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable"; |
| 102 #else | 115 #else |
| 103 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. | 116 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. |
| 104 return false; | 117 return false; |
| 105 #endif | 118 #endif |
| 106 } | 119 } |
| 107 | 120 |
| 121 const base::DictionaryValue* EasyUnlockService::GetPermitAccess() const { | |
| 122 const base::DictionaryValue* pairing_dict = | |
| 123 profile_->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing); | |
| 124 const base::DictionaryValue* permit_dict = NULL; | |
| 125 if (pairing_dict && | |
| 126 pairing_dict->GetDictionary(kKeyPermitAccess, &permit_dict)) { | |
| 127 return permit_dict; | |
| 128 } | |
| 129 | |
| 130 return NULL; | |
| 131 } | |
| 132 | |
| 133 void EasyUnlockService::SetPermitAccess(const base::DictionaryValue& permit) { | |
| 134 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(), | |
| 135 prefs::kEasyUnlockPairing); | |
| 136 pairing_update->SetWithoutPathExpansion(kKeyPermitAccess, permit.DeepCopy()); | |
| 137 } | |
| 138 | |
| 139 void EasyUnlockService::ClearPermitAccess() { | |
| 140 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(), | |
| 141 prefs::kEasyUnlockPairing); | |
| 142 pairing_update->RemoveWithoutPathExpansion(kKeyPermitAccess, NULL); | |
| 143 } | |
| 144 | |
| 145 const base::ListValue* EasyUnlockService::GetRemoteDevices() const { | |
| 146 const base::DictionaryValue* pairing_dict = | |
| 147 profile_->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing); | |
| 148 const base::ListValue* devices = NULL; | |
| 149 if (pairing_dict && pairing_dict->GetList(kKeyDevices, &devices)) { | |
| 150 return devices; | |
| 151 } | |
| 152 | |
| 153 return NULL; | |
| 154 } | |
| 155 | |
| 156 void EasyUnlockService::SetRemoteDevices(const base::ListValue& devices) { | |
| 157 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(), | |
| 158 prefs::kEasyUnlockPairing); | |
| 159 pairing_update->SetWithoutPathExpansion(kKeyDevices, devices.DeepCopy()); | |
| 160 } | |
| 161 | |
| 162 void EasyUnlockService::ClearRemoteDevices() { | |
| 163 DictionaryPrefUpdate pairing_update(profile_->GetPrefs(), | |
| 164 prefs::kEasyUnlockPairing); | |
| 165 pairing_update->RemoveWithoutPathExpansion(kKeyDevices, NULL); | |
| 166 } | |
| 167 | |
| 168 void EasyUnlockService::AddObserver(EasyUnlockServiceObserver* observer) { | |
| 169 observers_.AddObserver(observer); | |
| 170 } | |
| 171 | |
| 172 void EasyUnlockService::RemoveObserver(EasyUnlockServiceObserver* observer) { | |
| 173 observers_.RemoveObserver(observer); | |
| 174 } | |
| 175 | |
| 176 void EasyUnlockService::RunTurnOffFlow() { | |
| 177 if (turn_off_flow_status_ == PENDING) | |
| 178 return; | |
| 179 | |
| 180 SetTurnOffFlowStatus(PENDING); | |
| 181 | |
| 182 const base::DictionaryValue* pairing_dict = | |
| 183 profile_->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing); | |
| 184 const base::ListValue* devices_list = NULL; | |
| 185 const base::DictionaryValue* first_device = NULL; | |
| 186 std::string phone_public_key; | |
| 187 if (!pairing_dict || !pairing_dict->GetList(kKeyDevices, &devices_list) || | |
| 188 !devices_list || !devices_list->GetDictionary(0, &first_device) || | |
| 189 !first_device || | |
| 190 !first_device->GetString(kKeyPhoneId, &phone_public_key)) { | |
| 191 LOG(WARNING) << "Bad easy unlock pairing data, wiping out local data"; | |
| 192 OnTurnOffFlowFinished(true); | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 turn_off_flow_.reset(new EasyUnlockToggleFlow( | |
| 197 profile_, | |
| 198 phone_public_key, | |
|
tbarzic
2014/08/14 17:43:24
can you add a TODO for handling multiple phones (a
xiyuan
2014/08/14 22:09:19
Done.
| |
| 199 false, | |
| 200 base::Bind(&EasyUnlockService::OnTurnOffFlowFinished, | |
| 201 base::Unretained(this)))); | |
| 202 turn_off_flow_->Start(); | |
| 203 } | |
| 204 | |
| 205 void EasyUnlockService::ResetTurnOffFlow() { | |
| 206 turn_off_flow_.reset(); | |
| 207 SetTurnOffFlowStatus(IDLE); | |
| 208 } | |
| 209 | |
| 108 void EasyUnlockService::Initialize() { | 210 void EasyUnlockService::Initialize() { |
| 109 registrar_.Init(profile_->GetPrefs()); | 211 registrar_.Init(profile_->GetPrefs()); |
| 110 registrar_.Add( | 212 registrar_.Add( |
| 111 prefs::kEasyUnlockAllowed, | 213 prefs::kEasyUnlockAllowed, |
| 112 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); | 214 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); |
| 113 OnPrefsChanged(); | 215 OnPrefsChanged(); |
| 114 } | 216 } |
| 115 | 217 |
| 116 void EasyUnlockService::LoadApp() { | 218 void EasyUnlockService::LoadApp() { |
| 117 DCHECK(IsAllowed()); | 219 DCHECK(IsAllowed()); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 141 void EasyUnlockService::UnloadApp() { | 243 void EasyUnlockService::UnloadApp() { |
| 142 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); | 244 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); |
| 143 } | 245 } |
| 144 | 246 |
| 145 void EasyUnlockService::OnPrefsChanged() { | 247 void EasyUnlockService::OnPrefsChanged() { |
| 146 if (IsAllowed()) | 248 if (IsAllowed()) |
| 147 LoadApp(); | 249 LoadApp(); |
| 148 else | 250 else |
| 149 UnloadApp(); | 251 UnloadApp(); |
| 150 } | 252 } |
| 253 | |
| 254 void EasyUnlockService::SetTurnOffFlowStatus(TurnOffFlowStatus status) { | |
| 255 turn_off_flow_status_ = status; | |
| 256 FOR_EACH_OBSERVER( | |
| 257 EasyUnlockServiceObserver, observers_, OnTurnOffOperationStatusChanged()); | |
| 258 } | |
| 259 | |
| 260 void EasyUnlockService::OnTurnOffFlowFinished(bool success) { | |
| 261 turn_off_flow_.reset(); | |
| 262 | |
| 263 if (!success) { | |
| 264 SetTurnOffFlowStatus(FAIL); | |
| 265 return; | |
| 266 } | |
| 267 | |
| 268 ClearPermitAccess(); | |
|
Tim Song
2014/08/14 07:57:41
We shouldn't clear the permit access here. We are
xiyuan
2014/08/14 22:09:19
Done. And update the has_paring logic in browser_o
| |
| 269 ClearRemoteDevices(); | |
| 270 | |
| 271 extensions::BrowserContextKeyedAPIFactory< | |
| 272 extensions::api::EasyUnlockPrivateAPI>::Get(profile_) | |
| 273 ->SendTurnOffFlowFinished(); | |
| 274 SetTurnOffFlowStatus(IDLE); | |
| 275 } | |
| OLD | NEW |