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 // temporary forwarding header |
6 #define CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | 6 #include "content/browser/webui/url_data_manager.h" |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/sequenced_task_runner_helpers.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "chrome/browser/profiles/profile_keyed_service.h" | |
16 | |
17 class ChromeURLDataManagerBackend; | |
18 class MessageLoop; | |
19 class Profile; | |
20 | |
21 namespace base { | |
22 class RefCountedMemory; | |
23 } | |
24 | |
25 namespace content { | |
26 class URLDataSource; | |
27 class WebUIDataSource; | |
28 } | |
29 | |
30 class URLDataSourceImpl; | |
31 | |
32 // To serve dynamic data off of chrome: URLs, implement the | |
33 // ChromeURLDataManager::DataSource interface and register your handler | |
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 | |
36 // ChromeURLDataManagerBackend, see it for details. | |
37 class ChromeURLDataManager : public ProfileKeyedService { | |
38 public: | |
39 explicit ChromeURLDataManager( | |
40 const base::Callback<ChromeURLDataManagerBackend*(void)>& backend); | |
41 virtual ~ChromeURLDataManager(); | |
42 | |
43 // Adds a DataSource to the collection of data sources. This *must* be invoked | |
44 // on the UI thread. | |
45 // | |
46 // 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 | |
48 // as there are no other references to it. |DataSource| uses the | |
49 // |DeleteOnUIThread| trait to insure that the destructor is called on the UI | |
50 // thread. This is necessary as some |DataSource|s notably |FileIconSource| | |
51 // and |FaviconSource|, have members that will DCHECK if they are not | |
52 // destructed in the same thread as they are constructed (the UI thread). | |
53 void AddDataSource(URLDataSourceImpl* source); | |
54 | |
55 // 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). | |
57 static void DeleteDataSources(); | |
58 | |
59 // Convenience wrapper function to add |source| to |profile|'s | |
60 // |ChromeURLDataManager|. Creates a URLDataSourceImpl to wrap the given | |
61 // source. | |
62 static void AddDataSource(Profile* profile, | |
63 content::URLDataSource* source); | |
64 | |
65 // Adds a WebUI data source to |profile|'s |ChromeURLDataManager|. | |
66 static void AddWebUIDataSource(Profile* profile, | |
67 content::WebUIDataSource* source); | |
68 | |
69 private: | |
70 friend class URLDataSourceImpl; | |
71 friend struct DeleteURLDataSource; | |
72 typedef std::vector<const URLDataSourceImpl*> URLDataSources; | |
73 | |
74 // 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 // deletion on the UI thread. See note abouve DeleteDataSource for more info. | |
77 static void DeleteDataSource(const URLDataSourceImpl* data_source); | |
78 | |
79 // Returns true if |data_source| is scheduled for deletion (|DeleteDataSource| | |
80 // was invoked). | |
81 static bool IsScheduledForDeletion(const URLDataSourceImpl* data_source); | |
82 | |
83 // A callback that returns the ChromeURLDataManagerBackend. Only accessible on | |
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 | |
88 // |data_sources_| that are no longer referenced and scheduled for deletion. | |
89 // Protected by g_delete_lock in the .cc file. | |
90 static URLDataSources* data_sources_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(ChromeURLDataManager); | |
93 }; | |
94 | |
95 // Trait used to handle deleting a URLDataSource. Deletion happens on the UI | |
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 | |
167 #endif // CHROME_BROWSER_UI_WEBUI_CHROME_URL_DATA_MANAGER_H_ | |
OLD | NEW |