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/memory/ref_counted.h" | |
| 10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 11 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/component_loader.h" | 14 #include "chrome/browser/extensions/component_loader.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/easy_unlock_service_factory.h" | 17 #include "chrome/browser/signin/easy_unlock_service_factory.h" |
| 17 #include "chrome/browser/ui/extensions/application_launch.h" | 18 #include "chrome/browser/ui/extensions/application_launch.h" |
| 18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" | 21 #include "components/pref_registry/pref_registry_syncable.h" |
| 22 #include "device/bluetooth/bluetooth_adapter.h" | |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 21 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 22 #include "extensions/common/one_shot_event.h" | 25 #include "extensions/common/one_shot_event.h" |
| 23 #include "grit/browser_resources.h" | 26 #include "grit/browser_resources.h" |
| 24 | 27 |
| 25 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
| 26 #include "chrome/browser/chromeos/login/users/user_manager.h" | 29 #include "chrome/browser/chromeos/login/users/user_manager.h" |
| 27 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 30 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 28 #endif | 31 #endif |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 extensions::ComponentLoader* GetComponentLoader( | 35 extensions::ComponentLoader* GetComponentLoader( |
| 33 content::BrowserContext* context) { | 36 content::BrowserContext* context) { |
| 34 extensions::ExtensionSystem* extension_system = | 37 extensions::ExtensionSystem* extension_system = |
| 35 extensions::ExtensionSystem::Get(context); | 38 extensions::ExtensionSystem::Get(context); |
| 36 ExtensionService* extension_service = extension_system->extension_service(); | 39 ExtensionService* extension_service = extension_system->extension_service(); |
| 37 return extension_service->component_loader(); | 40 return extension_service->component_loader(); |
| 38 } | 41 } |
| 39 | 42 |
| 40 } // namespace | 43 } // namespace |
| 41 | 44 |
| 42 // static | 45 // static |
| 43 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { | 46 EasyUnlockService* EasyUnlockService::Get(Profile* profile) { |
| 44 return EasyUnlockServiceFactory::GetForProfile(profile); | 47 return EasyUnlockServiceFactory::GetForProfile(profile); |
| 45 } | 48 } |
| 46 | 49 |
| 50 class EasyUnlockService::BluetoothDetector | |
| 51 : public device::BluetoothAdapter::Observer { | |
| 52 public: | |
| 53 explicit BluetoothDetector(EasyUnlockService* service) | |
| 54 : service_(service), | |
| 55 weak_ptr_factory_(this) { | |
| 56 } | |
| 57 | |
| 58 virtual ~BluetoothDetector() { | |
| 59 if (adapter_) | |
| 60 adapter_->RemoveObserver(this); | |
| 61 } | |
| 62 | |
| 63 void Initialize() { | |
| 64 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | |
| 65 return; | |
| 66 | |
| 67 device::BluetoothAdapterFactory::GetAdapter( | |
| 68 base::Bind(&BluetoothDetector::OnAdapterInitialized, | |
| 69 weak_ptr_factory_.GetWeakPtr())); | |
| 70 } | |
| 71 | |
| 72 bool IsPresent() const { | |
| 73 return adapter_ && adapter_->IsPresent(); | |
| 74 } | |
| 75 | |
| 76 // device::BluetoothAdapter::Observer | |
|
Ilya Sherman
2014/08/14 21:38:28
nit: Please add a trailing colon.
xiyuan
2014/08/14 22:56:46
Done.
| |
| 77 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, | |
| 78 bool present) OVERRIDE { | |
| 79 ReportAdapterPresent(); | |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 84 adapter_ = adapter; | |
| 85 adapter_->AddObserver(this); | |
| 86 ReportAdapterPresent(); | |
| 87 } | |
| 88 | |
| 89 void ReportAdapterPresent() { | |
| 90 service_->OnBluetoothAdapterPresentChanged(); | |
|
Ilya Sherman
2014/08/14 21:38:27
nit: IMO it's slightly cleaner to just inline this
xiyuan
2014/08/14 22:56:46
Done.
| |
| 91 } | |
| 92 | |
| 93 // Owner of this class and should out-live this class. | |
| 94 EasyUnlockService* service_; | |
| 95 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 96 base::WeakPtrFactory<BluetoothDetector> weak_ptr_factory_; | |
|
Ilya Sherman
2014/08/14 21:38:27
nit: Please add a blank line after this one.
xiyuan
2014/08/14 22:56:46
Done.
| |
| 97 DISALLOW_COPY_AND_ASSIGN(BluetoothDetector); | |
| 98 }; | |
| 99 | |
| 47 EasyUnlockService::EasyUnlockService(Profile* profile) | 100 EasyUnlockService::EasyUnlockService(Profile* profile) |
| 48 : profile_(profile), weak_ptr_factory_(this) { | 101 : profile_(profile), |
| 102 bluetooth_detector_(new BluetoothDetector(this)), | |
| 103 weak_ptr_factory_(this) { | |
| 49 extensions::ExtensionSystem::Get(profile_)->ready().Post( | 104 extensions::ExtensionSystem::Get(profile_)->ready().Post( |
| 50 FROM_HERE, | 105 FROM_HERE, |
| 51 base::Bind(&EasyUnlockService::Initialize, | 106 base::Bind(&EasyUnlockService::Initialize, |
| 52 weak_ptr_factory_.GetWeakPtr())); | 107 weak_ptr_factory_.GetWeakPtr())); |
| 53 } | 108 } |
| 54 | 109 |
| 55 EasyUnlockService::~EasyUnlockService() { | 110 EasyUnlockService::~EasyUnlockService() { |
| 56 } | 111 } |
| 57 | 112 |
| 58 // static | 113 // static |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 #if defined(OS_CHROMEOS) | 145 #if defined(OS_CHROMEOS) |
| 91 if (chromeos::UserManager::Get()->IsLoggedInAsGuest()) | 146 if (chromeos::UserManager::Get()->IsLoggedInAsGuest()) |
| 92 return false; | 147 return false; |
| 93 | 148 |
| 94 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) | 149 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) |
| 95 return false; | 150 return false; |
| 96 | 151 |
| 97 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) | 152 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) |
| 98 return false; | 153 return false; |
| 99 | 154 |
| 100 // It is only disabled when the trial exists and is in "Disable" group. | 155 // It is disabled when the trial exists and is in "Disable" group. |
| 101 return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable"; | 156 if (base::FieldTrialList::FindFullName("EasyUnlock") == "Disable") |
| 157 return false; | |
| 158 | |
| 159 if (!bluetooth_detector_->IsPresent()) | |
| 160 return false; | |
| 161 | |
| 162 return true; | |
| 102 #else | 163 #else |
| 103 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. | 164 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. |
| 104 return false; | 165 return false; |
| 105 #endif | 166 #endif |
| 106 } | 167 } |
| 107 | 168 |
| 108 void EasyUnlockService::Initialize() { | 169 void EasyUnlockService::Initialize() { |
| 109 registrar_.Init(profile_->GetPrefs()); | 170 registrar_.Init(profile_->GetPrefs()); |
| 110 registrar_.Add( | 171 registrar_.Add( |
| 111 prefs::kEasyUnlockAllowed, | 172 prefs::kEasyUnlockAllowed, |
| 112 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); | 173 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); |
| 113 OnPrefsChanged(); | 174 OnPrefsChanged(); |
| 175 | |
| 176 bluetooth_detector_->Initialize(); | |
| 114 } | 177 } |
| 115 | 178 |
| 116 void EasyUnlockService::LoadApp() { | 179 void EasyUnlockService::LoadApp() { |
| 117 DCHECK(IsAllowed()); | 180 DCHECK(IsAllowed()); |
| 118 | 181 |
| 119 #if defined(GOOGLE_CHROME_BUILD) | 182 #if defined(GOOGLE_CHROME_BUILD) |
| 120 base::FilePath easy_unlock_path; | 183 base::FilePath easy_unlock_path; |
| 121 #if defined(OS_CHROMEOS) | 184 #if defined(OS_CHROMEOS) |
| 122 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); | 185 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); |
| 123 #endif // defined(OS_CHROMEOS) | 186 #endif // defined(OS_CHROMEOS) |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 135 GetComponentLoader(profile_) | 198 GetComponentLoader(profile_) |
| 136 ->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); | 199 ->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); |
| 137 } | 200 } |
| 138 #endif // defined(GOOGLE_CHROME_BUILD) | 201 #endif // defined(GOOGLE_CHROME_BUILD) |
| 139 } | 202 } |
| 140 | 203 |
| 141 void EasyUnlockService::UnloadApp() { | 204 void EasyUnlockService::UnloadApp() { |
| 142 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); | 205 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); |
| 143 } | 206 } |
| 144 | 207 |
| 145 void EasyUnlockService::OnPrefsChanged() { | 208 void EasyUnlockService::UpdateAppState() { |
| 146 if (IsAllowed()) | 209 if (IsAllowed()) |
| 147 LoadApp(); | 210 LoadApp(); |
| 148 else | 211 else |
| 149 UnloadApp(); | 212 UnloadApp(); |
| 150 } | 213 } |
| 214 | |
| 215 void EasyUnlockService::OnPrefsChanged() { | |
| 216 UpdateAppState(); | |
| 217 } | |
| 218 | |
| 219 void EasyUnlockService::OnBluetoothAdapterPresentChanged() { | |
| 220 UpdateAppState(); | |
| 221 } | |
| 222 | |
| OLD | NEW |