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.
5 #include "content/browser/webui/shared_resources_data_source.h"
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"
18 const char kAppImagesPath
[] = "images/apps/";
19 const char kAppImagesPath2x
[] = "images/2x/apps/";
21 const char kReplacement
[] = "../../resources/default_100_percent/common/";
22 const char kReplacement2x
[] = "../../resources/default_200_percent/common/";
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.
30 bool AppsRelativePathMatch(const std::string
& path
,
31 const std::string
& compareto
) {
32 if (StartsWithASCII(path
, kAppImagesPath
, false)) {
34 (kReplacement
+ path
.substr(arraysize(kAppImagesPath
) - 1)))
36 } else if (StartsWithASCII(path
, kAppImagesPath2x
, false)) {
38 (kReplacement2x
+ path
.substr(arraysize(kAppImagesPath2x
) - 1)))
44 int PathToIDR(const std::string
& path
) {
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
;
59 SharedResourcesDataSource::SharedResourcesDataSource() {
62 SharedResourcesDataSource::~SharedResourcesDataSource() {
65 std::string
SharedResourcesDataSource::GetSource() const {
66 return content::kChromeUIResourcesHost
;
69 void SharedResourcesDataSource::StartDataRequest(
70 const std::string
& path
,
71 int render_process_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
));
79 callback
.Run(bytes
.get());
82 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
87 base::ThreadRestrictions::ScopedAllowIO allow_io
;
88 std::string mime_type
;
89 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path
), &mime_type
);