Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2012 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_source.h"
      6 
      7 #include "base/memory/ref_counted_memory.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/resources_util.h"
     12 #include "chrome/browser/search/instant_io_context.h"
     13 #include "chrome/browser/themes/theme_properties.h"
     14 #include "chrome/browser/themes/theme_service.h"
     15 #include "chrome/browser/themes/theme_service_factory.h"
     16 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
     17 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
     18 #include "chrome/common/url_constants.h"
     19 #include "content/public/browser/browser_thread.h"
     20 #include "net/url_request/url_request.h"
     21 #include "ui/base/layout.h"
     22 #include "ui/base/resource/resource_bundle.h"
     23 #include "ui/base/webui/web_ui_util.h"
     24 #include "url/gurl.h"
     25 
     26 using content::BrowserThread;
     27 
     28 namespace {
     29 
     30 std::string GetThemePath() {
     31   return std::string(content::kChromeUIScheme) + "://" +
     32          std::string(chrome::kChromeUIThemePath) + "/";
     33 }
     34 
     35 // use a resource map rather than hard-coded strings.
     36 static const char* kNewTabCSSPath = "css/new_tab_theme.css";
     37 static const char* kNewIncognitoTabCSSPath = "css/incognito_new_tab_theme.css";
     38 
     39 }  // namespace
     40 
     41 ////////////////////////////////////////////////////////////////////////////////
     42 // ThemeSource, public:
     43 
     44 ThemeSource::ThemeSource(Profile* profile)
     45     : profile_(profile->GetOriginalProfile()) {
     46   NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType(
     47       profile_, NULL);
     48   css_bytes_ =
     49       NTPResourceCacheFactory::GetForProfile(profile)->GetNewTabCSS(win_type);
     50 }
     51 
     52 ThemeSource::~ThemeSource() {
     53 }
     54 
     55 std::string ThemeSource::GetSource() const {
     56   return chrome::kChromeUIThemePath;
     57 }
     58 
     59 void ThemeSource::StartDataRequest(
     60     const std::string& path,
     61     int render_process_id,
     62     int render_frame_id,
     63     const content::URLDataSource::GotDataCallback& callback) {
     64   // Default scale factor if not specified.
     65   float scale_factor = 1.0f;
     66   std::string uncached_path;
     67   webui::ParsePathAndScale(GURL(GetThemePath() + path),
     68                            &uncached_path,
     69                            &scale_factor);
     70 
     71   if (uncached_path == kNewTabCSSPath ||
     72       uncached_path == kNewIncognitoTabCSSPath) {
     73     DCHECK_CURRENTLY_ON(BrowserThread::IO);
     74 
     75     callback.Run(css_bytes_.get());
     76     return;
     77   }
     78 
     79 
     80   int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
     81   if (resource_id != -1) {
     82     SendThemeBitmap(callback, resource_id, scale_factor);
     83     return;
     84   }
     85 
     86   // We don't have any data to send back.
     87   callback.Run(NULL);
     88 }
     89 
     90 std::string ThemeSource::GetMimeType(const std::string& path) const {
     91   std::string uncached_path;
     92   webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
     93 
     94   if (uncached_path == kNewTabCSSPath ||
     95       uncached_path == kNewIncognitoTabCSSPath) {
     96     return "text/css";
     97   }
     98 
     99   return "image/png";
    100 }
    101 
    102 base::MessageLoop* ThemeSource::MessageLoopForRequestPath(
    103     const std::string& path) const {
    104   std::string uncached_path;
    105   webui::ParsePathAndScale(GURL(GetThemePath() + path), &uncached_path, NULL);
    106 
    107   if (uncached_path == kNewTabCSSPath ||
    108       uncached_path == kNewIncognitoTabCSSPath) {
    109     // We generated and cached this when we initialized the object.  We don't
    110     // have to go back to the UI thread to send the data.
    111     return NULL;
    112   }
    113 
    114   // If it's not a themeable image, we don't need to go to the UI thread.
    115   int resource_id = ResourcesUtil::GetThemeResourceId(uncached_path);
    116   if (!ThemeProperties::IsThemeableImage(resource_id))
    117     return NULL;
    118 
    119   return content::URLDataSource::MessageLoopForRequestPath(path);
    120 }
    121 
    122 bool ThemeSource::ShouldReplaceExistingSource() const {
    123   // We currently get the css_bytes_ in the ThemeSource constructor, so we need
    124   // to recreate the source itself when a theme changes.
    125   return true;
    126 }
    127 
    128 bool ThemeSource::ShouldServiceRequest(const net::URLRequest* request) const {
    129   if (request->url().SchemeIs(chrome::kChromeSearchScheme))
    130     return InstantIOContext::ShouldServiceRequest(request);
    131   return URLDataSource::ShouldServiceRequest(request);
    132 }
    133 
    134 ////////////////////////////////////////////////////////////////////////////////
    135 // ThemeSource, private:
    136 
    137 void ThemeSource::SendThemeBitmap(
    138     const content::URLDataSource::GotDataCallback& callback,
    139     int resource_id,
    140     float scale_factor) {
    141   ui::ScaleFactor resource_scale_factor =
    142       ui::GetSupportedScaleFactor(scale_factor);
    143   if (ThemeProperties::IsThemeableImage(resource_id)) {
    144     DCHECK_CURRENTLY_ON(BrowserThread::UI);
    145     ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
    146     DCHECK(tp);
    147 
    148     scoped_refptr<base::RefCountedMemory> image_data(
    149         tp->GetRawData(resource_id, resource_scale_factor));
    150     callback.Run(image_data.get());
    151   } else {
    152     DCHECK_CURRENTLY_ON(BrowserThread::IO);
    153     const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
    154     callback.Run(
    155         rb.LoadDataResourceBytesForScale(resource_id, resource_scale_factor));
    156   }
    157 }
    158