Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / apps / chrome_shell_window_delegate.cc
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.
4
5 #include "chrome/browser/ui/apps/chrome_shell_window_delegate.h"
6
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"
24
25 #if defined(USE_ASH)
26 #include "ash/shelf/shelf_constants.h"
27 #endif
28
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"
33 #else
34 #include "chrome/browser/printing/print_view_manager_basic.h"
35 #endif  // defined(ENABLE_FULL_PRINTING)
36 #endif  // defined(ENABLE_PRINTING)
37
38 namespace {
39
40 bool disable_external_open_for_testing_ = false;
41
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
48   // window.
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
52                                    ? params.disposition
53                                    : NEW_FOREGROUND_TAB;
54   new_tab_params.initiating_profile = Profile::FromBrowserContext(context);
55   chrome::Navigate(&new_tab_params);
56
57   return new_tab_params.target_contents;
58 }
59
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 {
65  public:
66   OpenURLFromTabBasedOnBrowserDefault(content::WebContents* source,
67                                       const content::OpenURLParams& params)
68       : source_(source), params_(params) {}
69
70   // Opens a URL when called with the result of if this is the default system
71   // browser or not.
72   virtual void SetDefaultWebClientUIState(
73       ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
74     Profile* profile =
75         Profile::FromBrowserContext(source_->GetBrowserContext());
76     DCHECK(profile);
77     if (!profile)
78       return;
79     switch (state) {
80       case ShellIntegration::STATE_PROCESSING:
81         break;
82       case ShellIntegration::STATE_IS_DEFAULT:
83         OpenURLFromTabInternal(profile, source_, params_);
84         break;
85       case ShellIntegration::STATE_NOT_DEFAULT:
86       case ShellIntegration::STATE_UNKNOWN:
87         platform_util::OpenExternal(profile, params_.url);
88         break;
89     }
90   }
91
92   virtual bool IsOwnedByWorker() OVERRIDE { return true; }
93
94  private:
95   content::WebContents* source_;
96   const content::OpenURLParams params_;
97 };
98
99 }  // namespace
100
101 ShellWindowLinkDelegate::ShellWindowLinkDelegate() {}
102
103 ShellWindowLinkDelegate::~ShellWindowLinkDelegate() {}
104
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) {
110   if (source) {
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();
120   }
121   return NULL;
122 }
123
124 ChromeShellWindowDelegate::ChromeShellWindowDelegate() {}
125
126 ChromeShellWindowDelegate::~ChromeShellWindowDelegate() {}
127
128 void ChromeShellWindowDelegate::DisableExternalOpenForTesting() {
129   disable_external_open_for_testing_ = true;
130 }
131
132 void ChromeShellWindowDelegate::InitWebContents(
133     content::WebContents* web_contents) {
134   FaviconTabHelper::CreateForWebContents(web_contents);
135
136 #if defined(ENABLE_PRINTING)
137 #if defined(ENABLE_FULL_PRINTING)
138   printing::PrintViewManager::CreateForWebContents(web_contents);
139   printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
140 #else
141   printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
142 #endif  // defined(ENABLE_FULL_PRINTING)
143 #endif  // defined(ENABLE_PRINTING)
144 }
145
146 apps::NativeAppWindow* ChromeShellWindowDelegate::CreateNativeAppWindow(
147     apps::AppWindow* window,
148     const apps::AppWindow::CreateParams& params) {
149   return CreateNativeAppWindowImpl(window, params);
150 }
151
152 content::WebContents* ChromeShellWindowDelegate::OpenURLFromTab(
153     content::BrowserContext* context,
154     content::WebContents* source,
155     const content::OpenURLParams& params) {
156   return OpenURLFromTabInternal(context, source, params);
157 }
158
159 void ChromeShellWindowDelegate::AddNewContents(
160     content::BrowserContext* context,
161     content::WebContents* new_contents,
162     WindowOpenDisposition disposition,
163     const gfx::Rect& initial_pos,
164     bool user_gesture,
165     bool* was_blocked) {
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());
170     return;
171   }
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
175   // new window.
176   disposition =
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);
180 }
181
182 content::ColorChooser* ChromeShellWindowDelegate::ShowColorChooser(
183     content::WebContents* web_contents,
184     SkColor initial_color) {
185   return chrome::ShowColorChooser(web_contents, initial_color);
186 }
187
188 void ChromeShellWindowDelegate::RunFileChooser(
189     content::WebContents* tab,
190     const content::FileChooserParams& params) {
191   FileSelectHelper::RunFileChooser(tab, params);
192 }
193
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);
201 }
202
203 int ChromeShellWindowDelegate::PreferredIconSize() {
204 #if defined(USE_ASH)
205   return ash::kShelfPreferredSize;
206 #else
207   return extension_misc::EXTENSION_ICON_SMALL;
208 #endif
209 }
210
211 void ChromeShellWindowDelegate::SetWebContentsBlocked(
212     content::WebContents* web_contents,
213     bool blocked) {
214   // RenderViewHost may be NULL during shutdown.
215   content::RenderViewHost* host = web_contents->GetRenderViewHost();
216   if (host) {
217     host->Send(new ChromeViewMsg_SetVisuallyDeemphasized(
218         host->GetRoutingID(), blocked));
219   }
220 }
221
222 bool ChromeShellWindowDelegate::IsWebContentsVisible(
223     content::WebContents* web_contents) {
224   return platform_util::IsVisible(web_contents->GetView()->GetNativeView());
225 }