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

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

Powered by Google App Engine
This is Rietveld 408576698