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

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: address comments from Devlin & MichaelN 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 }; 88 };
87 89
88 } // namespace 90 } // namespace
89 91
90 class ServiceWorkerTest : public ExtensionApiTest { 92 class ServiceWorkerTest : public ExtensionApiTest {
91 public: 93 public:
92 ServiceWorkerTest() : current_channel_(version_info::Channel::STABLE) {} 94 ServiceWorkerTest() : current_channel_(version_info::Channel::STABLE) {}
93 95
94 ~ServiceWorkerTest() override {} 96 ~ServiceWorkerTest() override {}
95 97
98 void DidGetPendingExternalRequestCount(const base::Closure& callback,
99 size_t external_request_count) {
100 last_worker_ref_count_ = external_request_count;
101 callback.Run();
102 }
103
96 protected: 104 protected:
97 // Returns the ProcessManager for the test's profile. 105 // Returns the ProcessManager for the test's profile.
98 ProcessManager* process_manager() { return ProcessManager::Get(profile()); } 106 ProcessManager* process_manager() { return ProcessManager::Get(profile()); }
99 107
100 // Starts running a test from the background page test extension. 108 // Starts running a test from the background page test extension.
101 // 109 //
102 // This registers a service worker with |script_name|, and fetches the 110 // This registers a service worker with |script_name|, and fetches the
103 // registration result. 111 // registration result.
104 // 112 //
105 // If |error_or_null| is null (kExpectSuccess), success is expected and this 113 // If |error_or_null| is null (kExpectSuccess), success is expected and this
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 163 }
156 return inner_text; 164 return inner_text;
157 } 165 }
158 166
159 // Navigates the browser to |url|, then returns the innerText of the new 167 // Navigates the browser to |url|, then returns the innerText of the new
160 // tab's WebContents' main frame. 168 // tab's WebContents' main frame.
161 std::string NavigateAndExtractInnerText(const GURL& url) { 169 std::string NavigateAndExtractInnerText(const GURL& url) {
162 return ExtractInnerText(Navigate(url)); 170 return ExtractInnerText(Navigate(url));
163 } 171 }
164 172
173 size_t last_worker_ref_count() const { return last_worker_ref_count_; }
174
165 private: 175 private:
166 // Sets the channel to "stable". 176 // Sets the channel to "stable".
167 // Not useful after we've opened extension Service Workers to stable 177 // Not useful after we've opened extension Service Workers to stable
168 // channel. 178 // channel.
169 // TODO(lazyboy): Remove this when ExtensionServiceWorkersEnabled() is 179 // TODO(lazyboy): Remove this when ExtensionServiceWorkersEnabled() is
170 // removed. 180 // removed.
171 ScopedCurrentChannel current_channel_; 181 ScopedCurrentChannel current_channel_;
172 182
183 size_t last_worker_ref_count_ = 0u;
184
173 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); 185 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest);
174 }; 186 };
175 187
176 class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest { 188 class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest {
177 public: 189 public:
178 ServiceWorkerBackgroundSyncTest() {} 190 ServiceWorkerBackgroundSyncTest() {}
179 ~ServiceWorkerBackgroundSyncTest() override {} 191 ~ServiceWorkerBackgroundSyncTest() override {}
180 192
181 void SetUpCommandLine(base::CommandLine* command_line) override { 193 void SetUpCommandLine(base::CommandLine* command_line) override {
182 // ServiceWorkerRegistration.sync requires experimental flag. 194 // ServiceWorkerRegistration.sync requires experimental flag.
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 635 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
624 web_contents, "window.runServiceWorker()", &result)); 636 web_contents, "window.runServiceWorker()", &result));
625 ASSERT_EQ("chrome.tabs.create callback", result); 637 ASSERT_EQ("chrome.tabs.create callback", result);
626 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count()); 638 EXPECT_EQ(starting_tab_count + 1, browser()->tab_strip_model()->count());
627 639
628 // Check extension shutdown path. 640 // Check extension shutdown path.
629 UnloadExtension(extension->id()); 641 UnloadExtension(extension->id());
630 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count()); 642 EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count());
631 } 643 }
632 644
645 // Tests that worker ref count increments while extension API function is
646 // active.
647 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WorkerRefCount) {
648 // Extensions APIs from SW are only enabled on trunk.
649 ScopedCurrentChannel current_channel_override(version_info::Channel::UNKNOWN);
650 const Extension* extension = LoadExtensionWithFlags(
651 test_data_dir_.AppendASCII("service_worker/api_worker_ref_count"),
652 kFlagNone);
653 ASSERT_TRUE(extension);
654 ui_test_utils::NavigateToURL(browser(),
655 extension->GetResourceURL("page.html"));
656 content::WebContents* web_contents =
657 browser()->tab_strip_model()->GetActiveWebContents();
658
Devlin 2016/10/07 15:13:22 Check ref count is 0?
lazyboy 2016/10/07 18:50:10 Done.
659 ExtensionTestMessageListener worker_listener("CHECK_REF_COUNT", true);
660 worker_listener.set_failure_message("FAILURE");
661 ASSERT_TRUE(
662 content::ExecuteScript(web_contents, "window.runServiceWorker()"));
663 ASSERT_TRUE(worker_listener.WaitUntilSatisfied());
664
665 {
666 content::ServiceWorkerContext* sw_context =
667 content::BrowserContext::GetDefaultStoragePartition(
668 browser()->profile())
669 ->GetServiceWorkerContext();
670 base::RunLoop run_loop;
671 sw_context->GetPendingExternalRequestCountForTest(
672 extension->url(),
673 base::Bind(&ServiceWorkerTest::DidGetPendingExternalRequestCount,
674 // Unretained is safe because we wait until the bound method
675 // is called.
676 base::Unretained(this), run_loop.QuitClosure()));
677 run_loop.Run();
678 }
679
680 // Service worker should have exactly one pending request because
681 // chrome.test.sendMessage() API call is in-flight.
682 EXPECT_EQ(1u, last_worker_ref_count());
683
684 ExtensionTestMessageListener extension_listener("SUCCESS", false);
685 extension_listener.set_failure_message("FAILURE");
686 // Finish executing chrome.test.sendMessage().
687 worker_listener.Reply("Hello world");
688 ASSERT_TRUE(extension_listener.WaitUntilSatisfied());
Devlin 2016/10/07 15:13:21 Check that ref count drops to 0? I also wonder if
lazyboy 2016/10/07 18:50:10 Done.
689 }
690
633 // This test loads a web page that has an iframe pointing to a 691 // 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 692 // chrome-extension:// URL. The URL is listed in the extension's
635 // web_accessible_resources. Initially the iframe is served from the extension's 693 // web_accessible_resources. Initially the iframe is served from the extension's
636 // resource file. After verifying that, we register a Service Worker that 694 // resource file. After verifying that, we register a Service Worker that
637 // controls the extension. Further requests to the same resource as before 695 // controls the extension. Further requests to the same resource as before
638 // should now be served by the Service Worker. 696 // should now be served by the Service Worker.
639 // This test also verifies that if the requested resource exists in the manifest 697 // 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 698 // but is not present in the extension directory, the Service Worker can still
641 // serve the resource file. 699 // serve the resource file.
642 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) { 700 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 message.sender_id = "1234567890"; 841 message.sender_id = "1234567890";
784 message.raw_data = "testdata"; 842 message.raw_data = "testdata";
785 message.decrypted = true; 843 message.decrypted = true;
786 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 844 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
787 push_service()->OnMessage(app_identifier.app_id(), message); 845 push_service()->OnMessage(app_identifier.app_id(), message);
788 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); 846 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
789 run_loop.Run(); // Wait until the message is handled by push service. 847 run_loop.Run(); // Wait until the message is handled by push service.
790 } 848 }
791 849
792 } // namespace extensions 850 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698