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
20ThemeHandler::ThemeHandler() {
21}
22
23ThemeHandler::~ThemeHandler() {
24}
25
26void 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
37void 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
48void ThemeHandler::InitializeCSSCaches() {
49  Profile* profile = GetProfile();
50  ThemeSource* theme = new ThemeSource(profile);
51  content::URLDataSource::Add(profile, theme);
52}
53
54Profile* ThemeHandler::GetProfile() const {
55  return Profile::FromWebUI(web_ui());
56}
57