Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chrome_web_contents_handler.cc
blob9b21e212b6b18b7daf432d68b7432b4ec912fe25
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.
5 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h"
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"
16 using content::BrowserContext;
17 using content::OpenURLParams;
18 using content::WebContents;
20 ChromeWebContentsHandler::ChromeWebContentsHandler() {
23 ChromeWebContentsHandler::~ChromeWebContentsHandler() {
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.
31 WebContents* ChromeWebContentsHandler::OpenURLFromTab(
32 content::BrowserContext* context,
33 WebContents* source,
34 const OpenURLParams& params) {
35 if (!context)
36 return NULL;
38 Profile* profile = Profile::FromBrowserContext(context);
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();
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;
61 nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
62 nav_params.user_gesture = true;
63 chrome::Navigate(&nav_params);
65 // Close the browser if chrome::Navigate created a new one.
66 if (browser_created && (browser != nav_params.browser))
67 browser->window()->Close();
69 return nav_params.target_contents;
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_rect| is the position and size of the window if a new window
76 // is created. |user_gesture| is true if the operation was started by a user
77 // gesture.
78 void ChromeWebContentsHandler::AddNewContents(
79 content::BrowserContext* context,
80 WebContents* source,
81 WebContents* new_contents,
82 WindowOpenDisposition disposition,
83 const gfx::Rect& initial_rect,
84 bool user_gesture) {
85 if (!context)
86 return;
88 Profile* profile = Profile::FromBrowserContext(context);
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();
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_rect;
106 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
107 params.user_gesture = true;
108 chrome::Navigate(&params);
110 // Close the browser if chrome::Navigate created a new one.
111 if (browser_created && (browser != params.browser))
112 browser->window()->Close();