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