1// Copyright (c) 2011 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 "chrome/browser/ui/webui/chrome_url_data_manager_backend.h"
6
7#include "base/command_line.h"
8#include "base/compiler_specific.h"
9#include "base/file_util.h"
10#include "base/memory/ref_counted_memory.h"
11#include "base/message_loop.h"
12#include "base/path_service.h"
13#include "base/string_util.h"
14#include "chrome/browser/net/chrome_url_request_context.h"
15#include "chrome/browser/net/view_blob_internals_job_factory.h"
16#include "chrome/browser/net/view_http_cache_job_factory.h"
17#include "chrome/browser/ui/webui/shared_resources_data_source.h"
18#include "chrome/common/chrome_paths.h"
19#include "chrome/common/chrome_switches.h"
20#include "chrome/common/url_constants.h"
21#include "content/browser/appcache/view_appcache_internals_job_factory.h"
22#include "content/browser/browser_thread.h"
23#include "googleurl/src/url_util.h"
24#include "grit/platform_locale_settings.h"
25#include "net/base/io_buffer.h"
26#include "net/base/net_errors.h"
27#include "net/http/http_response_headers.h"
28#include "net/url_request/url_request.h"
29#include "net/url_request/url_request_file_job.h"
30#include "net/url_request/url_request_job.h"
31
32namespace {
33
34ChromeURLDataManagerBackend* GetBackend(net::URLRequest* request) {
35  return static_cast<ChromeURLRequestContext*>(request->context())->
36      GetChromeURLDataManagerBackend();
37}
38
39// Parse a URL into the components used to resolve its request. |source_name|
40// is the hostname and |path| is the remaining portion of the URL.
41void URLToRequest(const GURL& url, std::string* source_name,
42                  std::string* path) {
43  DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) ||
44         url.SchemeIs(chrome::kChromeUIScheme));
45
46  if (!url.is_valid()) {
47    NOTREACHED();
48    return;
49  }
50
51  // Our input looks like: chrome://source_name/extra_bits?foo .
52  // So the url's "host" is our source, and everything after the host is
53  // the path.
54  source_name->assign(url.host());
55
56  const std::string& spec = url.possibly_invalid_spec();
57  const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
58  // + 1 to skip the slash at the beginning of the path.
59  int offset = parsed.CountCharactersBefore(url_parse::Parsed::PATH, false) + 1;
60
61  if (offset < static_cast<int>(spec.size()))
62    path->assign(spec.substr(offset));
63}
64
65}  // namespace
66
67// URLRequestChromeJob is a net::URLRequestJob that manages running
68// chrome-internal resource requests asynchronously.
69// It hands off URL requests to ChromeURLDataManager, which asynchronously
70// calls back once the data is available.
71class URLRequestChromeJob : public net::URLRequestJob {
72 public:
73  explicit URLRequestChromeJob(net::URLRequest* request);
74
75  // net::URLRequestJob implementation.
76  virtual void Start() OVERRIDE;
77  virtual void Kill() OVERRIDE;
78  virtual bool ReadRawData(net::IOBuffer* buf,
79                           int buf_size,
80                           int *bytes_read) OVERRIDE;
81  virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
82  virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
83
84  // Called by ChromeURLDataManager to notify us that the data blob is ready
85  // for us.
86  void DataAvailable(RefCountedMemory* bytes);
87
88  void SetMimeType(const std::string& mime_type) {
89    mime_type_ = mime_type;
90  }
91
92 private:
93  virtual ~URLRequestChromeJob();
94
95  // Helper for Start(), to let us start asynchronously.
96  // (This pattern is shared by most net::URLRequestJob implementations.)
97  void StartAsync();
98
99  // Do the actual copy from data_ (the data we're serving) into |buf|.
100  // Separate from ReadRawData so we can handle async I/O.
101  void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read);
102
103  // The actual data we're serving.  NULL until it's been fetched.
104  scoped_refptr<RefCountedMemory> data_;
105  // The current offset into the data that we're handing off to our
106  // callers via the Read interfaces.
107  int data_offset_;
108
109  // For async reads, we keep around a pointer to the buffer that
110  // we're reading into.
111  scoped_refptr<net::IOBuffer> pending_buf_;
112  int pending_buf_size_;
113  std::string mime_type_;
114
115  // The backend is owned by ChromeURLRequestContext and always outlives us.
116  ChromeURLDataManagerBackend* backend_;
117
118  ScopedRunnableMethodFactory<URLRequestChromeJob> method_factory_;
119
120  DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
121};
122
123// URLRequestChromeFileJob is a net::URLRequestJob that acts like a file:// URL
124class URLRequestChromeFileJob : public net::URLRequestFileJob {
125 public:
126  URLRequestChromeFileJob(net::URLRequest* request, const FilePath& path);
127
128 private:
129  virtual ~URLRequestChromeFileJob();
130
131  DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob);
132};
133
134class DevToolsJobFactory {
135 public:
136  static bool ShouldLoadFromDisk();
137  static bool IsSupportedURL(const GURL& url, FilePath* path);
138  static net::URLRequestJob* CreateJobForRequest(net::URLRequest* request,
139                                          const FilePath& path);
140};
141
142ChromeURLDataManagerBackend::ChromeURLDataManagerBackend()
143    : next_request_id_(0) {
144  AddDataSource(new SharedResourcesDataSource());
145}
146
147ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() {
148  for (DataSourceMap::iterator i = data_sources_.begin();
149       i != data_sources_.end(); ++i) {
150    i->second->backend_ = NULL;
151  }
152  data_sources_.clear();
153}
154
155// static
156void ChromeURLDataManagerBackend::Register() {
157  net::URLRequest::RegisterProtocolFactory(
158      chrome::kChromeDevToolsScheme,
159      &ChromeURLDataManagerBackend::Factory);
160  net::URLRequest::RegisterProtocolFactory(
161      chrome::kChromeUIScheme,
162      &ChromeURLDataManagerBackend::Factory);
163}
164
165void ChromeURLDataManagerBackend::AddDataSource(
166    ChromeURLDataManager::DataSource* source) {
167  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
168  DataSourceMap::iterator i = data_sources_.find(source->source_name());
169  if (i != data_sources_.end()) {
170    if (!source->ShouldReplaceExistingSource())
171      return;
172    i->second->backend_ = NULL;
173  }
174  data_sources_[source->source_name()] = source;
175  source->backend_ = this;
176}
177
178bool ChromeURLDataManagerBackend::HasPendingJob(
179    URLRequestChromeJob* job) const {
180  for (PendingRequestMap::const_iterator i = pending_requests_.begin();
181       i != pending_requests_.end(); ++i) {
182    if (i->second == job)
183      return true;
184  }
185  return false;
186}
187
188bool ChromeURLDataManagerBackend::StartRequest(const GURL& url,
189                                               URLRequestChromeJob* job) {
190  // Parse the URL into a request for a source and path.
191  std::string source_name;
192  std::string path;
193  URLToRequest(url, &source_name, &path);
194
195  // Look up the data source for the request.
196  DataSourceMap::iterator i = data_sources_.find(source_name);
197  if (i == data_sources_.end())
198    return false;
199
200  ChromeURLDataManager::DataSource* source = i->second;
201
202  // Save this request so we know where to send the data.
203  RequestID request_id = next_request_id_++;
204  pending_requests_.insert(std::make_pair(request_id, job));
205
206  // TODO(eroman): would be nicer if the mimetype were set at the same time
207  // as the data blob. For now do it here, since NotifyHeadersComplete() is
208  // going to get called once we return.
209  job->SetMimeType(source->GetMimeType(path));
210
211  ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>(
212      job->request()->context());
213
214  // Forward along the request to the data source.
215  MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path);
216  if (!target_message_loop) {
217    // The DataSource is agnostic to which thread StartDataRequest is called
218    // on for this path.  Call directly into it from this thread, the IO
219    // thread.
220    source->StartDataRequest(path, context->is_incognito(), request_id);
221  } else {
222    // The DataSource wants StartDataRequest to be called on a specific thread,
223    // usually the UI thread, for this path.
224    target_message_loop->PostTask(
225        FROM_HERE,
226        NewRunnableMethod(source,
227                          &ChromeURLDataManager::DataSource::StartDataRequest,
228                          path, context->is_incognito(), request_id));
229  }
230  return true;
231}
232
233void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) {
234  // Remove the request from our list of pending requests.
235  // If/when the source sends the data that was requested, the data will just
236  // be thrown away.
237  for (PendingRequestMap::iterator i = pending_requests_.begin();
238       i != pending_requests_.end(); ++i) {
239    if (i->second == job) {
240      pending_requests_.erase(i);
241      return;
242    }
243  }
244}
245
246void ChromeURLDataManagerBackend::DataAvailable(RequestID request_id,
247                                                RefCountedMemory* bytes) {
248  // Forward this data on to the pending net::URLRequest, if it exists.
249  PendingRequestMap::iterator i = pending_requests_.find(request_id);
250  if (i != pending_requests_.end()) {
251    // We acquire a reference to the job so that it doesn't disappear under the
252    // feet of any method invoked here (we could trigger a callback).
253    scoped_refptr<URLRequestChromeJob> job(i->second);
254    pending_requests_.erase(i);
255    job->DataAvailable(bytes);
256  }
257}
258
259// static
260net::URLRequestJob* ChromeURLDataManagerBackend::Factory(
261    net::URLRequest* request,
262    const std::string& scheme) {
263  if (DevToolsJobFactory::ShouldLoadFromDisk()) {
264    // Try loading chrome-devtools:// files from disk.
265    FilePath path;
266    if (DevToolsJobFactory::IsSupportedURL(request->url(), &path))
267      return DevToolsJobFactory::CreateJobForRequest(request, path);
268  }
269
270  // Next check for chrome://view-http-cache/*, which uses its own job type.
271  if (ViewHttpCacheJobFactory::IsSupportedURL(request->url()))
272    return ViewHttpCacheJobFactory::CreateJobForRequest(request);
273
274  // Next check for chrome://appcache-internals/, which uses its own job type.
275  if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url()))
276    return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request);
277
278  // Next check for chrome://blob-internals/, which uses its own job type.
279  if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url()))
280    return ViewBlobInternalsJobFactory::CreateJobForRequest(request);
281
282  // Fall back to using a custom handler
283  return new URLRequestChromeJob(request);
284}
285
286URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request)
287    : net::URLRequestJob(request),
288      data_offset_(0),
289      pending_buf_size_(0),
290      backend_(GetBackend(request)),
291      ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
292}
293
294URLRequestChromeJob::~URLRequestChromeJob() {
295  CHECK(!backend_->HasPendingJob(this));
296}
297
298void URLRequestChromeJob::Start() {
299  // Start reading asynchronously so that all error reporting and data
300  // callbacks happen as they would for network requests.
301  MessageLoop::current()->PostTask(FROM_HERE, method_factory_.NewRunnableMethod(
302      &URLRequestChromeJob::StartAsync));
303}
304
305void URLRequestChromeJob::Kill() {
306  backend_->RemoveRequest(this);
307}
308
309bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const {
310  *mime_type = mime_type_;
311  return !mime_type_.empty();
312}
313
314void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) {
315  DCHECK(!info->headers);
316  // Set the headers so that requests serviced by ChromeURLDataManager return a
317  // status code of 200. Without this they return a 0, which makes the status
318  // indistiguishable from other error types. Instant relies on getting a 200.
319  info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
320}
321
322void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) {
323  if (bytes) {
324    // The request completed, and we have all the data.
325    // Clear any IO pending status.
326    SetStatus(net::URLRequestStatus());
327
328    data_ = bytes;
329    int bytes_read;
330    if (pending_buf_.get()) {
331      CHECK(pending_buf_->data());
332      CompleteRead(pending_buf_, pending_buf_size_, &bytes_read);
333      pending_buf_ = NULL;
334      NotifyReadComplete(bytes_read);
335    }
336  } else {
337    // The request failed.
338    NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
339                                     net::ERR_FAILED));
340  }
341}
342
343bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size,
344                                      int* bytes_read) {
345  if (!data_.get()) {
346    SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
347    DCHECK(!pending_buf_.get());
348    CHECK(buf->data());
349    pending_buf_ = buf;
350    pending_buf_size_ = buf_size;
351    return false;  // Tell the caller we're still waiting for data.
352  }
353
354  // Otherwise, the data is available.
355  CompleteRead(buf, buf_size, bytes_read);
356  return true;
357}
358
359void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size,
360                                       int* bytes_read) {
361  int remaining = static_cast<int>(data_->size()) - data_offset_;
362  if (buf_size > remaining)
363    buf_size = remaining;
364  if (buf_size > 0) {
365    memcpy(buf->data(), data_->front() + data_offset_, buf_size);
366    data_offset_ += buf_size;
367  }
368  *bytes_read = buf_size;
369}
370
371void URLRequestChromeJob::StartAsync() {
372  if (!request_)
373    return;
374
375  if (backend_->StartRequest(request_->url(), this)) {
376    NotifyHeadersComplete();
377  } else {
378    NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
379                                           net::ERR_INVALID_URL));
380  }
381}
382
383URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request,
384                                                 const FilePath& path)
385    : net::URLRequestFileJob(request, path) {
386}
387
388URLRequestChromeFileJob::~URLRequestChromeFileJob() {}
389
390bool DevToolsJobFactory::ShouldLoadFromDisk() {
391#if defined(DEBUG_DEVTOOLS)
392  return true;
393#else
394  return CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugDevTools);
395#endif
396}
397
398bool DevToolsJobFactory::IsSupportedURL(const GURL& url, FilePath* path) {
399  if (!url.SchemeIs(chrome::kChromeDevToolsScheme))
400    return false;
401
402  if (!url.is_valid()) {
403    NOTREACHED();
404    return false;
405  }
406
407  // Remove Query and Ref from URL.
408  GURL stripped_url;
409  GURL::Replacements replacements;
410  replacements.ClearQuery();
411  replacements.ClearRef();
412  stripped_url = url.ReplaceComponents(replacements);
413
414  std::string source_name;
415  std::string relative_path;
416  URLToRequest(stripped_url, &source_name, &relative_path);
417
418  if (source_name != chrome::kChromeUIDevToolsHost)
419    return false;
420
421  // Check that |relative_path| is not an absolute path (otherwise
422  // AppendASCII() will DCHECK).  The awkward use of StringType is because on
423  // some systems FilePath expects a std::string, but on others a std::wstring.
424  FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end()));
425  if (p.IsAbsolute())
426    return false;
427
428  FilePath inspector_dir;
429  if (!PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir))
430    return false;
431
432  *path = inspector_dir.AppendASCII(relative_path);
433  return true;
434}
435
436net::URLRequestJob* DevToolsJobFactory::CreateJobForRequest(
437    net::URLRequest* request, const FilePath& path) {
438  return new URLRequestChromeFileJob(request, path);
439}
440