Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: content/browser/resource_protocol_handler.cc

Issue 647853002: Create a proprietary scheme for loading web-accessible resources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update include guards too Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_handler.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 class URLRequestResourceJob : public net::URLRequestSimpleJob {
26 public:
27 using WebAccessibleResource = ResourceProtocolHandler::WebAccessibleResource;
28
29 URLRequestResourceJob(net::URLRequest* request,
30 net::NetworkDelegate* network_delegate,
31 const WebAccessibleResource& resource_info,
32 ContentClient* content_client)
33 : net::URLRequestSimpleJob(request, network_delegate),
34 resource_info_(resource_info),
35 content_client_(content_client) {
36 std::string raw_headers("HTTP/1.1 200 OK");
37 raw_headers.append(2, '\0');
38 response_headers_ = new net::HttpResponseHeaders(raw_headers);
39 }
40
41 // Overridden from net::URLRequestSimpleJob:
42 int GetData(std::string* mime_type,
43 std::string* charset,
44 std::string* data,
45 const net::CompletionCallback& callback) const override {
46 *data = GetContentClient()
47 ->GetDataResource(resource_info_.resource_id,
48 resource_info_.scale_factor)
49 .as_string();
50
51 response_headers_->AddHeader(base::StringPrintf(
52 "%s: %" PRIuS, net::HttpRequestHeaders::kContentLength, data->size()));
53
54 response_headers_->AddHeader(
55 base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentType,
56 resource_info_.content_type.c_str()));
57 response_headers_->GetMimeTypeAndCharset(mime_type, charset);
58
59 return net::OK;
60 }
61
62 void GetResponseInfo(net::HttpResponseInfo* info) override {
63 info->headers = response_headers_;
64 }
65
66 private:
67 ~URLRequestResourceJob() override {}
68
69 ContentClient* GetContentClient() const {
70 return content_client_ ? content_client_ : content::GetContentClient();
71 }
72
73 WebAccessibleResource resource_info_;
74 scoped_refptr<net::HttpResponseHeaders> response_headers_;
75 ContentClient* content_client_;
76 };
77
78 } // namespace
79
80 ResourceProtocolHandler::ResourceProtocolHandler()
81 : ResourceProtocolHandler(nullptr) {
82 }
83
84 ResourceProtocolHandler::ResourceProtocolHandler(ContentClient* content_client)
85 : content_client_(content_client) {
86 }
87
88 ResourceProtocolHandler::~ResourceProtocolHandler() {
89 }
90
91 void ResourceProtocolHandler::RegisterResource(
92 const std::string& host,
93 const std::string& path,
94 int resource_id,
95 ui::ScaleFactor scale_factor,
96 const std::string& content_type) {
97 WebAccessibleResource& resource_info = resources_[std::make_pair(host, path)];
98 resource_info.resource_id = resource_id;
99 resource_info.scale_factor = scale_factor;
100 resource_info.content_type = content_type;
101 }
102
103 net::URLRequestJob* ResourceProtocolHandler::MaybeCreateJob(
104 net::URLRequest* request,
105 net::NetworkDelegate* network_delegate) const {
106 DCHECK(request);
107 DCHECK(request->url().is_valid());
108
109 const auto& url = request->url();
110 auto it = resources_.find(std::make_pair(url.host(), url.path()));
111 if (it != resources_.end()) {
112 return new URLRequestResourceJob(request, network_delegate, it->second,
113 content_client_);
114 }
115
116 return new net::URLRequestErrorJob(request, network_delegate,
117 net::ERR_FILE_NOT_FOUND);
118 }
119
120 bool ResourceProtocolHandler::IsSafeRedirectTarget(const GURL& location) const {
121 return false;
122 }
123
124 namespace {
125
126 struct BuiltinWebAccessibleResource {
127 const char* path;
128 int resource_id;
129 ui::ScaleFactor scale_factor;
130 const char* content_type;
131 };
132
133 const char kBlinkHostname[] = "blink";
134 const BuiltinWebAccessibleResource kBlinkWebAccessibleResources[] = {
135 // Warning: Resources in this list are exposed to all web content.
136 // Carefully consider whether the resource could be used for XSS before
137 // including it.
138 // TODO(jbroman): Put actual resources in here.
139 // A dummy entry is here because MSVC rejects zero-sized constant arrays.
140 {"", 0, ui::SCALE_FACTOR_NONE, ""},
141 };
142
143 } // namespace
144
145 void RegisterDefaultWebAccessibleResources(ResourceProtocolHandler* handler) {
146 std::string hostname = kBlinkHostname;
147 for (const auto& resource : kBlinkWebAccessibleResources) {
148 if (resource.resource_id == 0)
149 continue;
150 handler->RegisterResource(hostname, resource.path, resource.resource_id,
151 resource.scale_factor, resource.content_type);
152 }
153 }
154
155 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/resource_protocol_handler.h ('k') | content/browser/resource_protocol_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698