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