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