| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/network/cache_url_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "content/public/common/url_constants.h" |
| 11 #include "mojo/common/data_pipe_utils.h" |
| 12 #include "net/url_request/view_cache_helper.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class CacheURLLoader { |
| 19 public: |
| 20 CacheURLLoader(const ResourceRequest& request, |
| 21 net::URLRequestContext* request_context, |
| 22 mojom::URLLoaderClientPtr client) |
| 23 : client_(std::move(client)) { |
| 24 scoped_refptr<net::HttpResponseHeaders> headers( |
| 25 new net::HttpResponseHeaders("HTTP/1.1 200 OK")); |
| 26 ResourceResponseHead resource_response; |
| 27 resource_response.headers = headers; |
| 28 resource_response.mime_type = "text/html"; |
| 29 client_->OnReceiveResponse(resource_response, base::nullopt, nullptr); |
| 30 |
| 31 std::string cache_key = |
| 32 request.url.spec().substr(strlen(kChromeUINetworkViewCacheURL)); |
| 33 |
| 34 int rv; |
| 35 if (cache_key.empty()) { |
| 36 rv = cache_helper_.GetContentsHTML( |
| 37 request_context, kChromeUINetworkViewCacheURL, &data_, |
| 38 base::Bind(&CacheURLLoader::DataAvailable, base::Unretained(this))); |
| 39 } else { |
| 40 rv = cache_helper_.GetEntryInfoHTML( |
| 41 cache_key, request_context, &data_, |
| 42 base::Bind(&CacheURLLoader::DataAvailable, base::Unretained(this))); |
| 43 } |
| 44 |
| 45 if (rv != net::ERR_IO_PENDING) |
| 46 DataAvailable(rv); |
| 47 } |
| 48 |
| 49 private: |
| 50 ~CacheURLLoader() {} |
| 51 |
| 52 void DataAvailable(int result) { |
| 53 DCHECK_EQ(net::OK, result); |
| 54 MojoCreateDataPipeOptions options; |
| 55 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 56 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 57 options.element_num_bytes = 1; |
| 58 options.capacity_num_bytes = data_.size(); |
| 59 mojo::DataPipe data_pipe(options); |
| 60 |
| 61 DCHECK(data_pipe.producer_handle.is_valid()); |
| 62 DCHECK(data_pipe.consumer_handle.is_valid()); |
| 63 |
| 64 CHECK( |
| 65 mojo::common::BlockingCopyFromString(data_, data_pipe.producer_handle)); |
| 66 |
| 67 client_->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle)); |
| 68 |
| 69 ResourceRequestCompletionStatus request_complete_data; |
| 70 request_complete_data.error_code = net::OK; |
| 71 request_complete_data.exists_in_cache = false; |
| 72 request_complete_data.completion_time = base::TimeTicks::Now(); |
| 73 request_complete_data.encoded_data_length = data_.size(); |
| 74 request_complete_data.encoded_body_length = data_.size(); |
| 75 client_->OnComplete(request_complete_data); |
| 76 delete this; |
| 77 } |
| 78 |
| 79 std::string data_; |
| 80 mojom::URLLoaderClientPtr client_; |
| 81 net::ViewCacheHelper cache_helper_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(CacheURLLoader); |
| 84 }; |
| 85 } |
| 86 |
| 87 void StartCacheURLLoader(const ResourceRequest& request, |
| 88 net::URLRequestContext* request_context, |
| 89 mojom::URLLoaderClientPtr client) { |
| 90 new CacheURLLoader(request, request_context, std::move(client)); |
| 91 } |
| 92 |
| 93 } // namespace content |
| OLD | NEW |