Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2011 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/shared_resources_data_source.h"
      6 
      7 #include <string>
      8 
      9 #include "base/memory/singleton.h"
     10 #include "base/threading/thread_restrictions.h"
     11 #include "chrome/browser/io_thread.h"
     12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
     13 #include "chrome/common/url_constants.h"
     14 #include "content/browser/browser_thread.h"
     15 #include "grit/app_resources.h"
     16 #include "grit/generated_resources.h"
     17 #include "grit/shared_resources.h"
     18 #include "grit/shared_resources_map.h"
     19 #include "grit/theme_resources.h"
     20 #include "net/base/mime_util.h"
     21 #include "ui/base/resource/resource_bundle.h"
     22 
     23 namespace {
     24 
     25 int PathToIDR(const std::string& path) {
     26   int idr = -1;
     27   if (path == "app/resources/folder_closed.png") {
     28     idr = IDR_FOLDER_CLOSED;
     29   } else if (path == "app/resources/folder_closed_rtl.png") {
     30     idr = IDR_FOLDER_CLOSED_RTL;
     31   } else if (path == "app/resources/folder_open.png") {
     32     idr = IDR_FOLDER_OPEN;
     33   } else if (path == "app/resources/folder_open_rtl.png") {
     34     idr = IDR_FOLDER_OPEN_RTL;
     35   } else if (path == "app/resources/throbber.png") {
     36     idr = IDR_THROBBER;
     37   } else {
     38     // The name of the files in the grd list are prefixed with the following
     39     // directory:
     40     std::string key("shared/");
     41     key += path;
     42 
     43     for (size_t i = 0; i < kSharedResourcesSize; ++i) {
     44       if (kSharedResources[i].name == key) {
     45         idr = kSharedResources[i].value;
     46         break;
     47       }
     48     }
     49   }
     50 
     51   return idr;
     52 }
     53 
     54 }  // namespace
     55 
     56 SharedResourcesDataSource::SharedResourcesDataSource()
     57     : DataSource(chrome::kChromeUIResourcesHost, NULL) {
     58 }
     59 
     60 SharedResourcesDataSource::~SharedResourcesDataSource() {
     61 }
     62 
     63 void SharedResourcesDataSource::StartDataRequest(const std::string& path,
     64                                                  bool is_incognito,
     65                                                  int request_id) {
     66   int idr = PathToIDR(path);
     67   DCHECK_NE(-1, idr) << " path: " << path;
     68   const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
     69   scoped_refptr<RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes(idr));
     70   SendResponse(request_id, bytes);
     71 }
     72 
     73 std::string SharedResourcesDataSource::GetMimeType(
     74     const std::string& path) const {
     75   // Requests should not block on the disk!  On Windows this goes to the
     76   // registry.
     77   //   http://code.google.com/p/chromium/issues/detail?id=59849
     78   base::ThreadRestrictions::ScopedAllowIO allow_io;
     79 
     80   std::string mime_type;
     81   net::GetMimeTypeFromFile(FilePath().AppendASCII(path), &mime_type);
     82   return mime_type;
     83 }
     84