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

Side by Side Diff: ios/web/webui/url_data_manager_ios_backend.cc

Issue 1110213002: Upstream most of the iOS WebUI support in ios/web/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
(Empty)
1 // Copyright 2014 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 "ios/web/webui/url_data_manager_ios_backend.h"
6
7 #include <set>
8
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/debug/alias.h"
14 #include "base/lazy_instance.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/trace_event/trace_event.h"
22 #include "ios/web/public/browser_state.h"
23 #include "ios/web/public/web_client.h"
24 #include "ios/web/public/web_thread.h"
25 #include "ios/web/webui/shared_resources_data_source_ios.h"
26 #include "ios/web/webui/url_data_source_ios_impl.h"
27 #include "net/base/io_buffer.h"
28 #include "net/base/net_errors.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_status_code.h"
31 #include "net/url_request/url_request.h"
32 #include "net/url_request/url_request_context.h"
33 #include "net/url_request/url_request_job.h"
34 #include "net/url_request/url_request_job_factory.h"
35 #include "url/url_util.h"
36
37 using web::WebThread;
38
39 namespace web {
40
41 namespace {
42
43 // TODO(tsepez) remove unsafe-eval when bidichecker_packaged.js fixed.
44 const char kChromeURLContentSecurityPolicyHeaderBase[] =
45 "Content-Security-Policy: script-src chrome://resources "
46 "'self' 'unsafe-eval'; ";
47
48 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY";
49
50 bool SchemeIsInSchemes(const std::string& scheme,
51 const std::vector<std::string>& schemes) {
52 return std::find(schemes.begin(), schemes.end(), scheme) != schemes.end();
53 }
54
55 // Returns whether |url| passes some sanity checks and is a valid GURL.
56 bool CheckURLIsValid(const GURL& url) {
57 std::vector<std::string> additional_schemes;
58 DCHECK(GetWebClient()->IsAppSpecificURL(url) ||
59 (GetWebClient()->GetAdditionalWebUISchemes(&additional_schemes),
60 SchemeIsInSchemes(url.scheme(), additional_schemes)));
61
62 if (!url.is_valid()) {
63 NOTREACHED();
64 return false;
65 }
66
67 return true;
68 }
69
70 // Parse |url| to get the path which will be used to resolve the request. The
Eugene But (OOO till 7-30) 2015/04/28 20:23:01 NIT: s/Parse/Parses
71 // path is the remaining portion after the scheme and hostname.
72 void URLToRequestPath(const GURL& url, std::string* path) {
73 const std::string& spec = url.possibly_invalid_spec();
74 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
75 // + 1 to skip the slash at the beginning of the path.
76 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false) + 1;
77
78 if (offset < static_cast<int>(spec.size()))
79 path->assign(spec.substr(offset));
80 }
81
82 } // namespace
83
84 // URLRequestChromeJob is a net::URLRequestJob that manages running
85 // chrome-internal resource requests asynchronously.
86 // It hands off URL requests to ChromeURLDataManagerIOS, which asynchronously
87 // calls back once the data is available.
88 class URLRequestChromeJob : public net::URLRequestJob {
89 public:
90 // |is_incognito| set when job is generated from an incognito profile.
91 URLRequestChromeJob(net::URLRequest* request,
92 net::NetworkDelegate* network_delegate,
93 BrowserState* browser_state,
94 bool is_incognito);
95
96 // net::URLRequestJob implementation.
97 void Start() override;
98 void Kill() override;
99 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
100 bool GetMimeType(std::string* mime_type) const override;
101 int GetResponseCode() const override;
102 void GetResponseInfo(net::HttpResponseInfo* info) override;
103
104 // Used to notify that the requested data's |mime_type| is ready.
105 void MimeTypeAvailable(const std::string& mime_type);
106
107 // Called by ChromeURLDataManagerIOS to notify us that the data blob is ready
108 // for us.
109 void DataAvailable(base::RefCountedMemory* bytes);
110
111 void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
112
113 void set_allow_caching(bool allow_caching) { allow_caching_ = allow_caching; }
114
115 void set_add_content_security_policy(bool add_content_security_policy) {
116 add_content_security_policy_ = add_content_security_policy;
117 }
118
119 void set_content_security_policy_object_source(const std::string& data) {
120 content_security_policy_object_source_ = data;
121 }
122
123 void set_content_security_policy_frame_source(const std::string& data) {
124 content_security_policy_frame_source_ = data;
125 }
126
127 void set_deny_xframe_options(bool deny_xframe_options) {
128 deny_xframe_options_ = deny_xframe_options;
129 }
130
131 void set_send_content_type_header(bool send_content_type_header) {
132 send_content_type_header_ = send_content_type_header;
133 }
134
135 // Returns true when job was generated from an incognito profile.
136 bool is_incognito() const { return is_incognito_; }
137
138 private:
139 friend class URLDataManagerIOSBackend;
140
141 ~URLRequestChromeJob() override;
142
143 // Do the actual copy from data_ (the data we're serving) into |buf|.
144 // Separate from ReadRawData so we can handle async I/O.
145 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read);
146
147 // The actual data we're serving. NULL until it's been fetched.
148 scoped_refptr<base::RefCountedMemory> data_;
149 // The current offset into the data that we're handing off to our
150 // callers via the Read interfaces.
151 int data_offset_;
152
153 // For async reads, we keep around a pointer to the buffer that
154 // we're reading into.
155 scoped_refptr<net::IOBuffer> pending_buf_;
156 int pending_buf_size_;
157 std::string mime_type_;
158
159 // If true, set a header in the response to prevent it from being cached.
160 bool allow_caching_;
161
162 // If true, set the Content Security Policy (CSP) header.
163 bool add_content_security_policy_;
164
165 // These are used with the CSP.
166 std::string content_security_policy_object_source_;
167 std::string content_security_policy_frame_source_;
168
169 // If true, sets the "X-Frame-Options: DENY" header.
170 bool deny_xframe_options_;
171
172 // If true, sets the "Content-Type: <mime-type>" header.
173 bool send_content_type_header_;
174
175 // True when job is generated from an incognito profile.
176 const bool is_incognito_;
177
178 // The BrowserState with which this job is associated.
179 BrowserState* browser_state_;
180
181 // The backend is owned by the BrowserState and always outlives us. It is
182 // obtained from the BrowserState on the IO thread.
183 URLDataManagerIOSBackend* backend_;
184
185 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_;
186
187 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
188 };
189
190 URLRequestChromeJob::URLRequestChromeJob(
191 net::URLRequest* request,
192 net::NetworkDelegate* network_delegate,
193 BrowserState* browser_state,
194 bool is_incognito)
195 : net::URLRequestJob(request, network_delegate),
196 data_offset_(0),
197 pending_buf_size_(0),
198 allow_caching_(true),
199 add_content_security_policy_(true),
200 content_security_policy_object_source_("object-src 'none';"),
201 content_security_policy_frame_source_("frame-src 'none';"),
202 deny_xframe_options_(true),
203 send_content_type_header_(false),
204 is_incognito_(is_incognito),
205 browser_state_(browser_state),
206 backend_(NULL),
207 weak_factory_(this) {
208 DCHECK(browser_state_);
209 }
210
211 URLRequestChromeJob::~URLRequestChromeJob() {
212 if (backend_) {
213 CHECK(!backend_->HasPendingJob(this));
214 }
215 }
216
217 void URLRequestChromeJob::Start() {
218 TRACE_EVENT_ASYNC_BEGIN1("browser",
219 "DataManager:Request",
220 this,
221 "URL",
222 request_->url().possibly_invalid_spec());
223
224 if (!request_)
225 return;
226 DCHECK(browser_state_);
227
228 // Obtain the URLDataManagerIOSBackend instance that is associated with
229 // |browser_state_|. Note that this *must* be done on the IO thread.
230 backend_ = browser_state_->GetURLDataManagerIOSBackendOnIOThread();
231 DCHECK(backend_);
232
233 if (!backend_->StartRequest(request_, this)) {
234 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
235 net::ERR_INVALID_URL));
236 }
237 }
238
239 void URLRequestChromeJob::Kill() {
240 if (backend_)
241 backend_->RemoveRequest(this);
242 }
243
244 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const {
245 *mime_type = mime_type_;
246 return !mime_type_.empty();
247 }
248
249 int URLRequestChromeJob::GetResponseCode() const {
250 return net::HTTP_OK;
251 }
252
253 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) {
254 DCHECK(!info->headers.get());
255 // Set the headers so that requests serviced by ChromeURLDataManagerIOS
256 // return a status code of 200. Without this they return a 0, which makes the
257 // status indistiguishable from other error types. Instant relies on getting
258 // a 200.
259 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
260
261 // Determine the least-privileged content security policy header, if any,
262 // that is compatible with a given WebUI URL, and append it to the existing
263 // response headers.
264 if (add_content_security_policy_) {
265 std::string base = kChromeURLContentSecurityPolicyHeaderBase;
266 base.append(content_security_policy_object_source_);
267 base.append(content_security_policy_frame_source_);
268 info->headers->AddHeader(base);
269 }
270
271 if (deny_xframe_options_)
272 info->headers->AddHeader(kChromeURLXFrameOptionsHeader);
273
274 if (!allow_caching_)
275 info->headers->AddHeader("Cache-Control: no-cache");
276
277 if (send_content_type_header_ && !mime_type_.empty()) {
278 std::string content_type = base::StringPrintf(
279 "%s:%s", net::HttpRequestHeaders::kContentType, mime_type_.c_str());
280 info->headers->AddHeader(content_type);
281 }
282 }
283
284 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) {
285 set_mime_type(mime_type);
286 NotifyHeadersComplete();
287 }
288
289 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) {
290 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this);
291 if (bytes) {
292 // The request completed, and we have all the data.
293 // Clear any IO pending status.
294 SetStatus(net::URLRequestStatus());
295
296 data_ = bytes;
297 int bytes_read;
298 if (pending_buf_.get()) {
299 CHECK(pending_buf_->data());
300 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read);
301 pending_buf_ = NULL;
302 NotifyReadComplete(bytes_read);
303 }
304 } else {
305 // The request failed.
306 NotifyDone(
307 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED));
308 }
309 }
310
311 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf,
312 int buf_size,
313 int* bytes_read) {
314 if (!data_.get()) {
315 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
316 DCHECK(!pending_buf_.get());
317 CHECK(buf->data());
318 pending_buf_ = buf;
319 pending_buf_size_ = buf_size;
320 return false; // Tell the caller we're still waiting for data.
321 }
322
323 // Otherwise, the data is available.
324 CompleteRead(buf, buf_size, bytes_read);
325 return true;
326 }
327
328 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf,
329 int buf_size,
330 int* bytes_read) {
331 // http://crbug.com/373841
332 char url_buf[128];
333 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf));
334 base::debug::Alias(url_buf);
335
336 int remaining = static_cast<int>(data_->size()) - data_offset_;
337 if (buf_size > remaining)
338 buf_size = remaining;
339 if (buf_size > 0) {
340 memcpy(buf->data(), data_->front() + data_offset_, buf_size);
341 data_offset_ += buf_size;
342 }
343 *bytes_read = buf_size;
344 }
345
346 namespace {
347
348 // Gets mime type for data that is available from |source| by |path|.
349 // After that, notifies |job| that mime type is available. This method
350 // should be called on the UI thread, but notification is performed on
351 // the IO thread.
352 void GetMimeTypeOnUI(URLDataSourceIOSImpl* source,
353 const std::string& path,
354 const base::WeakPtr<URLRequestChromeJob>& job) {
355 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
356 std::string mime_type = source->source()->GetMimeType(path);
357 WebThread::PostTask(
358 WebThread::IO, FROM_HERE,
359 base::Bind(&URLRequestChromeJob::MimeTypeAvailable, job, mime_type));
360 }
361
362 } // namespace
363
364 namespace {
365
366 class ChromeProtocolHandler
367 : public net::URLRequestJobFactory::ProtocolHandler {
368 public:
369 // |is_incognito| should be set for incognito profiles.
370 ChromeProtocolHandler(BrowserState* browser_state,
371 bool is_incognito)
372 : browser_state_(browser_state), is_incognito_(is_incognito) {}
373 ~ChromeProtocolHandler() override {}
374
375 net::URLRequestJob* MaybeCreateJob(
376 net::URLRequest* request,
377 net::NetworkDelegate* network_delegate) const override {
378 DCHECK(request);
379
380 return new URLRequestChromeJob(
381 request, network_delegate, browser_state_, is_incognito_);
382 }
383
384 bool IsSafeRedirectTarget(const GURL& location) const override {
385 return false;
386 }
387
388 private:
389 BrowserState* browser_state_;
390
391 // True when generated from an incognito profile.
392 const bool is_incognito_;
393
394 DISALLOW_COPY_AND_ASSIGN(ChromeProtocolHandler);
395 };
396
397 } // namespace
398
399 URLDataManagerIOSBackend::URLDataManagerIOSBackend() : next_request_id_(0) {
400 URLDataSourceIOS* shared_source = new SharedResourcesDataSourceIOS();
401 URLDataSourceIOSImpl* source_impl =
402 new URLDataSourceIOSImpl(shared_source->GetSource(), shared_source);
403 AddDataSource(source_impl);
404 }
405
406 URLDataManagerIOSBackend::~URLDataManagerIOSBackend() {
407 for (DataSourceMap::iterator i = data_sources_.begin();
408 i != data_sources_.end();
409 ++i) {
410 i->second->backend_ = NULL;
411 }
412 data_sources_.clear();
413 }
414
415 // static
416 net::URLRequestJobFactory::ProtocolHandler*
417 URLDataManagerIOSBackend::CreateProtocolHandler(
418 BrowserState* browser_state) {
419 DCHECK(browser_state);
420 return new ChromeProtocolHandler(browser_state,
421 browser_state->IsOffTheRecord());
422 }
423
424 void URLDataManagerIOSBackend::AddDataSource(URLDataSourceIOSImpl* source) {
425 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::IO);
426 DataSourceMap::iterator i = data_sources_.find(source->source_name());
427 if (i != data_sources_.end()) {
428 if (!source->source()->ShouldReplaceExistingSource())
429 return;
430 i->second->backend_ = NULL;
431 }
432 data_sources_[source->source_name()] = source;
433 source->backend_ = this;
434 }
435
436 bool URLDataManagerIOSBackend::HasPendingJob(URLRequestChromeJob* job) const {
437 for (PendingRequestMap::const_iterator i = pending_requests_.begin();
438 i != pending_requests_.end();
439 ++i) {
440 if (i->second == job)
441 return true;
442 }
443 return false;
444 }
445
446 bool URLDataManagerIOSBackend::StartRequest(const net::URLRequest* request,
447 URLRequestChromeJob* job) {
448 if (!CheckURLIsValid(request->url()))
449 return false;
450
451 URLDataSourceIOSImpl* source = GetDataSourceFromURL(request->url());
452 if (!source)
453 return false;
454
455 if (!source->source()->ShouldServiceRequest(request))
456 return false;
457
458 std::string path;
459 URLToRequestPath(request->url(), &path);
460 source->source()->WillServiceRequest(request, &path);
461
462 // Save this request so we know where to send the data.
463 RequestID request_id = next_request_id_++;
464 pending_requests_.insert(std::make_pair(request_id, job));
465
466 job->set_allow_caching(source->source()->AllowCaching());
467 job->set_add_content_security_policy(true);
468 job->set_content_security_policy_object_source(
469 source->source()->GetContentSecurityPolicyObjectSrc());
470 job->set_content_security_policy_frame_source("frame-src 'none';");
471 job->set_deny_xframe_options(source->source()->ShouldDenyXFrameOptions());
472 job->set_send_content_type_header(false);
473
474 // Forward along the request to the data source.
475 // URLRequestChromeJob should receive mime type before data. This
476 // is guaranteed because request for mime type is placed in the
477 // message loop before request for data. And correspondingly their
478 // replies are put on the IO thread in the same order.
479 base::MessageLoop* target_message_loop =
480 web::WebThread::UnsafeGetMessageLoopForThread(web::WebThread::UI);
481 target_message_loop->PostTask(
482 FROM_HERE,
483 base::Bind(&GetMimeTypeOnUI,
484 scoped_refptr<URLDataSourceIOSImpl>(source),
485 path,
486 job->weak_factory_.GetWeakPtr()));
487
488 target_message_loop->PostTask(
489 FROM_HERE,
490 base::Bind(&URLDataManagerIOSBackend::CallStartRequest,
491 make_scoped_refptr(source),
492 path,
493 request_id));
494 return true;
495 }
496
497 URLDataSourceIOSImpl* URLDataManagerIOSBackend::GetDataSourceFromURL(
498 const GURL& url) {
499 // The input usually looks like: chrome://source_name/extra_bits?foo
500 // so do a lookup using the host of the URL.
501 DataSourceMap::iterator i = data_sources_.find(url.host());
502 if (i != data_sources_.end())
503 return i->second.get();
504
505 // No match using the host of the URL, so do a lookup using the scheme for
506 // URLs on the form source_name://extra_bits/foo .
507 i = data_sources_.find(url.scheme() + "://");
508 if (i != data_sources_.end())
509 return i->second.get();
510
511 // No matches found, so give up.
512 return NULL;
513 }
514
515 void URLDataManagerIOSBackend::CallStartRequest(
516 scoped_refptr<URLDataSourceIOSImpl> source,
517 const std::string& path,
518 int request_id) {
519 source->source()->StartDataRequest(
520 path,
521 base::Bind(&URLDataSourceIOSImpl::SendResponse, source, request_id));
522 }
523
524 void URLDataManagerIOSBackend::RemoveRequest(URLRequestChromeJob* job) {
525 // Remove the request from our list of pending requests.
526 // If/when the source sends the data that was requested, the data will just
527 // be thrown away.
528 for (PendingRequestMap::iterator i = pending_requests_.begin();
529 i != pending_requests_.end();
530 ++i) {
531 if (i->second == job) {
532 pending_requests_.erase(i);
533 return;
534 }
535 }
536 }
537
538 void URLDataManagerIOSBackend::DataAvailable(RequestID request_id,
539 base::RefCountedMemory* bytes) {
540 // Forward this data on to the pending net::URLRequest, if it exists.
541 PendingRequestMap::iterator i = pending_requests_.find(request_id);
542 if (i != pending_requests_.end()) {
543 URLRequestChromeJob* job(i->second);
544 pending_requests_.erase(i);
545 job->DataAvailable(bytes);
546 }
547 }
548
549 } // namespace web
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698