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.
5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/string16.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/navigation_type.h"
16 #include "content/public/common/media_stream_request.h"
17 #include "content/public/common/page_transition_types.h"
18 #include "content/public/common/window_container_type.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "ui/base/window_open_disposition.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gfx/rect_f.h"
35 class JavaScriptDialogManager
;
38 class WebContentsImpl
;
39 struct ContextMenuParams
;
40 struct FileChooserParams
;
41 struct NativeWebKeyboardEvent
;
59 // Objects implement this interface to get notified about changes in the
60 // WebContents and to provide necessary functionality.
61 class CONTENT_EXPORT WebContentsDelegate
{
63 WebContentsDelegate();
65 // Opens a new URL inside the passed in WebContents (if source is 0 open
66 // in the current front-most tab), unless |disposition| indicates the url
67 // should be opened in a new tab or window.
69 // A NULL source indicates the current tab (callers should probably use
70 // OpenURL() for these cases which does it for you).
72 // Returns the WebContents the URL is opened in, or NULL if the URL wasn't
73 // opened immediately.
74 virtual WebContents
* OpenURLFromTab(WebContents
* source
,
75 const OpenURLParams
& params
);
77 // Called to inform the delegate that the WebContents's navigation state
78 // changed. The |changed_flags| indicates the parts of the navigation state
79 // that have been updated, and is any combination of the
80 // |WebContents::InvalidateTypes| bits.
81 virtual void NavigationStateChanged(const WebContents
* source
,
82 unsigned changed_flags
) {}
84 // Adds the navigation request headers to |headers|. Use
85 // net::HttpUtil::AppendHeaderIfMissing to build the set of headers.
86 virtual void AddNavigationHeaders(const GURL
& url
, std::string
* headers
) {}
88 // Creates a new tab with the already-created WebContents 'new_contents'.
89 // The window for the added contents should be reparented correctly when this
90 // method returns. If |disposition| is NEW_POPUP, |pos| should hold the
91 // initial position. If |was_blocked| is non-NULL, then |*was_blocked| will
92 // be set to true if the popup gets blocked, and left unchanged otherwise.
93 virtual void AddNewContents(WebContents
* source
,
94 WebContents
* new_contents
,
95 WindowOpenDisposition disposition
,
96 const gfx::Rect
& initial_pos
,
100 // Selects the specified contents, bringing its container to the front.
101 virtual void ActivateContents(WebContents
* contents
) {}
103 // Deactivates the specified contents by deactivating its container and
104 // potentialy moving it to the back of the Z order.
105 virtual void DeactivateContents(WebContents
* contents
) {}
107 // Notifies the delegate that this contents is starting or is done loading
108 // some resource. The delegate should use this notification to represent
109 // loading feedback. See WebContents::IsLoading()
110 virtual void LoadingStateChanged(WebContents
* source
) {}
112 #if defined(OS_ANDROID)
113 // Notifies the delegate that the page has made some progress loading.
114 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully
116 virtual void LoadProgressChanged(WebContents
* source
,
120 // Request the delegate to close this web contents, and do whatever cleanup
122 virtual void CloseContents(WebContents
* source
) {}
124 // Informs the delegate that the underlying RenderViewHost has been swapped
125 // out so it can perform any cleanup necessary.
126 virtual void SwappedOut(WebContents
* source
) {}
128 // Request the delegate to move this WebContents to the specified position
129 // in screen coordinates.
130 virtual void MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {}
132 // Called to determine if the WebContents is contained in a popup window
133 // or a panel window.
134 virtual bool IsPopupOrPanel(const WebContents
* source
) const;
136 // Notification that the target URL has changed.
137 virtual void UpdateTargetURL(WebContents
* source
,
141 // Notification that there was a mouse event, along with the absolute
142 // coordinates of the mouse pointer and whether it was a normal motion event
143 // (otherwise, the pointer left the contents area).
144 virtual void ContentsMouseEvent(WebContents
* source
,
145 const gfx::Point
& location
,
148 // Request the delegate to change the zoom level of the current tab.
149 virtual void ContentsZoomChange(bool zoom_in
) {}
151 // Called to determine if the WebContents can be overscrolled with touch/wheel
153 virtual bool CanOverscrollContent() const;
155 // Check whether this contents is permitted to load data URLs in WebUI mode.
156 // This is normally disallowed for security.
157 virtual bool CanLoadDataURLsInWebUI() const;
159 // Return the rect where to display the resize corner, if any, otherwise
161 virtual gfx::Rect
GetRootWindowResizerRect() const;
163 // Invoked prior to showing before unload handler confirmation dialog.
164 virtual void WillRunBeforeUnloadConfirm() {}
166 // Returns true if javascript dialogs and unload alerts are suppressed.
168 virtual bool ShouldSuppressDialogs();
170 // Add a message to the console. Returning true indicates that the delegate
171 // handled the message. If false is returned the default logging mechanism
172 // will be used for the message.
173 virtual bool AddMessageToConsole(WebContents
* source
,
175 const string16
& message
,
177 const string16
& source_id
);
179 // Tells us that we've finished firing this tab's beforeunload event.
180 // The proceed bool tells us whether the user chose to proceed closing the
181 // tab. Returns true if the tab can continue on firing its unload event.
182 // If we're closing the entire browser, then we'll want to delay firing
183 // unload events until all the beforeunload events have fired.
184 virtual void BeforeUnloadFired(WebContents
* tab
,
186 bool* proceed_to_fire_unload
);
188 // Returns true if the location bar should be focused by default rather than
189 // the page contents. NOTE: this is only used if WebContents can't determine
190 // for itself whether the location bar should be focused by default. For a
191 // complete check, you should use WebContents::FocusLocationBarByDefault().
192 virtual bool ShouldFocusLocationBarByDefault(WebContents
* source
);
194 // Sets focus to the location bar or some other place that is appropriate.
195 // This is called when the tab wants to encourage user input, like for the
197 virtual void SetFocusToLocationBar(bool select_all
) {}
199 // Returns whether the page should be focused when transitioning from crashed
200 // to live. Default is true.
201 virtual bool ShouldFocusPageAfterCrash();
203 // Called when a popup select is about to be displayed. The delegate can use
204 // this to disable inactive rendering for the frame in the window the select
205 // is opened within if necessary.
206 virtual void RenderWidgetShowing() {}
208 // This is called when WebKit tells us that it is done tabbing through
209 // controls on the page. Provides a way for WebContentsDelegates to handle
210 // this. Returns true if the delegate successfully handled it.
211 virtual bool TakeFocus(WebContents
* source
,
214 // Invoked when the page loses mouse capture.
215 virtual void LostCapture() {}
217 // Notification that |contents| has gained focus.
218 virtual void WebContentsFocused(WebContents
* contents
) {}
220 // Asks the delegate if the given tab can download.
221 // Invoking the |callback| synchronously is OK.
222 virtual void CanDownload(RenderViewHost
* render_view_host
,
224 const std::string
& request_method
,
225 const base::Callback
<void(bool)>& callback
);
227 // Return much extra vertical space should be allotted to the
228 // render view widget during various animations (e.g. infobar closing).
229 // This is used to make painting look smoother.
230 virtual int GetExtraRenderViewHeight() const;
232 // Returns true if the context menu operation was handled by the delegate.
233 virtual bool HandleContextMenu(const content::ContextMenuParams
& params
);
235 // Opens source view for given WebContents that is navigated to the given
237 virtual void ViewSourceForTab(WebContents
* source
, const GURL
& page_url
);
239 // Opens source view for the given subframe.
240 virtual void ViewSourceForFrame(WebContents
* source
,
242 const std::string
& content_state
);
244 // Allows delegates to handle keyboard events before sending to the renderer.
245 // Returns true if the |event| was handled. Otherwise, if the |event| would be
246 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
247 // |*is_keyboard_shortcut| should be set to true.
248 virtual bool PreHandleKeyboardEvent(WebContents
* source
,
249 const NativeWebKeyboardEvent
& event
,
250 bool* is_keyboard_shortcut
);
252 // Allows delegates to handle unhandled keyboard messages coming back from
254 virtual void HandleKeyboardEvent(WebContents
* source
,
255 const NativeWebKeyboardEvent
& event
) {}
257 virtual void HandleMouseDown() {}
258 virtual void HandleMouseUp() {}
260 // Handles activation resulting from a pointer event (e.g. when mouse is
261 // pressed, or a touch-gesture begins).
262 virtual void HandlePointerActivate() {}
264 virtual void HandleGestureBegin() {}
265 virtual void HandleGestureEnd() {}
267 // Render view drag n drop ended.
268 virtual void DragEnded() {}
270 // Shows the repost form confirmation dialog box.
271 virtual void ShowRepostFormWarningDialog(WebContents
* source
) {}
273 // Allows delegate to override navigation to the history entries.
274 // Returns true to allow WebContents to continue with the default processing.
275 virtual bool OnGoToEntryOffset(int offset
);
277 // Allows delegate to control whether a WebContents will be created. Returns
278 // true to allow the creation. Default is to allow it. In cases where the
279 // delegate handles the creation/navigation itself, it will use |target_url|.
280 virtual bool ShouldCreateWebContents(
281 WebContents
* web_contents
,
283 WindowContainerType window_container_type
,
284 const string16
& frame_name
,
285 const GURL
& target_url
);
287 // Notifies the delegate about the creation of a new WebContents. This
288 // typically happens when popups are created.
289 virtual void WebContentsCreated(WebContents
* source_contents
,
290 int64 source_frame_id
,
291 const string16
& frame_name
,
292 const GURL
& target_url
,
293 WebContents
* new_contents
) {}
295 // Notifies the delegate that the content restrictions for this tab has
297 virtual void ContentRestrictionsChanged(WebContents
* source
) {}
299 // Notification that the tab is hung.
300 virtual void RendererUnresponsive(WebContents
* source
) {}
302 // Notification that the tab is no longer hung.
303 virtual void RendererResponsive(WebContents
* source
) {}
305 // Notification that a worker associated with this tab has crashed.
306 virtual void WorkerCrashed(WebContents
* source
) {}
308 // Invoked when a main fram navigation occurs.
309 virtual void DidNavigateMainFramePostCommit(WebContents
* source
) {}
311 // Invoked when navigating to a pending entry. When invoked the
312 // NavigationController has configured its pending entry, but it has not yet
314 virtual void DidNavigateToPendingEntry(WebContents
* source
) {}
316 // Returns a pointer to a service to manage JavaScript dialogs. May return
317 // NULL in which case dialogs aren't shown.
318 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager();
320 // Called when color chooser should open. Returns the opened color chooser.
321 // Ownership of the returned pointer is transferred to the caller.
322 virtual content::ColorChooser
* OpenColorChooser(WebContents
* web_contents
,
323 int color_chooser_id
,
326 virtual void DidEndColorChooser() {}
328 // Called when a file selection is to be done.
329 virtual void RunFileChooser(WebContents
* web_contents
,
330 const FileChooserParams
& params
) {}
332 // Request to enumerate a directory. This is equivalent to running the file
333 // chooser in directory-enumeration mode and having the user select the given
335 virtual void EnumerateDirectory(WebContents
* web_contents
,
337 const base::FilePath
& path
) {}
339 // Called when the renderer puts a tab into or out of fullscreen mode.
340 virtual void ToggleFullscreenModeForTab(WebContents
* web_contents
,
341 bool enter_fullscreen
) {}
342 virtual bool IsFullscreenForTabOrPending(
343 const WebContents
* web_contents
) const;
345 // Called when a Javascript out of memory notification is received.
346 virtual void JSOutOfMemory(WebContents
* web_contents
) {}
348 // Register a new handler for URL requests with the given scheme.
349 // |user_gesture| is true if the registration is made in the context of a user
351 virtual void RegisterProtocolHandler(WebContents
* web_contents
,
352 const std::string
& protocol
,
354 const string16
& title
,
355 bool user_gesture
) {}
357 // Result of string search in the page. This includes the number of matches
358 // found and the selection rect (in screen coordinates) for the string found.
359 // If |final_update| is false, it indicates that more results follow.
360 virtual void FindReply(WebContents
* web_contents
,
362 int number_of_matches
,
363 const gfx::Rect
& selection_rect
,
364 int active_match_ordinal
,
365 bool final_update
) {}
367 #if defined(OS_ANDROID)
368 // Provides the rects of the current find-in-page matches.
369 // Sent as a reply to RequestFindMatchRects.
370 virtual void FindMatchRectsReply(WebContents
* web_contents
,
372 const std::vector
<gfx::RectF
>& rects
,
373 const gfx::RectF
& active_rect
) {}
376 // Invoked when the preferred size of the contents has been changed.
377 virtual void UpdatePreferredSize(WebContents
* web_contents
,
378 const gfx::Size
& pref_size
) {}
380 // Invoked when the contents auto-resized and the container should match it.
381 virtual void ResizeDueToAutoResize(WebContents
* web_contents
,
382 const gfx::Size
& new_size
) {}
384 // Notification message from HTML UI.
385 virtual void WebUISend(WebContents
* web_contents
,
386 const GURL
& source_url
,
387 const std::string
& name
,
388 const base::ListValue
& args
) {}
390 // Requests to lock the mouse. Once the request is approved or rejected,
391 // GotResponseToLockMouseRequest() will be called on the requesting tab
393 virtual void RequestToLockMouse(WebContents
* web_contents
,
395 bool last_unlocked_by_target
) {}
397 // Notification that the page has lost the mouse lock.
398 virtual void LostMouseLock() {}
400 // Asks permission to use the camera and/or microphone. If permission is
401 // granted, a call should be made to |callback| with the devices. If the
402 // request is denied, a call should be made to |callback| with an empty list
403 // of devices. |request| has the details of the request (e.g. which of audio
404 // and/or video devices are requested, and lists of available devices).
405 virtual void RequestMediaAccessPermission(
406 WebContents
* web_contents
,
407 const MediaStreamRequest
& request
,
408 const MediaResponseCallback
& callback
) {}
410 // Requests permission to access the PPAPI broker. The delegate should return
411 // true and call the passed in |callback| with the result, or return false
412 // to indicate that it does not support asking for permission.
413 virtual bool RequestPpapiBrokerPermission(
414 WebContents
* web_contents
,
416 const base::FilePath
& plugin_path
,
417 const base::Callback
<void(bool)>& callback
);
420 virtual ~WebContentsDelegate();
423 friend class WebContentsImpl
;
425 // Called when |this| becomes the WebContentsDelegate for |source|.
426 void Attach(WebContents
* source
);
428 // Called when |this| is no longer the WebContentsDelegate for |source|.
429 void Detach(WebContents
* source
);
431 // The WebContents that this is currently a delegate for.
432 std::set
<WebContents
*> attached_contents_
;
435 } // namespace content
437 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_