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