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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/resource_protocol_handler.cc
diff --git a/content/browser/resource_protocol_handler.cc b/content/browser/resource_protocol_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8c333db2e3aac0f1afa284691ab8fabec37c08e7
--- /dev/null
+++ b/content/browser/resource_protocol_handler.cc
@@ -0,0 +1,155 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/resource_protocol_handler.h"
+
+#include "base/format_macros.h"
+#include "base/strings/stringprintf.h"
+#include "blink/public/resources/grit/blink_resources.h"
+#include "content/public/common/content_client.h"
+#include "net/base/net_errors.h"
+#include "net/http/http_request_headers.h"
+#include "net/http/http_response_headers.h"
+#include "net/http/http_response_info.h"
+#include "net/url_request/url_request.h"
+#include "net/url_request/url_request_error_job.h"
+#include "net/url_request/url_request_job.h"
+#include "net/url_request/url_request_job_factory.h"
+#include "net/url_request/url_request_simple_job.h"
+#include "ui/base/layout.h"
+
+namespace content {
+namespace {
+
+class URLRequestResourceJob : public net::URLRequestSimpleJob {
+ public:
+ using WebAccessibleResource = ResourceProtocolHandler::WebAccessibleResource;
+
+ URLRequestResourceJob(net::URLRequest* request,
+ net::NetworkDelegate* network_delegate,
+ const WebAccessibleResource& resource_info,
+ ContentClient* content_client)
+ : net::URLRequestSimpleJob(request, network_delegate),
+ resource_info_(resource_info),
+ content_client_(content_client) {
+ std::string raw_headers("HTTP/1.1 200 OK");
+ raw_headers.append(2, '\0');
+ response_headers_ = new net::HttpResponseHeaders(raw_headers);
+ }
+
+ // Overridden from net::URLRequestSimpleJob:
+ int GetData(std::string* mime_type,
+ std::string* charset,
+ std::string* data,
+ const net::CompletionCallback& callback) const override {
+ *data = GetContentClient()
+ ->GetDataResource(resource_info_.resource_id,
+ resource_info_.scale_factor)
+ .as_string();
+
+ response_headers_->AddHeader(base::StringPrintf(
+ "%s: %" PRIuS, net::HttpRequestHeaders::kContentLength, data->size()));
+
+ response_headers_->AddHeader(
+ base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentType,
+ resource_info_.content_type.c_str()));
+ response_headers_->GetMimeTypeAndCharset(mime_type, charset);
+
+ return net::OK;
+ }
+
+ void GetResponseInfo(net::HttpResponseInfo* info) override {
+ info->headers = response_headers_;
+ }
+
+ private:
+ ~URLRequestResourceJob() override {}
+
+ ContentClient* GetContentClient() const {
+ return content_client_ ? content_client_ : content::GetContentClient();
+ }
+
+ WebAccessibleResource resource_info_;
+ scoped_refptr<net::HttpResponseHeaders> response_headers_;
+ ContentClient* content_client_;
+};
+
+} // namespace
+
+ResourceProtocolHandler::ResourceProtocolHandler()
+ : ResourceProtocolHandler(nullptr) {
+}
+
+ResourceProtocolHandler::ResourceProtocolHandler(ContentClient* content_client)
+ : content_client_(content_client) {
+}
+
+ResourceProtocolHandler::~ResourceProtocolHandler() {
+}
+
+void ResourceProtocolHandler::RegisterResource(
+ const std::string& host,
+ const std::string& path,
+ int resource_id,
+ ui::ScaleFactor scale_factor,
+ const std::string& content_type) {
+ WebAccessibleResource& resource_info = resources_[std::make_pair(host, path)];
+ resource_info.resource_id = resource_id;
+ resource_info.scale_factor = scale_factor;
+ resource_info.content_type = content_type;
+}
+
+net::URLRequestJob* ResourceProtocolHandler::MaybeCreateJob(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const {
+ DCHECK(request);
+ DCHECK(request->url().is_valid());
+
+ const auto& url = request->url();
+ auto it = resources_.find(std::make_pair(url.host(), url.path()));
+ if (it != resources_.end()) {
+ return new URLRequestResourceJob(request, network_delegate, it->second,
+ content_client_);
+ }
+
+ return new net::URLRequestErrorJob(request, network_delegate,
+ net::ERR_FILE_NOT_FOUND);
+}
+
+bool ResourceProtocolHandler::IsSafeRedirectTarget(const GURL& location) const {
+ return false;
+}
+
+namespace {
+
+struct BuiltinWebAccessibleResource {
+ const char* path;
+ int resource_id;
+ ui::ScaleFactor scale_factor;
+ const char* content_type;
+};
+
+const char kBlinkHostname[] = "blink";
+const BuiltinWebAccessibleResource kBlinkWebAccessibleResources[] = {
+ // Warning: Resources in this list are exposed to all web content.
+ // Carefully consider whether the resource could be used for XSS before
+ // including it.
+ // TODO(jbroman): Put actual resources in here.
+ // A dummy entry is here because MSVC rejects zero-sized constant arrays.
+ {"", 0, ui::SCALE_FACTOR_NONE, ""},
+};
+
+} // namespace
+
+void RegisterDefaultWebAccessibleResources(ResourceProtocolHandler* handler) {
+ std::string hostname = kBlinkHostname;
+ for (const auto& resource : kBlinkWebAccessibleResources) {
+ if (resource.resource_id == 0)
+ continue;
+ handler->RegisterResource(hostname, resource.path, resource.resource_id,
+ resource.scale_factor, resource.content_type);
+ }
+}
+
+} // namespace content
« 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