Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / browser / web_contents_delegate.h
blob1367f0b6f9bca809bab22d6ad15d68d923a87cd0
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_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/strings/string16.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/invalidate_type.h"
16 #include "content/public/browser/navigation_type.h"
17 #include "content/public/common/media_stream_request.h"
18 #include "content/public/common/window_container_type.h"
19 #include "third_party/WebKit/public/web/WebDragOperation.h"
20 #include "third_party/skia/include/core/SkColor.h"
21 #include "ui/base/window_open_disposition.h"
22 #include "ui/gfx/geometry/rect_f.h"
23 #include "ui/gfx/native_widget_types.h"
25 class GURL;
27 namespace base {
28 class FilePath;
29 class ListValue;
32 namespace content {
33 class BrowserContext;
34 class ColorChooser;
35 class DownloadItem;
36 class JavaScriptDialogManager;
37 class PageState;
38 class RenderViewHost;
39 class SessionStorageNamespace;
40 class WebContents;
41 class WebContentsImpl;
42 struct ColorSuggestion;
43 struct ContextMenuParams;
44 struct DropData;
45 struct FileChooserParams;
46 struct NativeWebKeyboardEvent;
47 struct Referrer;
48 struct SSLStatus;
51 namespace gfx {
52 class Point;
53 class Rect;
54 class Size;
57 namespace blink {
58 class WebGestureEvent;
61 namespace content {
63 struct OpenURLParams;
65 // Objects implement this interface to get notified about changes in the
66 // WebContents and to provide necessary functionality.
67 class CONTENT_EXPORT WebContentsDelegate {
68 public:
69 WebContentsDelegate();
71 // Opens a new URL inside the passed in WebContents (if source is 0 open
72 // in the current front-most tab), unless |disposition| indicates the url
73 // should be opened in a new tab or window.
75 // A nullptr source indicates the current tab (callers should probably use
76 // OpenURL() for these cases which does it for you).
78 // Returns the WebContents the URL is opened in, or nullptr if the URL wasn't
79 // opened immediately.
80 virtual WebContents* OpenURLFromTab(WebContents* source,
81 const OpenURLParams& params);
83 // Called to inform the delegate that the WebContents's navigation state
84 // changed. The |changed_flags| indicates the parts of the navigation state
85 // that have been updated.
86 virtual void NavigationStateChanged(WebContents* source,
87 InvalidateTypes changed_flags) {}
89 // Called to inform the delegate that the WebContent's visible SSL state (as
90 // defined by SSLStatus) changed.
91 virtual void VisibleSSLStateChanged(const WebContents* source) {}
93 // Creates a new tab with the already-created WebContents 'new_contents'.
94 // The window for the added contents should be reparented correctly when this
95 // method returns. If |disposition| is NEW_POPUP, |initial_rect| should hold
96 // the initial position and size. If |was_blocked| is non-nullptr, then
97 // |*was_blocked| will be set to true if the popup gets blocked, and left
98 // unchanged otherwise.
99 virtual void AddNewContents(WebContents* source,
100 WebContents* new_contents,
101 WindowOpenDisposition disposition,
102 const gfx::Rect& initial_rect,
103 bool user_gesture,
104 bool* was_blocked) {}
106 // Selects the specified contents, bringing its container to the front.
107 virtual void ActivateContents(WebContents* contents) {}
109 // Deactivates the specified contents by deactivating its container and
110 // potentialy moving it to the back of the Z order.
111 virtual void DeactivateContents(WebContents* contents) {}
113 // Notifies the delegate that this contents is starting or is done loading
114 // some resource. The delegate should use this notification to represent
115 // loading feedback. See WebContents::IsLoading()
116 // |to_different_document| will be true unless the load is a fragment
117 // navigation, or triggered by history.pushState/replaceState.
118 virtual void LoadingStateChanged(WebContents* source,
119 bool to_different_document) {}
121 // Notifies the delegate that the page has made some progress loading.
122 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully
123 // loaded).
124 virtual void LoadProgressChanged(WebContents* source,
125 double progress) {}
127 // Request the delegate to close this web contents, and do whatever cleanup
128 // it needs to do.
129 virtual void CloseContents(WebContents* source) {}
131 // Informs the delegate that the underlying RenderViewHost has been swapped
132 // out so it can perform any cleanup necessary.
133 virtual void SwappedOut(WebContents* source) {}
135 // Request the delegate to move this WebContents to the specified position
136 // in screen coordinates.
137 virtual void MoveContents(WebContents* source, const gfx::Rect& pos) {}
139 // Called to determine if the WebContents is contained in a popup window
140 // or a panel window.
141 virtual bool IsPopupOrPanel(const WebContents* source) const;
143 // Notification that the target URL has changed.
144 virtual void UpdateTargetURL(WebContents* source,
145 const GURL& url) {}
147 // Notification that there was a mouse event, along with the absolute
148 // coordinates of the mouse pointer and whether it was a normal motion event
149 // (otherwise, the pointer left the contents area).
150 virtual void ContentsMouseEvent(WebContents* source,
151 const gfx::Point& location,
152 bool motion) {}
154 // Request the delegate to change the zoom level of the current tab.
155 virtual void ContentsZoomChange(bool zoom_in) {}
157 // Called to determine if the WebContents can be overscrolled with touch/wheel
158 // gestures.
159 virtual bool CanOverscrollContent() const;
161 // Callback that allows vertical overscroll activies to be communicated to the
162 // delegate. |delta_y| is the total amount of overscroll.
163 virtual void OverscrollUpdate(float delta_y) {}
165 // Invoked when a vertical overscroll completes.
166 virtual void OverscrollComplete() {}
168 // Return the rect where to display the resize corner, if any, otherwise
169 // an empty rect.
170 virtual gfx::Rect GetRootWindowResizerRect() const;
172 // Invoked prior to showing before unload handler confirmation dialog.
173 virtual void WillRunBeforeUnloadConfirm() {}
175 // Returns true if javascript dialogs and unload alerts are suppressed.
176 // Default is false.
177 virtual bool ShouldSuppressDialogs(WebContents* source);
179 // Returns whether pending NavigationEntries for aborted browser-initiated
180 // navigations should be preserved (and thus returned from GetVisibleURL).
181 // Defaults to false.
182 virtual bool ShouldPreserveAbortedURLs(WebContents* source);
184 // Add a message to the console. Returning true indicates that the delegate
185 // handled the message. If false is returned the default logging mechanism
186 // will be used for the message.
187 virtual bool AddMessageToConsole(WebContents* source,
188 int32 level,
189 const base::string16& message,
190 int32 line_no,
191 const base::string16& source_id);
193 // Tells us that we've finished firing this tab's beforeunload event.
194 // The proceed bool tells us whether the user chose to proceed closing the
195 // tab. Returns true if the tab can continue on firing its unload event.
196 // If we're closing the entire browser, then we'll want to delay firing
197 // unload events until all the beforeunload events have fired.
198 virtual void BeforeUnloadFired(WebContents* tab,
199 bool proceed,
200 bool* proceed_to_fire_unload);
202 // Returns true if the location bar should be focused by default rather than
203 // the page contents. NOTE: this is only used if WebContents can't determine
204 // for itself whether the location bar should be focused by default. For a
205 // complete check, you should use WebContents::FocusLocationBarByDefault().
206 virtual bool ShouldFocusLocationBarByDefault(WebContents* source);
208 // Sets focus to the location bar or some other place that is appropriate.
209 // This is called when the tab wants to encourage user input, like for the
210 // new tab page.
211 virtual void SetFocusToLocationBar(bool select_all) {}
213 // Returns whether the page should be focused when transitioning from crashed
214 // to live. Default is true.
215 virtual bool ShouldFocusPageAfterCrash();
217 // This is called when WebKit tells us that it is done tabbing through
218 // controls on the page. Provides a way for WebContentsDelegates to handle
219 // this. Returns true if the delegate successfully handled it.
220 virtual bool TakeFocus(WebContents* source,
221 bool reverse);
223 // Invoked when the page loses mouse capture.
224 virtual void LostCapture() {}
226 // Notification that |contents| has gained focus.
227 virtual void WebContentsFocused(WebContents* contents) {}
229 // Asks the delegate if the given tab can download.
230 // Invoking the |callback| synchronously is OK.
231 virtual void CanDownload(RenderViewHost* render_view_host,
232 const GURL& url,
233 const std::string& request_method,
234 const base::Callback<void(bool)>& callback);
236 // Returns true if the context menu operation was handled by the delegate.
237 virtual bool HandleContextMenu(const content::ContextMenuParams& params);
239 // Opens source view for given WebContents that is navigated to the given
240 // page url.
241 virtual void ViewSourceForTab(WebContents* source, const GURL& page_url);
243 // Opens source view for the given subframe.
244 virtual void ViewSourceForFrame(WebContents* source,
245 const GURL& url,
246 const PageState& page_state);
248 // Allows delegates to handle keyboard events before sending to the renderer.
249 // Returns true if the |event| was handled. Otherwise, if the |event| would be
250 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
251 // |*is_keyboard_shortcut| should be set to true.
252 virtual bool PreHandleKeyboardEvent(WebContents* source,
253 const NativeWebKeyboardEvent& event,
254 bool* is_keyboard_shortcut);
256 // Allows delegates to handle unhandled keyboard messages coming back from
257 // the renderer.
258 virtual void HandleKeyboardEvent(WebContents* source,
259 const NativeWebKeyboardEvent& event) {}
261 // Allows delegates to handle gesture events before sending to the renderer.
262 // Returns true if the |event| was handled and thus shouldn't be processed
263 // by the renderer's event handler. Note that the touch events that create
264 // the gesture are always passed to the renderer since the gesture is created
265 // and dispatched after the touches return without being "preventDefault()"ed.
266 virtual bool PreHandleGestureEvent(
267 WebContents* source,
268 const blink::WebGestureEvent& event);
270 // Called when an external drag event enters the web contents window. Return
271 // true to allow dragging and dropping on the web contents window or false to
272 // cancel the operation. This method is used by Chromium Embedded Framework.
273 virtual bool CanDragEnter(WebContents* source,
274 const DropData& data,
275 blink::WebDragOperationsMask operations_allowed);
277 // Shows the repost form confirmation dialog box.
278 virtual void ShowRepostFormWarningDialog(WebContents* source) {}
280 // Allows delegate to override navigation to the history entries.
281 // Returns true to allow WebContents to continue with the default processing.
282 virtual bool OnGoToEntryOffset(int offset);
284 // Allows delegate to control whether a WebContents will be created. Returns
285 // true to allow the creation. Default is to allow it. In cases where the
286 // delegate handles the creation/navigation itself, it will use |target_url|.
287 // The embedder has to synchronously adopt |route_id| or else the view will
288 // be destroyed.
289 virtual bool ShouldCreateWebContents(
290 WebContents* web_contents,
291 int route_id,
292 int main_frame_route_id,
293 WindowContainerType window_container_type,
294 const base::string16& frame_name,
295 const GURL& target_url,
296 const std::string& partition_id,
297 SessionStorageNamespace* session_storage_namespace);
299 // Notifies the delegate about the creation of a new WebContents. This
300 // typically happens when popups are created.
301 virtual void WebContentsCreated(WebContents* source_contents,
302 int opener_render_frame_id,
303 const base::string16& frame_name,
304 const GURL& target_url,
305 WebContents* new_contents) {}
307 // Notification that the tab is hung.
308 virtual void RendererUnresponsive(WebContents* source) {}
310 // Notification that the tab is no longer hung.
311 virtual void RendererResponsive(WebContents* source) {}
313 // Notification that a worker associated with this tab has crashed.
314 virtual void WorkerCrashed(WebContents* source) {}
316 // Invoked when a main fram navigation occurs.
317 virtual void DidNavigateMainFramePostCommit(WebContents* source) {}
319 // Returns a pointer to a service to manage JavaScript dialogs. May return
320 // nullptr in which case dialogs aren't shown.
321 virtual JavaScriptDialogManager* GetJavaScriptDialogManager(
322 WebContents* source);
324 // Called when color chooser should open. Returns the opened color chooser.
325 // Returns nullptr if we failed to open the color chooser (e.g. when there is
326 // a ColorChooserDialog already open on Windows). Ownership of the returned
327 // pointer is transferred to the caller.
328 virtual ColorChooser* OpenColorChooser(
329 WebContents* web_contents,
330 SkColor color,
331 const std::vector<ColorSuggestion>& suggestions);
333 // Called when a file selection is to be done.
334 virtual void RunFileChooser(WebContents* web_contents,
335 const FileChooserParams& params) {}
337 // Request to enumerate a directory. This is equivalent to running the file
338 // chooser in directory-enumeration mode and having the user select the given
339 // directory.
340 virtual void EnumerateDirectory(WebContents* web_contents,
341 int request_id,
342 const base::FilePath& path) {}
344 // Returns true if the delegate will embed a WebContents-owned fullscreen
345 // render widget. In this case, the delegate may access the widget by calling
346 // WebContents::GetFullscreenRenderWidgetHostView(). If false is returned,
347 // WebContents will be responsible for showing the fullscreen widget.
348 virtual bool EmbedsFullscreenWidget() const;
350 // Called when the renderer puts a tab into fullscreen mode.
351 // |origin| is the origin of the initiating frame inside the |web_contents|.
352 // |origin| can be empty in which case the |web_contents| last committed
353 // URL's origin should be used.
354 virtual void EnterFullscreenModeForTab(WebContents* web_contents,
355 const GURL& origin) {}
357 // Called when the renderer puts a tab out of fullscreen mode.
358 virtual void ExitFullscreenModeForTab(WebContents*) {}
360 virtual bool IsFullscreenForTabOrPending(
361 const WebContents* web_contents) const;
363 // Register a new handler for URL requests with the given scheme.
364 // |user_gesture| is true if the registration is made in the context of a user
365 // gesture.
366 virtual void RegisterProtocolHandler(WebContents* web_contents,
367 const std::string& protocol,
368 const GURL& url,
369 bool user_gesture) {}
371 // Unregister the registered handler for URL requests with the given scheme.
372 // |user_gesture| is true if the registration is made in the context of a user
373 // gesture.
374 virtual void UnregisterProtocolHandler(WebContents* web_contents,
375 const std::string& protocol,
376 const GURL& url,
377 bool user_gesture) {}
379 // Result of string search in the page. This includes the number of matches
380 // found and the selection rect (in screen coordinates) for the string found.
381 // If |final_update| is false, it indicates that more results follow.
382 virtual void FindReply(WebContents* web_contents,
383 int request_id,
384 int number_of_matches,
385 const gfx::Rect& selection_rect,
386 int active_match_ordinal,
387 bool final_update) {}
389 #if defined(OS_ANDROID)
390 // Provides the rects of the current find-in-page matches.
391 // Sent as a reply to RequestFindMatchRects.
392 virtual void FindMatchRectsReply(WebContents* web_contents,
393 int version,
394 const std::vector<gfx::RectF>& rects,
395 const gfx::RectF& active_rect) {}
396 #endif
398 // Invoked when the preferred size of the contents has been changed.
399 virtual void UpdatePreferredSize(WebContents* web_contents,
400 const gfx::Size& pref_size) {}
402 // Invoked when the contents auto-resized and the container should match it.
403 virtual void ResizeDueToAutoResize(WebContents* web_contents,
404 const gfx::Size& new_size) {}
406 // Notification message from HTML UI.
407 virtual void WebUISend(WebContents* web_contents,
408 const GURL& source_url,
409 const std::string& name,
410 const base::ListValue& args) {}
412 // Requests to lock the mouse. Once the request is approved or rejected,
413 // GotResponseToLockMouseRequest() will be called on the requesting tab
414 // contents.
415 virtual void RequestToLockMouse(WebContents* web_contents,
416 bool user_gesture,
417 bool last_unlocked_by_target) {}
419 // Notification that the page has lost the mouse lock.
420 virtual void LostMouseLock() {}
422 // Asks permission to use the camera and/or microphone. If permission is
423 // granted, a call should be made to |callback| with the devices. If the
424 // request is denied, a call should be made to |callback| with an empty list
425 // of devices. |request| has the details of the request (e.g. which of audio
426 // and/or video devices are requested, and lists of available devices).
427 virtual void RequestMediaAccessPermission(
428 WebContents* web_contents,
429 const MediaStreamRequest& request,
430 const MediaResponseCallback& callback);
432 // Checks if we have permission to access the microphone or camera. Note that
433 // this does not query the user. |type| must be MEDIA_DEVICE_AUDIO_CAPTURE
434 // or MEDIA_DEVICE_VIDEO_CAPTURE.
435 virtual bool CheckMediaAccessPermission(WebContents* web_contents,
436 const GURL& security_origin,
437 MediaStreamType type);
439 // Requests permission to access the PPAPI broker. The delegate should return
440 // true and call the passed in |callback| with the result, or return false
441 // to indicate that it does not support asking for permission.
442 virtual bool RequestPpapiBrokerPermission(
443 WebContents* web_contents,
444 const GURL& url,
445 const base::FilePath& plugin_path,
446 const base::Callback<void(bool)>& callback);
448 // Returns the size for the new render view created for the pending entry in
449 // |web_contents|; if there's no size, returns an empty size.
450 // This is optional for implementations of WebContentsDelegate; if the
451 // delegate doesn't provide a size, the current WebContentsView's size will be
452 // used.
453 virtual gfx::Size GetSizeForNewRenderView(WebContents* web_contents) const;
455 // Notification that validation of a form displayed by the |web_contents|
456 // has failed. There can only be one message per |web_contents| at a time.
457 virtual void ShowValidationMessage(WebContents* web_contents,
458 const gfx::Rect& anchor_in_root_view,
459 const base::string16& main_text,
460 const base::string16& sub_text) {}
462 // Notification that the delegate should hide any showing form validation
463 // message.
464 virtual void HideValidationMessage(WebContents* web_contents) {}
466 // Notification that the form element that triggered the validation failure
467 // has moved.
468 virtual void MoveValidationMessage(WebContents* web_contents,
469 const gfx::Rect& anchor_in_root_view) {}
471 // Returns true if the WebContents is never visible.
472 virtual bool IsNeverVisible(WebContents* web_contents);
474 // Called in response to a request to save a frame. If this returns true, the
475 // default behavior is suppressed.
476 virtual bool SaveFrame(const GURL& url, const Referrer& referrer);
478 protected:
479 virtual ~WebContentsDelegate();
481 private:
482 friend class WebContentsImpl;
484 // Called when |this| becomes the WebContentsDelegate for |source|.
485 void Attach(WebContents* source);
487 // Called when |this| is no longer the WebContentsDelegate for |source|.
488 void Detach(WebContents* source);
490 // The WebContents that this is currently a delegate for.
491 std::set<WebContents*> attached_contents_;
494 } // namespace content
496 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_