15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/chrome_web_contents_handler.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserContext;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::OpenURLParams;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ChromeWebContentsHandler::ChromeWebContentsHandler() {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ChromeWebContentsHandler::~ChromeWebContentsHandler() {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Opens a new URL inside |source|. |context| is the browser context that the
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// browser should be owned by. |params| contains the URL to open and various
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// attributes such as disposition. On return |out_new_contents| contains the
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WebContents the URL is opened in. Returns the web contents opened by the
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// browser.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* ChromeWebContentsHandler::OpenURLFromTab(
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::BrowserContext* context,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* source,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const OpenURLParams& params) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!context)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = Profile::FromBrowserContext(context);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::HostDesktopType desktop_type = chrome::HOST_DESKTOP_TYPE_NATIVE;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (source) {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Browser* source_browser = chrome::FindBrowserWithWebContents(source);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (source_browser)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      desktop_type = source_browser->host_desktop_type();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool browser_created = !browser;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!browser)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser = new Browser(
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Browser::CreateParams(Browser::TYPE_TABBED, profile, desktop_type));
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::NavigateParams nav_params(browser, params.url, params.transition);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nav_params.referrer = params.referrer;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (source && source->IsCrashed() &&
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      params.disposition == CURRENT_TAB &&
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      params.transition == ui::PAGE_TRANSITION_LINK) {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    nav_params.disposition = NEW_FOREGROUND_TAB;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    nav_params.disposition = params.disposition;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nav_params.user_gesture = true;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::Navigate(&nav_params);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close the browser if chrome::Navigate created a new one.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser_created && (browser != nav_params.browser))
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser->window()->Close();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return nav_params.target_contents;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates a new tab with |new_contents|. |context| is the browser context that
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the browser should be owned by. |source| is the WebContent where the
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// operation originated. |disposition| controls how the new tab should be
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// opened. |initial_pos| is the position of the window if a new window is
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created.  |user_gesture| is true if the operation was started by a user
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// gesture.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ChromeWebContentsHandler::AddNewContents(
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::BrowserContext* context,
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* source,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* new_contents,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WindowOpenDisposition disposition,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const gfx::Rect& initial_pos,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool user_gesture) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!context)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = Profile::FromBrowserContext(context);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::HostDesktopType desktop_type = chrome::HOST_DESKTOP_TYPE_NATIVE;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (source) {
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Browser* source_browser = chrome::FindBrowserWithWebContents(source);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (source_browser)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      desktop_type = source_browser->host_desktop_type();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool browser_created = !browser;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!browser)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser = new Browser(
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Browser::CreateParams(Browser::TYPE_TABBED, profile, desktop_type));
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chrome::NavigateParams params(browser, new_contents);
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  params.source_contents = source;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.disposition = disposition;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.window_bounds = initial_pos;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.window_action = chrome::NavigateParams::SHOW_WINDOW;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.user_gesture = true;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::Navigate(&params);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close the browser if chrome::Navigate created a new one.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser_created && (browser != params.browser))
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser->window()->Close();
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
114