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/test/base/test_tab_strip_model_observer.h" | 5 #include "chrome/test/base/test_tab_strip_model_observer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 9 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "content/public/browser/notification_service.h" |
11 #include "content/public/browser/notification_source.h" | 12 #include "content/public/browser/notification_source.h" |
| 13 #include "content/public/browser/notification_types.h" |
| 14 #include "content/public/browser/render_view_host_observer.h" |
12 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/test/js_injection_ready_observer.h" |
| 17 |
| 18 class TestTabStripModelObserver::RenderViewHostInitializedObserver |
| 19 : public content::RenderViewHostObserver { |
| 20 public: |
| 21 RenderViewHostInitializedObserver(content::RenderViewHost* render_view_host, |
| 22 content::JsInjectionReadyObserver* observer) |
| 23 : content::RenderViewHostObserver(render_view_host), |
| 24 injection_observer_(observer) { |
| 25 } |
| 26 |
| 27 // content::RenderViewHostObserver: |
| 28 virtual void RenderViewHostInitialized() OVERRIDE { |
| 29 injection_observer_->OnJsInjectionReady(render_view_host()); |
| 30 } |
| 31 |
| 32 private: |
| 33 content::JsInjectionReadyObserver* injection_observer_; |
| 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(RenderViewHostInitializedObserver); |
| 36 }; |
13 | 37 |
14 TestTabStripModelObserver::TestTabStripModelObserver( | 38 TestTabStripModelObserver::TestTabStripModelObserver( |
15 TabStripModel* tab_strip_model, | 39 TabStripModel* tab_strip_model, |
16 content::JsInjectionReadyObserver* js_injection_ready_observer) | 40 content::JsInjectionReadyObserver* js_injection_ready_observer) |
17 : TestNavigationObserver(js_injection_ready_observer, 1), | 41 : TestNavigationObserver(1), |
18 tab_strip_model_(tab_strip_model) { | 42 tab_strip_model_(tab_strip_model), |
| 43 injection_observer_(js_injection_ready_observer) { |
| 44 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, |
| 45 content::NotificationService::AllSources()); |
| 46 |
19 tab_strip_model_->AddObserver(this); | 47 tab_strip_model_->AddObserver(this); |
20 } | 48 } |
21 | 49 |
22 TestTabStripModelObserver::~TestTabStripModelObserver() { | 50 TestTabStripModelObserver::~TestTabStripModelObserver() { |
23 tab_strip_model_->RemoveObserver(this); | 51 tab_strip_model_->RemoveObserver(this); |
24 } | 52 } |
25 | 53 |
| 54 void TestTabStripModelObserver::Observe( |
| 55 int type, |
| 56 const content::NotificationSource& source, |
| 57 const content::NotificationDetails& details) { |
| 58 if (type == content::NOTIFICATION_RENDER_VIEW_HOST_CREATED) { |
| 59 rvh_observer_.reset( |
| 60 new RenderViewHostInitializedObserver( |
| 61 content::Source<content::RenderViewHost>(source).ptr(), |
| 62 injection_observer_)); |
| 63 } else { |
| 64 content::TestNavigationObserver::Observe(type, source, details); |
| 65 } |
| 66 } |
| 67 |
26 void TestTabStripModelObserver::TabBlockedStateChanged( | 68 void TestTabStripModelObserver::TabBlockedStateChanged( |
27 content::WebContents* contents, int index) { | 69 content::WebContents* contents, int index) { |
28 // Need to do this later - the print preview dialog has not been created yet. | 70 // Need to do this later - the print preview dialog has not been created yet. |
29 MessageLoop::current()->PostTask( | 71 MessageLoop::current()->PostTask( |
30 FROM_HERE, | 72 FROM_HERE, |
31 base::Bind(&TestTabStripModelObserver::ObservePrintPreviewDialog, | 73 base::Bind(&TestTabStripModelObserver::ObservePrintPreviewDialog, |
32 base::Unretained(this), | 74 base::Unretained(this), |
33 contents)); | 75 contents)); |
34 } | 76 } |
35 | 77 |
36 void TestTabStripModelObserver::ObservePrintPreviewDialog( | 78 void TestTabStripModelObserver::ObservePrintPreviewDialog( |
37 content::WebContents* contents) { | 79 content::WebContents* contents) { |
38 printing::PrintPreviewDialogController* dialog_controller = | 80 printing::PrintPreviewDialogController* dialog_controller = |
39 printing::PrintPreviewDialogController::GetInstance(); | 81 printing::PrintPreviewDialogController::GetInstance(); |
40 if (!dialog_controller) | 82 if (!dialog_controller) |
41 return; | 83 return; |
42 content::WebContents* preview_dialog = | 84 content::WebContents* preview_dialog = |
43 dialog_controller->GetPrintPreviewForContents(contents); | 85 dialog_controller->GetPrintPreviewForContents(contents); |
44 if (!preview_dialog) | 86 if (!preview_dialog) |
45 return; | 87 return; |
46 RegisterAsObserver(content::Source<content::NavigationController>( | 88 RegisterAsObserver(content::Source<content::NavigationController>( |
47 &preview_dialog->GetController())); | 89 &preview_dialog->GetController())); |
48 } | 90 } |
OLD | NEW |