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