| 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(int render_process_id, int render_view_id) { |
| 35 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 36 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 37 &TabWatcher::Watch, this, render_process_id, render_view_id)); |
| 38 return; |
| 39 } |
| 40 |
| 41 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, |
| 42 render_view_id); |
| 43 // Sessions initiated by speech input extension APIs will end up in a NULL |
| 44 // WebContent here, but they are properly managed by the |
| 45 // chrome::SpeechInputExtensionManager. However, sessions initiated within an |
| 46 // extension using the (new) speech JS APIs, will be properly handled here. |
| 47 // TODO(primiano) turn this line into a DCHECK once speech input extension |
| 48 // API is deprecated. |
| 49 if (!web_contents) |
| 50 return; |
| 51 |
| 52 // Avoid multiple registrations on |registrar_| for the same |web_contents|. |
| 53 if (registered_web_contents_.find(web_contents) != |
| 54 registered_web_contents_.end()) { |
| 55 return; |
| 56 } |
| 57 registered_web_contents_.insert(web_contents); |
| 58 |
| 59 // Lazy initialize the registrar. |
| 60 if (!registrar_.get()) |
| 61 registrar_.reset(new content::NotificationRegistrar()); |
| 62 |
| 63 registrar_->Add(this, |
| 64 kNotificationType, |
| 65 content::Source<WebContents>(web_contents)); |
| 66 } |
| 67 |
| 68 void TabWatcher::Observe( |
| 69 int type, |
| 70 const content::NotificationSource& source, |
| 71 const content::NotificationDetails& details) { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 73 DCHECK_EQ(kNotificationType, type); |
| 74 |
| 75 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 76 DCHECK(web_contents); |
| 77 const int render_process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 78 const int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); |
| 79 |
| 80 registrar_->Remove(this, |
| 81 kNotificationType, |
| 82 content::Source<WebContents>(web_contents)); |
| 83 registered_web_contents_.erase(web_contents); |
| 84 |
| 85 BrowserThread::PostTask(callback_thread_, FROM_HERE, base::Bind( |
| 86 tab_closed_callback_, render_process_id, render_view_id)); |
| 87 } |
| OLD | NEW |