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