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/interstitial_page.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/common/renderer_preferences.h"
24 class NavigationEntry;
25 class NavigationControllerImpl;
26 class RenderViewHostImpl;
27 class RenderWidgetHostView;
28 class WebContentsView;
29 class WebContentsImpl;
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 // Allows the user to navigate away by disabling the interstitial, canceling
72 // the pending request, and unblocking the hidden renderer. The interstitial
73 // will stay visible until the navigation completes.
74 void CancelForNavigation();
76 // Focus the first (last if reverse is true) element in the interstitial page.
77 // Called when tab traversing.
78 void FocusThroughTabTraversal(bool reverse);
80 RenderWidgetHostView* GetView();
82 // See description above field.
83 void set_reload_on_dont_proceed(bool value) {
84 reload_on_dont_proceed_ = value;
86 bool reload_on_dont_proceed() const { return reload_on_dont_proceed_; }
88 #if defined(OS_ANDROID)
89 // Android shares a single platform window for all tabs, so we need to expose
90 // the RenderViewHost to properly route gestures to the interstitial.
91 RenderViewHost* GetRenderViewHost() const;
94 // TODO(nasko): This should move to InterstitialPageNavigatorImpl, but in
95 // the meantime make it public, so it can be called directly.
97 RenderViewHost* render_view_host,
98 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
101 // NotificationObserver method:
102 virtual void Observe(int type,
103 const NotificationSource& source,
104 const NotificationDetails& details) OVERRIDE;
106 // WebContentsObserver implementation:
107 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
108 virtual void NavigationEntryCommitted(
109 const LoadCommittedDetails& load_details) OVERRIDE;
111 // RenderFrameHostDelegate implementation:
112 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) OVERRIDE;
114 // RenderViewHostDelegate implementation:
115 virtual RenderViewHostDelegateView* GetDelegateView() OVERRIDE;
116 virtual const GURL& GetURL() const OVERRIDE;
117 virtual void RenderViewTerminated(RenderViewHost* render_view_host,
118 base::TerminationStatus status,
119 int error_code) OVERRIDE;
120 virtual void UpdateTitle(RenderViewHost* render_view_host,
122 const base::string16& title,
123 base::i18n::TextDirection title_direction) OVERRIDE;
124 virtual RendererPreferences GetRendererPrefs(
125 BrowserContext* browser_context) const OVERRIDE;
126 virtual WebPreferences GetWebkitPrefs() OVERRIDE;
127 virtual gfx::Rect GetRootWindowResizerRect() const OVERRIDE;
128 virtual void CreateNewWindow(
129 int render_process_id,
131 int main_frame_route_id,
132 const ViewHostMsg_CreateWindow_Params& params,
133 SessionStorageNamespace* session_storage_namespace) OVERRIDE;
134 virtual void CreateNewWidget(int render_process_id,
136 blink::WebPopupType popup_type) OVERRIDE;
137 virtual void CreateNewFullscreenWidget(int render_process_id,
138 int route_id) OVERRIDE;
139 virtual void ShowCreatedWindow(int route_id,
140 WindowOpenDisposition disposition,
141 const gfx::Rect& initial_pos,
142 bool user_gesture) OVERRIDE;
143 virtual void ShowCreatedWidget(int route_id,
144 const gfx::Rect& initial_pos) OVERRIDE;
145 virtual void ShowCreatedFullscreenWidget(int route_id) OVERRIDE;
147 virtual SessionStorageNamespace* GetSessionStorageNamespace(
148 SiteInstance* instance) OVERRIDE;
150 virtual FrameTree* GetFrameTree() OVERRIDE;
152 // RenderWidgetHostDelegate implementation:
153 virtual void RenderWidgetDeleted(
154 RenderWidgetHostImpl* render_widget_host) OVERRIDE;
155 virtual bool PreHandleKeyboardEvent(
156 const NativeWebKeyboardEvent& event,
157 bool* is_keyboard_shortcut) OVERRIDE;
158 virtual void HandleKeyboardEvent(
159 const NativeWebKeyboardEvent& event) OVERRIDE;
161 virtual gfx::NativeViewAccessible GetParentNativeViewAccessible() OVERRIDE;
164 bool enabled() const { return enabled_; }
165 WebContents* web_contents() const;
166 const GURL& url() const { return url_; }
168 // Creates the RenderViewHost containing the interstitial content.
169 // Overriden in unit tests.
170 virtual RenderViewHost* CreateRenderViewHost();
172 // Creates the WebContentsView that shows the interstitial RVH.
173 // Overriden in unit tests.
174 virtual WebContentsView* CreateWebContentsView();
176 // Notification magic.
177 NotificationRegistrar notification_registrar_;
180 class InterstitialPageRVHDelegateView;
182 // Disable the interstitial:
183 // - if it is not yet showing, then it won't be shown.
184 // - any command sent by the RenderViewHost will be ignored.
187 // Delete ourselves, causing Shutdown on the RVH to be called.
190 void OnNavigatingAwayOrTabClosing();
192 // Executes the passed action on the ResourceDispatcher (on the IO thread).
193 // Used to block/resume/cancel requests for the RenderViewHost hidden by this
195 void TakeActionOnResourceDispatcher(ResourceRequestAction action);
197 // The contents in which we are displayed. This is valid until Hide is
198 // called, at which point it will be set to NULL because the WebContents
199 // itself may be deleted.
200 WebContents* web_contents_;
202 // The NavigationController for the content this page is being displayed over.
203 NavigationControllerImpl* controller_;
205 // Delegate for dispatching keyboard events and accessing the native view.
206 RenderWidgetHostDelegate* render_widget_host_delegate_;
208 // The URL that is shown when the interstitial is showing.
211 // Whether this interstitial is shown as a result of a new navigation (in
212 // which case a transient navigation entry is created).
213 bool new_navigation_;
215 // Whether we should discard the pending navigation entry when not proceeding.
216 // This is to deal with cases where |new_navigation_| is true but a new
217 // pending entry was created since this interstitial was shown and we should
219 bool should_discard_pending_nav_entry_;
221 // If true and the user chooses not to proceed the target NavigationController
222 // is reloaded. This is used when two NavigationControllers are merged
223 // (CopyStateFromAndPrune).
224 // The default is false.
225 bool reload_on_dont_proceed_;
227 // Whether this interstitial is enabled. See Disable() for more info.
230 // Whether the Proceed or DontProceed methods have been called yet.
231 ActionState action_taken_;
233 // The RenderViewHost displaying the interstitial contents. This is valid
234 // until Hide is called, at which point it will be set to NULL, signifying
235 // that shutdown has started.
236 // TODO(creis): This is now owned by the FrameTree. We should route things
237 // through the tree's root RenderFrameHost instead.
238 RenderViewHostImpl* render_view_host_;
240 // The frame tree structure of the current page.
241 FrameTree frame_tree_;
243 // The IDs for the Render[View|Process]Host hidden by this interstitial.
244 int original_child_id_;
245 int original_rvh_id_;
247 // Whether or not we should change the title of the contents when hidden (to
248 // revert it to its original value).
249 bool should_revert_web_contents_title_;
251 // Whether or not the contents was loading resources when the interstitial was
252 // shown. We restore this state if the user proceeds from the interstitial.
253 bool web_contents_was_loading_;
255 // Whether the ResourceDispatcherHost has been notified to cancel/resume the
256 // resource requests blocked for the RenderViewHost.
257 bool resource_dispatcher_host_notified_;
259 // The original title of the contents that should be reverted to when the
260 // interstitial is hidden.
261 base::string16 original_web_contents_title_;
263 // Our RenderViewHostViewDelegate, necessary for accelerators to work.
264 scoped_ptr<InterstitialPageRVHDelegateView> rvh_delegate_view_;
266 // Settings passed to the renderer.
267 mutable RendererPreferences renderer_preferences_;
271 scoped_ptr<InterstitialPageDelegate> delegate_;
273 base::WeakPtrFactory<InterstitialPageImpl> weak_ptr_factory_;
275 scoped_refptr<SessionStorageNamespace> session_storage_namespace_;
277 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImpl);
280 } // namespace content
282 #endif // CONTENT_BROWSER_FRAME_HOST_INTERSTITIAL_PAGE_IMPL_H_