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