chrome_web_contents_handler.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright (c) 2012 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/webui/chrome_web_contents_handler.h"
6
7#include "chrome/browser/profiles/profile.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_finder.h"
10#include "chrome/browser/ui/browser_navigator.h"
11#include "chrome/browser/ui/browser_window.h"
12#include "chrome/browser/ui/host_desktop.h"
13#include "chrome/browser/ui/tabs/tab_strip_model.h"
14#include "content/public/browser/web_contents.h"
15
16using content::BrowserContext;
17using content::OpenURLParams;
18using content::WebContents;
19
20ChromeWebContentsHandler::ChromeWebContentsHandler() {
21}
22
23ChromeWebContentsHandler::~ChromeWebContentsHandler() {
24}
25
26// Opens a new URL inside |source|. |context| is the browser context that the
27// browser should be owned by. |params| contains the URL to open and various
28// attributes such as disposition. On return |out_new_contents| contains the
29// WebContents the URL is opened in. Returns the web contents opened by the
30// browser.
31WebContents* ChromeWebContentsHandler::OpenURLFromTab(
32    content::BrowserContext* context,
33    WebContents* source,
34    const OpenURLParams& params) {
35  if (!context)
36    return NULL;
37
38  Profile* profile = Profile::FromBrowserContext(context);
39
40  chrome::HostDesktopType desktop_type = chrome::HOST_DESKTOP_TYPE_NATIVE;
41  if (source) {
42    Browser* source_browser = chrome::FindBrowserWithWebContents(source);
43    if (source_browser)
44      desktop_type = source_browser->host_desktop_type();
45  }
46
47  Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
48  const bool browser_created = !browser;
49  if (!browser)
50    browser = new Browser(
51        Browser::CreateParams(Browser::TYPE_TABBED, profile, desktop_type));
52  chrome::NavigateParams nav_params(browser, params.url, params.transition);
53  nav_params.referrer = params.referrer;
54  if (source && source->IsCrashed() &&
55      params.disposition == CURRENT_TAB &&
56      params.transition == ui::PAGE_TRANSITION_LINK) {
57    nav_params.disposition = NEW_FOREGROUND_TAB;
58  } else {
59    nav_params.disposition = params.disposition;
60  }
61  nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
62  nav_params.user_gesture = true;
63  chrome::Navigate(&nav_params);
64
65  // Close the browser if chrome::Navigate created a new one.
66  if (browser_created && (browser != nav_params.browser))
67    browser->window()->Close();
68
69  return nav_params.target_contents;
70}
71
72// Creates a new tab with |new_contents|. |context| is the browser context that
73// the browser should be owned by. |source| is the WebContent where the
74// operation originated. |disposition| controls how the new tab should be
75// opened. |initial_pos| is the position of the window if a new window is
76// created.  |user_gesture| is true if the operation was started by a user
77// gesture.
78void ChromeWebContentsHandler::AddNewContents(
79    content::BrowserContext* context,
80    WebContents* source,
81    WebContents* new_contents,
82    WindowOpenDisposition disposition,
83    const gfx::Rect& initial_pos,
84    bool user_gesture) {
85  if (!context)
86    return;
87
88  Profile* profile = Profile::FromBrowserContext(context);
89
90  chrome::HostDesktopType desktop_type = chrome::HOST_DESKTOP_TYPE_NATIVE;
91  if (source) {
92    Browser* source_browser = chrome::FindBrowserWithWebContents(source);
93    if (source_browser)
94      desktop_type = source_browser->host_desktop_type();
95  }
96
97  Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
98  const bool browser_created = !browser;
99  if (!browser)
100    browser = new Browser(
101        Browser::CreateParams(Browser::TYPE_TABBED, profile, desktop_type));
102  chrome::NavigateParams params(browser, new_contents);
103  params.source_contents = source;
104  params.disposition = disposition;
105  params.window_bounds = initial_pos;
106  params.window_action = chrome::NavigateParams::SHOW_WINDOW;
107  params.user_gesture = true;
108  chrome::Navigate(&params);
109
110  // Close the browser if chrome::Navigate created a new one.
111  if (browser_created && (browser != params.browser))
112    browser->window()->Close();
113}
114