| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/sequenced_task_runner_helpers.h" | 13 #include "base/sequenced_task_runner_helpers.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "chrome/browser/profiles/profile_keyed_service.h" | 15 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 16 | 16 |
| 17 class ChromeURLDataManagerBackend; | 17 class ChromeURLDataManagerBackend; |
| 18 class MessageLoop; | 18 class MessageLoop; |
| 19 class Profile; | 19 class Profile; |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class DictionaryValue; | |
| 23 class RefCountedMemory; | 22 class RefCountedMemory; |
| 24 } | 23 } |
| 25 | 24 |
| 26 namespace content { | 25 namespace content { |
| 27 class URLDataSourceDelegate; | 26 class URLDataSource; |
| 28 } | 27 } |
| 29 | 28 |
| 30 class URLDataSource; | 29 class URLDataSourceImpl; |
| 31 | 30 |
| 32 // To serve dynamic data off of chrome: URLs, implement the | 31 // To serve dynamic data off of chrome: URLs, implement the |
| 33 // ChromeURLDataManager::DataSource interface and register your handler | 32 // ChromeURLDataManager::DataSource interface and register your handler |
| 34 // with AddDataSource. DataSources must be added on the UI thread (they are also | 33 // with AddDataSource. DataSources must be added on the UI thread (they are also |
| 35 // deleted on the UI thread). Internally the DataSources are maintained by | 34 // deleted on the UI thread). Internally the DataSources are maintained by |
| 36 // ChromeURLDataManagerBackend, see it for details. | 35 // ChromeURLDataManagerBackend, see it for details. |
| 37 class ChromeURLDataManager : public ProfileKeyedService { | 36 class ChromeURLDataManager : public ProfileKeyedService { |
| 38 public: | 37 public: |
| 39 explicit ChromeURLDataManager( | 38 explicit ChromeURLDataManager( |
| 40 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend); | 39 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend); |
| 41 virtual ~ChromeURLDataManager(); | 40 virtual ~ChromeURLDataManager(); |
| 42 | 41 |
| 43 // Adds a DataSource to the collection of data sources. This *must* be invoked | 42 // Adds a DataSource to the collection of data sources. This *must* be invoked |
| 44 // on the UI thread. | 43 // on the UI thread. |
| 45 // | 44 // |
| 46 // If |AddDataSource| is called more than once for a particular name it will | 45 // If |AddDataSource| is called more than once for a particular name it will |
| 47 // release the old |DataSource|, most likely resulting in it getting deleted | 46 // release the old |DataSource|, most likely resulting in it getting deleted |
| 48 // as there are no other references to it. |DataSource| uses the | 47 // as there are no other references to it. |DataSource| uses the |
| 49 // |DeleteOnUIThread| trait to insure that the destructor is called on the UI | 48 // |DeleteOnUIThread| trait to insure that the destructor is called on the UI |
| 50 // thread. This is necessary as some |DataSource|s notably |FileIconSource| | 49 // thread. This is necessary as some |DataSource|s notably |FileIconSource| |
| 51 // and |FaviconSource|, have members that will DCHECK if they are not | 50 // and |FaviconSource|, have members that will DCHECK if they are not |
| 52 // destructed in the same thread as they are constructed (the UI thread). | 51 // destructed in the same thread as they are constructed (the UI thread). |
| 53 void AddDataSource(URLDataSource* source); | 52 void AddDataSource(URLDataSourceImpl* source); |
| 54 | 53 |
| 55 // Deletes any data sources no longer referenced. This is normally invoked | 54 // Deletes any data sources no longer referenced. This is normally invoked |
| 56 // for you, but can be invoked to force deletion (such as during shutdown). | 55 // for you, but can be invoked to force deletion (such as during shutdown). |
| 57 static void DeleteDataSources(); | 56 static void DeleteDataSources(); |
| 58 | 57 |
| 59 // Convenience wrapper function to add |source| to |profile|'s | 58 // Convenience wrapper function to add |source| to |profile|'s |
| 60 // |ChromeURLDataManager|. | 59 // |ChromeURLDataManager|. Creates a URLDataSourceImpl to wrap the given |
| 60 // source. |
| 61 static void AddDataSource(Profile* profile, | 61 static void AddDataSource(Profile* profile, |
| 62 content::URLDataSourceDelegate* delegate); | 62 content::URLDataSource* source); |
| 63 |
| 64 // Like above, but takes a URLDataSourceImpl directly. |
| 65 static void AddDataSourceImpl(Profile* profile, |
| 66 URLDataSourceImpl* source); |
| 63 | 67 |
| 64 private: | 68 private: |
| 65 friend class URLDataSource; | 69 friend class URLDataSourceImpl; |
| 66 friend struct DeleteURLDataSource; | 70 friend struct DeleteURLDataSource; |
| 67 typedef std::vector<const URLDataSource*> URLDataSources; | 71 typedef std::vector<const URLDataSourceImpl*> URLDataSources; |
| 68 | 72 |
| 69 // If invoked on the UI thread the DataSource is deleted immediatlye, | 73 // If invoked on the UI thread the DataSource is deleted immediatlye, |
| 70 // otherwise it is added to |data_sources_| and a task is scheduled to handle | 74 // otherwise it is added to |data_sources_| and a task is scheduled to handle |
| 71 // deletion on the UI thread. See note abouve DeleteDataSource for more info. | 75 // deletion on the UI thread. See note abouve DeleteDataSource for more info. |
| 72 static void DeleteDataSource(const URLDataSource* data_source); | 76 static void DeleteDataSource(const URLDataSourceImpl* data_source); |
| 73 | 77 |
| 74 // Returns true if |data_source| is scheduled for deletion (|DeleteDataSource| | 78 // Returns true if |data_source| is scheduled for deletion (|DeleteDataSource| |
| 75 // was invoked). | 79 // was invoked). |
| 76 static bool IsScheduledForDeletion(const URLDataSource* data_source); | 80 static bool IsScheduledForDeletion(const URLDataSourceImpl* data_source); |
| 77 | 81 |
| 78 // A callback that returns the ChromeURLDataManagerBackend. Only accessible on | 82 // A callback that returns the ChromeURLDataManagerBackend. Only accessible on |
| 79 // the IO thread. This is necessary because ChromeURLDataManager is created on | 83 // the IO thread. This is necessary because ChromeURLDataManager is created on |
| 80 // the UI thread, but ChromeURLDataManagerBackend lives on the IO thread. | 84 // the UI thread, but ChromeURLDataManagerBackend lives on the IO thread. |
| 81 const base::Callback<ChromeURLDataManagerBackend*(void)> backend_; | 85 const base::Callback<ChromeURLDataManagerBackend*(void)> backend_; |
| 82 | 86 |
| 83 // |data_sources_| that are no longer referenced and scheduled for deletion. | 87 // |data_sources_| that are no longer referenced and scheduled for deletion. |
| 84 // Protected by g_delete_lock in the .cc file. | 88 // Protected by g_delete_lock in the .cc file. |
| 85 static URLDataSources* data_sources_; | 89 static URLDataSources* data_sources_; |
| 86 | 90 |
| 87 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager); | 91 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager); |
| 88 }; | 92 }; |
| 89 | 93 |
| 90 // Trait used to handle deleting a URLDataSource. Deletion happens on the UI | 94 // Trait used to handle deleting a URLDataSource. Deletion happens on the UI |
| 91 // thread. | 95 // thread. |
| 92 // | 96 // |
| 93 // Implementation note: the normal shutdown sequence is for the UI loop to | 97 // Implementation note: the normal shutdown sequence is for the UI loop to |
| 94 // stop pumping events then the IO loop and thread are stopped. When the | 98 // stop pumping events then the IO loop and thread are stopped. When the |
| 95 // URLDataSources are no longer referenced (which happens when IO thread stops) | 99 // URLDataSources are no longer referenced (which happens when IO thread stops) |
| 96 // they get added to the UI message loop for deletion. But because the UI loop | 100 // they get added to the UI message loop for deletion. But because the UI loop |
| 97 // has stopped by the time this happens the URLDataSources would be leaked. | 101 // has stopped by the time this happens the URLDataSources would be leaked. |
| 98 // | 102 // |
| 99 // To make sure URLDataSources are properly deleted ChromeURLDataManager manages | 103 // To make sure URLDataSources are properly deleted ChromeURLDataManager manages |
| 100 // deletion of the URLDataSources. When a URLDataSource is no longer referenced | 104 // deletion of the URLDataSources. When a URLDataSource is no longer referenced |
| 101 // it is added to |data_sources_| and a task is posted to the UI thread to | 105 // it is added to |data_sources_| and a task is posted to the UI thread to |
| 102 // handle the actual deletion. During shutdown |DeleteDataSources| is invoked so | 106 // handle the actual deletion. During shutdown |DeleteDataSources| is invoked so |
| 103 // that all pending URLDataSources are properly deleted. | 107 // that all pending URLDataSources are properly deleted. |
| 104 struct DeleteURLDataSource { | 108 struct DeleteURLDataSource { |
| 105 static void Destruct(const URLDataSource* data_source) { | 109 static void Destruct(const URLDataSourceImpl* data_source) { |
| 106 ChromeURLDataManager::DeleteDataSource(data_source); | 110 ChromeURLDataManager::DeleteDataSource(data_source); |
| 107 } | 111 } |
| 108 }; | 112 }; |
| 109 | 113 |
| 110 // A URLDataSource is an object that can answer requests for data | 114 // A URLDataSource is an object that can answer requests for data |
| 111 // asynchronously. URLDataSources are collectively owned with refcounting smart | 115 // asynchronously. URLDataSources are collectively owned with refcounting smart |
| 112 // pointers and should never be deleted on the IO thread, since their calls | 116 // pointers and should never be deleted on the IO thread, since their calls |
| 113 // are handled almost always on the UI thread and there's a possibility of a | 117 // are handled almost always on the UI thread and there's a possibility of a |
| 114 // data race. The |DeleteDataSource| trait above is used to enforce this. | 118 // data race. The |DeleteDataSource| trait above is used to enforce this. |
| 115 // | 119 class URLDataSourceImpl : public base::RefCountedThreadSafe< |
| 116 // A delegate of URLDataSource should handle calls to StartDataRequest() by | 120 URLDataSourceImpl, DeleteURLDataSource> { |
| 117 // starting its (implementation-specific) asynchronous request for the data, | |
| 118 // then call SendResponse() to notify. | |
| 119 class URLDataSource : public base::RefCountedThreadSafe< | |
| 120 URLDataSource, DeleteURLDataSource> { | |
| 121 public: | 121 public: |
| 122 // See source_name_ below for docs on that parameter. Takes ownership of | 122 // See source_name_ below for docs on that parameter. Takes ownership of |
| 123 // |delegate|. | 123 // |source|. |
| 124 URLDataSource(const std::string& source_name, | 124 URLDataSourceImpl(const std::string& source_name, |
| 125 content::URLDataSourceDelegate* delegate); | 125 content::URLDataSource* source); |
| 126 | 126 |
| 127 // Report that a request has resulted in the data |bytes|. | 127 // Report that a request has resulted in the data |bytes|. |
| 128 // If the request can't be satisfied, pass NULL for |bytes| to indicate | 128 // If the request can't be satisfied, pass NULL for |bytes| to indicate |
| 129 // the request is over. | 129 // the request is over. |
| 130 virtual void SendResponse(int request_id, base::RefCountedMemory* bytes); | 130 virtual void SendResponse(int request_id, base::RefCountedMemory* bytes); |
| 131 | 131 |
| 132 const std::string& source_name() const { return source_name_; } | 132 const std::string& source_name() const { return source_name_; } |
| 133 content::URLDataSourceDelegate* delegate() const { return delegate_.get(); } | 133 content::URLDataSource* source() const { return source_.get(); } |
| 134 | |
| 135 static void SetFontAndTextDirection( | |
| 136 base::DictionaryValue* localized_strings); | |
| 137 | 134 |
| 138 protected: | 135 protected: |
| 139 virtual ~URLDataSource(); | 136 virtual ~URLDataSourceImpl(); |
| 140 | |
| 141 content::URLDataSourceDelegate* release_delegate() { | |
| 142 return delegate_.release(); | |
| 143 } | |
| 144 | 137 |
| 145 private: | 138 private: |
| 146 friend class ChromeURLDataManagerBackend; | 139 friend class ChromeURLDataManagerBackend; |
| 147 friend class ChromeURLDataManager; | 140 friend class ChromeURLDataManager; |
| 148 friend class base::DeleteHelper<URLDataSource>; | 141 friend class base::DeleteHelper<URLDataSourceImpl>; |
| 149 | 142 |
| 150 // SendResponse invokes this on the IO thread. Notifies the backend to | 143 // SendResponse invokes this on the IO thread. Notifies the backend to |
| 151 // handle the actual work of sending the data. | 144 // handle the actual work of sending the data. |
| 152 virtual void SendResponseOnIOThread( | 145 virtual void SendResponseOnIOThread( |
| 153 int request_id, | 146 int request_id, |
| 154 scoped_refptr<base::RefCountedMemory> bytes); | 147 scoped_refptr<base::RefCountedMemory> bytes); |
| 155 | 148 |
| 156 // The name of this source. | 149 // The name of this source. |
| 157 // E.g., for favicons, this could be "favicon", which results in paths for | 150 // E.g., for favicons, this could be "favicon", which results in paths for |
| 158 // specific resources like "favicon/34" getting sent to this source. | 151 // specific resources like "favicon/34" getting sent to this source. |
| 159 const std::string source_name_; | 152 const std::string source_name_; |
| 160 | 153 |
| 161 // This field is set and maintained by ChromeURLDataManagerBackend. It is | 154 // This field is set and maintained by ChromeURLDataManagerBackend. It is |
| 162 // set when the DataSource is added, and unset if the DataSource is removed. | 155 // set when the DataSource is added, and unset if the DataSource is removed. |
| 163 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend | 156 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend |
| 164 // is deleted, or another DataSource is registered with the same | 157 // is deleted, or another DataSource is registered with the same |
| 165 // name. backend_ should only be accessed on the IO thread. | 158 // name. backend_ should only be accessed on the IO thread. |
| 166 // This reference can't be via a scoped_refptr else there would be a cycle | 159 // This reference can't be via a scoped_refptr else there would be a cycle |
| 167 // between the backend and data source. | 160 // between the backend and data source. |
| 168 ChromeURLDataManagerBackend* backend_; | 161 ChromeURLDataManagerBackend* backend_; |
| 169 | 162 |
| 170 scoped_ptr<content::URLDataSourceDelegate> delegate_; | 163 scoped_ptr<content::URLDataSource> source_; |
| 171 }; | 164 }; |
| 172 | 165 |
| 173 #endif // CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | 166 #endif // CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ |
| OLD | NEW |