OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_BACKEND_H_ |
6 #define CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_ | 6 #define CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_BACKEND_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/ref_counted.h" |
| 11 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| 12 |
9 #include <map> | 13 #include <map> |
10 #include <string> | 14 #include <string> |
| 15 #include <vector> |
11 | 16 |
12 #include "base/singleton.h" | |
13 #include "base/task.h" | |
14 #include "base/ref_counted.h" | |
15 #include "chrome/browser/browser_thread.h" | |
16 | |
17 class DictionaryValue; | |
18 class FilePath; | 17 class FilePath; |
19 class GURL; | 18 class GURL; |
20 class MessageLoop; | |
21 class RefCountedMemory; | |
22 class URLRequestChromeJob; | 19 class URLRequestChromeJob; |
23 | 20 |
24 namespace net { | 21 namespace net { |
25 class URLRequest; | 22 class URLRequest; |
26 class URLRequestJob; | 23 class URLRequestJob; |
27 } // namespace net | 24 } |
28 | 25 |
29 // To serve dynamic data off of chrome: URLs, implement the | 26 // ChromeURLDataManagerBackend is used internally by ChromeURLDataManager on the |
30 // ChromeURLDataManager::DataSource interface and register your handler | 27 // IO thread. In most cases you can use the API in ChromeURLDataManager and |
31 // with AddDataSource. | 28 // ignore this class. ChromeURLDataManagerBackend is owned by |
32 | 29 // ChromeURLRequestContext. |
33 // ChromeURLDataManager lives on the IO thread, so any interfacing with | 30 class ChromeURLDataManagerBackend { |
34 // it from the UI thread needs to go through an InvokeLater. | |
35 class ChromeURLDataManager { | |
36 public: | 31 public: |
37 // Returns the singleton instance. | |
38 static ChromeURLDataManager* GetInstance(); | |
39 | |
40 typedef int RequestID; | 32 typedef int RequestID; |
41 | 33 |
42 // A DataSource is an object that can answer requests for data | 34 ChromeURLDataManagerBackend(); |
43 // asynchronously. DataSources are collectively owned with refcounting smart | 35 ~ChromeURLDataManagerBackend(); |
44 // pointers and should never be deleted on the IO thread, since their calls | |
45 // are handled almost always on the UI thread and there's a possibility of a | |
46 // data race. The |DeleteOnUIThread| trait is used to enforce this. | |
47 // | |
48 // An implementation of DataSource should handle calls to | |
49 // StartDataRequest() by starting its (implementation-specific) asynchronous | |
50 // request for the data, then call SendResponse() to notify. | |
51 class DataSource : public base::RefCountedThreadSafe< | |
52 DataSource, BrowserThread::DeleteOnUIThread> { | |
53 public: | |
54 // See source_name_ and message_loop_ below for docs on these parameters. | |
55 DataSource(const std::string& source_name, | |
56 MessageLoop* message_loop); | |
57 | 36 |
58 // Sent by the DataManager to request data at |path|. The source should | 37 // Invoked to register the protocol factories. |
59 // call SendResponse() when the data is available or if the request could | 38 static void Register(); |
60 // not be satisfied. | |
61 virtual void StartDataRequest(const std::string& path, | |
62 bool is_off_the_record, | |
63 int request_id) = 0; | |
64 | 39 |
65 // Return the mimetype that should be sent with this response, or empty | 40 // Adds a DataSource to the collection of data sources. |
66 // string to specify no mime type. | 41 void AddDataSource(ChromeURLDataManager::DataSource* source); |
67 virtual std::string GetMimeType(const std::string& path) const = 0; | |
68 | |
69 // Report that a request has resulted in the data |bytes|. | |
70 // If the request can't be satisfied, pass NULL for |bytes| to indicate | |
71 // the request is over. | |
72 virtual void SendResponse(int request_id, RefCountedMemory* bytes); | |
73 | |
74 // Returns the MessageLoop on which the DataSource wishes to have | |
75 // StartDataRequest called to handle the request for |path|. If the | |
76 // DataSource does not care which thread StartDataRequest is called on, | |
77 // this should return NULL. The default implementation always returns | |
78 // message_loop_, which generally results in processing on the UI thread. | |
79 // It may be beneficial to return NULL for requests that are safe to handle | |
80 // directly on the IO thread. This can improve performance by satisfying | |
81 // such requests more rapidly when there is a large amount of UI thread | |
82 // contention. | |
83 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) | |
84 const; | |
85 | |
86 const std::string& source_name() const { return source_name_; } | |
87 | |
88 static void SetFontAndTextDirection(DictionaryValue* localized_strings); | |
89 | |
90 protected: | |
91 friend class base::RefCountedThreadSafe<DataSource>; | |
92 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | |
93 friend class DeleteTask<DataSource>; | |
94 | |
95 virtual ~DataSource(); | |
96 | |
97 private: | |
98 // The name of this source. | |
99 // E.g., for favicons, this could be "favicon", which results in paths for | |
100 // specific resources like "favicon/34" getting sent to this source. | |
101 const std::string source_name_; | |
102 | |
103 // The MessageLoop for the thread where this DataSource lives. | |
104 // Used to send messages to the DataSource. | |
105 MessageLoop* message_loop_; | |
106 }; | |
107 | |
108 // Add a DataSource to the collection of data sources. | |
109 // Because we don't track users of a given path, we can't know when it's | |
110 // safe to remove them, so the added source effectively leaks. | |
111 // This could be improved in the future but currently the users of this | |
112 // interface are conceptually permanent registration anyway. | |
113 // Adding a second DataSource with the same name clobbers the first. | |
114 // NOTE: Calling this from threads other the IO thread must be done via | |
115 // InvokeLater. | |
116 void AddDataSource(scoped_refptr<DataSource> source); | |
117 // Called during shutdown, before destruction of |BrowserThread|. | |
118 void RemoveDataSourceForTest(const char* source_name); // For unit tests. | |
119 void RemoveAllDataSources(); // For the browser. | |
120 | 42 |
121 // Add/remove a path from the collection of file sources. | 43 // Add/remove a path from the collection of file sources. |
122 // A file source acts like a file:// URL to the specified path. | 44 // A file source acts like a file:// URL to the specified path. |
123 // Calling this from threads other the IO thread must be done via | 45 // Calling this from threads other the IO thread must be done via |
124 // InvokeLater. | 46 // InvokeLater. |
125 void AddFileSource(const std::string& source_name, const FilePath& path); | 47 void AddFileSource(const std::string& source_name, const FilePath& path); |
126 void RemoveFileSource(const std::string& source_name); | 48 |
| 49 // DataSource invokes this. Sends the data to the URLRequest. |
| 50 void DataAvailable(RequestID request_id, RefCountedMemory* bytes); |
127 | 51 |
128 static net::URLRequestJob* Factory(net::URLRequest* request, | 52 static net::URLRequestJob* Factory(net::URLRequest* request, |
129 const std::string& scheme); | 53 const std::string& scheme); |
130 | 54 |
131 private: | 55 private: |
132 friend class URLRequestChromeJob; | 56 friend class URLRequestChromeJob; |
133 friend struct DefaultSingletonTraits<ChromeURLDataManager>; | |
134 | 57 |
135 ChromeURLDataManager(); | 58 typedef std::map<std::string, |
136 ~ChromeURLDataManager(); | 59 scoped_refptr<ChromeURLDataManager::DataSource> > DataSourceMap; |
| 60 typedef std::map<std::string, FilePath> FileSourceMap; |
| 61 typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap; |
137 | 62 |
138 // Parse a URL into the components used to resolve its request. | 63 // Parse a URL into the components used to resolve its request. |
139 static void URLToRequest(const GURL& url, | 64 void URLToRequest(const GURL& url, std::string* source, std::string* path); |
140 std::string* source, | |
141 std::string* path); | |
142 | 65 |
143 // Translate a chrome resource URL into a local file path if there is one. | 66 // Translate a chrome resource URL into a local file path if there is one. |
144 // Returns false if there is no file handler for this URL | 67 // Returns false if there is no file handler for this URL |
145 static bool URLToFilePath(const GURL& url, FilePath* file_path); | 68 bool URLToFilePath(const GURL& url, FilePath* file_path); |
146 | 69 |
147 // Called by the job when it's starting up. | 70 // Called by the job when it's starting up. |
148 // Returns false if |url| is not a URL managed by this object. | 71 // Returns false if |url| is not a URL managed by this object. |
149 bool StartRequest(const GURL& url, URLRequestChromeJob* job); | 72 bool StartRequest(const GURL& url, URLRequestChromeJob* job); |
| 73 |
150 // Remove a request from the list of pending requests. | 74 // Remove a request from the list of pending requests. |
151 void RemoveRequest(URLRequestChromeJob* job); | 75 void RemoveRequest(URLRequestChromeJob* job); |
152 | 76 |
153 // Returns true if the job exists in |pending_requests_|. False otherwise. | 77 // Returns true if the job exists in |pending_requests_|. False otherwise. |
154 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept | 78 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept |
155 // up to date. | 79 // up to date. |
156 bool HasPendingJob(URLRequestChromeJob* job) const; | 80 bool HasPendingJob(URLRequestChromeJob* job) const; |
157 | 81 |
158 // Sent by Request::SendResponse. | |
159 void DataAvailable(RequestID request_id, | |
160 scoped_refptr<RefCountedMemory> bytes); | |
161 | |
162 // File sources of data, keyed by source name (e.g. "inspector"). | 82 // File sources of data, keyed by source name (e.g. "inspector"). |
163 typedef std::map<std::string, FilePath> FileSourceMap; | |
164 FileSourceMap file_sources_; | 83 FileSourceMap file_sources_; |
165 | 84 |
166 // Custom sources of data, keyed by source path (e.g. "favicon"). | 85 // Custom sources of data, keyed by source path (e.g. "favicon"). |
167 typedef std::map<std::string, scoped_refptr<DataSource> > DataSourceMap; | |
168 DataSourceMap data_sources_; | 86 DataSourceMap data_sources_; |
169 | 87 |
170 // All pending URLRequestChromeJobs, keyed by ID of the request. | 88 // All pending URLRequestChromeJobs, keyed by ID of the request. |
171 // URLRequestChromeJob calls into this object when it's constructed and | 89 // URLRequestChromeJob calls into this object when it's constructed and |
172 // destructed to ensure that the pointers in this map remain valid. | 90 // destructed to ensure that the pointers in this map remain valid. |
173 typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap; | |
174 PendingRequestMap pending_requests_; | 91 PendingRequestMap pending_requests_; |
175 | 92 |
176 // The ID we'll use for the next request we receive. | 93 // The ID we'll use for the next request we receive. |
177 RequestID next_request_id_; | 94 RequestID next_request_id_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManagerBackend); |
178 }; | 97 }; |
179 | 98 |
180 // Since we have a single global ChromeURLDataManager, we don't need to | 99 #endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_BACKEND_H_ |
181 // grab a reference to it when creating Tasks involving it. | |
182 DISABLE_RUNNABLE_METHOD_REFCOUNT(ChromeURLDataManager); | |
183 | |
184 // Register our special URL handler under our special URL scheme. | |
185 // Must be done once at startup. | |
186 void RegisterURLRequestChromeJob(); | |
187 | |
188 // Undoes the registration done by RegisterURLRequestChromeJob. | |
189 void UnregisterURLRequestChromeJob(); | |
190 | |
191 #endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_ | |
OLD | NEW |