Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/extensions/service_worker_apitest.cc

Issue 2166523003: Add ref count to service workers for extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync@tott Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 size_t ref_count = 0;
174 auto set_ref_count = [](size_t* ref_count, base::RunLoop* run_loop,
175 size_t external_request_count) {
176 *ref_count = external_request_count;
177 run_loop->Quit();
178 };
179 sw_context->CountExternalRequestsForTest(
180 origin, base::Bind(set_ref_count, &ref_count, &run_loop));
181 run_loop.Run();
182 return ref_count;
183 }
184
165 private: 185 private:
166 // Sets the channel to "stable". 186 // Sets the channel to "stable".
167 // Not useful after we've opened extension Service Workers to stable 187 // Not useful after we've opened extension Service Workers to stable
168 // channel. 188 // channel.
169 // TODO(lazyboy): Remove this when ExtensionServiceWorkersEnabled() is 189 // TODO(lazyboy): Remove this when ExtensionServiceWorkersEnabled() is
170 // removed. 190 // removed.
171 ScopedCurrentChannel current_channel_; 191 ScopedCurrentChannel current_channel_;
172 192
173 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); 193 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest);
174 }; 194 };
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 643 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
624 web_contents, "window.runServiceWorker()", &result)); 644 web_contents, "window.runServiceWorker()", &result));
625 ASSERT_EQ("chrome.tabs.create callback", result); 645 ASSERT_EQ("chrome.tabs.create callback", result);
626 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count()); 646 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count());
627 647
628 // Check extension shutdown path. 648 // Check extension shutdown path.
629 UnloadExtension(extension->id()); 649 UnloadExtension(extension->id());
630 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count()); 650 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count());
631 } 651 }
632 652
653 // Tests that worker ref count increments while extension API function is
654 // active.
655 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WorkerRefCount) {
656 // Extensions APIs from SW are only enabled on trunk.
657 ScopedCurrentChannel current_channel_override(version_info::Channel::UNKNOWN);
658 const Extension* extension = LoadExtensionWithFlags(
659 test_data_dir_.AppendASCII("service_worker/api_worker_ref_count"),
660 kFlagNone);
661 ASSERT_TRUE(extension);
662 ui_test_utils::NavigateToURL(browser(),
663 extension->GetResourceURL("page.html"));
664 content::WebContents* web_contents =
665 browser()->tab_strip_model()->GetActiveWebContents();
666
667 ExtensionTestMessageListener worker_start_listener("WORKER STARTED", false);
668 worker_start_listener.set_failure_message("FAILURE");
669 ASSERT_TRUE(
670 content::ExecuteScript(web_contents, "window.runServiceWorker()"));
671 ASSERT_TRUE(worker_start_listener.WaitUntilSatisfied());
672
673 // Service worker should have no pending requests because it hasn't peformed
674 // any extension API request yet.
675 EXPECT_EQ(0u, GetWorkerRefCount(extension->url()));
676
677 ExtensionTestMessageListener worker_listener("CHECK_REF_COUNT", true);
678 worker_listener.set_failure_message("FAILURE");
679 ASSERT_TRUE(content::ExecuteScript(web_contents, "window.testSendMessage()"));
680 ASSERT_TRUE(worker_listener.WaitUntilSatisfied());
681
682 // Service worker should have exactly one pending request because
683 // chrome.test.sendMessage() API call is in-flight.
684 EXPECT_EQ(1u, GetWorkerRefCount(extension->url()));
685
686 // Peform another extension API request while one is ongoing.
687 {
688 ExtensionTestMessageListener listener("CHECK_REF_COUNT", true);
689 listener.set_failure_message("FAILURE");
690 ASSERT_TRUE(
691 content::ExecuteScript(web_contents, "window.testSendMessage()"));
692 ASSERT_TRUE(listener.WaitUntilSatisfied());
693
694 // Service worker currently has two extension API requests in-flight.
695 EXPECT_EQ(2u, GetWorkerRefCount(extension->url()));
696 // Finish executing the nested chrome.test.sendMessage() first.
697 listener.Reply("Hello world");
698 }
699
700 ExtensionTestMessageListener extension_listener("SUCCESS", false);
701 extension_listener.set_failure_message("FAILURE");
702 // Finish executing chrome.test.sendMessage().
703 worker_listener.Reply("Hello world");
704 ASSERT_TRUE(extension_listener.WaitUntilSatisfied());
705
706 // The ref count should drop to 0.
707 EXPECT_EQ(0u, GetWorkerRefCount(extension->url()));
708 }
709
633 // This test loads a web page that has an iframe pointing to a 710 // 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 711 // chrome-extension:// URL. The URL is listed in the extension's
635 // web_accessible_resources. Initially the iframe is served from the extension's 712 // web_accessible_resources. Initially the iframe is served from the extension's
636 // resource file. After verifying that, we register a Service Worker that 713 // resource file. After verifying that, we register a Service Worker that
637 // controls the extension. Further requests to the same resource as before 714 // controls the extension. Further requests to the same resource as before
638 // should now be served by the Service Worker. 715 // should now be served by the Service Worker.
639 // This test also verifies that if the requested resource exists in the manifest 716 // 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 717 // but is not present in the extension directory, the Service Worker can still
641 // serve the resource file. 718 // serve the resource file.
642 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) { 719 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 message.sender_id = "1234567890"; 860 message.sender_id = "1234567890";
784 message.raw_data = "testdata"; 861 message.raw_data = "testdata";
785 message.decrypted = true; 862 message.decrypted = true;
786 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 863 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
787 push_service()->OnMessage(app_identifier.app_id(), message); 864 push_service()->OnMessage(app_identifier.app_id(), message);
788 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); 865 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
789 run_loop.Run(); // Wait until the message is handled by push service. 866 run_loop.Run(); // Wait until the message is handled by push service.
790 } 867 }
791 868
792 } // namespace extensions 869 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698