Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / webui / shared_resources_data_source.cc
blobaa1dc390100bc63a91f1af346013498c2dc9f85b
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"
16 namespace {
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)) {
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;
41 return false;
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;
54 return idr;
57 } // namespace
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,
72 int render_frame_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);
90 return mime_type;