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