diff options
Diffstat (limited to 'chromium/chrome/browser/extensions/api/tabs')
5 files changed, 88 insertions, 77 deletions
diff --git a/chromium/chrome/browser/extensions/api/tabs/tabs_api.cc b/chromium/chrome/browser/extensions/api/tabs/tabs_api.cc index 9a275120a54..a753fd57760 100644 --- a/chromium/chrome/browser/extensions/api/tabs/tabs_api.cc +++ b/chromium/chrome/browser/extensions/api/tabs/tabs_api.cc @@ -640,12 +640,13 @@ ExtensionFunction::ResponseAction WindowsCreateFunction::Run() { // a tabbed window. if ((window_type == Browser::TYPE_POPUP && urls.empty()) || window_type == Browser::TYPE_TABBED) { - if (source_tab_strip) - contents = source_tab_strip->DetachWebContentsAt(tab_index); - if (contents) { + if (source_tab_strip) { + std::unique_ptr<content::WebContents> detached_tab = + source_tab_strip->DetachWebContentsAt(tab_index); + contents = detached_tab.get(); TabStripModel* target_tab_strip = new_window->tab_strip_model(); - target_tab_strip->InsertWebContentsAt(urls.size(), contents, - TabStripModel::ADD_NONE); + target_tab_strip->InsertWebContentsAt( + urls.size(), std::move(detached_tab), TabStripModel::ADD_NONE); } } // Create a new tab if the created window is still empty. Don't create a new @@ -655,20 +656,21 @@ ExtensionFunction::ResponseAction WindowsCreateFunction::Run() { } chrome::SelectNumberedTab(new_window, 0); + if (focused) + new_window->window()->Show(); + else + new_window->window()->ShowInactive(); + #if defined(OS_CHROMEOS) // Lock the window fullscreen only after the new tab has been created - // (otherwise the tabstrip is empty). + // (otherwise the tabstrip is empty), and window()->show() has been called + // (otherwise that resets the locked mode for devices in tablet mode). if (create_data && create_data->state == windows::WINDOW_STATE_LOCKED_FULLSCREEN) { SetLockedFullscreenState(new_window, true); } #endif - if (focused) - new_window->window()->Show(); - else - new_window->window()->ShowInactive(); - std::unique_ptr<base::Value> result; if (new_window->profile()->IsOffTheRecord() && !browser_context()->IsOffTheRecord() && !include_incognito()) { @@ -1366,8 +1368,9 @@ bool TabsUpdateFunction::RunAsync() { if (params->update_properties.auto_discardable.get()) { bool state = *params->update_properties.auto_discardable; - g_browser_process->GetTabManager()->SetTabAutoDiscardableState(contents, - state); + resource_coordinator::TabLifecycleUnitExternal::FromWebContents( + web_contents_) + ->SetAutoDiscardable(state); } if (!is_async) { @@ -1395,11 +1398,13 @@ bool TabsUpdateFunction::UpdateURL(const std::string &url_string, return false; } + const bool is_javascript_scheme = url.SchemeIs(url::kJavaScriptScheme); + UMA_HISTOGRAM_BOOLEAN("Extensions.ApiTabUpdateJavascript", + is_javascript_scheme); // JavaScript URLs can do the same kinds of things as cross-origin XHR, so // we need to check host permissions before allowing them. - if (url.SchemeIs(url::kJavaScriptScheme)) { + if (is_javascript_scheme) { if (!extension()->permissions_data()->CanAccessPage( - extension(), web_contents_->GetURL(), tab_id, &error_)) { @@ -1564,7 +1569,7 @@ bool TabsMoveFunction::MoveTab(int tab_id, if (ExtensionTabUtil::GetWindowId(target_browser) != ExtensionTabUtil::GetWindowId(source_browser)) { TabStripModel* target_tab_strip = target_browser->tab_strip_model(); - WebContents* web_contents = + std::unique_ptr<content::WebContents> web_contents = source_tab_strip->DetachWebContentsAt(tab_index); if (!web_contents) { *error = ErrorUtils::FormatErrorMessage(keys::kTabNotFoundError, @@ -1578,12 +1583,13 @@ bool TabsMoveFunction::MoveTab(int tab_id, if (*new_index > target_tab_strip->count() || *new_index < 0) *new_index = target_tab_strip->count(); - target_tab_strip->InsertWebContentsAt( - *new_index, web_contents, TabStripModel::ADD_NONE); + content::WebContents* web_contents_raw = web_contents.get(); + target_tab_strip->InsertWebContentsAt(*new_index, std::move(web_contents), + TabStripModel::ADD_NONE); if (has_callback()) { tab_values->Append(ExtensionTabUtil::CreateTabObject( - web_contents, ExtensionTabUtil::kScrubTab, + web_contents_raw, ExtensionTabUtil::kScrubTab, extension(), target_tab_strip, *new_index) ->ToValue()); } @@ -1739,7 +1745,7 @@ WebContents* TabsCaptureVisibleTabFunction::GetWebContentsForID( } if (!extension()->permissions_data()->CanCaptureVisiblePage( - contents->GetLastCommittedURL(), extension(), + contents->GetLastCommittedURL(), SessionTabHelper::IdForTab(contents).id(), error)) { return nullptr; } @@ -2007,8 +2013,8 @@ bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage(std::string* error) { // NOTE: This can give the wrong answer due to race conditions, but it is OK, // we check again in the renderer. - if (!extension()->permissions_data()->CanAccessPage( - extension(), effective_document_url, execute_tab_id_, error)) { + if (!extension()->permissions_data()->CanAccessPage(effective_document_url, + execute_tab_id_, error)) { if (is_about_url && extension()->permissions_data()->active_permissions().HasAPIPermission( APIPermission::kTab)) { @@ -2084,7 +2090,7 @@ bool TabsSetZoomFunction::RunAsync() { return false; GURL url(web_contents->GetVisibleURL()); - if (PermissionsData::IsRestrictedUrl(url, extension(), &error_)) + if (extension()->permissions_data()->IsRestrictedUrl(url, &error_)) return false; ZoomController* zoom_controller = @@ -2136,7 +2142,7 @@ bool TabsSetZoomSettingsFunction::RunAsync() { return false; GURL url(web_contents->GetVisibleURL()); - if (PermissionsData::IsRestrictedUrl(url, extension(), &error_)) + if (extension()->permissions_data()->IsRestrictedUrl(url, &error_)) return false; // "per-origin" scope is only available in "automatic" mode. diff --git a/chromium/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc b/chromium/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc index 89c2d712616..90c6838376a 100644 --- a/chromium/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc +++ b/chromium/chrome/browser/extensions/api/tabs/tabs_api_unittest.cc @@ -94,18 +94,20 @@ TEST_F(TabsApiUnitTest, QueryWithoutTabsPermission) { std::string tab_titles[] = {"", "Sample title", "Sample title"}; // Add 3 web contentses to the browser. - std::unique_ptr<content::WebContents> web_contentses[arraysize(tab_urls)]; + content::WebContents* web_contentses[arraysize(tab_urls)]; for (size_t i = 0; i < arraysize(tab_urls); ++i) { - content::WebContents* web_contents = + std::unique_ptr<content::WebContents> web_contents = content::WebContentsTester::CreateTestWebContents(profile(), nullptr); - web_contentses[i].reset(web_contents); - browser()->tab_strip_model()->AppendWebContents(web_contents, true); + content::WebContents* raw_web_contents = web_contents.get(); + web_contentses[i] = raw_web_contents; + browser()->tab_strip_model()->AppendWebContents(std::move(web_contents), + true); EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), - web_contents); + raw_web_contents); content::WebContentsTester* web_contents_tester = - content::WebContentsTester::For(web_contents); + content::WebContentsTester::For(raw_web_contents); web_contents_tester->NavigateAndCommit(tab_urls[i]); - web_contents->GetController().GetVisibleEntry()->SetTitle( + raw_web_contents->GetController().GetVisibleEntry()->SetTitle( base::ASCIIToUTF16(tab_titles[i])); } @@ -140,7 +142,10 @@ TEST_F(TabsApiUnitTest, QueryWithoutTabsPermission) { ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); int third_tab_id = -1; ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); - EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2].get()), third_tab_id); + EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2]), third_tab_id); + + while (!browser()->tab_strip_model()->empty()) + browser()->tab_strip_model()->DetachWebContentsAt(0); } TEST_F(TabsApiUnitTest, QueryWithHostPermission) { @@ -150,18 +155,20 @@ TEST_F(TabsApiUnitTest, QueryWithHostPermission) { std::string tab_titles[] = {"", "Sample title", "Sample title"}; // Add 3 web contentses to the browser. - std::unique_ptr<content::WebContents> web_contentses[arraysize(tab_urls)]; + content::WebContents* web_contentses[arraysize(tab_urls)]; for (size_t i = 0; i < arraysize(tab_urls); ++i) { - content::WebContents* web_contents = + std::unique_ptr<content::WebContents> web_contents = content::WebContentsTester::CreateTestWebContents(profile(), nullptr); - web_contentses[i].reset(web_contents); - browser()->tab_strip_model()->AppendWebContents(web_contents, true); + content::WebContents* raw_web_contents = web_contents.get(); + web_contentses[i] = raw_web_contents; + browser()->tab_strip_model()->AppendWebContents(std::move(web_contents), + true); EXPECT_EQ(browser()->tab_strip_model()->GetActiveWebContents(), - web_contents); + raw_web_contents); content::WebContentsTester* web_contents_tester = - content::WebContentsTester::For(web_contents); + content::WebContentsTester::For(raw_web_contents); web_contents_tester->NavigateAndCommit(tab_urls[i]); - web_contents->GetController().GetVisibleEntry()->SetTitle( + raw_web_contents->GetController().GetVisibleEntry()->SetTitle( base::ASCIIToUTF16(tab_titles[i])); } @@ -192,8 +199,7 @@ TEST_F(TabsApiUnitTest, QueryWithHostPermission) { ASSERT_TRUE(tabs_list_with_permission->GetDictionary(0, &third_tab_info)); int third_tab_id = -1; ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); - EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2].get()), - third_tab_id); + EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contentses[2]), third_tab_id); } // Try the same without title, first and third tabs will match. @@ -211,10 +217,8 @@ TEST_F(TabsApiUnitTest, QueryWithHostPermission) { ASSERT_TRUE(tabs_list_with_permission->GetDictionary(1, &third_tab_info)); std::vector<int> expected_tabs_ids; - expected_tabs_ids.push_back( - ExtensionTabUtil::GetTabId(web_contentses[0].get())); - expected_tabs_ids.push_back( - ExtensionTabUtil::GetTabId(web_contentses[2].get())); + expected_tabs_ids.push_back(ExtensionTabUtil::GetTabId(web_contentses[0])); + expected_tabs_ids.push_back(ExtensionTabUtil::GetTabId(web_contentses[2])); int first_tab_id = -1; ASSERT_TRUE(first_tab_info->GetInteger("id", &first_tab_id)); @@ -224,6 +228,8 @@ TEST_F(TabsApiUnitTest, QueryWithHostPermission) { ASSERT_TRUE(third_tab_info->GetInteger("id", &third_tab_id)); EXPECT_TRUE(base::ContainsValue(expected_tabs_ids, third_tab_id)); } + while (!browser()->tab_strip_model()->empty()) + browser()->tab_strip_model()->DetachWebContentsAt(0); } // Test that using the PDF extension for tab updates is treated as a @@ -242,19 +248,21 @@ TEST_F(TabsApiUnitTest, PDFExtensionNavigation) { .Build(); ASSERT_TRUE(extension); - content::WebContents* web_contents = + std::unique_ptr<content::WebContents> web_contents = content::WebContentsTester::CreateTestWebContents(profile(), nullptr); - ASSERT_TRUE(web_contents); + content::WebContents* raw_web_contents = web_contents.get(); + ASSERT_TRUE(raw_web_contents); content::WebContentsTester* web_contents_tester = - content::WebContentsTester::For(web_contents); + content::WebContentsTester::For(raw_web_contents); const GURL kGoogle("http://www.google.com"); web_contents_tester->NavigateAndCommit(kGoogle); - EXPECT_EQ(kGoogle, web_contents->GetLastCommittedURL()); - EXPECT_EQ(kGoogle, web_contents->GetVisibleURL()); + EXPECT_EQ(kGoogle, raw_web_contents->GetLastCommittedURL()); + EXPECT_EQ(kGoogle, raw_web_contents->GetVisibleURL()); - SessionTabHelper::CreateForWebContents(web_contents); - int tab_id = SessionTabHelper::IdForTab(web_contents).id(); - browser()->tab_strip_model()->AppendWebContents(web_contents, true); + SessionTabHelper::CreateForWebContents(raw_web_contents); + int tab_id = SessionTabHelper::IdForTab(raw_web_contents).id(); + browser()->tab_strip_model()->AppendWebContents(std::move(web_contents), + true); scoped_refptr<TabsUpdateFunction> function = new TabsUpdateFunction(); function->set_extension(extension.get()); @@ -266,8 +274,8 @@ TEST_F(TabsApiUnitTest, PDFExtensionNavigation) { api_test_utils::SendResponseHelper response_helper(function.get()); function->RunWithValidation()->Execute(); - EXPECT_EQ(kGoogle, web_contents->GetLastCommittedURL()); - EXPECT_EQ(kGoogle, web_contents->GetVisibleURL()); + EXPECT_EQ(kGoogle, raw_web_contents->GetLastCommittedURL()); + EXPECT_EQ(kGoogle, raw_web_contents->GetVisibleURL()); // Clean up. response_helper.WaitForResponse(); diff --git a/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.cc b/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.cc index af63faf2661..05f4274c974 100644 --- a/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.cc +++ b/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.cc @@ -248,7 +248,9 @@ void TabsEventRouter::TabInsertedAt(TabStripModel* tab_strip_model, std::move(args), EventRouter::USER_GESTURE_UNKNOWN); } -void TabsEventRouter::TabDetachedAt(WebContents* contents, int index) { +void TabsEventRouter::TabDetachedAt(WebContents* contents, + int index, + bool was_active) { if (!GetTabEntry(contents)) { // The tab was removed. Don't send detach event. return; diff --git a/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.h b/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.h index 7f0ac1eec90..d0b11d82c81 100644 --- a/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.h +++ b/chromium/chrome/browser/extensions/api/tabs/tabs_event_router.h @@ -61,7 +61,9 @@ class TabsEventRouter : public TabStripModelObserver, void TabClosingAt(TabStripModel* tab_strip_model, content::WebContents* contents, int index) override; - void TabDetachedAt(content::WebContents* contents, int index) override; + void TabDetachedAt(content::WebContents* contents, + int index, + bool was_active) override; void ActiveTabChanged(content::WebContents* old_contents, content::WebContents* new_contents, int index, diff --git a/chromium/chrome/browser/extensions/api/tabs/tabs_test.cc b/chromium/chrome/browser/extensions/api/tabs/tabs_test.cc index b711fb4d26e..0dc983f83c5 100644 --- a/chromium/chrome/browser/extensions/api/tabs/tabs_test.cc +++ b/chromium/chrome/browser/extensions/api/tabs/tabs_test.cc @@ -11,7 +11,6 @@ #include "apps/test/app_window_waiter.h" #include "base/memory/ref_counted.h" -#include "base/message_loop/message_loop.h" #include "base/run_loop.h" #include "base/strings/pattern.h" #include "base/strings/string_split.h" @@ -889,12 +888,8 @@ base::Value* ExtensionWindowLastFocusedTest::RunFunction( IN_PROC_BROWSER_TEST_F(ExtensionWindowLastFocusedTest, ExtensionAPICannotNavigateDevtools) { - std::unique_ptr<base::DictionaryValue> test_extension_value( - api_test_utils::ParseDictionary( - "{\"name\": \"Test\", \"version\": \"1.0\", \"permissions\": " - "[\"tabs\"]}")); - scoped_refptr<Extension> extension( - api_test_utils::CreateExtension(test_extension_value.get())); + scoped_refptr<const Extension> extension = + ExtensionBuilder("Test").AddPermission("tabs").Build(); DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync( browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */); @@ -1033,7 +1028,13 @@ IN_PROC_BROWSER_TEST_F(ExtensionWindowLastFocusedTest, } #endif // !defined(OS_MACOSX) -IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, AcceptState) { +#if defined(OS_MACOSX) +// https://crbug.com/836327 +#define MAYBE_AcceptState DISABLED_AcceptState +#else +#define MAYBE_AcceptState AcceptState +#endif +IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, MAYBE_AcceptState) { #if defined(OS_MACOSX) if (base::mac::IsOS10_10()) return; // Fails when swarmed. http://crbug.com/660582 @@ -1125,12 +1126,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTab) { scoped_refptr<TabsDuplicateFunction> duplicate_tab_function( new TabsDuplicateFunction()); - std::unique_ptr<base::DictionaryValue> test_extension_value( - api_test_utils::ParseDictionary( - "{\"name\": \"Test\", \"version\": \"1.0\", \"permissions\": " - "[\"tabs\"]}")); - scoped_refptr<Extension> empty_tab_extension( - api_test_utils::CreateExtension(test_extension_value.get())); + scoped_refptr<const Extension> empty_tab_extension = + ExtensionBuilder("Test").AddPermission("tabs").Build(); duplicate_tab_function->set_extension(empty_tab_extension.get()); duplicate_tab_function->set_has_callback(true); @@ -1273,12 +1270,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_FilteredEvents) { } IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, ExecuteScriptOnDevTools) { - std::unique_ptr<base::DictionaryValue> test_extension_value( - api_test_utils::ParseDictionary( - "{\"name\": \"Test\", \"version\": \"1.0\", \"permissions\": " - "[\"tabs\"]}")); - scoped_refptr<Extension> extension( - api_test_utils::CreateExtension(test_extension_value.get())); + scoped_refptr<const Extension> extension = + ExtensionBuilder("Test").AddPermission("tabs").Build(); DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync( browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */); |