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 "content/browser/webui/shared_resources_data_source.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/memory/ref_counted_memory.h"
      9 #include "base/threading/thread_restrictions.h"
     10 #include "content/public/common/content_client.h"
     11 #include "content/public/common/url_constants.h"
     12 #include "grit/webui_resources_map.h"
     13 #include "net/base/mime_util.h"
     14 
     15 namespace {
     16 
     17 int PathToIDR(const std::string& path) {
     18   int idr = -1;
     19   for (size_t i = 0; i < kWebuiResourcesSize; ++i) {
     20     if (kWebuiResources[i].name == path) {
     21       idr = kWebuiResources[i].value;
     22       break;
     23     }
     24   }
     25 
     26   return idr;
     27 }
     28 
     29 }  // namespace
     30 
     31 SharedResourcesDataSource::SharedResourcesDataSource() {
     32 }
     33 
     34 SharedResourcesDataSource::~SharedResourcesDataSource() {
     35 }
     36 
     37 std::string SharedResourcesDataSource::GetSource() const {
     38   return content::kChromeUIResourcesHost;
     39 }
     40 
     41 void SharedResourcesDataSource::StartDataRequest(
     42     const std::string& path,
     43     int render_process_id,
     44     int render_view_id,
     45     const content::URLDataSource::GotDataCallback& callback) {
     46   int idr = PathToIDR(path);
     47   DCHECK_NE(-1, idr) << " path: " << path;
     48   scoped_refptr<base::RefCountedStaticMemory> bytes(
     49       content::GetContentClient()->GetDataResourceBytes(idr));
     50 
     51   callback.Run(bytes.get());
     52 }
     53 
     54 std::string SharedResourcesDataSource::GetMimeType(
     55     const std::string& path) const {
     56   // Requests should not block on the disk!  On POSIX this goes to disk.
     57   // http://code.google.com/p/chromium/issues/detail?id=59849
     58 
     59   base::ThreadRestrictions::ScopedAllowIO allow_io;
     60   std::string mime_type;
     61   net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type);
     62   return mime_type;
     63 }
     64