Chromium Code Reviews| 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 const char kBlinkHostname[] = "blink"; | |
| 33 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 {"", 0, ui::SCALE_FACTOR_NONE, ""}, | |
| 40 }; | |
| 41 | |
| 42 class URLRequestResourceJob : public net::URLRequestSimpleJob { | |
| 43 public: | |
| 44 using WebAccessibleResource = ResourceProtocolHandler::WebAccessibleResource; | |
| 45 | |
| 46 URLRequestResourceJob(net::URLRequest* request, | |
| 47 net::NetworkDelegate* network_delegate, | |
| 48 const WebAccessibleResource& resource_info, | |
| 49 ContentClient* content_client) | |
| 50 : net::URLRequestSimpleJob(request, network_delegate), | |
| 51 resource_info_(resource_info), | |
| 52 content_client_(content_client) { | |
| 53 std::string raw_headers("HTTP/1.1 200 OK"); | |
| 54 raw_headers.append(2, '\0'); | |
| 55 response_headers_ = new net::HttpResponseHeaders(raw_headers); | |
| 56 } | |
| 57 | |
| 58 // Overridden from net::URLRequestSimpleJob: | |
| 59 int GetData(std::string* mime_type, | |
| 60 std::string* charset, | |
| 61 std::string* data, | |
| 62 const net::CompletionCallback& callback) const override { | |
| 63 *data = GetContentClient() | |
| 64 ->GetDataResource(resource_info_.resource_id, | |
| 65 resource_info_.scale_factor) | |
| 66 .as_string(); | |
| 67 | |
| 68 response_headers_->AddHeader(base::StringPrintf( | |
| 69 "%s: %" PRIuS, net::HttpRequestHeaders::kContentLength, data->size())); | |
| 70 | |
| 71 response_headers_->AddHeader( | |
| 72 base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentType, | |
| 73 resource_info_.content_type.c_str())); | |
| 74 response_headers_->GetMimeTypeAndCharset(mime_type, charset); | |
| 75 | |
| 76 return net::OK; | |
| 77 } | |
| 78 | |
| 79 void GetResponseInfo(net::HttpResponseInfo* info) override { | |
| 80 info->headers = response_headers_; | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 ~URLRequestResourceJob() override {} | |
| 85 | |
| 86 ContentClient* GetContentClient() const { | |
| 87 return content_client_ ? content_client_ : content::GetContentClient(); | |
| 88 } | |
| 89 | |
| 90 WebAccessibleResource resource_info_; | |
| 91 scoped_refptr<net::HttpResponseHeaders> response_headers_; | |
| 92 ContentClient* content_client_; | |
| 93 }; | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 ResourceProtocolHandler::ResourceProtocolHandler() | |
| 98 : ResourceProtocolHandler(nullptr) { | |
| 99 } | |
| 100 | |
| 101 ResourceProtocolHandler::ResourceProtocolHandler(ContentClient* content_client) | |
| 102 : content_client_(content_client) { | |
| 103 std::string hostname = kBlinkHostname; | |
| 104 for (const auto& resource : kBlinkWebAccessibleResources) { | |
|
davidben
2014/11/19 00:21:21
Skip the dummy entry?
jbroman
2014/11/19 15:06:54
Done.
| |
| 105 RegisterResource(hostname, resource.path, resource.resource_id, | |
| 106 resource.scale_factor, resource.content_type); | |
|
davidben
2014/11/19 00:21:21
It's odd that this class simultaneously comes with
jbroman
2014/11/19 15:06:54
Moved out into a RegisterDefaultWebAccessibleResou
| |
| 107 } | |
| 108 } | |
| 109 | |
| 110 ResourceProtocolHandler::~ResourceProtocolHandler() { | |
| 111 } | |
| 112 | |
| 113 void ResourceProtocolHandler::RegisterResource( | |
| 114 const std::string& host, | |
| 115 const std::string& path, | |
| 116 int resource_id, | |
| 117 ui::ScaleFactor scale_factor, | |
| 118 const std::string& content_type) { | |
| 119 WebAccessibleResource& resource_info = resources_[std::make_pair(host, path)]; | |
| 120 resource_info.resource_id = resource_id; | |
| 121 resource_info.scale_factor = scale_factor; | |
| 122 resource_info.content_type = content_type; | |
| 123 } | |
| 124 | |
| 125 net::URLRequestJob* ResourceProtocolHandler::MaybeCreateJob( | |
| 126 net::URLRequest* request, | |
| 127 net::NetworkDelegate* network_delegate) const { | |
| 128 DCHECK(request); | |
| 129 DCHECK(request->url().is_valid()); | |
| 130 | |
| 131 const auto& url = request->url(); | |
| 132 auto it = resources_.find(std::make_pair(url.host(), url.path())); | |
| 133 if (it != resources_.end()) { | |
| 134 return new URLRequestResourceJob(request, network_delegate, it->second, | |
| 135 content_client_); | |
| 136 } | |
| 137 | |
| 138 return new net::URLRequestErrorJob(request, network_delegate, | |
| 139 net::ERR_FILE_NOT_FOUND); | |
| 140 } | |
| 141 | |
| 142 bool ResourceProtocolHandler::IsSafeRedirectTarget(const GURL& location) const { | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 } // namespace content | |
| OLD | NEW |