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

Side by Side Diff: content/network/cache_url_loader.cc

Issue 2875143002: Reduce boilerplate when creating simple mojom::URLLoaders. (Closed)
Patch Set: Created 3 years, 7 months 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/network/cache_url_loader.h" 5 #include "content/network/cache_url_loader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 if (rv != net::ERR_IO_PENDING) 46 if (rv != net::ERR_IO_PENDING)
47 DataAvailable(rv); 47 DataAvailable(rv);
48 } 48 }
49 49
50 ~CacheURLLoader() {} 50 ~CacheURLLoader() {}
51 51
52 private: 52 private:
53 void DataAvailable(int result) { 53 void DataAvailable(int result) {
54 DCHECK_EQ(net::OK, result); 54 DCHECK_EQ(net::OK, result);
55 MojoCreateDataPipeOptions options; 55 mojo::DataPipe data_pipe(data_.size());
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 56
65 CHECK( 57 CHECK(
66 mojo::common::BlockingCopyFromString(data_, data_pipe.producer_handle)); 58 mojo::common::BlockingCopyFromString(data_, data_pipe.producer_handle));
67 59
68 client_->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle)); 60 client_->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle));
69 61 client_->OnComplete(ResourceRequestCompletionStatus(data_.size()));
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 62
78 // So we don't delete |this| in the constructor. 63 // So we don't delete |this| in the constructor.
79 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); 64 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
80 } 65 }
81 66
82 std::string data_; 67 std::string data_;
83 mojom::URLLoaderClientPtr client_; 68 mojom::URLLoaderClientPtr client_;
84 net::ViewCacheHelper cache_helper_; 69 net::ViewCacheHelper cache_helper_;
85 70
86 DISALLOW_COPY_AND_ASSIGN(CacheURLLoader); 71 DISALLOW_COPY_AND_ASSIGN(CacheURLLoader);
87 }; 72 };
88 } 73 }
89 74
90 void StartCacheURLLoader(const ResourceRequest& request, 75 void StartCacheURLLoader(const ResourceRequest& request,
91 net::URLRequestContext* request_context, 76 net::URLRequestContext* request_context,
92 mojom::URLLoaderClientPtr client) { 77 mojom::URLLoaderClientPtr client) {
93 new CacheURLLoader(request, request_context, std::move(client)); 78 new CacheURLLoader(request, request_context, std::move(client));
94 } 79 }
95 80
96 } // namespace content 81 } // namespace content
OLDNEW
« no previous file with comments | « content/common/resource_request_completion_status.cc ('k') | content/network/url_loader_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698