1// Copyright 2013 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/chromeos/extensions/screenlock_private_api.h" 6 7#include "base/lazy_instance.h" 8#include "base/values.h" 9#include "chrome/browser/chromeos/login/screen_locker.h" 10#include "chrome/browser/extensions/extension_system.h" 11#include "chrome/common/extensions/api/screenlock_private.h" 12#include "chromeos/dbus/dbus_thread_manager.h" 13#include "extensions/browser/event_router.h" 14 15namespace screenlock = extensions::api::screenlock_private; 16 17namespace extensions { 18 19ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {} 20 21ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {} 22 23bool ScreenlockPrivateGetLockedFunction::RunImpl() { 24 bool locked = false; 25 chromeos::ScreenLocker* locker = 26 chromeos::ScreenLocker::default_screen_locker(); 27 if (locker) 28 locked = locker->locked(); 29 SetResult(new base::FundamentalValue(locked)); 30 SendResponse(error_.empty()); 31 return true; 32} 33 34ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {} 35 36ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {} 37 38bool ScreenlockPrivateSetLockedFunction::RunImpl() { 39 scoped_ptr<screenlock::SetLocked::Params> params( 40 screenlock::SetLocked::Params::Create(*args_)); 41 EXTENSION_FUNCTION_VALIDATE(params.get()); 42 if (params->locked) { 43 chromeos::SessionManagerClient* session_manager = 44 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); 45 session_manager->RequestLockScreen(); 46 } else { 47 chromeos::ScreenLocker* locker = 48 chromeos::ScreenLocker::default_screen_locker(); 49 if (locker) 50 chromeos::ScreenLocker::Hide(); 51 } 52 SendResponse(error_.empty()); 53 return true; 54} 55 56ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {} 57 58ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {} 59 60bool ScreenlockPrivateShowMessageFunction::RunImpl() { 61 scoped_ptr<screenlock::ShowMessage::Params> params( 62 screenlock::ShowMessage::Params::Create(*args_)); 63 EXTENSION_FUNCTION_VALIDATE(params.get()); 64 chromeos::ScreenLocker* locker = 65 chromeos::ScreenLocker::default_screen_locker(); 66 if (!locker) { 67 SendResponse(error_.empty()); 68 return true; 69 } 70 locker->ShowBannerMessage(params->message); 71 SendResponse(error_.empty()); 72 return true; 73} 74 75ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(Profile* profile) 76 : profile_(profile) { 77 chromeos::SessionManagerClient* session_manager = 78 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); 79 if (!session_manager->HasObserver(this)) 80 session_manager->AddObserver(this); 81} 82 83ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {} 84 85void ScreenlockPrivateEventRouter::ScreenIsLocked() { 86 DispatchEvent(screenlock::OnChanged::kEventName, 87 new base::FundamentalValue(true)); 88} 89 90void ScreenlockPrivateEventRouter::ScreenIsUnlocked() { 91 DispatchEvent(screenlock::OnChanged::kEventName, 92 new base::FundamentalValue(false)); 93} 94 95void ScreenlockPrivateEventRouter::DispatchEvent( 96 const std::string& event_name, 97 base::Value* arg) { 98 scoped_ptr<base::ListValue> args(new base::ListValue()); 99 args->Append(arg); 100 scoped_ptr<extensions::Event> event(new extensions::Event( 101 event_name, args.Pass())); 102 extensions::ExtensionSystem::Get(profile_)->event_router()-> 103 BroadcastEvent(event.Pass()); 104} 105 106static base::LazyInstance<extensions::ProfileKeyedAPIFactory< 107 ScreenlockPrivateEventRouter> > 108 g_factory = LAZY_INSTANCE_INITIALIZER; 109 110// static 111extensions::ProfileKeyedAPIFactory<ScreenlockPrivateEventRouter>* 112ScreenlockPrivateEventRouter::GetFactoryInstance() { 113 return &g_factory.Get(); 114} 115 116void ScreenlockPrivateEventRouter::Shutdown() { 117 chromeos::SessionManagerClient* session_manager = 118 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); 119 if (session_manager->HasObserver(this)) 120 session_manager->RemoveObserver(this); 121} 122 123} // namespace extensions 124