Chromium Code Reviews| Index: content/network/cache_url_loader.cc |
| diff --git a/content/network/cache_url_loader.cc b/content/network/cache_url_loader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d678a69e7c67da7a54ae1a747f6a55f6371ed236 |
| --- /dev/null |
| +++ b/content/network/cache_url_loader.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2017 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/network/cache_url_loader.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/macros.h" |
| +#include "base/strings/string_util.h" |
| +#include "content/public/common/url_constants.h" |
| +#include "mojo/common/data_pipe_utils.h" |
| +#include "net/url_request/view_cache_helper.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +class CacheURLLoader { |
| + public: |
| + CacheURLLoader(const ResourceRequest& request, |
| + net::URLRequestContext* request_context, |
| + mojom::URLLoaderClientPtr client) |
| + : client_(std::move(client)) { |
| + scoped_refptr<net::HttpResponseHeaders> headers( |
| + new net::HttpResponseHeaders("HTTP/1.1 200 OK")); |
| + ResourceResponseHead resource_response; |
| + resource_response.headers = headers; |
| + resource_response.mime_type = "text/html"; |
| + client_->OnReceiveResponse(resource_response, base::nullopt, nullptr); |
| + |
| + std::string cache_key = |
| + request.url.spec().substr(strlen(kChromeUINetworkViewCacheURL)); |
| + |
| + int rv; |
| + if (cache_key.empty()) { |
| + rv = cache_helper_.GetContentsHTML( |
| + request_context, kChromeUINetworkViewCacheURL, &data_, |
| + base::Bind(&CacheURLLoader::DataAvailable, base::Unretained(this))); |
| + } else { |
| + rv = cache_helper_.GetEntryInfoHTML( |
| + cache_key, request_context, &data_, |
| + base::Bind(&CacheURLLoader::DataAvailable, base::Unretained(this))); |
| + } |
| + |
| + if (rv != net::ERR_IO_PENDING) |
| + DataAvailable(rv); |
|
mmenke
2017/05/10 19:01:53
Is it safe to call delete this in a constructor?
jam
2017/05/10 22:02:53
It is for now, but you're right this is bordering
|
| + } |
| + |
| + private: |
| + ~CacheURLLoader() {} |
| + |
| + void DataAvailable(int result) { |
| + DCHECK_EQ(net::OK, result); |
| + MojoCreateDataPipeOptions options; |
| + options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| + options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| + options.element_num_bytes = 1; |
| + options.capacity_num_bytes = data_.size(); |
| + mojo::DataPipe data_pipe(options); |
| + |
| + DCHECK(data_pipe.producer_handle.is_valid()); |
| + DCHECK(data_pipe.consumer_handle.is_valid()); |
| + |
| + CHECK( |
| + mojo::common::BlockingCopyFromString(data_, data_pipe.producer_handle)); |
| + |
| + client_->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle)); |
| + |
| + ResourceRequestCompletionStatus request_complete_data; |
| + request_complete_data.error_code = net::OK; |
| + request_complete_data.exists_in_cache = false; |
| + request_complete_data.completion_time = base::TimeTicks::Now(); |
| + request_complete_data.encoded_data_length = data_.size(); |
| + request_complete_data.encoded_body_length = data_.size(); |
| + client_->OnComplete(request_complete_data); |
| + delete this; |
| + } |
| + |
| + std::string data_; |
| + mojom::URLLoaderClientPtr client_; |
| + net::ViewCacheHelper cache_helper_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CacheURLLoader); |
| +}; |
| +} |
| + |
| +void StartCacheURLLoader(const ResourceRequest& request, |
| + net::URLRequestContext* request_context, |
| + mojom::URLLoaderClientPtr client) { |
| + new CacheURLLoader(request, request_context, std::move(client)); |
|
mmenke
2017/05/10 19:01:53
Maybe a comment that the CacheURLLoader owns itsel
jam
2017/05/10 22:02:53
I think this is obvious from looking at the small
|
| +} |
| + |
| +} // namespace content |