| 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 CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | 6 #define CONTENT_BROWSER_WEBUI_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" | |
| 12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.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 "content/common/content_export.h" |
| 16 | 16 |
| 17 class ChromeURLDataManagerBackend; | 17 class ChromeURLDataManagerBackend; |
| 18 class MessageLoop; | 18 class MessageLoop; |
| 19 class Profile; | |
| 20 | 19 |
| 21 namespace base { | 20 namespace base { |
| 22 class RefCountedMemory; | 21 class RefCountedMemory; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace content { | 24 namespace content { |
| 25 class BrowserContext; |
| 26 class URLDataSource; | 26 class URLDataSource; |
| 27 class WebUIDataSource; | 27 class WebUIDataSource; |
| 28 } | 28 } |
| 29 | 29 |
| 30 class URLDataSourceImpl; | 30 class URLDataSourceImpl; |
| 31 | 31 |
| 32 // To serve dynamic data off of chrome: URLs, implement the | 32 // To serve dynamic data off of chrome: URLs, implement the |
| 33 // ChromeURLDataManager::DataSource interface and register your handler | 33 // ChromeURLDataManager::DataSource interface and register your handler |
| 34 // with AddDataSource. DataSources must be added on the UI thread (they are also | 34 // 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 | 35 // deleted on the UI thread). Internally the DataSources are maintained by |
| 36 // ChromeURLDataManagerBackend, see it for details. | 36 // ChromeURLDataManagerBackend, see it for details. |
| 37 class ChromeURLDataManager : public ProfileKeyedService { | 37 class ChromeURLDataManager { |
| 38 public: | 38 public: |
| 39 explicit ChromeURLDataManager( | 39 explicit ChromeURLDataManager(content::BrowserContext* browser_context); |
| 40 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(URLDataSourceImpl* 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 |browser_context|'s |
| 60 // |ChromeURLDataManager|. Creates a URLDataSourceImpl to wrap the given | 59 // |ChromeURLDataManager|. Creates a URLDataSourceImpl to wrap the given |
| 61 // source. | 60 // source. |
| 62 static void AddDataSource(Profile* profile, | 61 CONTENT_EXPORT static void AddDataSource( |
| 63 content::URLDataSource* source); | 62 content::BrowserContext* browser_context, |
| 63 content::URLDataSource* source); |
| 64 | 64 |
| 65 // Adds a WebUI data source to |profile|'s |ChromeURLDataManager|. | 65 // Adds a WebUI data source to |browser_context|'s |ChromeURLDataManager|. |
| 66 static void AddWebUIDataSource(Profile* profile, | 66 CONTENT_EXPORT static void AddWebUIDataSource( |
| 67 content::WebUIDataSource* source); | 67 content::BrowserContext* browser_context, |
| 68 content::WebUIDataSource* source); |
| 68 | 69 |
| 69 private: | 70 private: |
| 70 friend class URLDataSourceImpl; | 71 friend class URLDataSourceImpl; |
| 71 friend struct DeleteURLDataSource; | 72 friend struct DeleteURLDataSource; |
| 72 typedef std::vector<const URLDataSourceImpl*> URLDataSources; | 73 typedef std::vector<const URLDataSourceImpl*> URLDataSources; |
| 73 | 74 |
| 74 // If invoked on the UI thread the DataSource is deleted immediatlye, | 75 // If invoked on the UI thread the DataSource is deleted immediatlye, |
| 75 // otherwise it is added to |data_sources_| and a task is scheduled to handle | 76 // otherwise it is added to |data_sources_| and a task is scheduled to handle |
| 76 // deletion on the UI thread. See note abouve DeleteDataSource for more info. | 77 // deletion on the UI thread. See note abouve DeleteDataSource for more info. |
| 77 static void DeleteDataSource(const URLDataSourceImpl* data_source); | 78 static void DeleteDataSource(const URLDataSourceImpl* data_source); |
| 78 | 79 |
| 79 // Returns true if |data_source| is scheduled for deletion (|DeleteDataSource| | 80 // Returns true if |data_source| is scheduled for deletion (|DeleteDataSource| |
| 80 // was invoked). | 81 // was invoked). |
| 81 static bool IsScheduledForDeletion(const URLDataSourceImpl* data_source); | 82 static bool IsScheduledForDeletion(const URLDataSourceImpl* data_source); |
| 82 | 83 |
| 83 // A callback that returns the ChromeURLDataManagerBackend. Only accessible on | 84 content::BrowserContext* browser_context_; |
| 84 // the IO thread. This is necessary because ChromeURLDataManager is created on | |
| 85 // the UI thread, but ChromeURLDataManagerBackend lives on the IO thread. | |
| 86 const base::Callback<ChromeURLDataManagerBackend*(void)> backend_; | |
| 87 | 85 |
| 88 // |data_sources_| that are no longer referenced and scheduled for deletion. | 86 // |data_sources_| that are no longer referenced and scheduled for deletion. |
| 89 // Protected by g_delete_lock in the .cc file. | 87 // Protected by g_delete_lock in the .cc file. |
| 90 static URLDataSources* data_sources_; | 88 static URLDataSources* data_sources_; |
| 91 | 89 |
| 92 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager); | 90 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager); |
| 93 }; | 91 }; |
| 94 | 92 |
| 95 // Trait used to handle deleting a URLDataSource. Deletion happens on the UI | 93 // Trait used to handle deleting a URLDataSource. Deletion happens on the UI |
| 96 // thread. | 94 // thread. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend | 155 // A DataSource can be removed in two ways: the ChromeURLDataManagerBackend |
| 158 // is deleted, or another DataSource is registered with the same | 156 // is deleted, or another DataSource is registered with the same |
| 159 // name. backend_ should only be accessed on the IO thread. | 157 // name. backend_ should only be accessed on the IO thread. |
| 160 // This reference can't be via a scoped_refptr else there would be a cycle | 158 // This reference can't be via a scoped_refptr else there would be a cycle |
| 161 // between the backend and data source. | 159 // between the backend and data source. |
| 162 ChromeURLDataManagerBackend* backend_; | 160 ChromeURLDataManagerBackend* backend_; |
| 163 | 161 |
| 164 scoped_ptr<content::URLDataSource> source_; | 162 scoped_ptr<content::URLDataSource> source_; |
| 165 }; | 163 }; |
| 166 | 164 |
| 167 #endif // CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | 165 #endif // CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_H_ |
| OLD | NEW |