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

Side by Side Diff: chrome/browser/dom_ui/chrome_url_data_manager.h

Issue 6286131: Splits ChromeURLDataManager into 2 chunks:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
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_H_
6 #define CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_ 6 #define CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <string> 10 #include <string>
11 11
12 #include "base/singleton.h"
13 #include "base/task.h" 12 #include "base/task.h"
14 #include "base/ref_counted.h" 13 #include "base/ref_counted.h"
15 #include "chrome/browser/browser_thread.h" 14 #include "chrome/browser/browser_thread.h"
16 15
16 class ChromeURLDataManagerBackend;
17 class DictionaryValue; 17 class DictionaryValue;
18 class FilePath; 18 class FilePath;
19 class GURL;
20 class MessageLoop; 19 class MessageLoop;
20 class Profile;
21 class RefCountedMemory; 21 class RefCountedMemory;
22 class URLRequestChromeJob;
23
24 namespace net {
25 class URLRequest;
26 class URLRequestJob;
27 } // namespace net
28 22
29 // To serve dynamic data off of chrome: URLs, implement the 23 // To serve dynamic data off of chrome: URLs, implement the
30 // ChromeURLDataManager::DataSource interface and register your handler 24 // ChromeURLDataManager::DataSource interface and register your handler
31 // with AddDataSource. 25 // with AddDataSource.
Evan Martin 2011/02/07 19:06:21 Can you improve this comment? (It's probably my f
sky 2011/02/07 19:32:06 I added a bit more, but I'm not sure if I'm gettin
32
33 // ChromeURLDataManager lives on the IO thread, so any interfacing with
34 // it from the UI thread needs to go through an InvokeLater.
35 class ChromeURLDataManager { 26 class ChromeURLDataManager {
36 public: 27 public:
37 // Returns the singleton instance.
38 static ChromeURLDataManager* GetInstance();
39
40 typedef int RequestID;
41
42 // A DataSource is an object that can answer requests for data 28 // A DataSource is an object that can answer requests for data
43 // asynchronously. DataSources are collectively owned with refcounting smart 29 // asynchronously. DataSources are collectively owned with refcounting smart
44 // pointers and should never be deleted on the IO thread, since their calls 30 // 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 31 // 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. 32 // data race. The |DeleteOnUIThread| trait is used to enforce this.
47 // 33 //
48 // An implementation of DataSource should handle calls to 34 // An implementation of DataSource should handle calls to
49 // StartDataRequest() by starting its (implementation-specific) asynchronous 35 // StartDataRequest() by starting its (implementation-specific) asynchronous
50 // request for the data, then call SendResponse() to notify. 36 // request for the data, then call SendResponse() to notify.
51 class DataSource : public base::RefCountedThreadSafe< 37 class DataSource : public base::RefCountedThreadSafe<
52 DataSource, BrowserThread::DeleteOnUIThread> { 38 DataSource, BrowserThread::DeleteOnUIThread> {
53 public: 39 public:
54 // See source_name_ and message_loop_ below for docs on these parameters. 40 // See source_name_ and message_loop_ below for docs on these parameters.
55 DataSource(const std::string& source_name, 41 DataSource(const std::string& source_name, MessageLoop* message_loop);
56 MessageLoop* message_loop);
57 42
58 // Sent by the DataManager to request data at |path|. The source should 43 // Sent by the DataManager to request data at |path|. The source should
59 // call SendResponse() when the data is available or if the request could 44 // call SendResponse() when the data is available or if the request could
60 // not be satisfied. 45 // not be satisfied.
61 virtual void StartDataRequest(const std::string& path, 46 virtual void StartDataRequest(const std::string& path,
62 bool is_off_the_record, 47 bool is_off_the_record,
63 int request_id) = 0; 48 int request_id) = 0;
64 49
65 // Return the mimetype that should be sent with this response, or empty 50 // Return the mimetype that should be sent with this response, or empty
66 // string to specify no mime type. 51 // string to specify no mime type.
(...skipping 14 matching lines...) Expand all
81 // such requests more rapidly when there is a large amount of UI thread 66 // such requests more rapidly when there is a large amount of UI thread
82 // contention. 67 // contention.
83 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) 68 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path)
84 const; 69 const;
85 70
86 const std::string& source_name() const { return source_name_; } 71 const std::string& source_name() const { return source_name_; }
87 72
88 static void SetFontAndTextDirection(DictionaryValue* localized_strings); 73 static void SetFontAndTextDirection(DictionaryValue* localized_strings);
89 74
90 protected: 75 protected:
91 friend class base::RefCountedThreadSafe<DataSource>;
92 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
93 friend class DeleteTask<DataSource>;
94
95 virtual ~DataSource(); 76 virtual ~DataSource();
96 77
97 private: 78 private:
79 friend class base::RefCountedThreadSafe<DataSource>;
80 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
81 friend class ChromeURLDataManagerBackend;
82 friend class DeleteTask<DataSource>;
83
84 // SendResponse invokes this on the IO thread. Notifies the backend to
85 // handle the actual work of sending the data.
86 virtual void SendResponseOnIOThread(int request_id,
87 scoped_refptr<RefCountedMemory> bytes);
88
98 // The name of this source. 89 // The name of this source.
99 // E.g., for favicons, this could be "favicon", which results in paths for 90 // E.g., for favicons, this could be "favicon", which results in paths for
100 // specific resources like "favicon/34" getting sent to this source. 91 // specific resources like "favicon/34" getting sent to this source.
101 const std::string source_name_; 92 const std::string source_name_;
102 93
103 // The MessageLoop for the thread where this DataSource lives. 94 // The MessageLoop for the thread where this DataSource lives.
104 // Used to send messages to the DataSource. 95 // Used to send messages to the DataSource.
105 MessageLoop* message_loop_; 96 MessageLoop* message_loop_;
97
98 // This field is set and maintained by ChromeURLDataManagerBackend. It is
99 // set when the DataSource is added, and unset if the DataSource is removed.
100 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend
101 // is deleted, or another DataSource is registered with the same
102 // name. backend_ should only be accessed on the IO thread.
103 // This reference can't be via a scoped_refptr else there would be a cycle
104 // between the backend and data source.
105 ChromeURLDataManagerBackend* backend_;
106 }; 106 };
107 107
108 // Add a DataSource to the collection of data sources. 108 explicit ChromeURLDataManager(Profile* profile);
109 // Because we don't track users of a given path, we can't know when it's 109 ~ChromeURLDataManager();
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 110
121 // Add/remove a path from the collection of file sources. 111 // Adds a DataSource to the collection of data sources. This *must* be invoked
122 // A file source acts like a file:// URL to the specified path. 112 // on the UI thread.
123 // Calling this from threads other the IO thread must be done via 113 //
124 // InvokeLater. 114 // If |AddDataSource| is called more than once for a particular name it will
125 void AddFileSource(const std::string& source_name, const FilePath& path); 115 // release the old |DataSource|, most likely resulting in it getting deleted
126 void RemoveFileSource(const std::string& source_name); 116 // as there are no other references to it. |DataSource| uses the
127 117 // |DeleteOnUIThread| trait to insure that the destructor is called on the UI
128 static net::URLRequestJob* Factory(net::URLRequest* request, 118 // thread. This is necessary as some |DataSource|s notably |FileIconSource|
129 const std::string& scheme); 119 // and |WebUIFavIconSource|, have members that will DCHECK if they are not
120 // destructed in the same thread as they are constructed (the UI thread).
121 void AddDataSource(DataSource* source);
130 122
131 private: 123 private:
132 friend class URLRequestChromeJob; 124 Profile* profile_;
133 friend struct DefaultSingletonTraits<ChromeURLDataManager>;
134 125
135 ChromeURLDataManager(); 126 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager);
136 ~ChromeURLDataManager();
137
138 // Parse a URL into the components used to resolve its request.
139 static void URLToRequest(const GURL& url,
140 std::string* source,
141 std::string* path);
142
143 // 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
145 static bool URLToFilePath(const GURL& url, FilePath* file_path);
146
147 // Called by the job when it's starting up.
148 // Returns false if |url| is not a URL managed by this object.
149 bool StartRequest(const GURL& url, URLRequestChromeJob* job);
150 // Remove a request from the list of pending requests.
151 void RemoveRequest(URLRequestChromeJob* job);
152
153 // Returns true if the job exists in |pending_requests_|. False otherwise.
154 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
155 // up to date.
156 bool HasPendingJob(URLRequestChromeJob* job) const;
157
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").
163 typedef std::map<std::string, FilePath> FileSourceMap;
164 FileSourceMap file_sources_;
165
166 // 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_;
169
170 // All pending URLRequestChromeJobs, keyed by ID of the request.
171 // URLRequestChromeJob calls into this object when it's constructed and
172 // destructed to ensure that the pointers in this map remain valid.
173 typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap;
174 PendingRequestMap pending_requests_;
175
176 // The ID we'll use for the next request we receive.
177 RequestID next_request_id_;
178 }; 127 };
179 128
180 // Since we have a single global ChromeURLDataManager, we don't need to
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_ 129 #endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698