| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/speech/tab_watcher.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/tab_contents/tab_util.h" |
| 9 #include "content/public/browser/notification_registrar.h" |
| 10 #include "content/public/browser/notification_source.h" |
| 11 #include "content/public/browser/notification_types.h" |
| 12 #include "content/public/browser/render_process_host.h" |
| 13 #include "content/public/browser/render_view_host.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 using content::WebContents; |
| 18 |
| 19 namespace { |
| 20 const int kNotificationType = content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED; |
| 21 } |
| 22 |
| 23 TabWatcher::TabWatcher( |
| 24 TabClosedCallback tab_closed_callback, BrowserThread::ID callback_thread) |
| 25 : tab_closed_callback_(tab_closed_callback), |
| 26 callback_thread_(callback_thread) { |
| 27 } |
| 28 |
| 29 TabWatcher::~TabWatcher() { |
| 30 // Must be destroyed on the UI thread due to |registrar_| non thread-safety. |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 } |
| 33 |
| 34 void TabWatcher::Watch( |
| 35 int render_process_id, int render_view_id) { |
| 36 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 37 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 38 &TabWatcher::Watch, this, render_process_id, render_view_id)); |
| 39 return; |
| 40 } |
| 41 |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 43 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, |
| 44 render_view_id); |
| 45 |
| 46 // Sessions initiated by extensions might not have a corresponding WebContent. |
| 47 if (!web_contents) |
| 48 return; |
| 49 |
| 50 // Avoid multiple registrations on |registrar_| for the same |web_contents|. |
| 51 if (registered_contents_.find(web_contents) != registered_contents_.end()) |
| 52 return; |
| 53 registered_contents_.insert(web_contents); |
| 54 |
| 55 // Lazy initialize the registrar. |
| 56 if (!registrar_.get()) |
| 57 registrar_.reset(new content::NotificationRegistrar()); |
| 58 |
| 59 registrar_->Add(this, |
| 60 kNotificationType, |
| 61 content::Source<WebContents>(web_contents)); |
| 62 } |
| 63 |
| 64 void TabWatcher::Observe( |
| 65 int type, |
| 66 const content::NotificationSource& source, |
| 67 const content::NotificationDetails& details) { |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 69 DCHECK_EQ(kNotificationType, type); |
| 70 |
| 71 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 72 const int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 73 const int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 74 |
| 75 registrar_->Remove(this, |
| 76 kNotificationType, |
| 77 content::Source<WebContents>(web_contents)); |
| 78 registered_contents_.erase(web_contents); |
| 79 |
| 80 BrowserThread::PostTask(callback_thread_, FROM_HERE, base::Bind( |
| 81 tab_closed_callback_, render_process_id, render_view_id)); |
| 82 } |
| OLD | NEW |