ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / public / browser / web_contents_delegate.h
blobd88b27c92d7d9f2cf553d18405631ae270fb038b
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;
59 class WebLayer;
60 struct WebWindowFeatures;
63 namespace content {
65 struct OpenURLParams;
67 // Objects implement this interface to get notified about changes in the
68 // WebContents and to provide necessary functionality.
69 class CONTENT_EXPORT WebContentsDelegate {
70 public:
71 WebContentsDelegate();
73 // Opens a new URL inside the passed in WebContents (if source is 0 open
74 // in the current front-most tab), unless |disposition| indicates the url
75 // should be opened in a new tab or window.
77 // A nullptr source indicates the current tab (callers should probably use
78 // OpenURL() for these cases which does it for you).
80 // Returns the WebContents the URL is opened in, or nullptr if the URL wasn't
81 // opened immediately.
82 virtual WebContents* OpenURLFromTab(WebContents* source,
83 const OpenURLParams& params);
85 // Called to inform the delegate that the WebContents's navigation state
86 // changed. The |changed_flags| indicates the parts of the navigation state
87 // that have been updated.
88 virtual void NavigationStateChanged(WebContents* source,
89 InvalidateTypes changed_flags) {}
91 // Called to inform the delegate that the WebContent's visible SSL state (as
92 // defined by SSLStatus) changed.
93 virtual void VisibleSSLStateChanged(const WebContents* source) {}
95 // Creates a new tab with the already-created WebContents 'new_contents'.
96 // The window for the added contents should be reparented correctly when this
97 // method returns. If |disposition| is NEW_POPUP, |initial_rect| should hold
98 // the initial position and size. If |was_blocked| is non-nullptr, then
99 // |*was_blocked| will be set to true if the popup gets blocked, and left
100 // unchanged otherwise.
101 virtual void AddNewContents(WebContents* source,
102 WebContents* new_contents,
103 WindowOpenDisposition disposition,
104 const gfx::Rect& initial_rect,
105 bool user_gesture,
106 bool* was_blocked) {}
108 // Selects the specified contents, bringing its container to the front.
109 virtual void ActivateContents(WebContents* contents) {}
111 // Deactivates the specified contents by deactivating its container and
112 // potentialy moving it to the back of the Z order.
113 virtual void DeactivateContents(WebContents* contents) {}
115 // Notifies the delegate that this contents is starting or is done loading
116 // some resource. The delegate should use this notification to represent
117 // loading feedback. See WebContents::IsLoading()
118 // |to_different_document| will be true unless the load is a fragment
119 // navigation, or triggered by history.pushState/replaceState.
120 virtual void LoadingStateChanged(WebContents* source,
121 bool to_different_document) {}
123 // Notifies the delegate that the page has made some progress loading.
124 // |progress| is a value between 0.0 (nothing loaded) to 1.0 (page fully
125 // loaded).
126 virtual void LoadProgressChanged(WebContents* source,
127 double progress) {}
129 // Request the delegate to close this web contents, and do whatever cleanup
130 // it needs to do.
131 virtual void CloseContents(WebContents* source) {}
133 // Informs the delegate that the underlying RenderViewHost has been swapped
134 // out so it can perform any cleanup necessary.
135 virtual void SwappedOut(WebContents* source) {}
137 // Request the delegate to move this WebContents to the specified position
138 // in screen coordinates.
139 virtual void MoveContents(WebContents* source, const gfx::Rect& pos) {}
141 // Called to determine if the WebContents is contained in a popup window
142 // or a panel window.
143 virtual bool IsPopupOrPanel(const WebContents* source) const;
145 // Notification that the target URL has changed.
146 virtual void UpdateTargetURL(WebContents* source,
147 const GURL& url) {}
149 // Notification that there was a mouse event, along with the absolute
150 // coordinates of the mouse pointer and whether it was a normal motion event
151 // (otherwise, the pointer left the contents area).
152 virtual void ContentsMouseEvent(WebContents* source,
153 const gfx::Point& location,
154 bool motion) {}
156 // Request the delegate to change the zoom level of the current tab.
157 virtual void ContentsZoomChange(bool zoom_in) {}
159 // Called to determine if the WebContents can be overscrolled with touch/wheel
160 // gestures.
161 virtual bool CanOverscrollContent() const;
163 // Callback that allows vertical overscroll activies to be communicated to the
164 // delegate. |delta_y| is the total amount of overscroll.
165 virtual void OverscrollUpdate(float delta_y) {}
167 // Invoked when a vertical overscroll completes.
168 virtual void OverscrollComplete() {}
170 // Return the rect where to display the resize corner, if any, otherwise
171 // an empty rect.
172 virtual gfx::Rect GetRootWindowResizerRect() const;
174 // Invoked prior to showing before unload handler confirmation dialog.
175 virtual void WillRunBeforeUnloadConfirm() {}
177 // Returns true if javascript dialogs and unload alerts are suppressed.
178 // Default is false.
179 virtual bool ShouldSuppressDialogs(WebContents* source);
181 // Returns whether pending NavigationEntries for aborted browser-initiated
182 // navigations should be preserved (and thus returned from GetVisibleURL).
183 // Defaults to false.
184 virtual bool ShouldPreserveAbortedURLs(WebContents* source);
186 // Add a message to the console. Returning true indicates that the delegate
187 // handled the message. If false is returned the default logging mechanism
188 // will be used for the message.
189 virtual bool AddMessageToConsole(WebContents* source,
190 int32 level,
191 const base::string16& message,
192 int32 line_no,
193 const base::string16& source_id);
195 // Tells us that we've finished firing this tab's beforeunload event.
196 // The proceed bool tells us whether the user chose to proceed closing the
197 // tab. Returns true if the tab can continue on firing its unload event.
198 // If we're closing the entire browser, then we'll want to delay firing
199 // unload events until all the beforeunload events have fired.
200 virtual void BeforeUnloadFired(WebContents* tab,
201 bool proceed,
202 bool* proceed_to_fire_unload);
204 // Returns true if the location bar should be focused by default rather than
205 // the page contents. NOTE: this is only used if WebContents can't determine
206 // for itself whether the location bar should be focused by default. For a
207 // complete check, you should use WebContents::FocusLocationBarByDefault().
208 virtual bool ShouldFocusLocationBarByDefault(WebContents* source);
210 // Sets focus to the location bar or some other place that is appropriate.
211 // This is called when the tab wants to encourage user input, like for the
212 // new tab page.
213 virtual void SetFocusToLocationBar(bool select_all) {}
215 // Returns whether the page should be focused when transitioning from crashed
216 // to live. Default is true.
217 virtual bool ShouldFocusPageAfterCrash();
219 // This is called when WebKit tells us that it is done tabbing through
220 // controls on the page. Provides a way for WebContentsDelegates to handle
221 // this. Returns true if the delegate successfully handled it.
222 virtual bool TakeFocus(WebContents* source,
223 bool reverse);
225 // Invoked when the page loses mouse capture.
226 virtual void LostCapture() {}
228 // Notification that |contents| has gained focus.
229 virtual void WebContentsFocused(WebContents* contents) {}
231 // Asks the delegate if the given tab can download.
232 // Invoking the |callback| synchronously is OK.
233 virtual void CanDownload(RenderViewHost* render_view_host,
234 const GURL& url,
235 const std::string& request_method,
236 const base::Callback<void(bool)>& callback);
238 // Return much extra vertical space should be allotted to the
239 // render view widget during various animations (e.g. infobar closing).
240 // This is used to make painting look smoother.
241 virtual int GetExtraRenderViewHeight() const;
243 // Returns true if the context menu operation was handled by the delegate.
244 virtual bool HandleContextMenu(const content::ContextMenuParams& params);
246 // Opens source view for given WebContents that is navigated to the given
247 // page url.
248 virtual void ViewSourceForTab(WebContents* source, const GURL& page_url);
250 // Opens source view for the given subframe.
251 virtual void ViewSourceForFrame(WebContents* source,
252 const GURL& url,
253 const PageState& page_state);
255 // Allows delegates to handle keyboard events before sending to the renderer.
256 // Returns true if the |event| was handled. Otherwise, if the |event| would be
257 // handled in HandleKeyboardEvent() method as a normal keyboard shortcut,
258 // |*is_keyboard_shortcut| should be set to true.
259 virtual bool PreHandleKeyboardEvent(WebContents* source,
260 const NativeWebKeyboardEvent& event,
261 bool* is_keyboard_shortcut);
263 // Allows delegates to handle unhandled keyboard messages coming back from
264 // the renderer.
265 virtual void HandleKeyboardEvent(WebContents* source,
266 const NativeWebKeyboardEvent& event) {}
268 virtual void HandleMouseDown() {}
269 virtual void HandleMouseUp() {}
271 // Handles activation resulting from a pointer event (e.g. when mouse is
272 // pressed, or a touch-gesture begins).
273 virtual void HandlePointerActivate() {}
275 // Allows delegates to handle gesture events before sending to the renderer.
276 // Returns true if the |event| was handled and thus shouldn't be processed
277 // by the renderer's event handler. Note that the touch events that create
278 // the gesture are always passed to the renderer since the gesture is created
279 // and dispatched after the touches return without being "preventDefault()"ed.
280 virtual bool PreHandleGestureEvent(
281 WebContents* source,
282 const blink::WebGestureEvent& event);
284 virtual void HandleGestureBegin() {}
285 virtual void HandleGestureEnd() {}
287 // Called when an external drag event enters the web contents window. Return
288 // true to allow dragging and dropping on the web contents window or false to
289 // cancel the operation. This method is used by Chromium Embedded Framework.
290 virtual bool CanDragEnter(WebContents* source,
291 const DropData& data,
292 blink::WebDragOperationsMask operations_allowed);
294 // Render view drag n drop ended.
295 virtual void DragEnded() {}
297 // Shows the repost form confirmation dialog box.
298 virtual void ShowRepostFormWarningDialog(WebContents* source) {}
300 // Allows delegate to override navigation to the history entries.
301 // Returns true to allow WebContents to continue with the default processing.
302 virtual bool OnGoToEntryOffset(int offset);
304 // Allows delegate to control whether a WebContents will be created. Returns
305 // true to allow the creation. Default is to allow it. In cases where the
306 // delegate handles the creation/navigation itself, it will use |target_url|.
307 // The embedder has to synchronously adopt |route_id| or else the view will
308 // be destroyed.
309 virtual bool ShouldCreateWebContents(
310 WebContents* web_contents,
311 int route_id,
312 int main_frame_route_id,
313 WindowContainerType window_container_type,
314 const base::string16& frame_name,
315 const GURL& target_url,
316 const std::string& partition_id,
317 SessionStorageNamespace* session_storage_namespace);
319 // Notifies the delegate about the creation of a new WebContents. This
320 // typically happens when popups are created.
321 virtual void WebContentsCreated(WebContents* source_contents,
322 int opener_render_frame_id,
323 const base::string16& frame_name,
324 const GURL& target_url,
325 WebContents* new_contents) {}
327 // Notification that the tab is hung.
328 virtual void RendererUnresponsive(WebContents* source) {}
330 // Notification that the tab is no longer hung.
331 virtual void RendererResponsive(WebContents* source) {}
333 // Notification that a worker associated with this tab has crashed.
334 virtual void WorkerCrashed(WebContents* source) {}
336 // Invoked when a main fram navigation occurs.
337 virtual void DidNavigateMainFramePostCommit(WebContents* source) {}
339 // Returns a pointer to a service to manage JavaScript dialogs. May return
340 // nullptr in which case dialogs aren't shown.
341 virtual JavaScriptDialogManager* GetJavaScriptDialogManager(
342 WebContents* source);
344 // Called when color chooser should open. Returns the opened color chooser.
345 // Returns nullptr if we failed to open the color chooser (e.g. when there is
346 // a ColorChooserDialog already open on Windows). Ownership of the returned
347 // pointer is transferred to the caller.
348 virtual ColorChooser* OpenColorChooser(
349 WebContents* web_contents,
350 SkColor color,
351 const std::vector<ColorSuggestion>& suggestions);
353 // Called when a file selection is to be done.
354 virtual void RunFileChooser(WebContents* web_contents,
355 const FileChooserParams& params) {}
357 // Request to enumerate a directory. This is equivalent to running the file
358 // chooser in directory-enumeration mode and having the user select the given
359 // directory.
360 virtual void EnumerateDirectory(WebContents* web_contents,
361 int request_id,
362 const base::FilePath& path) {}
364 // Returns true if the delegate will embed a WebContents-owned fullscreen
365 // render widget. In this case, the delegate may access the widget by calling
366 // WebContents::GetFullscreenRenderWidgetHostView(). If false is returned,
367 // WebContents will be responsible for showing the fullscreen widget.
368 virtual bool EmbedsFullscreenWidget() const;
370 // Called when the renderer puts a tab into fullscreen mode.
371 // |origin| is the origin of the initiating frame inside the |web_contents|.
372 // |origin| can be empty in which case the |web_contents| last committed
373 // URL's origin should be used.
374 virtual void EnterFullscreenModeForTab(WebContents* web_contents,
375 const GURL& origin) {}
377 // Called when the renderer puts a tab out of fullscreen mode.
378 virtual void ExitFullscreenModeForTab(WebContents*) {}
380 virtual bool IsFullscreenForTabOrPending(
381 const WebContents* web_contents) const;
383 // Register a new handler for URL requests with the given scheme.
384 // |user_gesture| is true if the registration is made in the context of a user
385 // gesture.
386 virtual void RegisterProtocolHandler(WebContents* web_contents,
387 const std::string& protocol,
388 const GURL& url,
389 bool user_gesture) {}
391 // Unregister the registered handler for URL requests with the given scheme.
392 // |user_gesture| is true if the registration is made in the context of a user
393 // gesture.
394 virtual void UnregisterProtocolHandler(WebContents* web_contents,
395 const std::string& protocol,
396 const GURL& url,
397 bool user_gesture) {}
399 // Result of string search in the page. This includes the number of matches
400 // found and the selection rect (in screen coordinates) for the string found.
401 // If |final_update| is false, it indicates that more results follow.
402 virtual void FindReply(WebContents* web_contents,
403 int request_id,
404 int number_of_matches,
405 const gfx::Rect& selection_rect,
406 int active_match_ordinal,
407 bool final_update) {}
409 #if defined(OS_ANDROID)
410 // Provides the rects of the current find-in-page matches.
411 // Sent as a reply to RequestFindMatchRects.
412 virtual void FindMatchRectsReply(WebContents* web_contents,
413 int version,
414 const std::vector<gfx::RectF>& rects,
415 const gfx::RectF& active_rect) {}
416 #endif
418 // Invoked when the preferred size of the contents has been changed.
419 virtual void UpdatePreferredSize(WebContents* web_contents,
420 const gfx::Size& pref_size) {}
422 // Invoked when the contents auto-resized and the container should match it.
423 virtual void ResizeDueToAutoResize(WebContents* web_contents,
424 const gfx::Size& new_size) {}
426 // Notification message from HTML UI.
427 virtual void WebUISend(WebContents* web_contents,
428 const GURL& source_url,
429 const std::string& name,
430 const base::ListValue& args) {}
432 // Requests to lock the mouse. Once the request is approved or rejected,
433 // GotResponseToLockMouseRequest() will be called on the requesting tab
434 // contents.
435 virtual void RequestToLockMouse(WebContents* web_contents,
436 bool user_gesture,
437 bool last_unlocked_by_target) {}
439 // Notification that the page has lost the mouse lock.
440 virtual void LostMouseLock() {}
442 // Asks permission to use the camera and/or microphone. If permission is
443 // granted, a call should be made to |callback| with the devices. If the
444 // request is denied, a call should be made to |callback| with an empty list
445 // of devices. |request| has the details of the request (e.g. which of audio
446 // and/or video devices are requested, and lists of available devices).
447 virtual void RequestMediaAccessPermission(
448 WebContents* web_contents,
449 const MediaStreamRequest& request,
450 const MediaResponseCallback& callback);
452 // Checks if we have permission to access the microphone or camera. Note that
453 // this does not query the user. |type| must be MEDIA_DEVICE_AUDIO_CAPTURE
454 // or MEDIA_DEVICE_VIDEO_CAPTURE.
455 virtual bool CheckMediaAccessPermission(WebContents* web_contents,
456 const GURL& security_origin,
457 MediaStreamType type);
459 // Requests permission to access the PPAPI broker. The delegate should return
460 // true and call the passed in |callback| with the result, or return false
461 // to indicate that it does not support asking for permission.
462 virtual bool RequestPpapiBrokerPermission(
463 WebContents* web_contents,
464 const GURL& url,
465 const base::FilePath& plugin_path,
466 const base::Callback<void(bool)>& callback);
468 // Returns the size for the new render view created for the pending entry in
469 // |web_contents|; if there's no size, returns an empty size.
470 // This is optional for implementations of WebContentsDelegate; if the
471 // delegate doesn't provide a size, the current WebContentsView's size will be
472 // used.
473 virtual gfx::Size GetSizeForNewRenderView(WebContents* web_contents) const;
475 // Notification that validation of a form displayed by the |web_contents|
476 // has failed. There can only be one message per |web_contents| at a time.
477 virtual void ShowValidationMessage(WebContents* web_contents,
478 const gfx::Rect& anchor_in_root_view,
479 const base::string16& main_text,
480 const base::string16& sub_text) {}
482 // Notification that the delegate should hide any showing form validation
483 // message.
484 virtual void HideValidationMessage(WebContents* web_contents) {}
486 // Notification that the form element that triggered the validation failure
487 // has moved.
488 virtual void MoveValidationMessage(WebContents* web_contents,
489 const gfx::Rect& anchor_in_root_view) {}
491 // Returns true if the WebContents is never visible.
492 virtual bool IsNeverVisible(WebContents* web_contents);
494 // Called in response to a request to save a frame. If this returns true, the
495 // default behavior is suppressed.
496 virtual bool SaveFrame(const GURL& url, const Referrer& referrer);
498 protected:
499 virtual ~WebContentsDelegate();
501 private:
502 friend class WebContentsImpl;
504 // Called when |this| becomes the WebContentsDelegate for |source|.
505 void Attach(WebContents* source);
507 // Called when |this| is no longer the WebContentsDelegate for |source|.
508 void Detach(WebContents* source);
510 // The WebContents that this is currently a delegate for.
511 std::set<WebContents*> attached_contents_;
514 } // namespace content
516 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_DELEGATE_H_