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