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/profiles/profile.h"
13 #include "chrome/browser/shell_integration.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/browser_tabstrip.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
19 #include "chrome/common/render_messages.h"
20 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h"
26 #include "ash/shelf/shelf_constants.h"
29 #if defined(ENABLE_PRINTING)
30 #if defined(ENABLE_FULL_PRINTING)
31 #include "chrome/browser/printing/print_preview_message_handler.h"
32 #include "chrome/browser/printing/print_view_manager.h"
34 #include "chrome/browser/printing/print_view_manager_basic.h"
35 #endif // defined(ENABLE_FULL_PRINTING)
36 #endif // defined(ENABLE_PRINTING)
40 bool disable_external_open_for_testing_ = false;
42 // Opens a URL with Chromium (not external browser) with the right profile.
43 content::WebContents* OpenURLFromTabInternal(
44 content::BrowserContext* context,
45 content::WebContents* source,
46 const content::OpenURLParams& params) {
47 // Force all links to open in a new tab, even if they were trying to open a
49 chrome::NavigateParams new_tab_params(
50 static_cast<Browser*>(NULL), params.url, params.transition);
51 new_tab_params.disposition = params.disposition == NEW_BACKGROUND_TAB
54 new_tab_params.initiating_profile = Profile::FromBrowserContext(context);
55 chrome::Navigate(&new_tab_params);
57 return new_tab_params.target_contents;
60 // Helper class that opens a URL based on if this browser instance is the
61 // default system browser. If it is the default, open the URL directly instead
62 // of asking the system to open it.
63 class OpenURLFromTabBasedOnBrowserDefault
64 : public ShellIntegration::DefaultWebClientObserver {
66 OpenURLFromTabBasedOnBrowserDefault(content::WebContents* source,
67 const content::OpenURLParams& params)
68 : source_(source), params_(params) {}
70 // Opens a URL when called with the result of if this is the default system
72 virtual void SetDefaultWebClientUIState(
73 ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
75 Profile::FromBrowserContext(source_->GetBrowserContext());
80 case ShellIntegration::STATE_PROCESSING:
82 case ShellIntegration::STATE_IS_DEFAULT:
83 OpenURLFromTabInternal(profile, source_, params_);
85 case ShellIntegration::STATE_NOT_DEFAULT:
86 case ShellIntegration::STATE_UNKNOWN:
87 platform_util::OpenExternal(profile, params_.url);
92 virtual bool IsOwnedByWorker() OVERRIDE { return true; }
95 content::WebContents* source_;
96 const content::OpenURLParams params_;
101 ShellWindowLinkDelegate::ShellWindowLinkDelegate() {}
103 ShellWindowLinkDelegate::~ShellWindowLinkDelegate() {}
105 // TODO(rockot): Add a test that exercises this code. See
106 // http://crbug.com/254260.
107 content::WebContents* ShellWindowLinkDelegate::OpenURLFromTab(
108 content::WebContents* source,
109 const content::OpenURLParams& params) {
111 scoped_refptr<ShellIntegration::DefaultWebClientWorker>
112 check_if_default_browser_worker =
113 new ShellIntegration::DefaultBrowserWorker(
114 new OpenURLFromTabBasedOnBrowserDefault(source, params));
115 // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned
116 // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime
117 // ownership of check_if_default_browser_worker and will clean up after
118 // the asynchronous tasks.
119 check_if_default_browser_worker->StartCheckIsDefault();
124 ChromeShellWindowDelegate::ChromeShellWindowDelegate() {}
126 ChromeShellWindowDelegate::~ChromeShellWindowDelegate() {}
128 void ChromeShellWindowDelegate::DisableExternalOpenForTesting() {
129 disable_external_open_for_testing_ = true;
132 void ChromeShellWindowDelegate::InitWebContents(
133 content::WebContents* web_contents) {
134 FaviconTabHelper::CreateForWebContents(web_contents);
136 #if defined(ENABLE_PRINTING)
137 #if defined(ENABLE_FULL_PRINTING)
138 printing::PrintViewManager::CreateForWebContents(web_contents);
139 printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
141 printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
142 #endif // defined(ENABLE_FULL_PRINTING)
143 #endif // defined(ENABLE_PRINTING)
146 apps::NativeAppWindow* ChromeShellWindowDelegate::CreateNativeAppWindow(
147 apps::AppWindow* window,
148 const apps::AppWindow::CreateParams& params) {
149 return CreateNativeAppWindowImpl(window, params);
152 content::WebContents* ChromeShellWindowDelegate::OpenURLFromTab(
153 content::BrowserContext* context,
154 content::WebContents* source,
155 const content::OpenURLParams& params) {
156 return OpenURLFromTabInternal(context, source, params);
159 void ChromeShellWindowDelegate::AddNewContents(
160 content::BrowserContext* context,
161 content::WebContents* new_contents,
162 WindowOpenDisposition disposition,
163 const gfx::Rect& initial_pos,
166 if (!disable_external_open_for_testing_) {
167 if (!shell_window_link_delegate_.get())
168 shell_window_link_delegate_.reset(new ShellWindowLinkDelegate());
169 new_contents->SetDelegate(shell_window_link_delegate_.get());
172 chrome::ScopedTabbedBrowserDisplayer displayer(
173 Profile::FromBrowserContext(context), chrome::GetActiveDesktop());
174 // Force all links to open in a new tab, even if they were trying to open a
177 disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB;
178 chrome::AddWebContents(displayer.browser(), NULL, new_contents, disposition,
179 initial_pos, user_gesture, was_blocked);
182 content::ColorChooser* ChromeShellWindowDelegate::ShowColorChooser(
183 content::WebContents* web_contents,
184 SkColor initial_color) {
185 return chrome::ShowColorChooser(web_contents, initial_color);
188 void ChromeShellWindowDelegate::RunFileChooser(
189 content::WebContents* tab,
190 const content::FileChooserParams& params) {
191 FileSelectHelper::RunFileChooser(tab, params);
194 void ChromeShellWindowDelegate::RequestMediaAccessPermission(
195 content::WebContents* web_contents,
196 const content::MediaStreamRequest& request,
197 const content::MediaResponseCallback& callback,
198 const extensions::Extension* extension) {
199 MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
200 web_contents, request, callback, extension);
203 int ChromeShellWindowDelegate::PreferredIconSize() {
205 return ash::kShelfPreferredSize;
207 return extension_misc::EXTENSION_ICON_SMALL;
211 void ChromeShellWindowDelegate::SetWebContentsBlocked(
212 content::WebContents* web_contents,
214 // RenderViewHost may be NULL during shutdown.
215 content::RenderViewHost* host = web_contents->GetRenderViewHost();
217 host->Send(new ChromeViewMsg_SetVisuallyDeemphasized(
218 host->GetRoutingID(), blocked));
222 bool ChromeShellWindowDelegate::IsWebContentsVisible(
223 content::WebContents* web_contents) {
224 return platform_util::IsVisible(web_contents->GetView()->GetNativeView());