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