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.
5 #include "ui/web_dialogs/test/test_web_dialog_observer.h"
7 #include "base/logging.h"
8 #include "content/public/browser/navigation_controller.h"
9 #include "content/public/browser/notification_details.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_source.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_ui.h"
15 #include "content/public/test/js_injection_ready_observer.h"
16 #include "content/public/test/test_utils.h"
18 using content::NavigationController
;
23 TestWebDialogObserver::TestWebDialogObserver(
24 content::JsInjectionReadyObserver
* js_injection_ready_observer
)
25 : js_injection_ready_observer_(js_injection_ready_observer
),
31 TestWebDialogObserver::~TestWebDialogObserver() {
34 void TestWebDialogObserver::Observe(
36 const content::NotificationSource
& source
,
37 const content::NotificationDetails
& details
) {
40 case content::NOTIFICATION_LOAD_STOP
:
42 registrar_
.Remove(this, content::NOTIFICATION_LOAD_STOP
,
43 content::Source
<NavigationController
>(
44 &web_ui_
->GetWebContents()->GetController()));
46 // If the message loop is running stop it.
49 message_loop_runner_
->Quit();
57 void TestWebDialogObserver::OnDialogShown(
58 content::WebUI
* webui
,
59 content::RenderViewHost
* render_view_host
) {
60 if (js_injection_ready_observer_
) {
61 js_injection_ready_observer_
->OnJsInjectionReady(render_view_host
);
64 // Wait for navigation on the new WebUI instance to complete. This depends
65 // on receiving the notification of the WebDialog being shown before the
66 // NavigationController finishes loading. The WebDialog notification is
67 // issued from web_dialog_ui.cc on RenderView creation which results from
68 // the call to render_manager_.Navigate in the method
69 // WebContents::NavigateToEntry. The new RenderView is later told to
70 // navigate in this method, ensuring that this is not a race condition.
71 registrar_
.Add(this, content::NOTIFICATION_LOAD_STOP
,
72 content::Source
<NavigationController
>(
73 &web_ui_
->GetWebContents()->GetController()));
76 content::WebUI
* TestWebDialogObserver::GetWebUI() {
78 DCHECK(running_
== false);
80 message_loop_runner_
= new content::MessageLoopRunner
;
81 message_loop_runner_
->Run();