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

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

Issue 6461024: Revert 74292 - Splits ChromeURLDataManager into 2 chunks:... (Closed) Base URL: svn://svn.chromium.org/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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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"
12 #include "base/task.h" 13 #include "base/task.h"
13 #include "base/ref_counted.h" 14 #include "base/ref_counted.h"
14 #include "chrome/browser/browser_thread.h" 15 #include "chrome/browser/browser_thread.h"
15 16
16 class ChromeURLDataManagerBackend;
17 class DictionaryValue; 17 class DictionaryValue;
18 class FilePath; 18 class FilePath;
19 class GURL;
19 class MessageLoop; 20 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
22 28
23 // To serve dynamic data off of chrome: URLs, implement the 29 // To serve dynamic data off of chrome: URLs, implement the
24 // ChromeURLDataManager::DataSource interface and register your handler 30 // ChromeURLDataManager::DataSource interface and register your handler
25 // with AddDataSource. DataSources must be added on the UI thread (they are also 31 // with AddDataSource.
26 // deleted on the UI thread). Internally the DataSources are maintained by 32
27 // ChromeURLDataManagerBackend, see it for details. 33 // ChromeURLDataManager lives on the IO thread, so any interfacing with
34 // it from the UI thread needs to go through an InvokeLater.
28 class ChromeURLDataManager { 35 class ChromeURLDataManager {
29 public: 36 public:
30 class DataSource; 37 // Returns the singleton instance.
38 static ChromeURLDataManager* GetInstance();
31 39
32 // Trait used to handle deleting a DataSource. Deletion happens on the UI 40 typedef int RequestID;
33 // thread.
34 //
35 // Implementation note: the normal shutdown sequence is for the UI loop to
36 // stop pumping events then the IO loop and thread are stopped. When the
37 // DataSources are no longer referenced (which happens when IO thread stops)
38 // they get added to the UI message loop for deletion. But because the UI loop
39 // has stopped by the time this happens the DataSources would be leaked.
40 //
41 // To make sure DataSources are properly deleted ChromeURLDataManager manages
42 // deletion of the DataSources. When a DataSource is no longer referenced it
43 // is added to |data_sources_| and a task is posted to the UI thread to handle
44 // the actual deletion. During shutdown |DeleteDataSources| is invoked so that
45 // all pending DataSources are properly deleted.
46 struct DeleteDataSource {
47 static void Destruct(const DataSource* data_source) {
48 ChromeURLDataManager::DeleteDataSource(data_source);
49 }
50 };
51 41
52 // A DataSource is an object that can answer requests for data 42 // A DataSource is an object that can answer requests for data
53 // asynchronously. DataSources are collectively owned with refcounting smart 43 // asynchronously. DataSources are collectively owned with refcounting smart
54 // pointers and should never be deleted on the IO thread, since their calls 44 // pointers and should never be deleted on the IO thread, since their calls
55 // are handled almost always on the UI thread and there's a possibility of a 45 // are handled almost always on the UI thread and there's a possibility of a
56 // data race. The |DeleteOnUIThread| trait is used to enforce this. 46 // data race. The |DeleteOnUIThread| trait is used to enforce this.
57 // 47 //
58 // An implementation of DataSource should handle calls to 48 // An implementation of DataSource should handle calls to
59 // StartDataRequest() by starting its (implementation-specific) asynchronous 49 // StartDataRequest() by starting its (implementation-specific) asynchronous
60 // request for the data, then call SendResponse() to notify. 50 // request for the data, then call SendResponse() to notify.
61 class DataSource : public base::RefCountedThreadSafe< 51 class DataSource : public base::RefCountedThreadSafe<
62 DataSource, DeleteDataSource> { 52 DataSource, BrowserThread::DeleteOnUIThread> {
63 public: 53 public:
64 // See source_name_ and message_loop_ below for docs on these parameters. 54 // See source_name_ and message_loop_ below for docs on these parameters.
65 DataSource(const std::string& source_name, MessageLoop* message_loop); 55 DataSource(const std::string& source_name,
56 MessageLoop* message_loop);
66 57
67 // Sent by the DataManager to request data at |path|. The source should 58 // Sent by the DataManager to request data at |path|. The source should
68 // call SendResponse() when the data is available or if the request could 59 // call SendResponse() when the data is available or if the request could
69 // not be satisfied. 60 // not be satisfied.
70 virtual void StartDataRequest(const std::string& path, 61 virtual void StartDataRequest(const std::string& path,
71 bool is_off_the_record, 62 bool is_off_the_record,
72 int request_id) = 0; 63 int request_id) = 0;
73 64
74 // Return the mimetype that should be sent with this response, or empty 65 // Return the mimetype that should be sent with this response, or empty
75 // string to specify no mime type. 66 // string to specify no mime type.
(...skipping 14 matching lines...) Expand all
90 // such requests more rapidly when there is a large amount of UI thread 81 // such requests more rapidly when there is a large amount of UI thread
91 // contention. 82 // contention.
92 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) 83 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path)
93 const; 84 const;
94 85
95 const std::string& source_name() const { return source_name_; } 86 const std::string& source_name() const { return source_name_; }
96 87
97 static void SetFontAndTextDirection(DictionaryValue* localized_strings); 88 static void SetFontAndTextDirection(DictionaryValue* localized_strings);
98 89
99 protected: 90 protected:
91 friend class base::RefCountedThreadSafe<DataSource>;
92 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
93 friend class DeleteTask<DataSource>;
94
100 virtual ~DataSource(); 95 virtual ~DataSource();
101 96
102 private: 97 private:
103 friend class ChromeURLDataManagerBackend;
104 friend class ChromeURLDataManager;
105 friend class DeleteTask<DataSource>;
106
107 // SendResponse invokes this on the IO thread. Notifies the backend to
108 // handle the actual work of sending the data.
109 virtual void SendResponseOnIOThread(int request_id,
110 scoped_refptr<RefCountedMemory> bytes);
111
112 // The name of this source. 98 // The name of this source.
113 // E.g., for favicons, this could be "favicon", which results in paths for 99 // E.g., for favicons, this could be "favicon", which results in paths for
114 // specific resources like "favicon/34" getting sent to this source. 100 // specific resources like "favicon/34" getting sent to this source.
115 const std::string source_name_; 101 const std::string source_name_;
116 102
117 // The MessageLoop for the thread where this DataSource lives. 103 // The MessageLoop for the thread where this DataSource lives.
118 // Used to send messages to the DataSource. 104 // Used to send messages to the DataSource.
119 MessageLoop* message_loop_; 105 MessageLoop* message_loop_;
120
121 // This field is set and maintained by ChromeURLDataManagerBackend. It is
122 // set when the DataSource is added, and unset if the DataSource is removed.
123 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend
124 // is deleted, or another DataSource is registered with the same
125 // name. backend_ should only be accessed on the IO thread.
126 // This reference can't be via a scoped_refptr else there would be a cycle
127 // between the backend and data source.
128 ChromeURLDataManagerBackend* backend_;
129 }; 106 };
130 107
131 explicit ChromeURLDataManager(Profile* profile); 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
121 // Add/remove a path from the collection of file sources.
122 // 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
124 // InvokeLater.
125 void AddFileSource(const std::string& source_name, const FilePath& path);
126 void RemoveFileSource(const std::string& source_name);
127
128 static net::URLRequestJob* Factory(net::URLRequest* request,
129 const std::string& scheme);
130
131 private:
132 friend class URLRequestChromeJob;
133 friend struct DefaultSingletonTraits<ChromeURLDataManager>;
134
135 ChromeURLDataManager();
132 ~ChromeURLDataManager(); 136 ~ChromeURLDataManager();
133 137
134 // Adds a DataSource to the collection of data sources. This *must* be invoked 138 // Parse a URL into the components used to resolve its request.
135 // on the UI thread. 139 static void URLToRequest(const GURL& url,
136 // 140 std::string* source,
137 // If |AddDataSource| is called more than once for a particular name it will 141 std::string* path);
138 // release the old |DataSource|, most likely resulting in it getting deleted
139 // as there are no other references to it. |DataSource| uses the
140 // |DeleteOnUIThread| trait to insure that the destructor is called on the UI
141 // thread. This is necessary as some |DataSource|s notably |FileIconSource|
142 // and |WebUIFavIconSource|, have members that will DCHECK if they are not
143 // destructed in the same thread as they are constructed (the UI thread).
144 void AddDataSource(DataSource* source);
145 142
146 // Deletes any data sources no longer referenced. This is normally invoked 143 // Translate a chrome resource URL into a local file path if there is one.
147 // for you, but can be invoked to force deletion (such as during shutdown). 144 // Returns false if there is no file handler for this URL
148 static void DeleteDataSources(); 145 static bool URLToFilePath(const GURL& url, FilePath* file_path);
149 146
150 private: 147 // Called by the job when it's starting up.
151 typedef std::vector<const ChromeURLDataManager::DataSource*> DataSources; 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 152
153 // If invoked on the UI thread the DataSource is deleted immediatlye, 153 // Returns true if the job exists in |pending_requests_|. False otherwise.
154 // otherwise it is added to |data_sources_| and a task is scheduled to handle 154 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
155 // deletion on the UI thread. See note abouve DeleteDataSource for more info. 155 // up to date.
156 static void DeleteDataSource(const DataSource* data_source); 156 bool HasPendingJob(URLRequestChromeJob* job) const;
157 157
158 Profile* profile_; 158 // Sent by Request::SendResponse.
159 void DataAvailable(RequestID request_id,
160 scoped_refptr<RefCountedMemory> bytes);
159 161
160 // Lock used when accessing |data_sources_|. 162 // File sources of data, keyed by source name (e.g. "inspector").
161 static base::Lock delete_lock_; 163 typedef std::map<std::string, FilePath> FileSourceMap;
164 FileSourceMap file_sources_;
162 165
163 // |data_sources_| that are no longer referenced and scheduled for deletion. 166 // Custom sources of data, keyed by source path (e.g. "favicon").
164 static DataSources* data_sources_; 167 typedef std::map<std::string, scoped_refptr<DataSource> > DataSourceMap;
168 DataSourceMap data_sources_;
165 169
166 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager); 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_;
167 }; 178 };
168 179
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
169 #endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_ 191 #endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/dom_ui/bug_report_ui.cc ('k') | chrome/browser/dom_ui/chrome_url_data_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698