1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// found in the LICENSE file.
4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/ui/apps/chrome_app_delegate.h"
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/memory/scoped_ptr.h"
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/strings/stringprintf.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/app_mode/app_mode_utils.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/apps/scoped_keep_alive.h"
1103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/browser/chrome_notification_types.h"
1203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/favicon/favicon_tab_helper.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/file_select_helper.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/media/media_capture_devices_dispatcher.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/platform_util.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/shell_integration.h"
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/ui/browser.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/ui/browser_dialogs.h"
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/ui/browser_tabstrip.h"
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/ui/browser_window.h"
230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/web_contents_sizer.h"
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/zoom/zoom_controller.h"
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/common/extensions/chrome_extension_messages.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/browser_context.h"
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "content/public/browser/notification_service.h"
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "content/public/browser/render_view_host.h"
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "content/public/browser/web_contents.h"
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/browser/web_contents_delegate.h"
32c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "extensions/common/constants.h"
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(USE_ASH)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/shelf/shelf_constants.h"
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
38c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#if defined(ENABLE_PRINTING)
39c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#if defined(ENABLE_FULL_PRINTING)
40c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/printing/print_preview_message_handler.h"
41c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/printing/print_view_manager.h"
42c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#else
43c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "chrome/browser/printing/print_view_manager_basic.h"
44c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif  // defined(ENABLE_FULL_PRINTING)
45c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif  // defined(ENABLE_PRINTING)
46c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace {
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool disable_external_open_for_testing_ = false;
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Opens a URL with Chromium (not external browser) with the right profile.
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)content::WebContents* OpenURLFromTabInternal(
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::BrowserContext* context,
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const content::OpenURLParams& params) {
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Force all links to open in a new tab, even if they were trying to open a
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // window.
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  chrome::NavigateParams new_tab_params(
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      static_cast<Browser*>(NULL), params.url, params.transition);
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (params.disposition == NEW_BACKGROUND_TAB) {
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    new_tab_params.disposition = NEW_BACKGROUND_TAB;
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    new_tab_params.disposition = NEW_FOREGROUND_TAB;
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    new_tab_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new_tab_params.initiating_profile = Profile::FromBrowserContext(context);
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  chrome::Navigate(&new_tab_params);
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return new_tab_params.target_contents;
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Helper class that opens a URL based on if this browser instance is the
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// default system browser. If it is the default, open the URL directly instead
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// of asking the system to open it.
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class OpenURLFromTabBasedOnBrowserDefault
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : public ShellIntegration::DefaultWebClientObserver {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OpenURLFromTabBasedOnBrowserDefault(scoped_ptr<content::WebContents> source,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      const content::OpenURLParams& params)
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      : source_(source.Pass()), params_(params) {}
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Opens a URL when called with the result of if this is the default system
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // browser or not.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetDefaultWebClientUIState(
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Profile* profile =
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Profile::FromBrowserContext(source_->GetBrowserContext());
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK(profile);
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!profile)
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    switch (state) {
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case ShellIntegration::STATE_PROCESSING:
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case ShellIntegration::STATE_IS_DEFAULT:
95effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        OpenURLFromTabInternal(profile, params_);
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case ShellIntegration::STATE_NOT_DEFAULT:
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case ShellIntegration::STATE_UNKNOWN:
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        platform_util::OpenExternal(profile, params_.url);
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsOwnedByWorker() OVERRIDE { return true; }
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
107effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_ptr<content::WebContents> source_;
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const content::OpenURLParams params_;
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class ChromeAppDelegate::NewWindowContentsDelegate
114effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    : public content::WebContentsDelegate {
115effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public:
116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  NewWindowContentsDelegate() {}
117effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual ~NewWindowContentsDelegate() {}
118effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
119effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual content::WebContents* OpenURLFromTab(
120effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      content::WebContents* source,
121effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      const content::OpenURLParams& params) OVERRIDE;
122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
123effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch private:
124effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DISALLOW_COPY_AND_ASSIGN(NewWindowContentsDelegate);
125effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
1267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
127effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochcontent::WebContents*
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ChromeAppDelegate::NewWindowContentsDelegate::OpenURLFromTab(
129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    content::WebContents* source,
130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const content::OpenURLParams& params) {
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (source) {
132effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // This NewWindowContentsDelegate was given ownership of the incoming
133effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // WebContents by being assigned as its delegate within
1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // ChromeAppDelegate::AddNewContents, but this is the first time
135effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // NewWindowContentsDelegate actually sees the WebContents.
136effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Here it is captured for deletion.
137effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    scoped_ptr<content::WebContents> owned_source(source);
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<ShellIntegration::DefaultWebClientWorker>
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        check_if_default_browser_worker =
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            new ShellIntegration::DefaultBrowserWorker(
141effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                new OpenURLFromTabBasedOnBrowserDefault(owned_source.Pass(),
142effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                        params));
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // ownership of check_if_default_browser_worker and will clean up after
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // the asynchronous tasks.
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    check_if_default_browser_worker->StartCheckIsDefault();
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return NULL;
150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciChromeAppDelegate::ChromeAppDelegate(scoped_ptr<ScopedKeepAlive> keep_alive)
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : keep_alive_(keep_alive.Pass()),
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new_window_contents_delegate_(new NewWindowContentsDelegate()) {
15503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  registrar_.Add(this,
15603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                 chrome::NOTIFICATION_APP_TERMINATING,
15703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                 content::NotificationService::AllSources());
1585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
159eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ChromeAppDelegate::~ChromeAppDelegate() {
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Unregister now to prevent getting notified if |keep_alive_| is the last.
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  terminating_callback_.Reset();
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
164eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeAppDelegate::DisableExternalOpenForTesting() {
166eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  disable_external_open_for_testing_ = true;
167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
168eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeAppDelegate::InitWebContents(content::WebContents* web_contents) {
170eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  FaviconTabHelper::CreateForWebContents(web_contents);
1717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(ENABLE_PRINTING)
173c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#if defined(ENABLE_FULL_PRINTING)
1747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  printing::PrintViewManager::CreateForWebContents(web_contents);
175c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
176c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#else
177c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
178c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif  // defined(ENABLE_FULL_PRINTING)
179c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#endif  // defined(ENABLE_PRINTING)
18003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
18103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      web_contents);
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Kiosk app supports zooming.
1841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (chrome::IsRunningInForcedAppMode())
1851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ZoomController::CreateForWebContents(web_contents);
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ChromeAppDelegate::ResizeWebContents(content::WebContents* web_contents,
1891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                          const gfx::Size& size) {
1901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ::ResizeWebContents(web_contents, size);
191eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
192eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)content::WebContents* ChromeAppDelegate::OpenURLFromTab(
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::BrowserContext* context,
195eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    content::WebContents* source,
196eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const content::OpenURLParams& params) {
197effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return OpenURLFromTabInternal(context, params);
198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
199eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeAppDelegate::AddNewContents(content::BrowserContext* context,
2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                       content::WebContents* new_contents,
2025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                       WindowOpenDisposition disposition,
2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                       const gfx::Rect& initial_pos,
2045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                       bool user_gesture,
2055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                       bool* was_blocked) {
206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!disable_external_open_for_testing_) {
207effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // We don't really want to open a window for |new_contents|, but we need to
208effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // capture its intended navigation. Here we give ownership to the
209effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // NewWindowContentsDelegate, which will dispose of the contents once
210effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // a navigation is captured.
211effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    new_contents->SetDelegate(new_window_contents_delegate_.get());
212eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return;
213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
2140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::ScopedTabbedBrowserDisplayer displayer(
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Profile::FromBrowserContext(context), chrome::GetActiveDesktop());
216eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Force all links to open in a new tab, even if they were trying to open a
217eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // new window.
218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  disposition =
219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB;
2205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  chrome::AddWebContents(displayer.browser(),
2215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         NULL,
2225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         new_contents,
2235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         disposition,
2245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         initial_pos,
2255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         user_gesture,
2265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         was_blocked);
227eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
228eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)content::ColorChooser* ChromeAppDelegate::ShowColorChooser(
230eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    content::WebContents* web_contents,
231eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SkColor initial_color) {
232eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return chrome::ShowColorChooser(web_contents, initial_color);
233eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
234eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeAppDelegate::RunFileChooser(
236eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    content::WebContents* tab,
237eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const content::FileChooserParams& params) {
238eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  FileSelectHelper::RunFileChooser(tab, params);
239eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
240eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeAppDelegate::RequestMediaAccessPermission(
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::WebContents* web_contents,
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const content::MediaStreamRequest& request,
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const content::MediaResponseCallback& callback,
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const extensions::Extension* extension) {
246eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
247eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      web_contents, request, callback, extension);
248eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
249eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool ChromeAppDelegate::CheckMediaAccessPermission(
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    content::WebContents* web_contents,
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const GURL& security_origin,
2531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    content::MediaStreamType type,
2541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const extensions::Extension* extension) {
2551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return MediaCaptureDevicesDispatcher::GetInstance()
2561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ->CheckMediaAccessPermission(
2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          web_contents, security_origin, type, extension);
2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)int ChromeAppDelegate::PreferredIconSize() {
261eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(USE_ASH)
262a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return ash::kShelfSize;
263eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#else
264eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return extension_misc::EXTENSION_ICON_SMALL;
265eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
266eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
267eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeAppDelegate::SetWebContentsBlocked(
269eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    content::WebContents* web_contents,
270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    bool blocked) {
271eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // RenderViewHost may be NULL during shutdown.
272eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  content::RenderViewHost* host = web_contents->GetRenderViewHost();
273eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (host) {
2745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    host->Send(new ChromeViewMsg_SetVisuallyDeemphasized(host->GetRoutingID(),
2755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                         blocked));
276eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
277eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
278eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool ChromeAppDelegate::IsWebContentsVisible(
280eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    content::WebContents* web_contents) {
281010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return platform_util::IsVisible(web_contents->GetNativeView());
282eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
28303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
28403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void ChromeAppDelegate::SetTerminatingCallback(const base::Closure& callback) {
28503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  terminating_callback_ = callback;
28603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
28703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
28803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void ChromeAppDelegate::Observe(int type,
28903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                const content::NotificationSource& source,
29003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                const content::NotificationDetails& details) {
29103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  switch (type) {
29203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case chrome::NOTIFICATION_APP_TERMINATING:
29303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      if (!terminating_callback_.is_null())
29403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        terminating_callback_.Run();
29503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      break;
29603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    default:
29703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      NOTREACHED() << "Received unexpected notification";
29803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
29903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
300