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

Unified Diff: chrome/browser/signin/easy_unlock_service.cc

Issue 472733002: No Easy unlock if bluetooth is not available. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: attempt to fix gmock error Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/signin/easy_unlock_service.cc
diff --git a/chrome/browser/signin/easy_unlock_service.cc b/chrome/browser/signin/easy_unlock_service.cc
index 1afcbc9e128e45c78b357f083d75025b091eac08..b6f18c7f3ed0ee24a3c852d345a7271128f5c1a4 100644
--- a/chrome/browser/signin/easy_unlock_service.cc
+++ b/chrome/browser/signin/easy_unlock_service.cc
@@ -7,6 +7,7 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
+#include "base/memory/ref_counted.h"
#include "base/metrics/field_trial.h"
#include "base/prefs/pref_service.h"
#include "base/values.h"
@@ -18,6 +19,8 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
+#include "device/bluetooth/bluetooth_adapter.h"
+#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/one_shot_event.h"
#include "grit/browser_resources.h"
@@ -44,8 +47,60 @@ EasyUnlockService* EasyUnlockService::Get(Profile* profile) {
return EasyUnlockServiceFactory::GetForProfile(profile);
}
+class EasyUnlockService::BluetoothDetector
+ : public device::BluetoothAdapter::Observer {
+ public:
+ explicit BluetoothDetector(EasyUnlockService* service)
+ : service_(service),
+ weak_ptr_factory_(this) {
+ }
+
+ virtual ~BluetoothDetector() {
+ if (adapter_)
+ adapter_->RemoveObserver(this);
+ }
+
+ void Initialize() {
+ if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable())
+ return;
+
+ device::BluetoothAdapterFactory::GetAdapter(
+ base::Bind(&BluetoothDetector::OnAdapterInitialized,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+
+ bool IsPresent() const {
+ return adapter_ && adapter_->IsPresent();
+ }
+
+ // 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.
+ virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
+ bool present) OVERRIDE {
+ ReportAdapterPresent();
+ }
+
+ private:
+ void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter) {
+ adapter_ = adapter;
+ adapter_->AddObserver(this);
+ ReportAdapterPresent();
+ }
+
+ void ReportAdapterPresent() {
+ 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.
+ }
+
+ // Owner of this class and should out-live this class.
+ EasyUnlockService* service_;
+ scoped_refptr<device::BluetoothAdapter> adapter_;
+ 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.
+ DISALLOW_COPY_AND_ASSIGN(BluetoothDetector);
+};
+
EasyUnlockService::EasyUnlockService(Profile* profile)
- : profile_(profile), weak_ptr_factory_(this) {
+ : profile_(profile),
+ bluetooth_detector_(new BluetoothDetector(this)),
+ weak_ptr_factory_(this) {
extensions::ExtensionSystem::Get(profile_)->ready().Post(
FROM_HERE,
base::Bind(&EasyUnlockService::Initialize,
@@ -97,8 +152,14 @@ bool EasyUnlockService::IsAllowed() {
if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed))
return false;
- // It is only disabled when the trial exists and is in "Disable" group.
- return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable";
+ // It is disabled when the trial exists and is in "Disable" group.
+ if (base::FieldTrialList::FindFullName("EasyUnlock") == "Disable")
+ return false;
+
+ if (!bluetooth_detector_->IsPresent())
+ return false;
+
+ return true;
#else
// TODO(xiyuan): Revisit when non-chromeos platforms are supported.
return false;
@@ -111,6 +172,8 @@ void EasyUnlockService::Initialize() {
prefs::kEasyUnlockAllowed,
base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this)));
OnPrefsChanged();
+
+ bluetooth_detector_->Initialize();
}
void EasyUnlockService::LoadApp() {
@@ -142,9 +205,18 @@ void EasyUnlockService::UnloadApp() {
GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId);
}
-void EasyUnlockService::OnPrefsChanged() {
+void EasyUnlockService::UpdateAppState() {
if (IsAllowed())
LoadApp();
else
UnloadApp();
}
+
+void EasyUnlockService::OnPrefsChanged() {
+ UpdateAppState();
+}
+
+void EasyUnlockService::OnBluetoothAdapterPresentChanged() {
+ UpdateAppState();
+}
+

Powered by Google App Engine
This is Rietveld 408576698