Chromium Code Reviews| Index: chrome/browser/chromeos/accessibility/magnification_manager.cc |
| diff --git a/chrome/browser/chromeos/accessibility/magnification_manager.cc b/chrome/browser/chromeos/accessibility/magnification_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dae4335bcbd908f5f9d36697a0b7b1b15e66874a |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/accessibility/magnification_manager.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/accessibility/magnification_manager.h" |
| + |
| +#include "ash/magnifier/magnification_controller.h" |
| +#include "ash/shell.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/singleton.h" |
| +#include "chrome/browser/api/prefs/pref_change_registrar.h" |
| +#include "chrome/browser/api/prefs/pref_member.h" |
| +#include "chrome/browser/chromeos/login/user_manager.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.h" |
| +#include "content/public/browser/notification_service.h" |
| + |
| +namespace chromeos { |
| + |
| +class MagnificationManagerImpl : public MagnificationManager, |
| + public content::NotificationObserver { |
| + public: |
| + MagnificationManagerImpl() { |
| + DCHECK(!instance_); |
| + instance_ = this; |
| + |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_SESSION_STARTED, |
| + content::NotificationService::AllSources()); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_PROFILE_CREATED, |
| + content::NotificationService::AllSources()); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
| + content::NotificationService::AllSources()); |
| + |
| + Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| + SetProfile(profile); |
| + } |
| + virtual ~MagnificationManagerImpl() {} |
| + |
| + static MagnificationManagerImpl* GetInstance() { |
| + return instance_; |
| + } |
| + |
| + // MagnificationManager implimentation: |
| + bool IsEnabled() OVERRIDE { |
| +#if defined(USE_ASH) |
| + return ash::Shell::GetInstance()->magnification_controller()->IsEnabled(); |
| +#else |
| + return false; |
| +#endif |
| + } |
| + |
| + void SetEnabled(bool enabled) OVERRIDE { |
| +#if defined(USE_ASH) |
|
DaveMoore
2012/10/11 20:09:27
Should there be a check here that it's not called
yoshiki
2012/10/12 15:00:55
Done.
|
| + ash::Shell::GetInstance()->magnification_controller()->SetEnabled(enabled); |
| +#endif |
| + } |
| + |
| + private: |
| + void SetProfile(Profile* profile) { |
| + if (pref_change_registrar_) { |
| + pref_change_registrar_->Remove(prefs::kScreenMagnifierEnabled, this); |
| + pref_change_registrar_.reset(); |
| + } |
| + |
| + if (profile) { |
| + pref_change_registrar_.reset(new PrefChangeRegistrar); |
| + pref_change_registrar_->Init(profile->GetPrefs()); |
| + pref_change_registrar_->Add(prefs::kScreenMagnifierEnabled, this); |
| + } |
| + |
| + profile_ = profile; |
| + UpdateMagnifierStatus(); |
| + } |
| + |
| + void UpdateMagnifierStatus() { |
| + UserManager* manager = UserManager::Get(); |
| + if (!profile_) { |
| + SetEnabled(false); |
| + } else if (manager && !manager->IsSessionStarted()) { |
| + SetEnabled(true); |
| + } else { |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + bool enabled = pref_service->GetBoolean(prefs::kScreenMagnifierEnabled); |
| + SetEnabled(enabled); |
| + } |
| + } |
| + |
| + // content::NotificationObserver implimentation: |
| + virtual void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) OVERRIDE { |
| + switch (type) { |
| + case chrome::NOTIFICATION_PREF_CHANGED: { |
| + std::string pref = *content::Details<std::string>(details).ptr(); |
| + if (pref == prefs::kScreenMagnifierEnabled) |
| + UpdateMagnifierStatus(); |
| + break; |
| + } |
| + case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: |
| + case chrome::NOTIFICATION_SESSION_STARTED: { |
| + Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| + SetProfile(profile); |
| + break; |
| + } |
| + } |
| + } |
| + |
| + static MagnificationManagerImpl* instance_; |
| + |
| + Profile* profile_; |
| + content::NotificationRegistrar registrar_; |
| + scoped_ptr<PrefChangeRegistrar> pref_change_registrar_; |
| + |
| + friend struct DefaultSingletonTraits<MagnificationManagerImpl>; |
| + DISALLOW_COPY_AND_ASSIGN(MagnificationManagerImpl); |
| +}; |
| + |
| +MagnificationManagerImpl* MagnificationManagerImpl::instance_ = NULL; |
| + |
| +MagnificationManager* MagnificationManager::GetInstance() { |
| + return MagnificationManagerImpl::GetInstance(); |
| +} |
| + |
| +MagnificationManager* MagnificationManager::CreateInstance() { |
|
DaveMoore
2012/10/11 20:09:27
Please add a check that this isn't called more tha
yoshiki
2012/10/12 15:00:55
Done.
|
| + return new MagnificationManagerImpl(); |
| +} |
| + |
| +} // namespace chromeos |