OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/browser/resource_protocol.h" | |
6 | |
7 #include "base/format_macros.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "blink/public/resources/grit/blink_resources.h" | |
10 #include "content/public/common/content_client.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "net/http/http_request_headers.h" | |
13 #include "net/http/http_response_headers.h" | |
14 #include "net/http/http_response_info.h" | |
15 #include "net/url_request/url_request.h" | |
16 #include "net/url_request/url_request_error_job.h" | |
17 #include "net/url_request/url_request_job.h" | |
18 #include "net/url_request/url_request_job_factory.h" | |
19 #include "net/url_request/url_request_simple_job.h" | |
20 #include "ui/base/layout.h" | |
21 | |
22 namespace content { | |
23 namespace { | |
24 | |
25 struct BuiltinWebAccessibleResource { | |
26 const char* path; | |
27 int resource_id; | |
28 ui::ScaleFactor scale_factor; | |
29 const char* content_type; | |
30 }; | |
31 | |
32 static const char kBlinkHostname[] = "blink"; | |
Tom Sepez
2014/11/17 18:09:11
nit: static redundant if we're in empty namespace
jbroman
2014/11/17 18:35:16
Done.
| |
33 static const BuiltinWebAccessibleResource kBlinkWebAccessibleResources[] = { | |
34 // Warning: Resources in this list are exposed to all web content. | |
35 // Carefully consider whether the resource could be used for XSS before | |
36 // including it. | |
37 // TODO(jbroman): Put actual resources in here. | |
38 // A dummy entry is here because MSVC rejects zero-sized constant arrays. | |
39 {"", | |
40 0, | |
41 ui::SCALE_FACTOR_NONE, | |
42 ""}, | |
43 }; | |
44 | |
45 class URLRequestResourceJob : public net::URLRequestSimpleJob { | |
46 public: | |
47 using WebAccessibleResource = ResourceProtocolHandler::WebAccessibleResource; | |
48 | |
49 URLRequestResourceJob(net::URLRequest* request, | |
50 net::NetworkDelegate* network_delegate, | |
51 const WebAccessibleResource& resource_info, | |
52 ContentClient* content_client) | |
53 : net::URLRequestSimpleJob(request, network_delegate), | |
54 resource_info_(resource_info), | |
55 content_client_(content_client) { | |
56 std::string raw_headers("HTTP/1.1 200 OK"); | |
57 raw_headers.append(2, '\0'); | |
58 response_headers_ = new net::HttpResponseHeaders(raw_headers); | |
59 } | |
60 | |
61 // Overridden from net::URLRequestSimpleJob: | |
62 int GetData(std::string* mime_type, | |
63 std::string* charset, | |
64 std::string* data, | |
65 const net::CompletionCallback& callback) const override { | |
66 *data = GetContentClient() | |
Tom Sepez
2014/11/17 18:09:11
nit: -> on previous line, indentation dubious.
jbroman
2014/11/17 18:35:16
This is what clang-format (with Chromium style) do
| |
67 ->GetDataResource(resource_info_.resource_id, | |
68 resource_info_.scale_factor) | |
69 .as_string(); | |
70 | |
71 response_headers_->AddHeader(base::StringPrintf( | |
72 "%s: %" PRIuS, net::HttpRequestHeaders::kContentLength, data->size())); | |
73 | |
74 response_headers_->AddHeader( | |
75 base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentType, | |
76 resource_info_.content_type.c_str())); | |
77 response_headers_->GetMimeTypeAndCharset(mime_type, charset); | |
78 | |
79 return net::OK; | |
80 } | |
81 | |
82 void GetResponseInfo(net::HttpResponseInfo* info) override { | |
83 info->headers = response_headers_; | |
84 } | |
85 | |
86 private: | |
87 ~URLRequestResourceJob() override {} | |
88 | |
89 ContentClient* GetContentClient() const { | |
90 return content_client_ ? content_client_ : content::GetContentClient(); | |
91 } | |
92 | |
93 WebAccessibleResource resource_info_; | |
94 scoped_refptr<net::HttpResponseHeaders> response_headers_; | |
95 ContentClient* content_client_; | |
96 }; | |
97 | |
98 } // namespace | |
99 | |
100 ResourceProtocolHandler::ResourceProtocolHandler() | |
101 : ResourceProtocolHandler(nullptr) { | |
102 } | |
103 | |
104 ResourceProtocolHandler::ResourceProtocolHandler(ContentClient* content_client) | |
105 : content_client_(content_client) { | |
106 std::string hostname = kBlinkHostname; | |
107 for (const auto& resource : kBlinkWebAccessibleResources) { | |
108 RegisterResource(hostname, resource.path, resource.resource_id, | |
109 resource.scale_factor, resource.content_type); | |
110 } | |
111 } | |
112 | |
113 ResourceProtocolHandler::~ResourceProtocolHandler() { | |
114 } | |
115 | |
116 void ResourceProtocolHandler::RegisterResource( | |
117 const std::string& host, | |
118 const std::string& path, | |
119 int resource_id, | |
120 ui::ScaleFactor scale_factor, | |
121 const std::string& content_type) { | |
122 WebAccessibleResource& resource_info = resources_[std::make_pair(host, path)]; | |
Tom Sepez
2014/11/17 18:09:11
Do we care about detecting duplicates? Probably f
jbroman
2014/11/17 18:35:16
Not sure. One could conceive of embedders wanting
| |
123 resource_info.resource_id = resource_id; | |
124 resource_info.scale_factor = scale_factor; | |
125 resource_info.content_type = content_type; | |
126 } | |
127 | |
128 net::URLRequestJob* ResourceProtocolHandler::MaybeCreateJob( | |
129 net::URLRequest* request, | |
130 net::NetworkDelegate* network_delegate) const { | |
131 DCHECK(request); | |
132 DCHECK(request->url().is_valid()); | |
133 | |
134 const auto& url = request->url(); | |
135 auto it = resources_.find(std::make_pair(url.host(), url.path())); | |
136 if (it != resources_.end()) { | |
137 return new URLRequestResourceJob(request, network_delegate, it->second, | |
138 content_client_); | |
139 } | |
140 | |
141 return new net::URLRequestErrorJob(request, network_delegate, | |
142 net::ERR_FILE_NOT_FOUND); | |
143 } | |
144 | |
145 bool ResourceProtocolHandler::IsSafeRedirectTarget(const GURL& location) const { | |
146 return false; | |
147 } | |
148 | |
149 } // namespace content | |
OLD | NEW |