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