• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DELEGATE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_DELEGATE_H_
7 
8 #include <string>
9 
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/i18n/rtl.h"
13 #include "base/process/kill.h"
14 #include "base/strings/string16.h"
15 #include "content/common/content_export.h"
16 #include "content/public/common/javascript_message_type.h"
17 #include "content/public/common/media_stream_request.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "net/base/load_states.h"
20 #include "third_party/WebKit/public/web/WebPopupType.h"
21 #include "ui/base/window_open_disposition.h"
22 
23 class GURL;
24 class SkBitmap;
25 class WebKeyboardEvent;
26 struct ViewHostMsg_CreateWindow_Params;
27 struct ViewHostMsg_DidFailProvisionalLoadWithError_Params;
28 struct ViewHostMsg_FrameNavigate_Params;
29 struct ViewMsg_PostMessage_Params;
30 struct WebPreferences;
31 
32 namespace base {
33 class ListValue;
34 class TimeTicks;
35 }
36 
37 namespace IPC {
38 class Message;
39 }
40 
41 namespace gfx {
42 class Point;
43 class Rect;
44 class Size;
45 }
46 
47 namespace content {
48 
49 class BrowserContext;
50 class FrameTree;
51 class PageState;
52 class RenderViewHost;
53 class RenderViewHostDelegateView;
54 class SessionStorageNamespace;
55 class SiteInstance;
56 class WebContents;
57 class WebContentsImpl;
58 struct ContextMenuParams;
59 struct FileChooserParams;
60 struct GlobalRequestID;
61 struct NativeWebKeyboardEvent;
62 struct Referrer;
63 struct RendererPreferences;
64 struct ResourceRedirectDetails;
65 struct ResourceRequestDetails;
66 
67 //
68 // RenderViewHostDelegate
69 //
70 //  An interface implemented by an object interested in knowing about the state
71 //  of the RenderViewHost.
72 //
73 //  This interface currently encompasses every type of message that was
74 //  previously being sent by WebContents itself. Some of these notifications
75 //  may not be relevant to all users of RenderViewHost and we should consider
76 //  exposing a more generic Send function on RenderViewHost and a response
77 //  listener here to serve that need.
78 class CONTENT_EXPORT RenderViewHostDelegate {
79  public:
80   // RendererManagerment -------------------------------------------------------
81   // Functions for managing switching of Renderers. For WebContents, this is
82   // implemented by the RenderFrameHostManager.
83 
84   class CONTENT_EXPORT RendererManagement {
85    public:
86     // Notification whether we should close the page, after an explicit call to
87     // AttemptToClosePage.  This is called before a cross-site request or before
88     // a tab/window is closed (as indicated by the first parameter) to allow the
89     // appropriate renderer to approve or deny the request.  |proceed| indicates
90     // whether the user chose to proceed.  |proceed_time| is the time when the
91     // request was allowed to proceed.
92     virtual void ShouldClosePage(
93         bool for_cross_site_transition,
94         bool proceed,
95         const base::TimeTicks& proceed_time) = 0;
96 
97     // The |pending_render_view_host| is ready to commit a page.  The delegate
98     // should ensure that the old RenderViewHost runs its unload handler first
99     // and determine whether a RenderViewHost transfer is needed.
100     virtual void OnCrossSiteResponse(
101         RenderViewHost* pending_render_view_host,
102         const GlobalRequestID& global_request_id,
103         bool is_transfer,
104         const std::vector<GURL>& transfer_url_chain,
105         const Referrer& referrer,
106         PageTransition page_transition,
107         int64 frame_id,
108         bool should_replace_current_entry) = 0;
109 
110    protected:
~RendererManagement()111     virtual ~RendererManagement() {}
112   };
113 
114   // ---------------------------------------------------------------------------
115 
116   // Returns the current delegate associated with a feature. May return NULL if
117   // there is no corresponding delegate.
118   virtual RenderViewHostDelegateView* GetDelegateView();
119   virtual RendererManagement* GetRendererManagementDelegate();
120 
121   // This is used to give the delegate a chance to filter IPC messages.
122   virtual bool OnMessageReceived(RenderViewHost* render_view_host,
123                                  const IPC::Message& message);
124 
125   // Gets the URL that is currently being displayed, if there is one.
126   virtual const GURL& GetURL() const;
127 
128   // Return this object cast to a WebContents, if it is one. If the object is
129   // not a WebContents, returns NULL. DEPRECATED: Be sure to include brettw or
130   // jam as reviewers before you use this method. http://crbug.com/82582
131   virtual WebContents* GetAsWebContents();
132 
133   // Return the rect where to display the resize corner, if any, otherwise
134   // an empty rect.
135   virtual gfx::Rect GetRootWindowResizerRect() const = 0;
136 
137   // The RenderView is being constructed (message sent to the renderer process
138   // to construct a RenderView).  Now is a good time to send other setup events
139   // to the RenderView.  This precedes any other commands to the RenderView.
RenderViewCreated(RenderViewHost * render_view_host)140   virtual void RenderViewCreated(RenderViewHost* render_view_host) {}
141 
142   // The RenderView has been constructed.
RenderViewReady(RenderViewHost * render_view_host)143   virtual void RenderViewReady(RenderViewHost* render_view_host) {}
144 
145   // The RenderView died somehow (crashed or was killed by the user).
RenderViewTerminated(RenderViewHost * render_view_host,base::TerminationStatus status,int error_code)146   virtual void RenderViewTerminated(RenderViewHost* render_view_host,
147                                     base::TerminationStatus status,
148                                     int error_code) {}
149 
150   // The RenderView is going to be deleted. This is called when each
151   // RenderView is going to be destroyed
RenderViewDeleted(RenderViewHost * render_view_host)152   virtual void RenderViewDeleted(RenderViewHost* render_view_host) {}
153 
154   // The RenderView processed a redirect during a provisional load.
155   //
156   // TODO(creis): Remove this method and have the pre-rendering code listen to
157   // WebContentsObserver::DidGetRedirectForResourceRequest instead.
158   // See http://crbug.com/78512.
DidRedirectProvisionalLoad(RenderViewHost * render_view_host,int32 page_id,const GURL & source_url,const GURL & target_url)159   virtual void DidRedirectProvisionalLoad(
160       RenderViewHost* render_view_host,
161       int32 page_id,
162       const GURL& source_url,
163       const GURL& target_url) {}
164 
165   // A provisional load in the RenderView failed.
DidFailProvisionalLoadWithError(RenderViewHost * render_view_host,const ViewHostMsg_DidFailProvisionalLoadWithError_Params & params)166   virtual void DidFailProvisionalLoadWithError(
167       RenderViewHost* render_view_host,
168       const ViewHostMsg_DidFailProvisionalLoadWithError_Params& params) {}
169 
170   // A response has been received for a resource request.
DidGetResourceResponseStart(const ResourceRequestDetails & details)171   virtual void DidGetResourceResponseStart(
172       const ResourceRequestDetails& details) {}
173 
174   // A redirect was received while requesting a resource.
DidGetRedirectForResourceRequest(const ResourceRedirectDetails & details)175   virtual void DidGetRedirectForResourceRequest(
176       const ResourceRedirectDetails& details) {}
177 
178   // The RenderView was navigated to a different page.
DidNavigate(RenderViewHost * render_view_host,const ViewHostMsg_FrameNavigate_Params & params)179   virtual void DidNavigate(RenderViewHost* render_view_host,
180                            const ViewHostMsg_FrameNavigate_Params& params) {}
181 
182   // The state for the page changed and should be updated.
UpdateState(RenderViewHost * render_view_host,int32 page_id,const PageState & state)183   virtual void UpdateState(RenderViewHost* render_view_host,
184                            int32 page_id,
185                            const PageState& state) {}
186 
187   // The page's title was changed and should be updated.
UpdateTitle(RenderViewHost * render_view_host,int32 page_id,const base::string16 & title,base::i18n::TextDirection title_direction)188   virtual void UpdateTitle(RenderViewHost* render_view_host,
189                            int32 page_id,
190                            const base::string16& title,
191                            base::i18n::TextDirection title_direction) {}
192 
193   // The page's encoding was changed and should be updated.
UpdateEncoding(RenderViewHost * render_view_host,const std::string & encoding)194   virtual void UpdateEncoding(RenderViewHost* render_view_host,
195                               const std::string& encoding) {}
196 
197   // The destination URL has changed should be updated
UpdateTargetURL(int32 page_id,const GURL & url)198   virtual void UpdateTargetURL(int32 page_id, const GURL& url) {}
199 
200   // The page is trying to close the RenderView's representation in the client.
Close(RenderViewHost * render_view_host)201   virtual void Close(RenderViewHost* render_view_host) {}
202 
203   // The RenderViewHost has been swapped out.
SwappedOut(RenderViewHost * render_view_host)204   virtual void SwappedOut(RenderViewHost* render_view_host) {}
205 
206   // The page is trying to move the RenderView's representation in the client.
RequestMove(const gfx::Rect & new_bounds)207   virtual void RequestMove(const gfx::Rect& new_bounds) {}
208 
209   // The RenderView began loading a new page. This corresponds to WebKit's
210   // notion of the throbber starting.
DidStartLoading(RenderViewHost * render_view_host)211   virtual void DidStartLoading(RenderViewHost* render_view_host) {}
212 
213   // The RenderView stopped loading a page. This corresponds to WebKit's
214   // notion of the throbber stopping.
DidStopLoading(RenderViewHost * render_view_host)215   virtual void DidStopLoading(RenderViewHost* render_view_host) {}
216 
217   // The pending page load was canceled.
DidCancelLoading()218   virtual void DidCancelLoading() {}
219 
220   // The RenderView made progress loading a page's top frame.
221   // |progress| is a value between 0 (nothing loaded) to 1.0 (top frame
222   // entirely loaded).
DidChangeLoadProgress(double progress)223   virtual void DidChangeLoadProgress(double progress) {}
224 
225   // The RenderView set its opener to null, disowning it for the lifetime of
226   // the window.
DidDisownOpener(RenderViewHost * rvh)227   virtual void DidDisownOpener(RenderViewHost* rvh) {}
228 
229   // Another page accessed the initial empty document of this RenderView,
230   // which means it is no longer safe to display a pending URL without
231   // risking a URL spoof.
DidAccessInitialDocument()232   virtual void DidAccessInitialDocument() {}
233 
234   // The RenderView's main frame document element is ready. This happens when
235   // the document has finished parsing.
DocumentAvailableInMainFrame(RenderViewHost * render_view_host)236   virtual void DocumentAvailableInMainFrame(RenderViewHost* render_view_host) {}
237 
238   // The onload handler in the RenderView's main frame has completed.
DocumentOnLoadCompletedInMainFrame(RenderViewHost * render_view_host,int32 page_id)239   virtual void DocumentOnLoadCompletedInMainFrame(
240       RenderViewHost* render_view_host,
241       int32 page_id) {}
242 
243   // The page wants to open a URL with the specified disposition.
RequestOpenURL(RenderViewHost * rvh,const GURL & url,const Referrer & referrer,WindowOpenDisposition disposition,int64 source_frame_id,bool is_redirect,bool user_gesture)244   virtual void RequestOpenURL(RenderViewHost* rvh,
245                               const GURL& url,
246                               const Referrer& referrer,
247                               WindowOpenDisposition disposition,
248                               int64 source_frame_id,
249                               bool is_redirect,
250                               bool user_gesture) {}
251 
252   // The page wants to transfer the request to a new renderer.
253   // |redirect_chain| contains any redirect URLs (excluding |url|) that happened
254   // before the transfer.
RequestTransferURL(const GURL & url,const std::vector<GURL> & redirect_chain,const Referrer & referrer,PageTransition page_transition,WindowOpenDisposition disposition,int64 source_frame_id,const GlobalRequestID & old_request_id,bool is_redirect,bool user_gesture)255   virtual void RequestTransferURL(
256       const GURL& url,
257       const std::vector<GURL>& redirect_chain,
258       const Referrer& referrer,
259       PageTransition page_transition,
260       WindowOpenDisposition disposition,
261       int64 source_frame_id,
262       const GlobalRequestID& old_request_id,
263       bool is_redirect,
264       bool user_gesture) {}
265 
266   // The page wants to close the active view in this tab.
RouteCloseEvent(RenderViewHost * rvh)267   virtual void RouteCloseEvent(RenderViewHost* rvh) {}
268 
269   // The page wants to post a message to the active view in this tab.
RouteMessageEvent(RenderViewHost * rvh,const ViewMsg_PostMessage_Params & params)270   virtual void RouteMessageEvent(
271       RenderViewHost* rvh,
272       const ViewMsg_PostMessage_Params& params) {}
273 
274   // A javascript message, confirmation or prompt should be shown.
RunJavaScriptMessage(RenderViewHost * rvh,const base::string16 & message,const base::string16 & default_prompt,const GURL & frame_url,JavaScriptMessageType type,IPC::Message * reply_msg,bool * did_suppress_message)275   virtual void RunJavaScriptMessage(RenderViewHost* rvh,
276                                     const base::string16& message,
277                                     const base::string16& default_prompt,
278                                     const GURL& frame_url,
279                                     JavaScriptMessageType type,
280                                     IPC::Message* reply_msg,
281                                     bool* did_suppress_message) {}
282 
RunBeforeUnloadConfirm(RenderViewHost * rvh,const base::string16 & message,bool is_reload,IPC::Message * reply_msg)283   virtual void RunBeforeUnloadConfirm(RenderViewHost* rvh,
284                                       const base::string16& message,
285                                       bool is_reload,
286                                       IPC::Message* reply_msg) {}
287 
288   // A message was added to to the console.
289   virtual bool AddMessageToConsole(int32 level,
290                                    const base::string16& message,
291                                    int32 line_no,
292                                    const base::string16& source_id);
293 
294   // Return a dummy RendererPreferences object that will be used by the renderer
295   // associated with the owning RenderViewHost.
296   virtual RendererPreferences GetRendererPrefs(
297       BrowserContext* browser_context) const = 0;
298 
299   // Returns a WebPreferences object that will be used by the renderer
300   // associated with the owning render view host.
301   virtual WebPreferences GetWebkitPrefs();
302 
303   // Notification the user has made a gesture while focus was on the
304   // page. This is used to avoid uninitiated user downloads (aka carpet
305   // bombing), see DownloadRequestLimiter for details.
OnUserGesture()306   virtual void OnUserGesture() {}
307 
308   // Notification from the renderer host that blocked UI event occurred.
309   // This happens when there are tab-modal dialogs. In this case, the
310   // notification is needed to let us draw attention to the dialog (i.e.
311   // refocus on the modal dialog, flash title etc).
OnIgnoredUIEvent()312   virtual void OnIgnoredUIEvent() {}
313 
314   // Notification that the renderer has become unresponsive. The
315   // delegate can use this notification to show a warning to the user.
RendererUnresponsive(RenderViewHost * render_view_host,bool is_during_before_unload,bool is_during_unload)316   virtual void RendererUnresponsive(RenderViewHost* render_view_host,
317                                     bool is_during_before_unload,
318                                     bool is_during_unload) {}
319 
320   // Notification that a previously unresponsive renderer has become
321   // responsive again. The delegate can use this notification to end the
322   // warning shown to the user.
RendererResponsive(RenderViewHost * render_view_host)323   virtual void RendererResponsive(RenderViewHost* render_view_host) {}
324 
325   // Notification that the RenderViewHost's load state changed.
LoadStateChanged(const GURL & url,const net::LoadStateWithParam & load_state,uint64 upload_position,uint64 upload_size)326   virtual void LoadStateChanged(const GURL& url,
327                                 const net::LoadStateWithParam& load_state,
328                                 uint64 upload_position,
329                                 uint64 upload_size) {}
330 
331   // Notification that a worker process has crashed.
WorkerCrashed()332   virtual void WorkerCrashed() {}
333 
334   // The page wants the hosting window to activate/deactivate itself (it
335   // called the JavaScript window.focus()/blur() method).
Activate()336   virtual void Activate() {}
Deactivate()337   virtual void Deactivate() {}
338 
339   // Notification that the view has lost capture.
LostCapture()340   virtual void LostCapture() {}
341 
342   // Notifications about mouse events in this view.  This is useful for
343   // implementing global 'on hover' features external to the view.
HandleMouseMove()344   virtual void HandleMouseMove() {}
HandleMouseDown()345   virtual void HandleMouseDown() {}
HandleMouseLeave()346   virtual void HandleMouseLeave() {}
HandleMouseUp()347   virtual void HandleMouseUp() {}
HandlePointerActivate()348   virtual void HandlePointerActivate() {}
HandleGestureBegin()349   virtual void HandleGestureBegin() {}
HandleGestureEnd()350   virtual void HandleGestureEnd() {}
351 
352   // Called when a file selection is to be done.
RunFileChooser(RenderViewHost * render_view_host,const FileChooserParams & params)353   virtual void RunFileChooser(
354       RenderViewHost* render_view_host,
355       const FileChooserParams& params) {}
356 
357   // Notification that the page wants to go into or out of fullscreen mode.
ToggleFullscreenMode(bool enter_fullscreen)358   virtual void ToggleFullscreenMode(bool enter_fullscreen) {}
359   virtual bool IsFullscreenForCurrentTab() const;
360 
361   // The contents' preferred size changed.
UpdatePreferredSize(const gfx::Size & pref_size)362   virtual void UpdatePreferredSize(const gfx::Size& pref_size) {}
363 
364   // The contents auto-resized and the container should match it.
ResizeDueToAutoResize(const gfx::Size & new_size)365   virtual void ResizeDueToAutoResize(const gfx::Size& new_size) {}
366 
367   // Requests to lock the mouse. Once the request is approved or rejected,
368   // GotResponseToLockMouseRequest() will be called on the requesting render
369   // view host.
RequestToLockMouse(bool user_gesture,bool last_unlocked_by_target)370   virtual void RequestToLockMouse(bool user_gesture,
371                                   bool last_unlocked_by_target) {}
372 
373   // Notification that the view has lost the mouse lock.
LostMouseLock()374   virtual void LostMouseLock() {}
375 
376   // The page is trying to open a new page (e.g. a popup window). The window
377   // should be created associated with the given |route_id| in process
378   // |render_process_id|, but it should not be shown yet. That should happen in
379   // response to ShowCreatedWindow.
380   // |params.window_container_type| describes the type of RenderViewHost
381   // container that is requested -- in particular, the window.open call may
382   // have specified 'background' and 'persistent' in the feature string.
383   //
384   // The passed |params.frame_name| parameter is the name parameter that was
385   // passed to window.open(), and will be empty if none was passed.
386   //
387   // Note: this is not called "CreateWindow" because that will clash with
388   // the Windows function which is actually a #define.
CreateNewWindow(int render_process_id,int route_id,int main_frame_route_id,const ViewHostMsg_CreateWindow_Params & params,SessionStorageNamespace * session_storage_namespace)389   virtual void CreateNewWindow(
390       int render_process_id,
391       int route_id,
392       int main_frame_route_id,
393       const ViewHostMsg_CreateWindow_Params& params,
394       SessionStorageNamespace* session_storage_namespace) {}
395 
396   // The page is trying to open a new widget (e.g. a select popup). The
397   // widget should be created associated with the given |route_id| in the
398   // process |render_process_id|, but it should not be shown yet. That should
399   // happen in response to ShowCreatedWidget.
400   // |popup_type| indicates if the widget is a popup and what kind of popup it
401   // is (select, autofill...).
CreateNewWidget(int render_process_id,int route_id,blink::WebPopupType popup_type)402   virtual void CreateNewWidget(int render_process_id,
403                                int route_id,
404                                blink::WebPopupType popup_type) {}
405 
406   // Creates a full screen RenderWidget. Similar to above.
CreateNewFullscreenWidget(int render_process_id,int route_id)407   virtual void CreateNewFullscreenWidget(int render_process_id, int route_id) {}
408 
409   // Show a previously created page with the specified disposition and bounds.
410   // The window is identified by the route_id passed to CreateNewWindow.
411   //
412   // Note: this is not called "ShowWindow" because that will clash with
413   // the Windows function which is actually a #define.
ShowCreatedWindow(int route_id,WindowOpenDisposition disposition,const gfx::Rect & initial_pos,bool user_gesture)414   virtual void ShowCreatedWindow(int route_id,
415                                  WindowOpenDisposition disposition,
416                                  const gfx::Rect& initial_pos,
417                                  bool user_gesture) {}
418 
419   // Show the newly created widget with the specified bounds.
420   // The widget is identified by the route_id passed to CreateNewWidget.
ShowCreatedWidget(int route_id,const gfx::Rect & initial_pos)421   virtual void ShowCreatedWidget(int route_id,
422                                  const gfx::Rect& initial_pos) {}
423 
424   // Show the newly created full screen widget. Similar to above.
ShowCreatedFullscreenWidget(int route_id)425   virtual void ShowCreatedFullscreenWidget(int route_id) {}
426 
427   // A context menu should be shown, to be built using the context information
428   // provided in the supplied params.
ShowContextMenu(const ContextMenuParams & params)429   virtual void ShowContextMenu(const ContextMenuParams& params) {}
430 
431   // The render view has requested access to media devices listed in
432   // |request|, and the client should grant or deny that permission by
433   // calling |callback|.
RequestMediaAccessPermission(const MediaStreamRequest & request,const MediaResponseCallback & callback)434   virtual void RequestMediaAccessPermission(
435       const MediaStreamRequest& request,
436       const MediaResponseCallback& callback) {}
437 
438   // Returns the SessionStorageNamespace the render view should use. Might
439   // create the SessionStorageNamespace on the fly.
440   virtual SessionStorageNamespace* GetSessionStorageNamespace(
441       SiteInstance* instance);
442 
443   // Returns the FrameTree the render view should use. Guaranteed to be constant
444   // for the lifetime of the render view.
445   //
446   // TODO(ajwong): Remove once the main frame RenderFrameHost is no longer
447   // created by the RenderViewHost.
448   virtual FrameTree* GetFrameTree();
449 
450  protected:
~RenderViewHostDelegate()451   virtual ~RenderViewHostDelegate() {}
452 };
453 
454 }  // namespace content
455 
456 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_DELEGATE_H_
457