1 // Copyright (c) 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/ui/webui/theme_handler.h"
6
7 #include "base/values.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/themes/theme_service.h"
11 #include "chrome/browser/themes/theme_service_factory.h"
12 #include "chrome/browser/ui/webui/theme_source.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/web_ui.h"
15 #include "grit/theme_resources.h"
16
17 ///////////////////////////////////////////////////////////////////////////////
18 // ThemeHandler
19
ThemeHandler()20 ThemeHandler::ThemeHandler() {
21 }
22
~ThemeHandler()23 ThemeHandler::~ThemeHandler() {
24 }
25
RegisterMessages()26 void ThemeHandler::RegisterMessages() {
27 // These are not actual message registrations, but can't be done in the
28 // constructor since they need the web_ui value to be set, which is done
29 // post-construction, but before registering messages.
30 InitializeCSSCaches();
31 // Listen for theme installation.
32 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
33 content::Source<ThemeService>(
34 ThemeServiceFactory::GetForProfile(GetProfile())));
35 }
36
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)37 void ThemeHandler::Observe(int type,
38 const content::NotificationSource& source,
39 const content::NotificationDetails& details) {
40 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
41 InitializeCSSCaches();
42 base::FundamentalValue attribution(
43 ThemeServiceFactory::GetForProfile(GetProfile())->HasCustomImage(
44 IDR_THEME_NTP_ATTRIBUTION));
45 web_ui()->CallJavascriptFunction("ntp.themeChanged", attribution);
46 }
47
InitializeCSSCaches()48 void ThemeHandler::InitializeCSSCaches() {
49 Profile* profile = GetProfile();
50 ThemeSource* theme = new ThemeSource(profile);
51 content::URLDataSource::Add(profile, theme);
52 }
53
GetProfile() const54 Profile* ThemeHandler::GetProfile() const {
55 return Profile::FromWebUI(web_ui());
56 }
57