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