ec9f2adeecd319a1b3a54026199d55bdf2c226ce
[platform/framework/web/crosswalk-tizen.git] /
1 // Copyright (c) 2012 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_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <vector>
15
16 #include "base/callback.h"
17 #include "base/compiler_specific.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/logging.h"
20 #include "base/macros.h"
21 #include "base/process/kill.h"
22 #include "build/build_config.h"
23 #include "content/browser/renderer_host/render_widget_host_impl.h"
24 #include "content/browser/renderer_host/render_widget_host_owner_delegate.h"
25 #include "content/browser/site_instance_impl.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/render_process_host_observer.h"
28 #include "content/public/browser/render_view_host.h"
29 #include "content/public/common/window_container_type.h"
30 #include "net/base/load_states.h"
31 #include "third_party/WebKit/public/web/WebAXEnums.h"
32 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
33 #include "third_party/WebKit/public/web/WebPopupType.h"
34 #include "third_party/skia/include/core/SkColor.h"
35 #include "ui/base/window_open_disposition.h"
36
37 class SkBitmap;
38 #if defined(OS_TIZEN_TV_PRODUCT)
39 struct ViewHostMsg_FocusedNodeChanged_Params;
40 #endif
41
42 namespace content {
43
44 class PageState;
45 class SessionStorageNamespace;
46 struct FrameReplicationState;
47
48 namespace mojom {
49 class CreateNewWindowParams;
50 }
51
52 // This implements the RenderViewHost interface that is exposed to
53 // embedders of content, and adds things only visible to content.
54 //
55 // The exact API of this object needs to be more thoroughly designed. Right
56 // now it mimics what WebContentsImpl exposed, which is a fairly large API and
57 // may contain things that are not relevant to a common subset of views. See
58 // also the comment in render_view_host_delegate.h about the size and scope of
59 // the delegate API.
60 //
61 // Right now, the concept of page navigation (both top level and frame) exists
62 // in the WebContentsImpl still, so if you instantiate one of these elsewhere,
63 // you will not be able to traverse pages back and forward. We need to determine
64 // if we want to bring that and other functionality down into this object so it
65 // can be shared by others.
66 //
67 // DEPRECATED: RenderViewHostImpl is being removed as part of the SiteIsolation
68 // project. New code should not be added here, but to either RenderFrameHostImpl
69 // (if frame specific) or WebContentsImpl (if page specific).
70 //
71 // For context, please see https://crbug.com/467770 and
72 // http://www.chromium.org/developers/design-documents/site-isolation.
73 class CONTENT_EXPORT RenderViewHostImpl : public RenderViewHost,
74                                           public RenderWidgetHostOwnerDelegate,
75                                           public RenderProcessHostObserver {
76  public:
77   // Convenience function, just like RenderViewHost::FromID.
78   static RenderViewHostImpl* FromID(int render_process_id, int render_view_id);
79
80   // Convenience function, just like RenderViewHost::From.
81   static RenderViewHostImpl* From(RenderWidgetHost* rwh);
82
83   RenderViewHostImpl(SiteInstance* instance,
84                      std::unique_ptr<RenderWidgetHostImpl> widget,
85                      RenderViewHostDelegate* delegate,
86                      int32_t main_frame_routing_id,
87                      bool swapped_out,
88                      bool has_initialized_audio_host);
89   ~RenderViewHostImpl() override;
90
91   // Shuts down this RenderViewHost and deletes it.
92   void ShutdownAndDestroy();
93
94   // RenderViewHost implementation.
95   bool Send(IPC::Message* msg) override;
96   RenderWidgetHostImpl* GetWidget() const override;
97   RenderProcessHost* GetProcess() const override;
98   int GetRoutingID() const override;
99   RenderFrameHost* GetMainFrame() override;
100   void AllowBindings(int binding_flags) override;
101   void ClearFocusedElement() override;
102   bool IsFocusedElementEditable() override;
103   void DirectoryEnumerationFinished(
104       int request_id,
105       const std::vector<base::FilePath>& files) override;
106   void DisableScrollbarsForThreshold(const gfx::Size& size) override;
107   void EnableAutoResize(const gfx::Size& min_size,
108                         const gfx::Size& max_size) override;
109   void DisableAutoResize(const gfx::Size& new_size) override;
110   void EnablePreferredSizeMode() override;
111   void ExecuteMediaPlayerActionAtLocation(
112       const gfx::Point& location,
113       const blink::WebMediaPlayerAction& action) override;
114   void ExecutePluginActionAtLocation(
115       const gfx::Point& location,
116       const blink::WebPluginAction& action) override;
117   RenderViewHostDelegate* GetDelegate() const override;
118   int GetEnabledBindings() const override;
119   SiteInstanceImpl* GetSiteInstance() const override;
120   bool IsRenderViewLive() const override;
121   void NotifyMoveOrResizeStarted() override;
122   void SetWebUIProperty(const std::string& name,
123                         const std::string& value) override;
124   void Zoom(PageZoom zoom) override;
125   void SyncRendererPrefs() override;
126   WebPreferences GetWebkitPreferences() override;
127   void UpdateWebkitPreferences(const WebPreferences& prefs) override;
128
129   void SavePageAs(const base::FilePath::StringType& pure_file_name) override;
130
131   void OnReceiveBitmapFromCache(const SkBitmap& bitmap,
132                                 const int request_id) override;
133
134   void OnWebkitPreferencesChanged() override;
135   void SelectWordAroundCaret() override;
136
137 #if defined(OS_ANDROID)
138   void GetSelectionMarkupWithBounds() override;
139   void OnHoverHitTestResult(const blink::WebHoverContentType contentType) override;
140   void OnUpdateFixedElementHeightAtPoint(int height);
141   void OnSelectedMarkupWithStartContentRect(
142     const base::string16& markup, const gfx::Rect& start_rect);
143 #endif
144
145   // RenderProcessHostObserver implementation
146   void RenderProcessReady(RenderProcessHost* host) override;
147   void RenderProcessExited(RenderProcessHost* host,
148                            base::TerminationStatus status,
149                            int exit_code) override;
150
151   void set_delegate(RenderViewHostDelegate* d) {
152     CHECK(d);  // http://crbug.com/82827
153     delegate_ = d;
154   }
155
156   // Set up the RenderView child process. Virtual because it is overridden by
157   // TestRenderViewHost.
158   // The |opener_route_id| parameter indicates which RenderView created this
159   // (MSG_ROUTING_NONE if none).
160   // |window_was_created_with_opener| is true if this top-level frame was
161   // created with an opener. (The opener may have been closed since.)
162   // The |proxy_route_id| is only used when creating a RenderView in swapped out
163   // state.
164   // |replicated_frame_state| contains replicated data for the top-level frame,
165   // such as its name and sandbox flags.
166   virtual bool CreateRenderView(
167       int opener_frame_route_id,
168       int proxy_route_id,
169       const FrameReplicationState& replicated_frame_state,
170       bool window_was_created_with_opener);
171
172   base::TerminationStatus render_view_termination_status() const {
173     return render_view_termination_status_;
174   }
175
176   // Tracks whether this RenderViewHost is in an active state (rather than
177   // pending swap out or swapped out), according to its main frame
178   // RenderFrameHost.
179   bool is_active() const { return is_active_; }
180   void set_is_active(bool is_active) { is_active_ = is_active; }
181
182   // Tracks whether this RenderViewHost is swapped out, according to its main
183   // frame RenderFrameHost.
184   void set_is_swapped_out(bool is_swapped_out) {
185     is_swapped_out_ = is_swapped_out;
186   }
187
188   // TODO(creis): Remove as part of http://crbug.com/418265.
189   bool is_waiting_for_close_ack() const { return is_waiting_for_close_ack_; }
190
191   // Tells the renderer process to run the page's unload handler.
192   // A ClosePage_ACK ack is sent back when the handler execution completes.
193   void ClosePage();
194
195   // Close the page ignoring whether it has unload events registers.
196   // This is called after the beforeunload and unload events have fired
197   // and the user has agreed to continue with closing the page.
198   void ClosePageIgnoringUnloadEvents();
199
200   // Tells the renderer view to focus the first (last if reverse is true) node.
201   void SetInitialFocus(bool reverse);
202
203   bool SuddenTerminationAllowed() const;
204   void set_sudden_termination_allowed(bool enabled) {
205     sudden_termination_allowed_ = enabled;
206   }
207
208   // Creates a new RenderView with the given route id.
209   void CreateNewWindow(int32_t route_id,
210                        int32_t main_frame_route_id,
211                        int32_t main_frame_widget_route_id,
212                        const mojom::CreateNewWindowParams& params,
213                        SessionStorageNamespace* session_storage_namespace);
214
215   // Creates a new RenderWidget with the given route id.  |popup_type| indicates
216   // if this widget is a popup and what kind of popup it is (select, autofill).
217   void CreateNewWidget(int32_t route_id, blink::WebPopupType popup_type);
218
219   // Creates a full screen RenderWidget.
220   void CreateNewFullscreenWidget(int32_t route_id);
221
222   // Send RenderViewReady to observers once the process is launched, but not
223   // re-entrantly.
224   void PostRenderViewReady();
225
226   // TODO(creis): Remove after debugging https:/crbug.com/575245.
227   int main_frame_routing_id() const {
228     return main_frame_routing_id_;
229   }
230
231   void set_main_frame_routing_id(int routing_id) {
232     main_frame_routing_id_ = routing_id;
233   }
234
235   // Increases the refcounting on this RVH. This is done by the FrameTree on
236   // creation of a RenderFrameHost or RenderFrameProxyHost.
237   void increment_ref_count() { ++frames_ref_count_; }
238
239   // Decreases the refcounting on this RVH. This is done by the FrameTree on
240   // destruction of a RenderFrameHost or RenderFrameProxyHost.
241   void decrement_ref_count() { --frames_ref_count_; }
242
243   // Returns the refcount on this RVH, that is the number of RenderFrameHosts
244   // and RenderFrameProxyHosts currently using it.
245   int ref_count() { return frames_ref_count_; }
246
247   // NOTE: Do not add functions that just send an IPC message that are called in
248   // one or two places. Have the caller send the IPC message directly (unless
249   // the caller places are in different platforms, in which case it's better
250   // to keep them consistent).
251
252   void RetrieveWebState();
253   void RestoreWebState(const std::string& web_state);
254   void HandleSelectionDrop(int x, int y, base::string16& text,
255       base::string16& html_text);
256   void GetSelectionMarkup();
257
258  protected:
259   // RenderWidgetHostOwnerDelegate overrides.
260   bool OnMessageReceived(const IPC::Message& msg) override;
261   void RenderWidgetDidInit() override;
262   void RenderWidgetWillSetIsLoading(bool is_loading) override;
263   void RenderWidgetGotFocus() override;
264   void RenderWidgetDidForwardMouseEvent(
265       const blink::WebMouseEvent& mouse_event) override;
266   bool MayRenderWidgetForwardKeyboardEvent(
267       const NativeWebKeyboardEvent& key_event) override;
268
269   // IPC message handlers.
270   void OnShowView(int route_id,
271                   WindowOpenDisposition disposition,
272                   const gfx::Rect& initial_rect,
273                   bool user_gesture);
274   void OnShowWidget(int route_id, const gfx::Rect& initial_rect);
275   void OnShowFullscreenWidget(int route_id);
276   void OnRenderProcessGone(int status, int error_code);
277   void OnUpdateState(const PageState& state);
278   void OnUpdateTargetURL(const GURL& url);
279   void OnClose();
280   void OnRequestMove(const gfx::Rect& pos);
281   void OnDocumentAvailableInMainFrame(bool uses_temporary_zoom_level);
282   void OnDidContentsPreferredSizeChange(const gfx::Size& new_size);
283   void OnPasteFromSelectionClipboard();
284   void OnRouteCloseEvent();
285   void OnTakeFocus(bool reverse);
286   void OnFocusedNodeChanged(bool is_editable_node,
287                             const gfx::Rect& node_bounds_in_viewport,
288                             bool is_select_node,
289                             base::string16 node_id
290 #if defined(OS_TIZEN_TV_PRODUCT)
291                             ,
292                             const ViewHostMsg_FocusedNodeChanged_Params& params
293 #endif
294                             );
295   void OnClosePageACK();
296   void OnDidZoomURL(double zoom_level, const GURL& url);
297   void OnFocusedNodeTouched(bool editable);
298   void OnFocus();
299
300   void OnSelectedMarkup(const base::string16& markup) override;
301   void OnSelectedBitmap(const SkBitmap& bitmap) override;
302   void OnRecognizeArticleResult(
303       bool is_article, const base::string16& page_url);
304   void MoveFocusToPrevNextInput(bool is_next) override;
305
306   //SBROWSER_FORM_NAVIGATION
307   //FIXME: FORM Navigation Features to be re-implemented for m39
308   //Bug: http://107.108.218.239/bugzilla/show_bug.cgi?id=324 
309
310   //virtual void moveToPrevInput() override;
311   //virtual void moveToNextInput() override;
312
313  private:
314   // TODO(nasko): Temporarily friend RenderFrameHostImpl, so we don't duplicate
315   // utility functions and state needed in both classes, while we move frame
316   // specific code away from this class.
317   friend class RenderFrameHostImpl;
318   friend class TestRenderViewHost;
319   FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, BasicRenderFrameHost);
320   FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, RoutingIdSane);
321   FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
322                            CleanUpSwappedOutRVHOnProcessCrash);
323   FRIEND_TEST_ALL_PREFIXES(SitePerProcessBrowserTest,
324                            NavigateMainFrameToChildSite);
325
326   void RenderViewReady();
327
328   // TODO(creis): Move to a private namespace on RenderFrameHostImpl.
329   // Delay to wait on closing the WebContents for a beforeunload/unload handler
330   // to fire.
331   static const int64_t kUnloadTimeoutMS;
332
333   // Returns the content specific prefs for this RenderViewHost.
334   // TODO(creis): Move most of this method to RenderProcessHost, since it's
335   // mostly the same across all RVHs in a process.  Move the rest to RFH.
336   // See https://crbug.com/304341.
337   WebPreferences ComputeWebkitPrefs();
338
339   // The RenderWidgetHost.
340   std::unique_ptr<RenderWidgetHostImpl> render_widget_host_;
341
342   // The number of RenderFrameHosts which have a reference to this RVH.
343   int frames_ref_count_;
344
345   // Our delegate, which wants to know about changes in the RenderView.
346   RenderViewHostDelegate* delegate_;
347
348   // The SiteInstance associated with this RenderViewHost.  All pages drawn
349   // in this RenderViewHost are part of this SiteInstance.  Cannot change
350   // over time.
351   scoped_refptr<SiteInstanceImpl> instance_;
352
353   // A bitwise OR of bindings types that have been enabled for this RenderView.
354   // See BindingsPolicy for details.
355   int enabled_bindings_;
356
357   // Tracks whether this RenderViewHost is in an active state.  False if the
358   // main frame is pending swap out, pending deletion, or swapped out, because
359   // it is not visible to the user in any of these cases.
360   bool is_active_;
361
362   // Tracks whether the main frame RenderFrameHost is swapped out.  Unlike
363   // is_active_, this is false when the frame is pending swap out or deletion.
364   // TODO(creis): Remove this when we no longer use swappedout://.
365   // See http://crbug.com/357747.
366   bool is_swapped_out_;
367
368   // Routing ID for the main frame's RenderFrameHost.
369   int main_frame_routing_id_;
370
371   // Set to true when waiting for a ViewHostMsg_ClosePageACK.
372   // TODO(creis): Move to RenderFrameHost and RenderWidgetHost.
373   // See http://crbug.com/418265.
374   bool is_waiting_for_close_ack_;
375
376   // True if the render view can be shut down suddenly.
377   bool sudden_termination_allowed_;
378
379   // The termination status of the last render view that terminated.
380   base::TerminationStatus render_view_termination_status_;
381
382   // True if the current focused element is editable.
383   bool is_focused_element_editable_;
384
385   // This is updated every time UpdateWebkitPreferences is called. That method
386   // is in turn called when any of the settings change that the WebPreferences
387   // values depend on.
388   std::unique_ptr<WebPreferences> web_preferences_;
389
390   bool updating_web_preferences_;
391
392   bool render_view_ready_on_process_launch_;
393
394   base::WeakPtrFactory<RenderViewHostImpl> weak_factory_;
395
396   DISALLOW_COPY_AND_ASSIGN(RenderViewHostImpl);
397 };
398
399 }  // namespace content
400
401 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_