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