| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/ui/webui/settings/appearance_handler.h" | 5 #include "chrome/browser/ui/webui/settings/appearance_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
| 13 #include "chrome/browser/themes/theme_service_factory.h" | 13 #include "chrome/browser/themes/theme_service_factory.h" |
| 14 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
| 15 #include "content/public/browser/web_ui.h" | 15 #include "content/public/browser/web_ui.h" |
| 16 | 16 |
| 17 #if defined(OS_CHROMEOS) | 17 #if defined(OS_CHROMEOS) |
| 18 #include "ash/desktop_background/user_wallpaper_delegate.h" | 18 #include "ash/desktop_background/user_wallpaper_delegate.h" |
| 19 #include "ash/shell.h" | 19 #include "ash/shell.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 namespace settings { | 22 namespace settings { |
| 23 | 23 |
| 24 AppearanceHandler::AppearanceHandler(content::WebUI* webui) | 24 AppearanceHandler::AppearanceHandler(content::WebUI* webui) |
| 25 : profile_(Profile::FromWebUI(webui)) { | 25 : profile_(Profile::FromWebUI(webui)) { |
| 26 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | |
| 27 content::Source<ThemeService>( | |
| 28 ThemeServiceFactory::GetForProfile(profile_))); | |
| 29 } | 26 } |
| 30 | 27 |
| 31 AppearanceHandler::~AppearanceHandler() { | 28 AppearanceHandler::~AppearanceHandler() {} |
| 32 registrar_.RemoveAll(); | |
| 33 } | |
| 34 | 29 |
| 35 void AppearanceHandler::RegisterMessages() { | 30 void AppearanceHandler::RegisterMessages() { |
| 36 web_ui()->RegisterMessageCallback( | 31 web_ui()->RegisterMessageCallback( |
| 37 "resetTheme", | 32 "resetTheme", |
| 38 base::Bind(&AppearanceHandler::HandleResetTheme, base::Unretained(this))); | 33 base::Bind(&AppearanceHandler::HandleResetTheme, base::Unretained(this))); |
| 39 web_ui()->RegisterMessageCallback( | 34 web_ui()->RegisterMessageCallback( |
| 40 "getResetThemeEnabled", | 35 "getResetThemeEnabled", |
| 41 base::Bind(&AppearanceHandler::HandleGetResetThemeEnabled, | 36 base::Bind(&AppearanceHandler::HandleGetResetThemeEnabled, |
| 42 base::Unretained(this))); | 37 base::Unretained(this))); |
| 43 #if defined(OS_CHROMEOS) | 38 #if defined(OS_CHROMEOS) |
| 44 web_ui()->RegisterMessageCallback( | 39 web_ui()->RegisterMessageCallback( |
| 45 "openWallpaperManager", | 40 "openWallpaperManager", |
| 46 base::Bind(&AppearanceHandler::HandleOpenWallpaperManager, | 41 base::Bind(&AppearanceHandler::HandleOpenWallpaperManager, |
| 47 base::Unretained(this))); | 42 base::Unretained(this))); |
| 48 #endif | 43 #endif |
| 49 } | 44 } |
| 50 | 45 |
| 46 void AppearanceHandler::OnJavascriptAllowed() { |
| 47 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 48 content::Source<ThemeService>( |
| 49 ThemeServiceFactory::GetForProfile(profile_))); |
| 50 } |
| 51 |
| 52 void AppearanceHandler::OnJavascriptDisallowed() { |
| 53 registrar_.RemoveAll(); |
| 54 } |
| 55 |
| 51 void AppearanceHandler::Observe( | 56 void AppearanceHandler::Observe( |
| 52 int type, | 57 int type, |
| 53 const content::NotificationSource& source, | 58 const content::NotificationSource& source, |
| 54 const content::NotificationDetails& details) { | 59 const content::NotificationDetails& details) { |
| 55 switch (type) { | 60 switch (type) { |
| 56 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { | 61 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { |
| 57 web_ui()->CallJavascriptFunction( | 62 CallJavascriptFunction("cr.webUIListenerCallback", |
| 58 "cr.webUIListenerCallback", | 63 base::StringValue("reset-theme-enabled-changed"), |
| 59 base::StringValue("reset-theme-enabled-changed"), | 64 base::FundamentalValue(ResetThemeEnabled())); |
| 60 base::FundamentalValue(ResetThemeEnabled())); | |
| 61 break; | 65 break; |
| 62 } | 66 } |
| 63 default: | 67 default: |
| 64 NOTREACHED(); | 68 NOTREACHED(); |
| 65 } | 69 } |
| 66 } | 70 } |
| 67 | 71 |
| 68 void AppearanceHandler::HandleResetTheme(const base::ListValue* /*args*/) { | 72 void AppearanceHandler::HandleResetTheme(const base::ListValue* /*args*/) { |
| 69 ThemeServiceFactory::GetForProfile(profile_)->UseDefaultTheme(); | 73 ThemeServiceFactory::GetForProfile(profile_)->UseDefaultTheme(); |
| 70 } | 74 } |
| 71 | 75 |
| 72 bool AppearanceHandler::ResetThemeEnabled() const { | 76 bool AppearanceHandler::ResetThemeEnabled() const { |
| 73 // TODO(jhawkins): Handle native/system theme button. | 77 // TODO(jhawkins): Handle native/system theme button. |
| 74 return !ThemeServiceFactory::GetForProfile(profile_)->UsingDefaultTheme(); | 78 return !ThemeServiceFactory::GetForProfile(profile_)->UsingDefaultTheme(); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void AppearanceHandler::HandleGetResetThemeEnabled( | 81 void AppearanceHandler::HandleGetResetThemeEnabled( |
| 78 const base::ListValue* args) { | 82 const base::ListValue* args) { |
| 83 AllowJavascript(); |
| 84 |
| 79 CHECK_EQ(1U, args->GetSize()); | 85 CHECK_EQ(1U, args->GetSize()); |
| 80 const base::Value* callback_id; | 86 const base::Value* callback_id; |
| 81 CHECK(args->Get(0, &callback_id)); | 87 CHECK(args->Get(0, &callback_id)); |
| 82 ResolveJavascriptCallback(*callback_id, | 88 ResolveJavascriptCallback(*callback_id, |
| 83 base::FundamentalValue(ResetThemeEnabled())); | 89 base::FundamentalValue(ResetThemeEnabled())); |
| 84 } | 90 } |
| 85 | 91 |
| 86 #if defined(OS_CHROMEOS) | 92 #if defined(OS_CHROMEOS) |
| 87 void AppearanceHandler::HandleOpenWallpaperManager( | 93 void AppearanceHandler::HandleOpenWallpaperManager( |
| 88 const base::ListValue* /*args*/) { | 94 const base::ListValue* /*args*/) { |
| 89 ash::Shell::GetInstance()->user_wallpaper_delegate()->OpenSetWallpaperPage(); | 95 ash::Shell::GetInstance()->user_wallpaper_delegate()->OpenSetWallpaperPage(); |
| 90 } | 96 } |
| 91 #endif | 97 #endif |
| 92 | 98 |
| 93 } // namespace settings | 99 } // namespace settings |
| OLD | NEW |