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