1 // Copyright 2014 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 "ios/web/webui/shared_resources_data_source_ios.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/strings/string_util.h"
10 #include "grit/webui_resources_map.h"
11 #include "ios/web/public/web_client.h"
12 #include "net/base/mime_util.h"
13 #include "ui/base/webui/web_ui_util.h"
14 #include "ui/resources/grit/webui_resources.h"
20 // Value duplicated from content/public/common/url_constants.h
21 // TODO(stuartmorgan): Revisit how to share this in a more maintainable way.
22 const char kWebUIResourcesHost
[] = "resources";
24 int PathToIDR(const std::string
& path
) {
26 for (size_t i
= 0; i
< kWebuiResourcesSize
; ++i
) {
27 if (path
== kWebuiResources
[i
].name
) {
28 idr
= kWebuiResources
[i
].value
;
38 SharedResourcesDataSourceIOS::SharedResourcesDataSourceIOS() {
41 SharedResourcesDataSourceIOS::~SharedResourcesDataSourceIOS() {
44 std::string
SharedResourcesDataSourceIOS::GetSource() const {
45 return kWebUIResourcesHost
;
48 void SharedResourcesDataSourceIOS::StartDataRequest(
49 const std::string
& path
,
50 const URLDataSourceIOS::GotDataCallback
& callback
) {
51 int idr
= PathToIDR(path
);
52 DCHECK_NE(-1, idr
) << " path: " << path
;
53 scoped_refptr
<base::RefCountedMemory
> bytes
;
55 WebClient
* web_client
= GetWebClient();
57 if (idr
== IDR_WEBUI_CSS_TEXT_DEFAULTS
) {
58 std::vector
<std::string
> placeholders
;
59 placeholders
.push_back(webui::GetTextDirection()); // $1
60 placeholders
.push_back(webui::GetFontFamily()); // $2
61 placeholders
.push_back(webui::GetFontSize()); // $3
63 const std::string
& chrome_shared
=
64 web_client
->GetDataResource(idr
, ui::SCALE_FACTOR_NONE
).as_string();
65 std::string replaced
=
66 base::ReplaceStringPlaceholders(chrome_shared
, placeholders
, nullptr);
67 bytes
= base::RefCountedString::TakeString(&replaced
);
69 bytes
= web_client
->GetDataResourceBytes(idr
);
72 callback
.Run(bytes
.get());
75 std::string
SharedResourcesDataSourceIOS::GetMimeType(
76 const std::string
& path
) const {
77 std::string mime_type
;
78 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path
), &mime_type
);