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
31 WebContents
* ChromeWebContentsHandler::OpenURLFromTab(
32 content::BrowserContext
* context
,
34 const OpenURLParams
& params
) {
38 Profile
* profile
= Profile::FromBrowserContext(context
);
40 chrome::HostDesktopType desktop_type
= chrome::HOST_DESKTOP_TYPE_NATIVE
;
42 Browser
* source_browser
= chrome::FindBrowserWithWebContents(source
);
44 desktop_type
= source_browser
->host_desktop_type();
47 Browser
* browser
= chrome::FindTabbedBrowser(profile
, false, desktop_type
);
48 const bool browser_created
= !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
== content::PAGE_TRANSITION_LINK
) {
57 nav_params
.disposition
= NEW_FOREGROUND_TAB
;
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_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
78 void ChromeWebContentsHandler::AddNewContents(
79 content::BrowserContext
* context
,
81 WebContents
* new_contents
,
82 WindowOpenDisposition disposition
,
83 const gfx::Rect
& initial_pos
,
88 Profile
* profile
= Profile::FromBrowserContext(context
);
90 chrome::HostDesktopType desktop_type
= chrome::HOST_DESKTOP_TYPE_NATIVE
;
92 Browser
* source_browser
= chrome::FindBrowserWithWebContents(source
);
94 desktop_type
= source_browser
->host_desktop_type();
97 Browser
* browser
= chrome::FindTabbedBrowser(profile
, false, desktop_type
);
98 const bool browser_created
= !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(¶ms
);
110 // Close the browser if chrome::Navigate created a new one.
111 if (browser_created
&& (browser
!= params
.browser
))
112 browser
->window()->Close();