Home | History | Annotate | Download | only in apps
      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/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"
     21 
     22 #if defined(USE_ASH)
     23 #include "ash/launcher/launcher_types.h"
     24 #endif
     25 
     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"
     30 #else
     31 #include "chrome/browser/printing/print_view_manager_basic.h"
     32 #endif  // defined(ENABLE_FULL_PRINTING)
     33 #endif  // defined(ENABLE_PRINTING)
     34 
     35 namespace {
     36 
     37 bool disable_external_open_for_testing_ = false;
     38 
     39 }  // namespace
     40 
     41 ShellWindowLinkDelegate::ShellWindowLinkDelegate() {}
     42 
     43 ShellWindowLinkDelegate::~ShellWindowLinkDelegate() {}
     44 
     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) {
     50   if (source) {
     51     platform_util::OpenExternal(
     52         Profile::FromBrowserContext(source->GetBrowserContext()), params.url);
     53   }
     54   delete source;
     55   return NULL;
     56 }
     57 
     58 ChromeShellWindowDelegate::ChromeShellWindowDelegate() {}
     59 
     60 ChromeShellWindowDelegate::~ChromeShellWindowDelegate() {}
     61 
     62 void ChromeShellWindowDelegate::DisableExternalOpenForTesting() {
     63   disable_external_open_for_testing_ = true;
     64 }
     65 
     66 void ChromeShellWindowDelegate::InitWebContents(
     67     content::WebContents* web_contents) {
     68   FaviconTabHelper::CreateForWebContents(web_contents);
     69 
     70 #if defined(ENABLE_PRINTING)
     71 #if defined(ENABLE_FULL_PRINTING)
     72   printing::PrintViewManager::CreateForWebContents(web_contents);
     73   printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
     74 #else
     75   printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
     76 #endif  // defined(ENABLE_FULL_PRINTING)
     77 #endif  // defined(ENABLE_PRINTING)
     78 }
     79 
     80 apps::NativeAppWindow* ChromeShellWindowDelegate::CreateNativeAppWindow(
     81     apps::ShellWindow* window,
     82     const apps::ShellWindow::CreateParams& params) {
     83   return CreateNativeAppWindowImpl(window, params);
     84 }
     85 
     86 content::WebContents* ChromeShellWindowDelegate::OpenURLFromTab(
     87     Profile* profile,
     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
     91   // window.
     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);
     98 
     99   return new_tab_params.target_contents;
    100 }
    101 
    102 void ChromeShellWindowDelegate::AddNewContents(
    103     Profile* profile,
    104     content::WebContents* new_contents,
    105     WindowOpenDisposition disposition,
    106     const gfx::Rect& initial_pos,
    107     bool user_gesture,
    108     bool* was_blocked) {
    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());
    113     return;
    114   }
    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
    118   // new window.
    119   disposition =
    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);
    123 }
    124 
    125 content::ColorChooser* ChromeShellWindowDelegate::ShowColorChooser(
    126     content::WebContents* web_contents,
    127     SkColor initial_color) {
    128   return chrome::ShowColorChooser(web_contents, initial_color);
    129 }
    130 
    131 void ChromeShellWindowDelegate::RunFileChooser(
    132     content::WebContents* tab,
    133     const content::FileChooserParams& params) {
    134   FileSelectHelper::RunFileChooser(tab, params);
    135 }
    136 
    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);
    144 }
    145 
    146 int ChromeShellWindowDelegate::PreferredIconSize() {
    147 #if defined(USE_ASH)
    148   return ash::kLauncherPreferredSize;
    149 #else
    150   return extension_misc::EXTENSION_ICON_SMALL;
    151 #endif
    152 }
    153 
    154 void ChromeShellWindowDelegate::SetWebContentsBlocked(
    155     content::WebContents* web_contents,
    156     bool blocked) {
    157   // RenderViewHost may be NULL during shutdown.
    158   content::RenderViewHost* host = web_contents->GetRenderViewHost();
    159   if (host) {
    160     host->Send(new ChromeViewMsg_SetVisuallyDeemphasized(
    161         host->GetRoutingID(), blocked));
    162   }
    163 }
    164 
    165 bool ChromeShellWindowDelegate::IsWebContentsVisible(
    166     content::WebContents* web_contents) {
    167   return platform_util::IsVisible(web_contents->GetView()->GetNativeView());
    168 }
    169