• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/strings/string_util.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "content/public/common/content_client.h"
12 #include "content/public/common/url_constants.h"
13 #include "grit/webui_resources_map.h"
14 #include "net/base/mime_util.h"
15 
16 namespace {
17 
18 const char kAppImagesPath[] = "images/apps/";
19 const char kAppImagesPath2x[] = "images/2x/apps/";
20 
21 const char kReplacement[] = "../../resources/default_100_percent/common/";
22 const char kReplacement2x[] = "../../resources/default_200_percent/common/";
23 
24 // This entire method is a hack introduced to be able to handle apps images
25 // that exist in the ui/resources directory. From JS/CSS, we still load the
26 // image as if it were chrome://resources/images/apps/myappimage.png, if that
27 // path doesn't exist, we check to see if it that image exists in the relative
28 // path to ui/resources instead.
29 // TODO(rkc): Once we have a separate source for apps, remove this code.
AppsRelativePathMatch(const std::string & path,const std::string & compareto)30 bool AppsRelativePathMatch(const std::string& path,
31                            const std::string& compareto) {
32   if (StartsWithASCII(path, kAppImagesPath, false)) {
33     if (compareto ==
34         (kReplacement + path.substr(arraysize(kAppImagesPath) - 1)))
35       return true;
36   } else if (StartsWithASCII(path, kAppImagesPath2x, false)) {
37     if (compareto ==
38         (kReplacement2x + path.substr(arraysize(kAppImagesPath2x) - 1)))
39       return true;
40   }
41   return false;
42 }
43 
PathToIDR(const std::string & path)44 int PathToIDR(const std::string& path) {
45   int idr = -1;
46   for (size_t i = 0; i < kWebuiResourcesSize; ++i) {
47     if ((path == kWebuiResources[i].name) ||
48         AppsRelativePathMatch(path, kWebuiResources[i].name)) {
49       idr = kWebuiResources[i].value;
50       break;
51     }
52   }
53 
54   return idr;
55 }
56 
57 }  // namespace
58 
SharedResourcesDataSource()59 SharedResourcesDataSource::SharedResourcesDataSource() {
60 }
61 
~SharedResourcesDataSource()62 SharedResourcesDataSource::~SharedResourcesDataSource() {
63 }
64 
GetSource() const65 std::string SharedResourcesDataSource::GetSource() const {
66   return content::kChromeUIResourcesHost;
67 }
68 
StartDataRequest(const std::string & path,int render_process_id,int render_view_id,const content::URLDataSource::GotDataCallback & callback)69 void SharedResourcesDataSource::StartDataRequest(
70     const std::string& path,
71     int render_process_id,
72     int render_view_id,
73     const content::URLDataSource::GotDataCallback& callback) {
74   int idr = PathToIDR(path);
75   DCHECK_NE(-1, idr) << " path: " << path;
76   scoped_refptr<base::RefCountedStaticMemory> bytes(
77       content::GetContentClient()->GetDataResourceBytes(idr));
78 
79   callback.Run(bytes.get());
80 }
81 
GetMimeType(const std::string & path) const82 std::string SharedResourcesDataSource::GetMimeType(
83     const std::string& path) const {
84   // Requests should not block on the disk!  On POSIX this goes to disk.
85   // http://code.google.com/p/chromium/issues/detail?id=59849
86 
87   base::ThreadRestrictions::ScopedAllowIO allow_io;
88   std::string mime_type;
89   net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type);
90   return mime_type;
91 }
92