1 // Copyright 2013 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 #include "chrome/browser/ui/apps/chrome_shell_window_delegate.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/favicon/favicon_tab_helper.h"
9 #include "chrome/browser/file_select_helper.h"
10 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
11 #include "chrome/browser/platform_util.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_dialogs.h"
14 #include "chrome/browser/ui/browser_tabstrip.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
17 #include "chrome/common/render_messages.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_view.h"
23 #include "ash/shelf/shelf_constants.h"
26 #if defined(ENABLE_PRINTING)
27 #if defined(ENABLE_FULL_PRINTING)
28 #include "chrome/browser/printing/print_preview_message_handler.h"
29 #include "chrome/browser/printing/print_view_manager.h"
31 #include "chrome/browser/printing/print_view_manager_basic.h"
32 #endif // defined(ENABLE_FULL_PRINTING)
33 #endif // defined(ENABLE_PRINTING)
37 bool disable_external_open_for_testing_
= false;
41 ShellWindowLinkDelegate::ShellWindowLinkDelegate() {}
43 ShellWindowLinkDelegate::~ShellWindowLinkDelegate() {}
45 // TODO(rockot): Add a test that exercises this code. See
46 // http://crbug.com/254260.
47 content::WebContents
* ShellWindowLinkDelegate::OpenURLFromTab(
48 content::WebContents
* source
,
49 const content::OpenURLParams
& params
) {
51 platform_util::OpenExternal(
52 Profile::FromBrowserContext(source
->GetBrowserContext()), params
.url
);
58 ChromeShellWindowDelegate::ChromeShellWindowDelegate() {}
60 ChromeShellWindowDelegate::~ChromeShellWindowDelegate() {}
62 void ChromeShellWindowDelegate::DisableExternalOpenForTesting() {
63 disable_external_open_for_testing_
= true;
66 void ChromeShellWindowDelegate::InitWebContents(
67 content::WebContents
* web_contents
) {
68 FaviconTabHelper::CreateForWebContents(web_contents
);
70 #if defined(ENABLE_PRINTING)
71 #if defined(ENABLE_FULL_PRINTING)
72 printing::PrintViewManager::CreateForWebContents(web_contents
);
73 printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents
);
75 printing::PrintViewManagerBasic::CreateForWebContents(web_contents
);
76 #endif // defined(ENABLE_FULL_PRINTING)
77 #endif // defined(ENABLE_PRINTING)
80 apps::NativeAppWindow
* ChromeShellWindowDelegate::CreateNativeAppWindow(
81 apps::ShellWindow
* window
,
82 const apps::ShellWindow::CreateParams
& params
) {
83 return CreateNativeAppWindowImpl(window
, params
);
86 content::WebContents
* ChromeShellWindowDelegate::OpenURLFromTab(
88 content::WebContents
* source
,
89 const content::OpenURLParams
& params
) {
90 // Force all links to open in a new tab, even if they were trying to open a
92 chrome::NavigateParams
new_tab_params(
93 static_cast<Browser
*>(NULL
), params
.url
, params
.transition
);
94 new_tab_params
.disposition
= params
.disposition
== NEW_BACKGROUND_TAB
?
95 params
.disposition
: NEW_FOREGROUND_TAB
;
96 new_tab_params
.initiating_profile
= profile
;
97 chrome::Navigate(&new_tab_params
);
99 return new_tab_params
.target_contents
;
102 void ChromeShellWindowDelegate::AddNewContents(
104 content::WebContents
* new_contents
,
105 WindowOpenDisposition disposition
,
106 const gfx::Rect
& initial_pos
,
109 if (!disable_external_open_for_testing_
) {
110 if (!shell_window_link_delegate_
.get())
111 shell_window_link_delegate_
.reset(new ShellWindowLinkDelegate());
112 new_contents
->SetDelegate(shell_window_link_delegate_
.get());
115 chrome::ScopedTabbedBrowserDisplayer
displayer(
116 profile
, chrome::GetActiveDesktop());
117 // Force all links to open in a new tab, even if they were trying to open a
120 disposition
== NEW_BACKGROUND_TAB
? disposition
: NEW_FOREGROUND_TAB
;
121 chrome::AddWebContents(displayer
.browser(), NULL
, new_contents
, disposition
,
122 initial_pos
, user_gesture
, was_blocked
);
125 content::ColorChooser
* ChromeShellWindowDelegate::ShowColorChooser(
126 content::WebContents
* web_contents
,
127 SkColor initial_color
) {
128 return chrome::ShowColorChooser(web_contents
, initial_color
);
131 void ChromeShellWindowDelegate::RunFileChooser(
132 content::WebContents
* tab
,
133 const content::FileChooserParams
& params
) {
134 FileSelectHelper::RunFileChooser(tab
, params
);
137 void ChromeShellWindowDelegate::RequestMediaAccessPermission(
138 content::WebContents
* web_contents
,
139 const content::MediaStreamRequest
& request
,
140 const content::MediaResponseCallback
& callback
,
141 const extensions::Extension
* extension
) {
142 MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
143 web_contents
, request
, callback
, extension
);
146 int ChromeShellWindowDelegate::PreferredIconSize() {
148 return ash::kShelfPreferredSize
;
150 return extension_misc::EXTENSION_ICON_SMALL
;
154 void ChromeShellWindowDelegate::SetWebContentsBlocked(
155 content::WebContents
* web_contents
,
157 // RenderViewHost may be NULL during shutdown.
158 content::RenderViewHost
* host
= web_contents
->GetRenderViewHost();
160 host
->Send(new ChromeViewMsg_SetVisuallyDeemphasized(
161 host
->GetRoutingID(), blocked
));
165 bool ChromeShellWindowDelegate::IsWebContentsVisible(
166 content::WebContents
* web_contents
) {
167 return platform_util::IsVisible(web_contents
->GetView()->GetNativeView());