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 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 5 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
17 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 18 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
18 #include "chrome/browser/media_galleries/gallery_watch_manager.h" | 19 #include "chrome/browser/media_galleries/gallery_watch_manager.h" |
19 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 20 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
20 #include "chrome/browser/media_galleries/media_file_system_context.h" | 21 #include "chrome/browser/media_galleries/media_file_system_context.h" |
21 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 22 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
22 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 23 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
23 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" | 24 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // back informs the caller. | 88 // back informs the caller. |
88 class RPHReferenceManager { | 89 class RPHReferenceManager { |
89 public: | 90 public: |
90 // |no_references_callback| is called when the last WebContents reference | 91 // |no_references_callback| is called when the last WebContents reference |
91 // goes away. WebContents references are added through | 92 // goes away. WebContents references are added through |
92 // ReferenceFromWebContents(). | 93 // ReferenceFromWebContents(). |
93 explicit RPHReferenceManager(const base::Closure& no_references_callback); | 94 explicit RPHReferenceManager(const base::Closure& no_references_callback); |
94 virtual ~RPHReferenceManager(); | 95 virtual ~RPHReferenceManager(); |
95 | 96 |
96 // Remove all references, but don't call |no_references_callback|. | 97 // Remove all references, but don't call |no_references_callback|. |
97 void Reset() { base::STLDeleteValues(&observer_map_); } | 98 void Reset() { observer_map_.clear(); } |
98 | 99 |
99 // Returns true if there are no references; | 100 // Returns true if there are no references. |
100 bool empty() const { return observer_map_.empty(); } | 101 bool empty() const { return observer_map_.empty(); } |
101 | 102 |
102 // Adds a reference to the passed |contents|. Calling this multiple times with | 103 // Adds a reference to the passed |contents|. Calling this multiple times with |
103 // the same |contents| is a no-op. | 104 // the same |contents| is a no-op. |
104 void ReferenceFromWebContents(content::WebContents* contents); | 105 void ReferenceFromWebContents(content::WebContents* contents); |
105 | 106 |
106 private: | 107 private: |
107 class RPHWebContentsObserver : public content::WebContentsObserver { | 108 class RPHWebContentsObserver : public content::WebContentsObserver { |
108 public: | 109 public: |
109 RPHWebContentsObserver(RPHReferenceManager* manager, | 110 RPHWebContentsObserver(RPHReferenceManager* manager, |
(...skipping 17 matching lines...) Expand all Loading... |
127 void RemoveWebContentsObserver(WebContents* web_contents); | 128 void RemoveWebContentsObserver(WebContents* web_contents); |
128 bool HasWebContentsObservers() { | 129 bool HasWebContentsObservers() { |
129 return observed_web_contentses_.size() > 0; | 130 return observed_web_contentses_.size() > 0; |
130 } | 131 } |
131 | 132 |
132 private: | 133 private: |
133 void RenderProcessHostDestroyed(RenderProcessHost* host) override; | 134 void RenderProcessHostDestroyed(RenderProcessHost* host) override; |
134 | 135 |
135 RPHReferenceManager* manager_; | 136 RPHReferenceManager* manager_; |
136 RenderProcessHost* host_; | 137 RenderProcessHost* host_; |
137 typedef std::map<WebContents*, RPHWebContentsObserver*> WCObserverMap; | 138 std::map<WebContents*, std::unique_ptr<RPHWebContentsObserver>> |
138 WCObserverMap observed_web_contentses_; | 139 observed_web_contentses_; |
139 }; | 140 }; |
140 typedef std::map<const RenderProcessHost*, RPHObserver*> RPHObserverMap; | |
141 | 141 |
142 // Handlers for observed events. | 142 // Handlers for observed events. |
143 void OnRenderProcessHostDestroyed(RenderProcessHost* rph); | 143 void OnRenderProcessHostDestroyed(RenderProcessHost* rph); |
144 void OnWebContentsDestroyedOrNavigated(WebContents* contents); | 144 void OnWebContentsDestroyedOrNavigated(WebContents* contents); |
145 | 145 |
146 // A callback to call when the last RVH reference goes away. | 146 // A callback to call when the last RVH reference goes away. |
147 base::Closure no_references_callback_; | 147 base::Closure no_references_callback_; |
148 | 148 |
149 // The set of render processes and web contents that may have references to | 149 // The set of render processes and web contents that may have references to |
150 // the file system ids this instance manages. | 150 // the file system ids this instance manages. |
151 RPHObserverMap observer_map_; | 151 std::map<const RenderProcessHost*, std::unique_ptr<RPHObserver>> |
| 152 observer_map_; |
152 }; | 153 }; |
153 | 154 |
154 RPHReferenceManager::RPHReferenceManager( | 155 RPHReferenceManager::RPHReferenceManager( |
155 const base::Closure& no_references_callback) | 156 const base::Closure& no_references_callback) |
156 : no_references_callback_(no_references_callback) { | 157 : no_references_callback_(no_references_callback) { |
157 } | 158 } |
158 | 159 |
159 RPHReferenceManager::~RPHReferenceManager() { | 160 RPHReferenceManager::~RPHReferenceManager() { |
160 Reset(); | 161 Reset(); |
161 } | 162 } |
162 | 163 |
163 void RPHReferenceManager::ReferenceFromWebContents( | 164 void RPHReferenceManager::ReferenceFromWebContents( |
164 content::WebContents* contents) { | 165 content::WebContents* contents) { |
165 RenderProcessHost* rph = contents->GetRenderProcessHost(); | 166 RenderProcessHost* rph = contents->GetRenderProcessHost(); |
166 RPHObserver* state = NULL; | |
167 if (!base::ContainsKey(observer_map_, rph)) { | 167 if (!base::ContainsKey(observer_map_, rph)) { |
168 state = new RPHObserver(this, rph); | 168 observer_map_[rph] = base::MakeUnique<RPHObserver>(this, rph); |
169 observer_map_[rph] = state; | |
170 } else { | |
171 state = observer_map_[rph]; | |
172 } | 169 } |
173 | 170 observer_map_[rph]->AddWebContentsObserver(contents); |
174 state->AddWebContentsObserver(contents); | |
175 } | 171 } |
176 | 172 |
177 RPHReferenceManager::RPHWebContentsObserver::RPHWebContentsObserver( | 173 RPHReferenceManager::RPHWebContentsObserver::RPHWebContentsObserver( |
178 RPHReferenceManager* manager, | 174 RPHReferenceManager* manager, |
179 WebContents* web_contents) | 175 WebContents* web_contents) |
180 : content::WebContentsObserver(web_contents), | 176 : content::WebContentsObserver(web_contents), |
181 manager_(manager) { | 177 manager_(manager) { |
182 } | 178 } |
183 | 179 |
184 void RPHReferenceManager::RPHWebContentsObserver::WebContentsDestroyed() { | 180 void RPHReferenceManager::RPHWebContentsObserver::WebContentsDestroyed() { |
185 manager_->OnWebContentsDestroyedOrNavigated(web_contents()); | 181 manager_->OnWebContentsDestroyedOrNavigated(web_contents()); |
186 } | 182 } |
187 | 183 |
188 void RPHReferenceManager::RPHWebContentsObserver::NavigationEntryCommitted( | 184 void RPHReferenceManager::RPHWebContentsObserver::NavigationEntryCommitted( |
189 const content::LoadCommittedDetails& load_details) { | 185 const content::LoadCommittedDetails& load_details) { |
190 if (load_details.is_in_page) | 186 if (load_details.is_in_page) |
191 return; | 187 return; |
192 | 188 |
193 manager_->OnWebContentsDestroyedOrNavigated(web_contents()); | 189 manager_->OnWebContentsDestroyedOrNavigated(web_contents()); |
194 } | 190 } |
195 | 191 |
196 RPHReferenceManager::RPHObserver::RPHObserver( | 192 RPHReferenceManager::RPHObserver::RPHObserver( |
197 RPHReferenceManager* manager, RenderProcessHost* host) | 193 RPHReferenceManager* manager, RenderProcessHost* host) |
198 : manager_(manager), | 194 : manager_(manager), |
199 host_(host) { | 195 host_(host) { |
200 host->AddObserver(this); | 196 host->AddObserver(this); |
201 } | 197 } |
202 | 198 |
203 RPHReferenceManager::RPHObserver::~RPHObserver() { | 199 RPHReferenceManager::RPHObserver::~RPHObserver() { |
204 base::STLDeleteValues(&observed_web_contentses_); | 200 observed_web_contentses_.clear(); |
205 if (host_) | 201 if (host_) |
206 host_->RemoveObserver(this); | 202 host_->RemoveObserver(this); |
207 } | 203 } |
208 | 204 |
209 void RPHReferenceManager::RPHObserver::AddWebContentsObserver( | 205 void RPHReferenceManager::RPHObserver::AddWebContentsObserver( |
210 WebContents* web_contents) { | 206 WebContents* web_contents) { |
211 if (base::ContainsKey(observed_web_contentses_, web_contents)) | 207 if (base::ContainsKey(observed_web_contentses_, web_contents)) |
212 return; | 208 return; |
213 | 209 |
214 RPHWebContentsObserver* observer = | 210 observed_web_contentses_[web_contents] = |
215 new RPHWebContentsObserver(manager_, web_contents); | 211 base::MakeUnique<RPHWebContentsObserver>(manager_, web_contents); |
216 observed_web_contentses_[web_contents] = observer; | |
217 } | 212 } |
218 | 213 |
219 void RPHReferenceManager::RPHObserver::RemoveWebContentsObserver( | 214 void RPHReferenceManager::RPHObserver::RemoveWebContentsObserver( |
220 WebContents* web_contents) { | 215 WebContents* web_contents) { |
221 WCObserverMap::iterator wco_iter = | 216 DCHECK(observed_web_contentses_.find(web_contents) != |
222 observed_web_contentses_.find(web_contents); | 217 observed_web_contentses_.end()); |
223 DCHECK(wco_iter != observed_web_contentses_.end()); | 218 observed_web_contentses_.erase(web_contents); |
224 delete wco_iter->second; | |
225 observed_web_contentses_.erase(wco_iter); | |
226 } | 219 } |
227 | 220 |
228 void RPHReferenceManager::RPHObserver::RenderProcessHostDestroyed( | 221 void RPHReferenceManager::RPHObserver::RenderProcessHostDestroyed( |
229 RenderProcessHost* host) { | 222 RenderProcessHost* host) { |
230 host_ = NULL; | 223 host_ = NULL; |
231 manager_->OnRenderProcessHostDestroyed(host); | 224 manager_->OnRenderProcessHostDestroyed(host); |
232 } | 225 } |
233 | 226 |
234 void RPHReferenceManager::OnRenderProcessHostDestroyed( | 227 void RPHReferenceManager::OnRenderProcessHostDestroyed( |
235 RenderProcessHost* rph) { | 228 RenderProcessHost* rph) { |
236 RPHObserverMap::iterator rph_info = observer_map_.find(rph); | 229 auto rph_info = observer_map_.find(rph); |
237 // This could be a potential problem if the RPH is navigated to a page on the | 230 // This could be a potential problem if the RPH is navigated to a page on the |
238 // same renderer (triggering OnWebContentsDestroyedOrNavigated()) and then the | 231 // same renderer (triggering OnWebContentsDestroyedOrNavigated()) and then the |
239 // renderer crashes. | 232 // renderer crashes. |
240 if (rph_info == observer_map_.end()) { | 233 if (rph_info == observer_map_.end()) { |
241 NOTREACHED(); | 234 NOTREACHED(); |
242 return; | 235 return; |
243 } | 236 } |
244 delete rph_info->second; | |
245 observer_map_.erase(rph_info); | 237 observer_map_.erase(rph_info); |
246 if (observer_map_.empty()) | 238 if (observer_map_.empty()) |
247 no_references_callback_.Run(); | 239 no_references_callback_.Run(); |
248 } | 240 } |
249 | 241 |
250 void RPHReferenceManager::OnWebContentsDestroyedOrNavigated( | 242 void RPHReferenceManager::OnWebContentsDestroyedOrNavigated( |
251 WebContents* contents) { | 243 WebContents* contents) { |
252 RenderProcessHost* rph = contents->GetRenderProcessHost(); | 244 RenderProcessHost* rph = contents->GetRenderProcessHost(); |
253 RPHObserverMap::iterator rph_info = observer_map_.find(rph); | 245 auto rph_info = observer_map_.find(rph); |
254 DCHECK(rph_info != observer_map_.end()); | 246 DCHECK(rph_info != observer_map_.end()); |
255 | 247 |
256 rph_info->second->RemoveWebContentsObserver(contents); | 248 rph_info->second->RemoveWebContentsObserver(contents); |
257 if (!rph_info->second->HasWebContentsObservers()) | 249 if (!rph_info->second->HasWebContentsObservers()) |
258 OnRenderProcessHostDestroyed(rph); | 250 OnRenderProcessHostDestroyed(rph); |
259 } | 251 } |
260 | 252 |
261 } // namespace | 253 } // namespace |
262 | 254 |
263 MediaFileSystemInfo::MediaFileSystemInfo(const base::string16& fs_name, | 255 MediaFileSystemInfo::MediaFileSystemInfo(const base::string16& fs_name, |
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 870 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
879 | 871 |
880 auto extension_hosts_it = extension_hosts_map_.find(profile); | 872 auto extension_hosts_it = extension_hosts_map_.find(profile); |
881 DCHECK(extension_hosts_it != extension_hosts_map_.end()); | 873 DCHECK(extension_hosts_it != extension_hosts_map_.end()); |
882 extension_hosts_map_.erase(extension_hosts_it); | 874 extension_hosts_map_.erase(extension_hosts_it); |
883 | 875 |
884 auto profile_subscription_it = profile_subscription_map_.find(profile); | 876 auto profile_subscription_it = profile_subscription_map_.find(profile); |
885 DCHECK(profile_subscription_it != profile_subscription_map_.end()); | 877 DCHECK(profile_subscription_it != profile_subscription_map_.end()); |
886 profile_subscription_map_.erase(profile_subscription_it); | 878 profile_subscription_map_.erase(profile_subscription_it); |
887 } | 879 } |
OLD | NEW |