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

Side by Side Diff: chrome/browser/dom_ui/chrome_url_data_manager_backend.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_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/ref_counted.h"
10 #include "base/synchronization/lock.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 (and
30 // ChromeURLDataManager::DataSource interface and register your handler 27 // during shutdown). In most cases you can use the API in ChromeURLDataManager
31 // with AddDataSource. 28 // and ignore this class.
32 29 //
33 // ChromeURLDataManager lives on the IO thread, so any interfacing with 30 // ChromeURLDataManagerBackend stores the DataSources and is responsible for
34 // it from the UI thread needs to go through an InvokeLater. 31 // mapping URLRequests to DataSources. ChromeURLDataManagerBackend is used on
35 class ChromeURLDataManager { 32 // the IO thread (except during shutdown). ChromeURLDataManager ensures
33 // ChromeURLDataManagerBackend is used on the IO thread.
34 class ChromeURLDataManagerBackend
35 : public base::RefCountedThreadSafe<ChromeURLDataManagerBackend> {
36 public: 36 public:
37 // Returns the singleton instance.
38 static ChromeURLDataManager* GetInstance();
39
40 typedef int RequestID; 37 typedef int RequestID;
41 38
42 // A DataSource is an object that can answer requests for data 39 ChromeURLDataManagerBackend();
43 // asynchronously. DataSources are collectively owned with refcounting smart
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 40
58 // Sent by the DataManager to request data at |path|. The source should 41 // Invoked to register the protocol factories.
59 // call SendResponse() when the data is available or if the request could 42 static void Register();
willchan no longer on Chromium 2011/02/08 04:57:52 You should probably document that this has to be r
sky 2011/02/08 05:35:26 Done.
60 // not be satisfied.
61 virtual void StartDataRequest(const std::string& path,
62 bool is_off_the_record,
63 int request_id) = 0;
64 43
65 // Return the mimetype that should be sent with this response, or empty 44 // Invoked on the UI thread prior to shutdown.
66 // string to specify no mime type. 45 static void PrepareForShutdown();
67 virtual std::string GetMimeType(const std::string& path) const = 0;
68 46
69 // Report that a request has resulted in the data |bytes|. 47 // Finishes shutdown so that the none of the existing
70 // If the request can't be satisfied, pass NULL for |bytes| to indicate 48 // ChromeURLDataManagerBackends reference any DataSources. This is invoked on
71 // the request is over. 49 // the UI thread so that the DataSources can be destroyed on the UI thread.
72 virtual void SendResponse(int request_id, RefCountedMemory* bytes); 50 static void CompleteShutdown();
73 51
74 // Returns the MessageLoop on which the DataSource wishes to have 52 // Adds a DataSource to the collection of data sources.
75 // StartDataRequest called to handle the request for |path|. If the 53 void AddDataSource(ChromeURLDataManager::DataSource* source);
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 54
121 // Add/remove a path from the collection of file sources. 55 // Add/remove a path from the collection of file sources.
122 // A file source acts like a file:// URL to the specified path. 56 // 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 57 // Calling this from threads other the IO thread must be done via
124 // InvokeLater. 58 // InvokeLater.
125 void AddFileSource(const std::string& source_name, const FilePath& path); 59 void AddFileSource(const std::string& source_name, const FilePath& path);
126 void RemoveFileSource(const std::string& source_name); 60
61 // DataSource invokes this. Sends the data to the URLRequest.
62 void DataAvailable(RequestID request_id, RefCountedMemory* bytes);
127 63
128 static net::URLRequestJob* Factory(net::URLRequest* request, 64 static net::URLRequestJob* Factory(net::URLRequest* request,
129 const std::string& scheme); 65 const std::string& scheme);
130 66
131 private: 67 private:
68 friend class base::RefCountedThreadSafe<ChromeURLDataManagerBackend>;
132 friend class URLRequestChromeJob; 69 friend class URLRequestChromeJob;
133 friend struct DefaultSingletonTraits<ChromeURLDataManager>;
134 70
135 ChromeURLDataManager(); 71 ~ChromeURLDataManagerBackend();
136 ~ChromeURLDataManager();
137 72
138 // Parse a URL into the components used to resolve its request. 73 // Parse a URL into the components used to resolve its request.
139 static void URLToRequest(const GURL& url, 74 void URLToRequest(const GURL& url, std::string* source, std::string* path);
140 std::string* source,
141 std::string* path);
142 75
143 // Translate a chrome resource URL into a local file path if there is one. 76 // 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 77 // Returns false if there is no file handler for this URL
145 static bool URLToFilePath(const GURL& url, FilePath* file_path); 78 bool URLToFilePath(const GURL& url, FilePath* file_path);
146 79
147 // Called by the job when it's starting up. 80 // Called by the job when it's starting up.
148 // Returns false if |url| is not a URL managed by this object. 81 // Returns false if |url| is not a URL managed by this object.
149 bool StartRequest(const GURL& url, URLRequestChromeJob* job); 82 bool StartRequest(const GURL& url, URLRequestChromeJob* job);
83
150 // Remove a request from the list of pending requests. 84 // Remove a request from the list of pending requests.
151 void RemoveRequest(URLRequestChromeJob* job); 85 void RemoveRequest(URLRequestChromeJob* job);
152 86
153 // Returns true if the job exists in |pending_requests_|. False otherwise. 87 // Returns true if the job exists in |pending_requests_|. False otherwise.
154 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept 88 // Called by ~URLRequestChromeJob to verify that |pending_requests_| is kept
155 // up to date. 89 // up to date.
156 bool HasPendingJob(URLRequestChromeJob* job) const; 90 bool HasPendingJob(URLRequestChromeJob* job) const;
157 91
158 // Sent by Request::SendResponse. 92 // Resets the |backend_| of each DataSource and clears out |data_sources_|.
159 void DataAvailable(RequestID request_id, 93 // This may be invoked on any thread.
160 scoped_refptr<RefCountedMemory> bytes); 94 void ReleaseDataSources();
161 95
162 // File sources of data, keyed by source name (e.g. "inspector"). 96 // File sources of data, keyed by source name (e.g. "inspector").
163 typedef std::map<std::string, FilePath> FileSourceMap; 97 typedef std::map<std::string, FilePath> FileSourceMap;
164 FileSourceMap file_sources_; 98 FileSourceMap file_sources_;
165 99
166 // Custom sources of data, keyed by source path (e.g. "favicon"). 100 // Custom sources of data, keyed by source path (e.g. "favicon").
167 typedef std::map<std::string, scoped_refptr<DataSource> > DataSourceMap; 101 typedef std::map<std::string,
willchan no longer on Chromium 2011/02/08 04:57:52 I know it's inconvenient to put them further away
sky 2011/02/08 05:35:26 I didn't realize that was part of the style guide.
102 scoped_refptr<ChromeURLDataManager::DataSource> > DataSourceMap;
103 // WARNING: this may be accessed from the IO or UI threads. Access must be
104 // guarded by |data_sources_lock_|.
168 DataSourceMap data_sources_; 105 DataSourceMap data_sources_;
169 106
107 // Lock used to access data sources.
108 base::Lock data_sources_lock_;
109
170 // All pending URLRequestChromeJobs, keyed by ID of the request. 110 // All pending URLRequestChromeJobs, keyed by ID of the request.
171 // URLRequestChromeJob calls into this object when it's constructed and 111 // URLRequestChromeJob calls into this object when it's constructed and
172 // destructed to ensure that the pointers in this map remain valid. 112 // destructed to ensure that the pointers in this map remain valid.
173 typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap; 113 typedef std::map<RequestID, URLRequestChromeJob*> PendingRequestMap;
174 PendingRequestMap pending_requests_; 114 PendingRequestMap pending_requests_;
175 115
176 // The ID we'll use for the next request we receive. 116 // The ID we'll use for the next request we receive.
177 RequestID next_request_id_; 117 RequestID next_request_id_;
118
119 // Lock used to access |instances_| and |shutdown_instances_|.
120 static base::Lock* instances_lock_;
121
122 // ChromeURLDataManagerBackend instances. All access must be done with
123 // |instance_lock_| held. See comment in PrepareForShutdown for details.
124 typedef std::vector<ChromeURLDataManagerBackend*> Instances;
125 static Instances* instances_;
126
127 typedef std::vector<scoped_refptr<ChromeURLDataManagerBackend> >
128 LockedInstances;
129 // If non-null, we're in the process of shuting down. See comment in
130 // PrepareForShutdown for details.
131 static LockedInstances* shutdown_instances_;
132
133 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManagerBackend);
willchan no longer on Chromium 2011/02/08 04:57:52 You need basictypes.h for this header file.
sky 2011/02/08 05:35:26 Done.
178 }; 134 };
179 135
180 // Since we have a single global ChromeURLDataManager, we don't need to 136 #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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698