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::string css
= webui::GetWebUiCssTextDefaults();
59 bytes
= base::RefCountedString::TakeString(&css
);
61 bytes
= web_client
->GetDataResourceBytes(idr
);
64 callback
.Run(bytes
.get());
67 std::string
SharedResourcesDataSourceIOS::GetMimeType(
68 const std::string
& path
) const {
69 std::string mime_type
;
70 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path
), &mime_type
);