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