chrome_url_data_manager_backend.h revision dc0f95d653279beabeb9817299e2902918ba123e
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#ifndef CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_BACKEND_H_
6#define CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_BACKEND_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/ref_counted.h"
11#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
12
13#include <map>
14#include <string>
15#include <vector>
16
17class FilePath;
18class GURL;
19class URLRequestChromeJob;
20
21namespace net {
22class URLRequest;
23class URLRequestJob;
24}
25
26// ChromeURLDataManagerBackend is used internally by ChromeURLDataManager on the
27// IO thread. In most cases you can use the API in ChromeURLDataManager and
28// ignore this class. ChromeURLDataManagerBackend is owned by
29// ChromeURLRequestContext.
30class ChromeURLDataManagerBackend {
31 public:
32  typedef int RequestID;
33
34  ChromeURLDataManagerBackend();
35  ~ChromeURLDataManagerBackend();
36
37  // Invoked to register the protocol factories.
38  static void Register();
39
40  // Adds a DataSource to the collection of data sources.
41  void AddDataSource(ChromeURLDataManager::DataSource* source);
42
43  // Add/remove a path from the collection of file sources.
44  // A file source acts like a file:// URL to the specified path.
45  // Calling this from threads other the IO thread must be done via
46  // InvokeLater.
47  void AddFileSource(const std::string& source_name, const FilePath& path);
48
49  // DataSource invokes this. Sends the data to the URLRequest.
50  void DataAvailable(RequestID request_id, RefCountedMemory* bytes);
51
52  static net::URLRequestJob* Factory(net::URLRequest* request,
53                                     const std::string& scheme);
54
55 private:
56  friend class URLRequestChromeJob;
57
58  typedef std::map<std::string,
59      scoped_refptr<ChromeURLDataManager::DataSource> > DataSourceMap;
60  typedef std::map<std::string, FilePath> FileSourceMap;
61  typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap;
62
63  // Parse a URL into the components used to resolve its request.
64  void URLToRequest(const GURL& url, std::string* source, std::string* path);
65
66  // Translate a chrome resource URL into a local file path if there is one.
67  // Returns false if there is no file handler for this URL
68  bool URLToFilePath(const GURL& url, FilePath* file_path);
69
70  // Called by the job when it's starting up.
71  // Returns false if |url| is not a URL managed by this object.
72  bool StartRequest(const GURL& url, URLRequestChromeJob* job);
73
74  // Remove a request from the list of pending requests.
75  void RemoveRequest(URLRequestChromeJob* job);
76
77  // Returns true if the job exists in |pending_requests_|. False otherwise.
78  // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
79  // up to date.
80  bool HasPendingJob(URLRequestChromeJob* job) const;
81
82  // File sources of data, keyed by source name (e.g. "inspector").
83  FileSourceMap file_sources_;
84
85  // Custom sources of data, keyed by source path (e.g. "favicon").
86  DataSourceMap data_sources_;
87
88  // All pending URLRequestChromeJobs, keyed by ID of the request.
89  // URLRequestChromeJob calls into this object when it's constructed and
90  // destructed to ensure that the pointers in this map remain valid.
91  PendingRequestMap pending_requests_;
92
93  // The ID we'll use for the next request we receive.
94  RequestID next_request_id_;
95
96  DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManagerBackend);
97};
98
99#endif  // CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_BACKEND_H_
100