web_contents_delegate.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "content/public/browser/web_contents_delegate.h"
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/compiler_specific.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/logging.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/singleton.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/web_contents.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/common/url_constants.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/common/bindings_policy.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/gfx/rect.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebContentsDelegate::WebContentsDelegate() {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WebContents* WebContentsDelegate::OpenURLFromTab(WebContents* source,
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                 const OpenURLParams& params) {
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return NULL;
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::IsPopupOrPanel(const WebContents* source) const {
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::CanLoadDataURLsInWebUI() const { return false; }
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::CanOverscrollContent() const { return false; }
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)gfx::Rect WebContentsDelegate::GetRootWindowResizerRect() const {
35c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return gfx::Rect();
36c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbool WebContentsDelegate::ShouldSuppressDialogs() {
39c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return false;
40c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::AddMessageToConsole(WebContents* source,
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              int32 level,
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              const string16& message,
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              int32 line_no,
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              const string16& source_id) {
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WebContentsDelegate::BeforeUnloadFired(WebContents* web_contents,
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            bool proceed,
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            bool* proceed_to_fire_unload) {
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  *proceed_to_fire_unload = true;
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::ShouldFocusLocationBarByDefault(WebContents* source) {
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::ShouldFocusPageAfterCrash() {
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return true;
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WebContentsDelegate::TakeFocus(WebContents* source, bool reverse) {
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int WebContentsDelegate::GetExtraRenderViewHeight() const {
69c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return 0;
70c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
71c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
72c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid WebContentsDelegate::CanDownload(
73c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    RenderViewHost* render_view_host,
74c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    int request_id,
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& request_method,
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::Callback<void(bool)>& callback) {
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  callback.Run(true);
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool WebContentsDelegate::HandleContextMenu(
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const content::ContextMenuParams& params) {
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return false;
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WebContentsDelegate::ViewSourceForTab(WebContents* source,
86c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                           const GURL& page_url) {
87c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Fall back implementation based entirely on the view-source scheme.
88c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // It suffers from http://crbug.com/523 and that is why browser overrides
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // it with proper implementation.
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL url = GURL(kViewSourceScheme + std::string(":") + page_url.spec());
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  OpenURLFromTab(source, OpenURLParams(url, Referrer(),
92c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                       NEW_FOREGROUND_TAB,
93c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                       PAGE_TRANSITION_LINK, false));
94c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
96c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid WebContentsDelegate::ViewSourceForFrame(WebContents* source,
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                             const GURL& frame_url,
98c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                             const PageState& page_state) {
99c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Same as ViewSourceForTab, but for given subframe.
100c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GURL url = GURL(kViewSourceScheme + std::string(":") + frame_url.spec());
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  OpenURLFromTab(source, OpenURLParams(url, Referrer(),
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                       NEW_FOREGROUND_TAB,
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                       PAGE_TRANSITION_LINK, false));
104}
105
106bool WebContentsDelegate::PreHandleKeyboardEvent(
107    WebContents* source,
108    const NativeWebKeyboardEvent& event,
109    bool* is_keyboard_shortcut) {
110  return false;
111}
112
113bool WebContentsDelegate::CanDragEnter(
114    WebContents* source,
115    const DropData& data,
116    WebKit::WebDragOperationsMask operations_allowed) {
117  return true;
118}
119
120bool WebContentsDelegate::OnGoToEntryOffset(int offset) {
121  return true;
122}
123
124bool WebContentsDelegate::ShouldCreateWebContents(
125    WebContents* web_contents,
126    int route_id,
127    WindowContainerType window_container_type,
128    const string16& frame_name,
129    const GURL& target_url,
130    WindowOpenDisposition disposition,
131    bool user_gesture) {
132  return true;
133}
134
135JavaScriptDialogManager* WebContentsDelegate::GetJavaScriptDialogManager() {
136  return NULL;
137}
138
139bool WebContentsDelegate::IsFullscreenForTabOrPending(
140    const WebContents* web_contents) const {
141  return false;
142}
143
144content::ColorChooser* WebContentsDelegate::OpenColorChooser(
145    WebContents* web_contents, SkColor color) {
146  return NULL;
147}
148
149bool WebContentsDelegate::RequestPpapiBrokerPermission(
150    WebContents* web_contents,
151    const GURL& url,
152    const base::FilePath& plugin_path,
153    const base::Callback<void(bool)>& callback) {
154  return false;
155}
156
157WebContentsDelegate::~WebContentsDelegate() {
158  while (!attached_contents_.empty()) {
159    WebContents* web_contents = *attached_contents_.begin();
160    web_contents->SetDelegate(NULL);
161  }
162  DCHECK(attached_contents_.empty());
163}
164
165void WebContentsDelegate::Attach(WebContents* web_contents) {
166  DCHECK(attached_contents_.find(web_contents) == attached_contents_.end());
167  attached_contents_.insert(web_contents);
168}
169
170void WebContentsDelegate::Detach(WebContents* web_contents) {
171  DCHECK(attached_contents_.find(web_contents) != attached_contents_.end());
172  attached_contents_.erase(web_contents);
173}
174
175}  // namespace content
176