easy_unlock_service.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.h" 6 7#include "base/bind.h" 8#include "base/command_line.h" 9#include "base/logging.h" 10#include "base/metrics/field_trial.h" 11#include "base/prefs/pref_service.h" 12#include "base/values.h" 13#include "chrome/browser/extensions/component_loader.h" 14#include "chrome/browser/extensions/extension_service.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/browser/signin/easy_unlock_service_factory.h" 17#include "chrome/browser/ui/extensions/application_launch.h" 18#include "chrome/common/chrome_switches.h" 19#include "chrome/common/pref_names.h" 20#include "components/pref_registry/pref_registry_syncable.h" 21#include "extensions/browser/extension_system.h" 22#include "extensions/common/one_shot_event.h" 23#include "grit/browser_resources.h" 24 25#if defined(OS_CHROMEOS) 26#include "chrome/browser/chromeos/login/users/user_manager.h" 27#include "chrome/browser/chromeos/profiles/profile_helper.h" 28#endif 29 30namespace { 31 32extensions::ComponentLoader* GetComponentLoader( 33 content::BrowserContext* context) { 34 extensions::ExtensionSystem* extension_system = 35 extensions::ExtensionSystem::Get(context); 36 ExtensionService* extension_service = extension_system->extension_service(); 37 return extension_service->component_loader(); 38} 39 40} // namespace 41 42// static 43EasyUnlockService* EasyUnlockService::Get(Profile* profile) { 44 return EasyUnlockServiceFactory::GetForProfile(profile); 45} 46 47EasyUnlockService::EasyUnlockService(Profile* profile) 48 : profile_(profile), weak_ptr_factory_(this) { 49 extensions::ExtensionSystem::Get(profile_)->ready().Post( 50 FROM_HERE, 51 base::Bind(&EasyUnlockService::Initialize, 52 weak_ptr_factory_.GetWeakPtr())); 53} 54 55EasyUnlockService::~EasyUnlockService() { 56} 57 58// static 59void EasyUnlockService::RegisterProfilePrefs( 60 user_prefs::PrefRegistrySyncable* registry) { 61 registry->RegisterBooleanPref( 62 prefs::kEasyUnlockEnabled, 63 false, 64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 65 registry->RegisterBooleanPref( 66 prefs::kEasyUnlockShowTutorial, 67 false, 68 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 69 registry->RegisterDictionaryPref( 70 prefs::kEasyUnlockPairing, 71 new base::DictionaryValue(), 72 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 73 registry->RegisterBooleanPref( 74 prefs::kEasyUnlockAllowed, 75 true, 76 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 77} 78 79void EasyUnlockService::LaunchSetup() { 80 ExtensionService* service = 81 extensions::ExtensionSystem::Get(profile_)->extension_service(); 82 const extensions::Extension* extension = 83 service->GetExtensionById(extension_misc::kEasyUnlockAppId, false); 84 85 OpenApplication(AppLaunchParams( 86 profile_, extension, extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW)); 87} 88 89bool EasyUnlockService::IsAllowed() { 90#if defined(OS_CHROMEOS) 91 if (chromeos::UserManager::Get()->IsLoggedInAsGuest()) 92 return false; 93 94 if (!chromeos::ProfileHelper::IsPrimaryProfile(profile_)) 95 return false; 96 97 if (!profile_->GetPrefs()->GetBoolean(prefs::kEasyUnlockAllowed)) 98 return false; 99 100 // It is only disabled when the trial exists and is in "Disable" group. 101 return base::FieldTrialList::FindFullName("EasyUnlock") != "Disable"; 102#else 103 // TODO(xiyuan): Revisit when non-chromeos platforms are supported. 104 return false; 105#endif 106} 107 108void EasyUnlockService::Initialize() { 109 registrar_.Init(profile_->GetPrefs()); 110 registrar_.Add( 111 prefs::kEasyUnlockAllowed, 112 base::Bind(&EasyUnlockService::OnPrefsChanged, base::Unretained(this))); 113 OnPrefsChanged(); 114} 115 116void EasyUnlockService::LoadApp() { 117 DCHECK(IsAllowed()); 118 119#if defined(GOOGLE_CHROME_BUILD) 120 base::FilePath easy_unlock_path; 121#if defined(OS_CHROMEOS) 122 easy_unlock_path = base::FilePath("/usr/share/chromeos-assets/easy_unlock"); 123#endif // defined(OS_CHROMEOS) 124 125#ifndef NDEBUG 126 // Only allow app path override switch for debug build. 127 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 128 if (command_line->HasSwitch(switches::kEasyUnlockAppPath)) { 129 easy_unlock_path = 130 command_line->GetSwitchValuePath(switches::kEasyUnlockAppPath); 131 } 132#endif // !defined(NDEBUG) 133 134 if (!easy_unlock_path.empty()) { 135 GetComponentLoader(profile_) 136 ->Add(IDR_EASY_UNLOCK_MANIFEST, easy_unlock_path); 137 } 138#endif // defined(GOOGLE_CHROME_BUILD) 139} 140 141void EasyUnlockService::UnloadApp() { 142 GetComponentLoader(profile_)->Remove(extension_misc::kEasyUnlockAppId); 143} 144 145void EasyUnlockService::OnPrefsChanged() { 146 if (IsAllowed()) 147 LoadApp(); 148 else 149 UnloadApp(); 150} 151