OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_tab_util.h" | 5 #include "chrome/browser/extensions/extension_tab_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 if (error_message) | 91 if (error_message) |
92 *error_message = ErrorUtils::FormatErrorMessage( | 92 *error_message = ErrorUtils::FormatErrorMessage( |
93 keys::kWindowNotFoundError, base::IntToString(window_id)); | 93 keys::kWindowNotFoundError, base::IntToString(window_id)); |
94 | 94 |
95 return NULL; | 95 return NULL; |
96 } | 96 } |
97 | 97 |
98 Browser* CreateBrowser(Profile* profile, int window_id, std::string* error) { | 98 Browser* CreateBrowser(Profile* profile, |
| 99 int window_id, |
| 100 bool user_gesture, |
| 101 std::string* error) { |
99 Browser::CreateParams params(Browser::TYPE_TABBED, profile); | 102 Browser::CreateParams params(Browser::TYPE_TABBED, profile); |
| 103 params.user_gesture = user_gesture; |
100 Browser* browser = new Browser(params); | 104 Browser* browser = new Browser(params); |
101 browser->window()->Show(); | 105 browser->window()->Show(); |
102 return browser; | 106 return browser; |
103 } | 107 } |
104 | 108 |
105 // Use this function for reporting a tab id to an extension. It will | 109 // Use this function for reporting a tab id to an extension. It will |
106 // take care of setting the id to TAB_ID_NONE if necessary (for | 110 // take care of setting the id to TAB_ID_NONE if necessary (for |
107 // example with devtools). | 111 // example with devtools). |
108 int GetTabIdForExtensions(const WebContents* web_contents) { | 112 int GetTabIdForExtensions(const WebContents* web_contents) { |
109 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 113 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
110 if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser)) | 114 if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser)) |
111 return -1; | 115 return -1; |
112 return SessionTabHelper::IdForTab(web_contents); | 116 return SessionTabHelper::IdForTab(web_contents); |
113 } | 117 } |
114 | 118 |
115 } // namespace | 119 } // namespace |
116 | 120 |
117 ExtensionTabUtil::OpenTabParams::OpenTabParams() | 121 ExtensionTabUtil::OpenTabParams::OpenTabParams() |
118 : create_browser_if_needed(false) { | 122 : create_browser_if_needed(false) { |
119 } | 123 } |
120 | 124 |
121 ExtensionTabUtil::OpenTabParams::~OpenTabParams() { | 125 ExtensionTabUtil::OpenTabParams::~OpenTabParams() { |
122 } | 126 } |
123 | 127 |
124 // Opens a new tab for a given extension. Returns NULL and sets |error| if an | 128 // Opens a new tab for a given extension. Returns NULL and sets |error| if an |
125 // error occurs. | 129 // error occurs. |
126 base::DictionaryValue* ExtensionTabUtil::OpenTab( | 130 base::DictionaryValue* ExtensionTabUtil::OpenTab( |
127 UIThreadExtensionFunction* function, | 131 UIThreadExtensionFunction* function, |
128 const OpenTabParams& params, | 132 const OpenTabParams& params, |
| 133 bool user_gesture, |
129 std::string* error) { | 134 std::string* error) { |
130 ChromeExtensionFunctionDetails chrome_details(function); | 135 ChromeExtensionFunctionDetails chrome_details(function); |
131 Profile* profile = chrome_details.GetProfile(); | 136 Profile* profile = chrome_details.GetProfile(); |
132 // windowId defaults to "current" window. | 137 // windowId defaults to "current" window. |
133 int window_id = extension_misc::kCurrentWindowId; | 138 int window_id = extension_misc::kCurrentWindowId; |
134 if (params.window_id.get()) | 139 if (params.window_id.get()) |
135 window_id = *params.window_id; | 140 window_id = *params.window_id; |
136 | 141 |
137 Browser* browser = GetBrowserFromWindowID(chrome_details, window_id, error); | 142 Browser* browser = GetBrowserFromWindowID(chrome_details, window_id, error); |
138 if (!browser) { | 143 if (!browser) { |
139 if (!params.create_browser_if_needed) { | 144 if (!params.create_browser_if_needed) { |
140 return NULL; | 145 return NULL; |
141 } | 146 } |
142 browser = CreateBrowser(profile, window_id, error); | 147 browser = CreateBrowser(profile, window_id, user_gesture, error); |
143 if (!browser) | 148 if (!browser) |
144 return NULL; | 149 return NULL; |
145 } | 150 } |
146 | 151 |
147 // Ensure the selected browser is tabbed. | 152 // Ensure the selected browser is tabbed. |
148 if (!browser->is_type_tabbed() && browser->IsAttemptingToCloseBrowser()) | 153 if (!browser->is_type_tabbed() && browser->IsAttemptingToCloseBrowser()) |
149 browser = chrome::FindTabbedBrowser(profile, function->include_incognito()); | 154 browser = chrome::FindTabbedBrowser(profile, function->include_incognito()); |
150 if (!browser || !browser->window()) { | 155 if (!browser || !browser->window()) { |
151 if (error) | 156 if (error) |
152 *error = keys::kNoCurrentWindowError; | 157 *error = keys::kNoCurrentWindowError; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 213 |
209 // We can't load extension URLs into incognito windows unless the extension | 214 // We can't load extension URLs into incognito windows unless the extension |
210 // uses split mode. Special case to fall back to a tabbed window. | 215 // uses split mode. Special case to fall back to a tabbed window. |
211 if (url.SchemeIs(kExtensionScheme) && | 216 if (url.SchemeIs(kExtensionScheme) && |
212 !IncognitoInfo::IsSplitMode(function->extension()) && | 217 !IncognitoInfo::IsSplitMode(function->extension()) && |
213 browser->profile()->IsOffTheRecord()) { | 218 browser->profile()->IsOffTheRecord()) { |
214 Profile* profile = browser->profile()->GetOriginalProfile(); | 219 Profile* profile = browser->profile()->GetOriginalProfile(); |
215 | 220 |
216 browser = chrome::FindTabbedBrowser(profile, false); | 221 browser = chrome::FindTabbedBrowser(profile, false); |
217 if (!browser) { | 222 if (!browser) { |
218 browser = | 223 Browser::CreateParams params = |
219 new Browser(Browser::CreateParams(Browser::TYPE_TABBED, profile)); | 224 Browser::CreateParams(Browser::TYPE_TABBED, profile); |
| 225 params.user_gesture = user_gesture; |
| 226 browser = new Browser(params); |
220 browser->window()->Show(); | 227 browser->window()->Show(); |
221 } | 228 } |
222 } | 229 } |
223 | 230 |
224 // If index is specified, honor the value, but keep it bound to | 231 // If index is specified, honor the value, but keep it bound to |
225 // -1 <= index <= tab_strip->count() where -1 invokes the default behavior. | 232 // -1 <= index <= tab_strip->count() where -1 invokes the default behavior. |
226 int index = -1; | 233 int index = -1; |
227 if (params.index.get()) | 234 if (params.index.get()) |
228 index = *params.index; | 235 index = *params.index; |
229 | 236 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 | 583 |
577 void ExtensionTabUtil::CreateTab(WebContents* web_contents, | 584 void ExtensionTabUtil::CreateTab(WebContents* web_contents, |
578 const std::string& extension_id, | 585 const std::string& extension_id, |
579 WindowOpenDisposition disposition, | 586 WindowOpenDisposition disposition, |
580 const gfx::Rect& initial_rect, | 587 const gfx::Rect& initial_rect, |
581 bool user_gesture) { | 588 bool user_gesture) { |
582 Profile* profile = | 589 Profile* profile = |
583 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 590 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
584 Browser* browser = chrome::FindTabbedBrowser(profile, false); | 591 Browser* browser = chrome::FindTabbedBrowser(profile, false); |
585 const bool browser_created = !browser; | 592 const bool browser_created = !browser; |
586 if (!browser) | 593 if (!browser) { |
587 browser = new Browser(Browser::CreateParams(profile)); | 594 Browser::CreateParams params = Browser::CreateParams(profile); |
| 595 params.user_gesture = user_gesture; |
| 596 browser = new Browser(params); |
| 597 } |
588 chrome::NavigateParams params(browser, web_contents); | 598 chrome::NavigateParams params(browser, web_contents); |
589 | 599 |
590 // The extension_app_id parameter ends up as app_name in the Browser | 600 // The extension_app_id parameter ends up as app_name in the Browser |
591 // which causes the Browser to return true for is_app(). This affects | 601 // which causes the Browser to return true for is_app(). This affects |
592 // among other things, whether the location bar gets displayed. | 602 // among other things, whether the location bar gets displayed. |
593 // TODO(mpcomplete): This seems wrong. What if the extension content is hosted | 603 // TODO(mpcomplete): This seems wrong. What if the extension content is hosted |
594 // in a tab? | 604 // in a tab? |
595 if (disposition == WindowOpenDisposition::NEW_POPUP) | 605 if (disposition == WindowOpenDisposition::NEW_POPUP) |
596 params.extension_app_id = extension_id; | 606 params.extension_app_id = extension_id; |
597 | 607 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 chrome::ShowSingletonTabOverwritingNTP(browser, params); | 677 chrome::ShowSingletonTabOverwritingNTP(browser, params); |
668 return true; | 678 return true; |
669 } | 679 } |
670 | 680 |
671 // static | 681 // static |
672 bool ExtensionTabUtil::BrowserSupportsTabs(Browser* browser) { | 682 bool ExtensionTabUtil::BrowserSupportsTabs(Browser* browser) { |
673 return browser && browser->tab_strip_model() && !browser->is_devtools(); | 683 return browser && browser->tab_strip_model() && !browser->is_devtools(); |
674 } | 684 } |
675 | 685 |
676 } // namespace extensions | 686 } // namespace extensions |
OLD | NEW |