1 // Copyright 2013 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 #ifndef CONTENT_BROWSER_FRAME_HOST_INTERSTITIAL_PAGE_IMPL_H_
6 #define CONTENT_BROWSER_FRAME_HOST_INTERSTITIAL_PAGE_IMPL_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/frame_host/frame_tree.h"
12 #include "content/browser/frame_host/navigator_delegate.h"
13 #include "content/browser/frame_host/render_frame_host_delegate.h"
14 #include "content/browser/renderer_host/render_view_host_delegate.h"
15 #include "content/browser/renderer_host/render_widget_host_delegate.h"
16 #include "content/public/browser/dom_operation_notification_details.h"
17 #include "content/public/browser/interstitial_page.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/common/renderer_preferences.h"
25 class NavigationEntry
;
26 class NavigationControllerImpl
;
27 class RenderViewHostImpl
;
28 class RenderWidgetHostView
;
29 class WebContentsView
;
31 enum ResourceRequestAction
{
37 class CONTENT_EXPORT InterstitialPageImpl
38 : public NON_EXPORTED_BASE(InterstitialPage
),
39 public NotificationObserver
,
40 public WebContentsObserver
,
41 public NON_EXPORTED_BASE(RenderFrameHostDelegate
),
42 public RenderViewHostDelegate
,
43 public RenderWidgetHostDelegate
,
44 public NON_EXPORTED_BASE(NavigatorDelegate
) {
46 // The different state of actions the user can take in an interstitial.
48 NO_ACTION
, // No action has been taken yet.
49 PROCEED_ACTION
, // "Proceed" was selected.
50 DONT_PROCEED_ACTION
// "Don't proceed" was selected.
53 InterstitialPageImpl(WebContents
* web_contents
,
54 RenderWidgetHostDelegate
* render_widget_host_delegate
,
57 InterstitialPageDelegate
* delegate
);
58 virtual ~InterstitialPageImpl();
60 // InterstitialPage implementation:
61 virtual void Show() OVERRIDE
;
62 virtual void Hide() OVERRIDE
;
63 virtual void DontProceed() OVERRIDE
;
64 virtual void Proceed() OVERRIDE
;
65 virtual RenderViewHost
* GetRenderViewHostForTesting() const OVERRIDE
;
66 virtual InterstitialPageDelegate
* GetDelegateForTesting() OVERRIDE
;
67 virtual void DontCreateViewForTesting() OVERRIDE
;
68 virtual void SetSize(const gfx::Size
& size
) OVERRIDE
;
69 virtual void Focus() OVERRIDE
;
71 // This is called by a hosting WebContents to notify this interstitial page
72 // that it is going away.
73 void WebContentsWillBeDestroyed();
75 // Allows the user to navigate away by disabling the interstitial, canceling
76 // the pending request, and unblocking the hidden renderer. The interstitial
77 // will stay visible until the navigation completes.
78 void CancelForNavigation();
80 // Focus the first (last if reverse is true) element in the interstitial page.
81 // Called when tab traversing.
82 void FocusThroughTabTraversal(bool reverse
);
84 RenderWidgetHostView
* GetView();
86 // See description above field.
87 void set_reload_on_dont_proceed(bool value
) {
88 reload_on_dont_proceed_
= value
;
90 bool reload_on_dont_proceed() const { return reload_on_dont_proceed_
; }
92 #if defined(OS_ANDROID)
93 // Android shares a single platform window for all tabs, so we need to expose
94 // the RenderViewHost to properly route gestures to the interstitial.
95 RenderViewHost
* GetRenderViewHost() const;
98 // TODO(nasko): This should move to InterstitialPageNavigatorImpl, but in
99 // the meantime make it public, so it can be called directly.
101 RenderViewHost
* render_view_host
,
102 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
);
105 // NotificationObserver method:
106 virtual void Observe(int type
,
107 const NotificationSource
& source
,
108 const NotificationDetails
& details
) OVERRIDE
;
110 // WebContentsObserver implementation:
111 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
112 virtual void WebContentsDestroyed(WebContents
* web_contents
) OVERRIDE
;
113 virtual void NavigationEntryCommitted(
114 const LoadCommittedDetails
& load_details
) OVERRIDE
;
116 // RenderFrameHostDelegate implementation:
117 virtual bool OnMessageReceived(RenderFrameHost
* render_frame_host
,
118 const IPC::Message
& message
) OVERRIDE
;
119 virtual void RenderFrameCreated(RenderFrameHost
* render_frame_host
) OVERRIDE
;
121 // RenderViewHostDelegate implementation:
122 virtual RenderViewHostDelegateView
* GetDelegateView() OVERRIDE
;
123 virtual bool OnMessageReceived(RenderViewHost
* render_view_host
,
124 const IPC::Message
& message
) OVERRIDE
;
125 virtual const GURL
& GetMainFrameLastCommittedURL() const OVERRIDE
;
126 virtual void RenderViewTerminated(RenderViewHost
* render_view_host
,
127 base::TerminationStatus status
,
128 int error_code
) OVERRIDE
;
129 virtual void UpdateTitle(RenderViewHost
* render_view_host
,
131 const base::string16
& title
,
132 base::i18n::TextDirection title_direction
) OVERRIDE
;
133 virtual RendererPreferences
GetRendererPrefs(
134 BrowserContext
* browser_context
) const OVERRIDE
;
135 virtual WebPreferences
GetWebkitPrefs() OVERRIDE
;
136 virtual gfx::Rect
GetRootWindowResizerRect() const OVERRIDE
;
137 virtual void CreateNewWindow(
138 int render_process_id
,
140 int main_frame_route_id
,
141 const ViewHostMsg_CreateWindow_Params
& params
,
142 SessionStorageNamespace
* session_storage_namespace
) OVERRIDE
;
143 virtual void CreateNewWidget(int render_process_id
,
145 blink::WebPopupType popup_type
) OVERRIDE
;
146 virtual void CreateNewFullscreenWidget(int render_process_id
,
147 int route_id
) OVERRIDE
;
148 virtual void ShowCreatedWindow(int route_id
,
149 WindowOpenDisposition disposition
,
150 const gfx::Rect
& initial_pos
,
151 bool user_gesture
) OVERRIDE
;
152 virtual void ShowCreatedWidget(int route_id
,
153 const gfx::Rect
& initial_pos
) OVERRIDE
;
154 virtual void ShowCreatedFullscreenWidget(int route_id
) OVERRIDE
;
156 virtual SessionStorageNamespace
* GetSessionStorageNamespace(
157 SiteInstance
* instance
) OVERRIDE
;
159 virtual FrameTree
* GetFrameTree() OVERRIDE
;
161 // RenderWidgetHostDelegate implementation:
162 virtual void RenderWidgetDeleted(
163 RenderWidgetHostImpl
* render_widget_host
) OVERRIDE
;
164 virtual bool PreHandleKeyboardEvent(
165 const NativeWebKeyboardEvent
& event
,
166 bool* is_keyboard_shortcut
) OVERRIDE
;
167 virtual void HandleKeyboardEvent(
168 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
170 virtual gfx::NativeViewAccessible
GetParentNativeViewAccessible() OVERRIDE
;
173 bool enabled() const { return enabled_
; }
174 WebContents
* web_contents() const;
175 const GURL
& url() const { return url_
; }
177 // Creates the RenderViewHost containing the interstitial content.
178 // Overriden in unit tests.
179 virtual RenderViewHost
* CreateRenderViewHost();
181 // Creates the WebContentsView that shows the interstitial RVH.
182 // Overriden in unit tests.
183 virtual WebContentsView
* CreateWebContentsView();
185 // Notification magic.
186 NotificationRegistrar notification_registrar_
;
189 class InterstitialPageRVHDelegateView
;
191 // Disable the interstitial:
192 // - if it is not yet showing, then it won't be shown.
193 // - any command sent by the RenderViewHost will be ignored.
196 // Delete ourselves, causing Shutdown on the RVH to be called.
199 void OnNavigatingAwayOrTabClosing();
201 // Executes the passed action on the ResourceDispatcher (on the IO thread).
202 // Used to block/resume/cancel requests for the RenderViewHost hidden by this
204 void TakeActionOnResourceDispatcher(ResourceRequestAction action
);
206 // IPC message handlers.
207 void OnDomOperationResponse(const std::string
& json_string
,
210 // The contents in which we are displayed. This is valid until Hide is
211 // called, at which point it will be set to NULL because the WebContents
212 // itself may be deleted.
213 WebContents
* web_contents_
;
215 // The NavigationController for the content this page is being displayed over.
216 NavigationControllerImpl
* controller_
;
218 // Delegate for dispatching keyboard events and accessing the native view.
219 RenderWidgetHostDelegate
* render_widget_host_delegate_
;
221 // The URL that is shown when the interstitial is showing.
224 // Whether this interstitial is shown as a result of a new navigation (in
225 // which case a transient navigation entry is created).
226 bool new_navigation_
;
228 // Whether we should discard the pending navigation entry when not proceeding.
229 // This is to deal with cases where |new_navigation_| is true but a new
230 // pending entry was created since this interstitial was shown and we should
232 bool should_discard_pending_nav_entry_
;
234 // If true and the user chooses not to proceed the target NavigationController
235 // is reloaded. This is used when two NavigationControllers are merged
236 // (CopyStateFromAndPrune).
237 // The default is false.
238 bool reload_on_dont_proceed_
;
240 // Whether this interstitial is enabled. See Disable() for more info.
243 // Whether the Proceed or DontProceed methods have been called yet.
244 ActionState action_taken_
;
246 // The RenderViewHost displaying the interstitial contents. This is valid
247 // until Hide is called, at which point it will be set to NULL, signifying
248 // that shutdown has started.
249 // TODO(creis): This is now owned by the FrameTree. We should route things
250 // through the tree's root RenderFrameHost instead.
251 RenderViewHostImpl
* render_view_host_
;
253 // The frame tree structure of the current page.
254 FrameTree frame_tree_
;
256 // The IDs for the Render[View|Process]Host hidden by this interstitial.
257 int original_child_id_
;
258 int original_rvh_id_
;
260 // Whether or not we should change the title of the contents when hidden (to
261 // revert it to its original value).
262 bool should_revert_web_contents_title_
;
264 // Whether or not the contents was loading resources when the interstitial was
265 // shown. We restore this state if the user proceeds from the interstitial.
266 bool web_contents_was_loading_
;
268 // Whether the ResourceDispatcherHost has been notified to cancel/resume the
269 // resource requests blocked for the RenderViewHost.
270 bool resource_dispatcher_host_notified_
;
272 // The original title of the contents that should be reverted to when the
273 // interstitial is hidden.
274 base::string16 original_web_contents_title_
;
276 // Our RenderViewHostViewDelegate, necessary for accelerators to work.
277 scoped_ptr
<InterstitialPageRVHDelegateView
> rvh_delegate_view_
;
279 // Settings passed to the renderer.
280 mutable RendererPreferences renderer_preferences_
;
284 scoped_ptr
<InterstitialPageDelegate
> delegate_
;
286 base::WeakPtrFactory
<InterstitialPageImpl
> weak_ptr_factory_
;
288 scoped_refptr
<SessionStorageNamespace
> session_storage_namespace_
;
290 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImpl
);
293 } // namespace content
295 #endif // CONTENT_BROWSER_FRAME_HOST_INTERSTITIAL_PAGE_IMPL_H_