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_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. DataSources must be added on the UI thread (they are also |
| 32 | 26 // deleted on the UI thread). Internally the DataSources are maintained by |
| 33 // ChromeURLDataManager lives on the IO thread, so any interfacing with | 27 // ChromeURLDataManagerBackend, see if for details. |
|
ahendrickson
2011/02/07 22:57:06
Nit: 'it', not 'if'
sky
2011/02/08 05:35:26
Done.
| |
| 34 // it from the UI thread needs to go through an InvokeLater. | |
| 35 class ChromeURLDataManager { | 28 class ChromeURLDataManager { |
| 36 public: | 29 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 | 30 // A DataSource is an object that can answer requests for data |
| 43 // asynchronously. DataSources are collectively owned with refcounting smart | 31 // asynchronously. DataSources are collectively owned with refcounting smart |
| 44 // pointers and should never be deleted on the IO thread, since their calls | 32 // 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 | 33 // 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. | 34 // data race. The |DeleteOnUIThread| trait is used to enforce this. |
| 47 // | 35 // |
| 48 // An implementation of DataSource should handle calls to | 36 // An implementation of DataSource should handle calls to |
| 49 // StartDataRequest() by starting its (implementation-specific) asynchronous | 37 // StartDataRequest() by starting its (implementation-specific) asynchronous |
| 50 // request for the data, then call SendResponse() to notify. | 38 // request for the data, then call SendResponse() to notify. |
| 51 class DataSource : public base::RefCountedThreadSafe< | 39 class DataSource : public base::RefCountedThreadSafe< |
| 52 DataSource, BrowserThread::DeleteOnUIThread> { | 40 DataSource, BrowserThread::DeleteOnUIThread> { |
| 53 public: | 41 public: |
| 54 // See source_name_ and message_loop_ below for docs on these parameters. | 42 // See source_name_ and message_loop_ below for docs on these parameters. |
| 55 DataSource(const std::string& source_name, | 43 DataSource(const std::string& source_name, MessageLoop* message_loop); |
| 56 MessageLoop* message_loop); | |
| 57 | 44 |
| 58 // Sent by the DataManager to request data at |path|. The source should | 45 // 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 | 46 // call SendResponse() when the data is available or if the request could |
| 60 // not be satisfied. | 47 // not be satisfied. |
| 61 virtual void StartDataRequest(const std::string& path, | 48 virtual void StartDataRequest(const std::string& path, |
| 62 bool is_off_the_record, | 49 bool is_off_the_record, |
| 63 int request_id) = 0; | 50 int request_id) = 0; |
| 64 | 51 |
| 65 // Return the mimetype that should be sent with this response, or empty | 52 // Return the mimetype that should be sent with this response, or empty |
| 66 // string to specify no mime type. | 53 // string to specify no mime type. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 81 // such requests more rapidly when there is a large amount of UI thread | 68 // such requests more rapidly when there is a large amount of UI thread |
| 82 // contention. | 69 // contention. |
| 83 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) | 70 virtual MessageLoop* MessageLoopForRequestPath(const std::string& path) |
| 84 const; | 71 const; |
| 85 | 72 |
| 86 const std::string& source_name() const { return source_name_; } | 73 const std::string& source_name() const { return source_name_; } |
| 87 | 74 |
| 88 static void SetFontAndTextDirection(DictionaryValue* localized_strings); | 75 static void SetFontAndTextDirection(DictionaryValue* localized_strings); |
| 89 | 76 |
| 90 protected: | 77 protected: |
| 91 friend class base::RefCountedThreadSafe<DataSource>; | |
| 92 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | |
| 93 friend class DeleteTask<DataSource>; | |
| 94 | |
| 95 virtual ~DataSource(); | 78 virtual ~DataSource(); |
| 96 | 79 |
| 97 private: | 80 private: |
| 81 friend class base::RefCountedThreadSafe<DataSource>; | |
| 82 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | |
| 83 friend class ChromeURLDataManagerBackend; | |
| 84 friend class DeleteTask<DataSource>; | |
| 85 | |
| 86 // SendResponse invokes this on the IO thread. Notifies the backend to | |
| 87 // handle the actual work of sending the data. | |
| 88 virtual void SendResponseOnIOThread(int request_id, | |
| 89 scoped_refptr<RefCountedMemory> bytes); | |
| 90 | |
| 98 // The name of this source. | 91 // The name of this source. |
| 99 // E.g., for favicons, this could be "favicon", which results in paths for | 92 // E.g., for favicons, this could be "favicon", which results in paths for |
| 100 // specific resources like "favicon/34" getting sent to this source. | 93 // specific resources like "favicon/34" getting sent to this source. |
| 101 const std::string source_name_; | 94 const std::string source_name_; |
| 102 | 95 |
| 103 // The MessageLoop for the thread where this DataSource lives. | 96 // The MessageLoop for the thread where this DataSource lives. |
| 104 // Used to send messages to the DataSource. | 97 // Used to send messages to the DataSource. |
| 105 MessageLoop* message_loop_; | 98 MessageLoop* message_loop_; |
| 99 | |
| 100 // This field is set and maintained by ChromeURLDataManagerBackend. It is | |
| 101 // set when the DataSource is added, and unset if the DataSource is removed. | |
| 102 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend | |
| 103 // is deleted, or another DataSource is registered with the same | |
| 104 // name. backend_ should only be accessed on the IO thread. | |
| 105 // This reference can't be via a scoped_refptr else there would be a cycle | |
| 106 // between the backend and data source. | |
| 107 ChromeURLDataManagerBackend* backend_; | |
| 106 }; | 108 }; |
| 107 | 109 |
| 108 // Add a DataSource to the collection of data sources. | 110 explicit ChromeURLDataManager(Profile* profile); |
| 109 // Because we don't track users of a given path, we can't know when it's | 111 ~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 | 112 |
| 121 // Add/remove a path from the collection of file sources. | 113 // 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. | 114 // on the UI thread. |
| 123 // Calling this from threads other the IO thread must be done via | 115 // |
| 124 // InvokeLater. | 116 // If |AddDataSource| is called more than once for a particular name it will |
| 125 void AddFileSource(const std::string& source_name, const FilePath& path); | 117 // release the old |DataSource|, most likely resulting in it getting deleted |
| 126 void RemoveFileSource(const std::string& source_name); | 118 // as there are no other references to it. |DataSource| uses the |
| 127 | 119 // |DeleteOnUIThread| trait to insure that the destructor is called on the UI |
| 128 static net::URLRequestJob* Factory(net::URLRequest* request, | 120 // thread. This is necessary as some |DataSource|s notably |FileIconSource| |
| 129 const std::string& scheme); | 121 // and |WebUIFavIconSource|, have members that will DCHECK if they are not |
| 122 // destructed in the same thread as they are constructed (the UI thread). | |
| 123 void AddDataSource(DataSource* source); | |
| 130 | 124 |
| 131 private: | 125 private: |
| 132 friend class URLRequestChromeJob; | 126 Profile* profile_; |
| 133 friend struct DefaultSingletonTraits<ChromeURLDataManager>; | |
| 134 | 127 |
| 135 ChromeURLDataManager(); | 128 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 }; | 129 }; |
| 179 | 130 |
| 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_ | 131 #endif // CHROME_BROWSER_DOM_UI_CHROME_URL_DATA_MANAGER_H_ |
| OLD | NEW |