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

Side by Side Diff: content/browser/webui/web_ui_url_loader_factory.cc

Issue 2867083002: Move the new WebUI's code to read the data off the IO thread as it's generally from the memory-mapp… (Closed)
Patch Set: review comments and a TODO 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
« no previous file with comments | « content/browser/webui/url_data_manager_backend.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/browser/webui/web_ui_url_loader_factory.h" 5 #include "content/browser/webui/web_ui_url_loader_factory.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
14 #include "base/sys_byteorder.h"
15 #include "content/browser/frame_host/frame_tree_node.h" 14 #include "content/browser/frame_host/frame_tree_node.h"
16 #include "content/browser/frame_host/render_frame_host_impl.h" 15 #include "content/browser/frame_host/render_frame_host_impl.h"
17 #include "content/browser/resource_context_impl.h" 16 #include "content/browser/resource_context_impl.h"
18 #include "content/browser/webui/url_data_manager_backend.h" 17 #include "content/browser/webui/url_data_manager_backend.h"
19 #include "content/browser/webui/url_data_source_impl.h" 18 #include "content/browser/webui/url_data_source_impl.h"
20 #include "content/public/browser/browser_context.h" 19 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/render_process_host.h" 21 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
24 #include "mojo/public/cpp/bindings/binding_set.h" 23 #include "mojo/public/cpp/bindings/binding_set.h"
25 #include "third_party/zlib/google/compression_utils.h" 24 #include "third_party/zlib/google/compression_utils.h"
26 #include "ui/base/template_expressions.h" 25 #include "ui/base/template_expressions.h"
27 26
28 namespace content { 27 namespace content {
29 28
30 namespace { 29 namespace {
31 30
32 class WebUIURLLoaderFactory; 31 class WebUIURLLoaderFactory;
33 base::LazyInstance<std::map<int, std::unique_ptr<WebUIURLLoaderFactory>>>::Leaky 32 base::LazyInstance<std::map<int, std::unique_ptr<WebUIURLLoaderFactory>>>::Leaky
34 g_factories = LAZY_INSTANCE_INITIALIZER; 33 g_factories = LAZY_INSTANCE_INITIALIZER;
35 34
36 class URLLoaderImpl : public mojom::URLLoader { 35 void CallOnError(mojom::URLLoaderClientPtrInfo client_info, int error_code) {
37 public: 36 mojom::URLLoaderClientPtr client;
38 static void Create(mojom::URLLoaderAssociatedRequest loader, 37 client.Bind(std::move(client_info));
39 const ResourceRequest& request, 38
40 int frame_tree_node_id, 39 ResourceRequestCompletionStatus status;
41 mojo::InterfacePtrInfo<mojom::URLLoaderClient> client_info, 40 status.error_code = error_code;
42 ResourceContext* resource_context) { 41 client->OnComplete(status);
43 mojom::URLLoaderClientPtr client; 42 }
44 client.Bind(std::move(client_info)); 43
45 new URLLoaderImpl(std::move(loader), request, frame_tree_node_id, 44 void ReadData(scoped_refptr<ResourceResponse> headers,
46 std::move(client), resource_context); 45 const ui::TemplateReplacements* replacements,
46 bool gzipped,
47 scoped_refptr<URLDataSourceImpl> data_source,
48 mojom::URLLoaderClientPtrInfo client_info,
49 scoped_refptr<base::RefCountedMemory> bytes) {
50 if (!bytes) {
51 CallOnError(std::move(client_info), net::ERR_FAILED);
52 return;
47 } 53 }
48 54
49 private: 55 mojom::URLLoaderClientPtr client;
50 URLLoaderImpl(mojom::URLLoaderAssociatedRequest loader, 56 client.Bind(std::move(client_info));
51 const ResourceRequest& request, 57 client->OnReceiveResponse(headers->head, base::nullopt, nullptr);
52 int frame_tree_node_id, 58
53 mojom::URLLoaderClientPtr client, 59 base::StringPiece input(reinterpret_cast<const char*>(bytes->front()),
54 ResourceContext* resource_context) 60 bytes->size());
55 : binding_(this, std::move(loader)), 61
56 client_(std::move(client)), 62 if (replacements) {
57 replacements_(nullptr), 63 std::string temp_string;
58 weak_factory_(this) { 64 // We won't know the the final output size ahead of time, so we have to
59 // NOTE: this duplicates code in URLDataManagerBackend::StartRequest. 65 // use an intermediate string.
60 if (!URLDataManagerBackend::CheckURLIsValid(request.url)) { 66 base::StringPiece source;
61 OnError(net::ERR_INVALID_URL); 67 std::string temp_str;
62 return; 68 if (gzipped) {
69 temp_str.resize(compression::GetUncompressedSize(input));
70 source.set(temp_str.c_str(), temp_str.size());
71 CHECK(compression::GzipUncompress(input, source));
72 gzipped = false;
73 } else {
74 source = input;
63 } 75 }
64 76 temp_str = ui::ReplaceTemplateExpressions(source, *replacements);
65 URLDataSourceImpl* source = 77 bytes = base::RefCountedString::TakeString(&temp_str);
66 GetURLDataManagerForResourceContext(resource_context) 78 input.set(reinterpret_cast<const char*>(bytes->front()), bytes->size());
67 ->GetDataSourceFromURL(request.url);
68 if (!source) {
69 OnError(net::ERR_INVALID_URL);
70 return;
71 }
72
73 if (!source->source()->ShouldServiceRequest(request.url, resource_context,
74 -1)) {
75 OnError(net::ERR_INVALID_URL);
76 return;
77 }
78
79 std::string path;
80 URLDataManagerBackend::URLToRequestPath(request.url, &path);
81 gzipped_ = source->source()->IsGzipped(path);
82 if (source->source()->GetMimeType(path) == "text/html")
83 replacements_ = source->GetReplacements();
84
85 net::HttpRequestHeaders request_headers;
86 request_headers.AddHeadersFromString(request.headers);
87 std::string origin_header;
88 request_headers.GetHeader(net::HttpRequestHeaders::kOrigin, &origin_header);
89
90 scoped_refptr<net::HttpResponseHeaders> headers =
91 URLDataManagerBackend::GetHeaders(source, path, origin_header);
92
93 ResourceResponseHead head;
94 head.headers = headers;
95 head.mime_type = source->source()->GetMimeType(path);
96 // TODO: fill all the time related field i.e. request_time response_time
97 // request_start response_start
98 client_->OnReceiveResponse(head, base::nullopt, nullptr);
99
100 ResourceRequestInfo::WebContentsGetter wc_getter =
101 base::Bind(WebContents::FromFrameTreeNodeId, frame_tree_node_id);
102
103 // Forward along the request to the data source.
104 // TODO(jam): once we only have this code path for WebUI, and not the
105 // URLLRequestJob one, then we should switch data sources to run on the UI
106 // thread by default.
107 scoped_refptr<base::SingleThreadTaskRunner> target_runner =
108 source->source()->TaskRunnerForRequestPath(path);
109 if (!target_runner) {
110 source->source()->StartDataRequest(
111 path, wc_getter,
112 base::Bind(&URLLoaderImpl::DataAvailable,
113 weak_factory_.GetWeakPtr()));
114 } else {
115 // The DataSource wants StartDataRequest to be called on a specific
116 // thread, usually the UI thread, for this path.
117 target_runner->PostTask(
118 FROM_HERE, base::Bind(&URLLoaderImpl::CallStartDataRequest,
119 base::RetainedRef(source), path, wc_getter,
120 weak_factory_.GetWeakPtr()));
121 }
122 } 79 }
123 80
124 void FollowRedirect() override { NOTREACHED(); } 81 uint32_t output_size =
125 void SetPriority(net::RequestPriority priority, 82 gzipped ? compression::GetUncompressedSize(input) : bytes->size();
126 int32_t intra_priority_value) override { 83
127 NOTREACHED(); 84 MojoCreateDataPipeOptions options;
85 options.struct_size = sizeof(MojoCreateDataPipeOptions);
86 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
87 options.element_num_bytes = 1;
88 options.capacity_num_bytes = output_size;
89 mojo::DataPipe data_pipe(options);
90
91 DCHECK(data_pipe.producer_handle.is_valid());
92 DCHECK(data_pipe.consumer_handle.is_valid());
93
94 void* buffer = nullptr;
95 uint32_t num_bytes = output_size;
96 MojoResult result =
97 BeginWriteDataRaw(data_pipe.producer_handle.get(), &buffer, &num_bytes,
98 MOJO_WRITE_DATA_FLAG_NONE);
99 CHECK_EQ(result, MOJO_RESULT_OK);
100 CHECK_EQ(num_bytes, output_size);
101
102 if (gzipped) {
103 base::StringPiece output(static_cast<char*>(buffer), num_bytes);
104 CHECK(compression::GzipUncompress(input, output));
105 } else {
106 memcpy(buffer, bytes->front(), output_size);
107 }
108 result = EndWriteDataRaw(data_pipe.producer_handle.get(), num_bytes);
109 CHECK_EQ(result, MOJO_RESULT_OK);
110
111 client->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle));
112
113 ResourceRequestCompletionStatus request_complete_data;
114 request_complete_data.error_code = net::OK;
115 request_complete_data.exists_in_cache = false;
116 request_complete_data.completion_time = base::TimeTicks::Now();
117 request_complete_data.encoded_data_length = output_size;
118 request_complete_data.encoded_body_length = output_size;
119 client->OnComplete(request_complete_data);
120 }
121
122 void DataAvailable(scoped_refptr<ResourceResponse> headers,
123 const ui::TemplateReplacements* replacements,
124 bool gzipped,
125 scoped_refptr<URLDataSourceImpl> source,
126 mojom::URLLoaderClientPtrInfo client_info,
127 scoped_refptr<base::RefCountedMemory> bytes) {
128 // Since the bytes are from the memory mapped resource file, copying the
129 // data can lead to disk access.
130 // TODO(jam): once http://crbug.com/678155 is fixed, use task scheduler:
131 // base::PostTaskWithTraits(
132 // FROM_HERE,
133 // {base::TaskPriority::USER_BLOCKING,
134 // base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
135 BrowserThread::PostTask(
136 BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
137 base::BindOnce(ReadData, headers, replacements, gzipped, source,
138 std::move(client_info), bytes));
139 }
140
141 void StartURLLoader(const ResourceRequest& request,
142 int frame_tree_node_id,
143 mojom::URLLoaderClientPtrInfo client_info,
144 ResourceContext* resource_context) {
145 // NOTE: this duplicates code in URLDataManagerBackend::StartRequest.
146 if (!URLDataManagerBackend::CheckURLIsValid(request.url)) {
147 CallOnError(std::move(client_info), net::ERR_INVALID_URL);
148 return;
128 } 149 }
129 150
130 static void CallStartDataRequest( 151 URLDataSourceImpl* source =
131 scoped_refptr<URLDataSourceImpl> source, 152 GetURLDataManagerForResourceContext(resource_context)
132 const std::string& path, 153 ->GetDataSourceFromURL(request.url);
133 const ResourceRequestInfo::WebContentsGetter& wc_getter, 154 if (!source) {
134 const base::WeakPtr<URLLoaderImpl>& weak_ptr) { 155 CallOnError(std::move(client_info), net::ERR_INVALID_URL);
135 source->source()->StartDataRequest( 156 return;
136 path, wc_getter,
137 base::Bind(URLLoaderImpl::DataAvailableOnTargetThread, weak_ptr));
138 } 157 }
139 158
140 static void DataAvailableOnTargetThread( 159 if (!source->source()->ShouldServiceRequest(request.url, resource_context,
141 const base::WeakPtr<URLLoaderImpl>& weak_ptr, 160 -1)) {
142 scoped_refptr<base::RefCountedMemory> bytes) { 161 CallOnError(std::move(client_info), net::ERR_INVALID_URL);
143 BrowserThread::PostTask( 162 return;
144 BrowserThread::IO, FROM_HERE,
145 base::Bind(&URLLoaderImpl::DataAvailable, weak_ptr, bytes));
146 } 163 }
147 164
148 void DataAvailable(scoped_refptr<base::RefCountedMemory> bytes) { 165 std::string path;
149 if (!bytes) { 166 URLDataManagerBackend::URLToRequestPath(request.url, &path);
150 OnError(net::ERR_FAILED);
151 return;
152 }
153 167
154 base::StringPiece input(reinterpret_cast<const char*>(bytes->front()), 168 net::HttpRequestHeaders request_headers;
155 bytes->size()); 169 request_headers.AddHeadersFromString(request.headers);
156 if (replacements_) { 170 std::string origin_header;
157 std::string temp_string; 171 request_headers.GetHeader(net::HttpRequestHeaders::kOrigin, &origin_header);
158 // We won't know the the final output size ahead of time, so we have to
159 // use an intermediate string.
160 base::StringPiece source;
161 std::string temp_str;
162 if (gzipped_) {
163 temp_str.resize(compression::GetUncompressedSize(input));
164 source.set(temp_str.c_str(), temp_str.size());
165 CHECK(compression::GzipUncompress(input, source));
166 gzipped_ = false;
167 } else {
168 source = input;
169 }
170 temp_str = ui::ReplaceTemplateExpressions(source, *replacements_);
171 bytes = base::RefCountedString::TakeString(&temp_str);
172 input.set(reinterpret_cast<const char*>(bytes->front()), bytes->size());
173 }
174 172
175 uint32_t output_size = 173 scoped_refptr<net::HttpResponseHeaders> headers =
176 gzipped_ ? compression::GetUncompressedSize(input) : bytes->size(); 174 URLDataManagerBackend::GetHeaders(source, path, origin_header);
177 175
178 MojoCreateDataPipeOptions options; 176 scoped_refptr<ResourceResponse> resource_response(new ResourceResponse);
179 options.struct_size = sizeof(MojoCreateDataPipeOptions); 177 resource_response->head.headers = headers;
180 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; 178 resource_response->head.mime_type = source->source()->GetMimeType(path);
181 options.element_num_bytes = 1; 179 // TODO: fill all the time related field i.e. request_time response_time
182 options.capacity_num_bytes = output_size; 180 // request_start response_start
183 mojo::DataPipe data_pipe(options);
184 181
185 DCHECK(data_pipe.producer_handle.is_valid()); 182 ResourceRequestInfo::WebContentsGetter wc_getter =
186 DCHECK(data_pipe.consumer_handle.is_valid()); 183 base::Bind(WebContents::FromFrameTreeNodeId, frame_tree_node_id);
187 184
188 void* buffer = nullptr; 185 bool gzipped = source->source()->IsGzipped(path);
189 uint32_t num_bytes = output_size; 186 const ui::TemplateReplacements* replacements = nullptr;
190 MojoResult result = 187 if (source->source()->GetMimeType(path) == "text/html")
191 BeginWriteDataRaw(data_pipe.producer_handle.get(), &buffer, &num_bytes, 188 replacements = source->GetReplacements();
192 MOJO_WRITE_DATA_FLAG_NONE); 189 // To keep the same behavior as the old WebUI code, we call the source to get
193 CHECK_EQ(result, MOJO_RESULT_OK); 190 // the value for |gzipped| and |replacements| on the IO thread. Since
194 CHECK_EQ(num_bytes, output_size); 191 // |replacements| is owned by |source| keep a reference to it in the callback.
192 auto data_available_callback =
193 base::Bind(DataAvailable, resource_response, replacements, gzipped,
194 base::RetainedRef(source), base::Passed(&client_info));
195 195
196 if (gzipped_) { 196 // TODO(jam): once we only have this code path for WebUI, and not the
197 base::StringPiece output(static_cast<char*>(buffer), num_bytes); 197 // URLLRequestJob one, then we should switch data sources to run on the UI
198 CHECK(compression::GzipUncompress(input, output)); 198 // thread by default.
199 } else { 199 scoped_refptr<base::SingleThreadTaskRunner> target_runner =
200 memcpy(buffer, bytes->front(), output_size); 200 source->source()->TaskRunnerForRequestPath(path);
201 } 201 if (!target_runner) {
202 result = EndWriteDataRaw(data_pipe.producer_handle.get(), num_bytes); 202 source->source()->StartDataRequest(path, wc_getter,
203 CHECK_EQ(result, MOJO_RESULT_OK); 203 data_available_callback);
204 204 return;
205 client_->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle));
206
207 ResourceRequestCompletionStatus request_complete_data;
208 request_complete_data.error_code = net::OK;
209 request_complete_data.exists_in_cache = false;
210 request_complete_data.completion_time = base::TimeTicks::Now();
211 request_complete_data.encoded_data_length = output_size;
212 request_complete_data.encoded_body_length = output_size;
213 client_->OnComplete(request_complete_data);
214 delete this;
215 } 205 }
216 206
217 void OnError(int error_code) { 207 // The DataSource wants StartDataRequest to be called on a specific
218 ResourceRequestCompletionStatus status; 208 // thread, usually the UI thread, for this path.
219 status.error_code = error_code; 209 target_runner->PostTask(
220 client_->OnComplete(status); 210 FROM_HERE, base::BindOnce(&URLDataSource::StartDataRequest,
221 delete this; 211 base::Unretained(source->source()), path,
222 } 212 wc_getter, data_available_callback));
223 213 }
224 mojo::AssociatedBinding<mojom::URLLoader> binding_;
225 mojom::URLLoaderClientPtr client_;
226 bool gzipped_;
227 // Replacement dictionary for i18n.
228 const ui::TemplateReplacements* replacements_;
229 base::WeakPtrFactory<URLLoaderImpl> weak_factory_;
230
231 DISALLOW_COPY_AND_ASSIGN(URLLoaderImpl);
232 };
233 214
234 class WebUIURLLoaderFactory : public mojom::URLLoaderFactory, 215 class WebUIURLLoaderFactory : public mojom::URLLoaderFactory,
235 public FrameTreeNode::Observer { 216 public FrameTreeNode::Observer {
236 public: 217 public:
237 WebUIURLLoaderFactory(FrameTreeNode* ftn) 218 WebUIURLLoaderFactory(FrameTreeNode* ftn)
238 : frame_tree_node_id_(ftn->frame_tree_node_id()), 219 : frame_tree_node_id_(ftn->frame_tree_node_id()),
239 resource_context_(ftn->current_frame_host() 220 resource_context_(ftn->current_frame_host()
240 ->GetProcess() 221 ->GetProcess()
241 ->GetBrowserContext() 222 ->GetBrowserContext()
242 ->GetResourceContext()) { 223 ->GetResourceContext()) {
243 ftn->AddObserver(this); 224 ftn->AddObserver(this);
244 } 225 }
245 226
246 ~WebUIURLLoaderFactory() override {} 227 ~WebUIURLLoaderFactory() override {}
247 228
248 mojom::URLLoaderFactoryPtr CreateBinding() { 229 mojom::URLLoaderFactoryPtr CreateBinding() {
249 return loader_factory_bindings_.CreateInterfacePtrAndBind(this); 230 return loader_factory_bindings_.CreateInterfacePtrAndBind(this);
250 } 231 }
251 232
252 // mojom::URLLoaderFactory implementation: 233 // mojom::URLLoaderFactory implementation:
253 void CreateLoaderAndStart(mojom::URLLoaderAssociatedRequest loader, 234 void CreateLoaderAndStart(mojom::URLLoaderAssociatedRequest loader,
254 int32_t routing_id, 235 int32_t routing_id,
255 int32_t request_id, 236 int32_t request_id,
256 uint32_t options, 237 uint32_t options,
257 const ResourceRequest& request, 238 const ResourceRequest& request,
258 mojom::URLLoaderClientPtr client) override { 239 mojom::URLLoaderClientPtr client) override {
259 DCHECK_CURRENTLY_ON(BrowserThread::UI); 240 DCHECK_CURRENTLY_ON(BrowserThread::UI);
260 BrowserThread::PostTask( 241 BrowserThread::PostTask(
261 BrowserThread::IO, FROM_HERE, 242 BrowserThread::IO, FROM_HERE,
262 base::BindOnce(&URLLoaderImpl::Create, std::move(loader), request, 243 base::BindOnce(&StartURLLoader, request, frame_tree_node_id_,
263 frame_tree_node_id_, client.PassInterface(), 244 client.PassInterface(), resource_context_));
264 resource_context_));
265 } 245 }
266 246
267 void SyncLoad(int32_t routing_id, 247 void SyncLoad(int32_t routing_id,
268 int32_t request_id, 248 int32_t request_id,
269 const ResourceRequest& request, 249 const ResourceRequest& request,
270 SyncLoadCallback callback) override { 250 SyncLoadCallback callback) override {
271 NOTREACHED(); 251 NOTREACHED();
272 } 252 }
273 253
274 // FrameTreeNode::Observer implementation: 254 // FrameTreeNode::Observer implementation:
(...skipping 12 matching lines...) Expand all
287 } // namespace 267 } // namespace
288 268
289 mojom::URLLoaderFactoryPtr GetWebUIURLLoader(FrameTreeNode* node) { 269 mojom::URLLoaderFactoryPtr GetWebUIURLLoader(FrameTreeNode* node) {
290 int ftn_id = node->frame_tree_node_id(); 270 int ftn_id = node->frame_tree_node_id();
291 if (g_factories.Get()[ftn_id].get() == nullptr) 271 if (g_factories.Get()[ftn_id].get() == nullptr)
292 g_factories.Get()[ftn_id] = base::MakeUnique<WebUIURLLoaderFactory>(node); 272 g_factories.Get()[ftn_id] = base::MakeUnique<WebUIURLLoaderFactory>(node);
293 return g_factories.Get()[ftn_id]->CreateBinding(); 273 return g_factories.Get()[ftn_id]->CreateBinding();
294 } 274 }
295 275
296 } // namespace content 276 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/webui/url_data_manager_backend.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698