Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 13 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 14 #include "chrome/browser/permissions/permission_manager.h" | 14 #include "chrome/browser/permissions/permission_manager.h" |
| 15 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 15 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 16 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 16 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 17 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" | 17 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 18 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 18 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 22 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" | 22 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" |
| 23 #include "components/version_info/version_info.h" | 23 #include "components/version_info/version_info.h" |
| 24 #include "content/public/browser/navigation_controller.h" | 24 #include "content/public/browser/navigation_controller.h" |
| 25 #include "content/public/browser/navigation_entry.h" | 25 #include "content/public/browser/navigation_entry.h" |
| 26 #include "content/public/browser/permission_type.h" | 26 #include "content/public/browser/permission_type.h" |
| 27 #include "content/public/browser/service_worker_context.h" | |
| 28 #include "content/public/browser/storage_partition.h" | |
| 27 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
| 29 #include "content/public/common/origin_util.h" | 31 #include "content/public/common/origin_util.h" |
| 30 #include "content/public/common/page_type.h" | 32 #include "content/public/common/page_type.h" |
| 31 #include "content/public/test/background_sync_test_util.h" | 33 #include "content/public/test/background_sync_test_util.h" |
| 32 #include "content/public/test/browser_test_utils.h" | 34 #include "content/public/test/browser_test_utils.h" |
| 33 #include "extensions/browser/extension_host.h" | 35 #include "extensions/browser/extension_host.h" |
| 34 #include "extensions/browser/extension_registry.h" | 36 #include "extensions/browser/extension_registry.h" |
| 35 #include "extensions/browser/process_manager.h" | 37 #include "extensions/browser/process_manager.h" |
| 36 #include "extensions/test/background_page_watcher.h" | 38 #include "extensions/test/background_page_watcher.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 157 } |
| 156 return inner_text; | 158 return inner_text; |
| 157 } | 159 } |
| 158 | 160 |
| 159 // Navigates the browser to |url|, then returns the innerText of the new | 161 // Navigates the browser to |url|, then returns the innerText of the new |
| 160 // tab's WebContents' main frame. | 162 // tab's WebContents' main frame. |
| 161 std::string NavigateAndExtractInnerText(const GURL& url) { | 163 std::string NavigateAndExtractInnerText(const GURL& url) { |
| 162 return ExtractInnerText(Navigate(url)); | 164 return ExtractInnerText(Navigate(url)); |
| 163 } | 165 } |
| 164 | 166 |
| 167 size_t GetWorkerRefCount(const GURL& origin) { | |
| 168 content::ServiceWorkerContext* sw_context = | |
| 169 content::BrowserContext::GetDefaultStoragePartition( | |
| 170 browser()->profile()) | |
| 171 ->GetServiceWorkerContext(); | |
| 172 base::RunLoop run_loop; | |
| 173 sw_context->GetPendingExternalRequestCountForTest( | |
| 174 origin, | |
| 175 base::Bind(&ServiceWorkerTest::DidGetPendingExternalRequestCount, | |
| 176 // Unretained is safe because we wait until the bound method | |
| 177 // is called. | |
| 178 base::Unretained(this), run_loop.QuitClosure())); | |
|
Devlin
2016/10/07 19:06:44
Optional nit: you might be able to do this in a la
lazyboy
2016/10/07 21:28:41
Ya, less members and callback functions outside.
D
| |
| 179 run_loop.Run(); | |
| 180 return last_worker_ref_count_; | |
| 181 } | |
| 182 | |
| 165 private: | 183 private: |
| 184 void DidGetPendingExternalRequestCount(const base::Closure& callback, | |
| 185 size_t external_request_count) { | |
| 186 last_worker_ref_count_ = external_request_count; | |
| 187 callback.Run(); | |
| 188 } | |
| 189 | |
| 166 // Sets the channel to "stable". | 190 // Sets the channel to "stable". |
| 167 // Not useful after we've opened extension Service Workers to stable | 191 // Not useful after we've opened extension Service Workers to stable |
| 168 // channel. | 192 // channel. |
| 169 // TODO(lazyboy): Remove this when ExtensionServiceWorkersEnabled() is | 193 // TODO(lazyboy): Remove this when ExtensionServiceWorkersEnabled() is |
| 170 // removed. | 194 // removed. |
| 171 ScopedCurrentChannel current_channel_; | 195 ScopedCurrentChannel current_channel_; |
| 172 | 196 |
| 197 size_t last_worker_ref_count_ = 0u; | |
| 198 | |
| 173 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); | 199 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); |
| 174 }; | 200 }; |
| 175 | 201 |
| 176 class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest { | 202 class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest { |
| 177 public: | 203 public: |
| 178 ServiceWorkerBackgroundSyncTest() {} | 204 ServiceWorkerBackgroundSyncTest() {} |
| 179 ~ServiceWorkerBackgroundSyncTest() override {} | 205 ~ServiceWorkerBackgroundSyncTest() override {} |
| 180 | 206 |
| 181 void SetUpCommandLine(base::CommandLine* command_line) override { | 207 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 182 // ServiceWorkerRegistration.sync requires experimental flag. | 208 // ServiceWorkerRegistration.sync requires experimental flag. |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 623 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 649 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 624 web_contents, "window.runServiceWorker()", &result)); | 650 web_contents, "window.runServiceWorker()", &result)); |
| 625 ASSERT_EQ("chrome.tabs.create callback", result); | 651 ASSERT_EQ("chrome.tabs.create callback", result); |
| 626 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count()); | 652 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count()); |
| 627 | 653 |
| 628 // Check extension shutdown path. | 654 // Check extension shutdown path. |
| 629 UnloadExtension(extension->id()); | 655 UnloadExtension(extension->id()); |
| 630 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count()); | 656 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count()); |
| 631 } | 657 } |
| 632 | 658 |
| 659 // Tests that worker ref count increments while extension API function is | |
| 660 // active. | |
| 661 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WorkerRefCount) { | |
| 662 // Extensions APIs from SW are only enabled on trunk. | |
| 663 ScopedCurrentChannel current_channel_override(version_info::Channel::UNKNOWN); | |
| 664 const Extension* extension = LoadExtensionWithFlags( | |
| 665 test_data_dir_.AppendASCII("service_worker/api_worker_ref_count"), | |
| 666 kFlagNone); | |
| 667 ASSERT_TRUE(extension); | |
| 668 ui_test_utils::NavigateToURL(browser(), | |
| 669 extension->GetResourceURL("page.html")); | |
| 670 content::WebContents* web_contents = | |
| 671 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 672 | |
| 673 ExtensionTestMessageListener worker_start_listener("WORKER STARTED", false); | |
| 674 worker_start_listener.set_failure_message("FAILURE"); | |
| 675 ASSERT_TRUE( | |
| 676 content::ExecuteScript(web_contents, "window.runServiceWorker()")); | |
| 677 ASSERT_TRUE(worker_start_listener.WaitUntilSatisfied()); | |
| 678 | |
| 679 // Service worker should have no pending requests because it hasn't peformed | |
| 680 // any extension API request yet. | |
| 681 EXPECT_EQ(0u, GetWorkerRefCount(extension->url())); | |
| 682 | |
| 683 ExtensionTestMessageListener worker_listener("CHECK_REF_COUNT", true); | |
| 684 worker_listener.set_failure_message("FAILURE"); | |
| 685 ASSERT_TRUE(content::ExecuteScript(web_contents, "window.testSendMessage()")); | |
| 686 ASSERT_TRUE(worker_listener.WaitUntilSatisfied()); | |
| 687 | |
| 688 // Service worker should have exactly one pending request because | |
| 689 // chrome.test.sendMessage() API call is in-flight. | |
| 690 EXPECT_EQ(1u, GetWorkerRefCount(extension->url())); | |
| 691 | |
| 692 // Peform another extension API request while one is ongoing. | |
| 693 { | |
| 694 ExtensionTestMessageListener listener("CHECK_REF_COUNT", true); | |
| 695 listener.set_failure_message("FAILURE"); | |
| 696 ASSERT_TRUE( | |
| 697 content::ExecuteScript(web_contents, "window.testSendMessage()")); | |
| 698 ASSERT_TRUE(listener.WaitUntilSatisfied()); | |
| 699 // Finish executing the nested chrome.test.sendMessage() first. | |
| 700 listener.Reply("Hello world"); | |
|
Devlin
2016/10/07 19:06:44
TL;DR: Put the EXPECT_EQ(2u, GetWorkerRefCount(ext
lazyboy
2016/10/07 21:28:41
Good catch.
Done.
| |
| 701 } | |
| 702 | |
| 703 // Service worker currently has two extension API requests in-flight. | |
| 704 EXPECT_EQ(2u, GetWorkerRefCount(extension->url())); | |
| 705 | |
| 706 ExtensionTestMessageListener extension_listener("SUCCESS", false); | |
| 707 extension_listener.set_failure_message("FAILURE"); | |
| 708 // Finish executing chrome.test.sendMessage(). | |
| 709 worker_listener.Reply("Hello world"); | |
| 710 ASSERT_TRUE(extension_listener.WaitUntilSatisfied()); | |
| 711 | |
| 712 // The ref count should drop to 0. | |
| 713 EXPECT_EQ(0u, GetWorkerRefCount(extension->url())); | |
| 714 } | |
| 715 | |
| 633 // This test loads a web page that has an iframe pointing to a | 716 // This test loads a web page that has an iframe pointing to a |
| 634 // chrome-extension:// URL. The URL is listed in the extension's | 717 // chrome-extension:// URL. The URL is listed in the extension's |
| 635 // web_accessible_resources. Initially the iframe is served from the extension's | 718 // web_accessible_resources. Initially the iframe is served from the extension's |
| 636 // resource file. After verifying that, we register a Service Worker that | 719 // resource file. After verifying that, we register a Service Worker that |
| 637 // controls the extension. Further requests to the same resource as before | 720 // controls the extension. Further requests to the same resource as before |
| 638 // should now be served by the Service Worker. | 721 // should now be served by the Service Worker. |
| 639 // This test also verifies that if the requested resource exists in the manifest | 722 // This test also verifies that if the requested resource exists in the manifest |
| 640 // but is not present in the extension directory, the Service Worker can still | 723 // but is not present in the extension directory, the Service Worker can still |
| 641 // serve the resource file. | 724 // serve the resource file. |
| 642 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) { | 725 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 783 message.sender_id = "1234567890"; | 866 message.sender_id = "1234567890"; |
| 784 message.raw_data = "testdata"; | 867 message.raw_data = "testdata"; |
| 785 message.decrypted = true; | 868 message.decrypted = true; |
| 786 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); | 869 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); |
| 787 push_service()->OnMessage(app_identifier.app_id(), message); | 870 push_service()->OnMessage(app_identifier.app_id(), message); |
| 788 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); | 871 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); |
| 789 run_loop.Run(); // Wait until the message is handled by push service. | 872 run_loop.Run(); // Wait until the message is handled by push service. |
| 790 } | 873 } |
| 791 | 874 |
| 792 } // namespace extensions | 875 } // namespace extensions |
| OLD | NEW |