OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/service_worker/embedded_worker_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 void SaveStatusAndCall(ServiceWorkerStatusCode* out, | 38 void SaveStatusAndCall(ServiceWorkerStatusCode* out, |
39 const base::Closure& callback, | 39 const base::Closure& callback, |
40 ServiceWorkerStatusCode status) { | 40 ServiceWorkerStatusCode status) { |
41 *out = status; | 41 *out = status; |
42 callback.Run(); | 42 callback.Run(); |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
| 47 class MockServiceWorkerProviderHost : public ServiceWorkerProviderHost { |
| 48 public: |
| 49 MockServiceWorkerProviderHost(base::WeakPtr<ServiceWorkerContextCore> context) |
| 50 : ServiceWorkerProviderHost(ChildProcessHost::kInvalidUniqueID, |
| 51 ServiceWorkerProviderHostInfo( |
| 52 kInvalidServiceWorkerProviderId, |
| 53 MSG_ROUTING_NONE, |
| 54 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, |
| 55 false), |
| 56 context, |
| 57 nullptr), |
| 58 binding_(this) {} |
| 59 |
| 60 ~MockServiceWorkerProviderHost() override {} |
| 61 |
| 62 void CompleteStartWorkerPreparation(int process_id, |
| 63 mojom::ServiceWorkerProviderClientInfoPtr* |
| 64 provider_client_info) override { |
| 65 // Nothing to do. |
| 66 DCHECK(!binding_.is_bound()); |
| 67 DCHECK(!client_.is_bound()); |
| 68 binding_.Bind(mojo::MakeRequest(&(*provider_client_info)->host_ptr_info)); |
| 69 (*provider_client_info)->client_request = mojo::MakeRequest(&client_); |
| 70 } |
| 71 |
| 72 private: |
| 73 mojom::ServiceWorkerProviderAssociatedPtr client_; |
| 74 mojo::AssociatedBinding<mojom::ServiceWorkerProviderHost> binding_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(MockServiceWorkerProviderHost); |
| 77 }; |
| 78 |
47 class EmbeddedWorkerInstanceTest : public testing::Test, | 79 class EmbeddedWorkerInstanceTest : public testing::Test, |
48 public EmbeddedWorkerInstance::Listener { | 80 public EmbeddedWorkerInstance::Listener { |
49 protected: | 81 protected: |
50 EmbeddedWorkerInstanceTest() | 82 EmbeddedWorkerInstanceTest() |
51 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 83 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
52 | 84 |
53 enum EventType { | 85 enum EventType { |
54 PROCESS_ALLOCATED, | 86 PROCESS_ALLOCATED, |
55 START_WORKER_MESSAGE_SENT, | 87 START_WORKER_MESSAGE_SENT, |
56 STARTED, | 88 STARTED, |
(...skipping 18 matching lines...) Expand all Loading... |
75 RecordEvent(START_WORKER_MESSAGE_SENT); | 107 RecordEvent(START_WORKER_MESSAGE_SENT); |
76 } | 108 } |
77 void OnStarted() override { RecordEvent(STARTED); } | 109 void OnStarted() override { RecordEvent(STARTED); } |
78 void OnStopped(EmbeddedWorkerStatus old_status) override { | 110 void OnStopped(EmbeddedWorkerStatus old_status) override { |
79 RecordEvent(STOPPED, old_status); | 111 RecordEvent(STOPPED, old_status); |
80 } | 112 } |
81 void OnDetached(EmbeddedWorkerStatus old_status) override { | 113 void OnDetached(EmbeddedWorkerStatus old_status) override { |
82 RecordEvent(DETACHED, old_status); | 114 RecordEvent(DETACHED, old_status); |
83 } | 115 } |
84 | 116 |
85 bool OnMessageReceived(const IPC::Message& message) override { return false; } | 117 bool OnMessageReceived(const IPC::Message&) override { return false; } |
86 | 118 |
87 void SetUp() override { | 119 void SetUp() override { |
88 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 120 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
89 } | 121 } |
90 | 122 |
91 void TearDown() override { helper_.reset(); } | 123 void TearDown() override { helper_.reset(); } |
92 | 124 |
93 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, | 125 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, |
94 int id, const GURL& pattern, | 126 int id, const GURL& pattern, |
95 const GURL& url) { | 127 const GURL& url) { |
(...skipping 10 matching lines...) Expand all Loading... |
106 | 138 |
107 std::unique_ptr<EmbeddedWorkerStartParams> | 139 std::unique_ptr<EmbeddedWorkerStartParams> |
108 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { | 140 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { |
109 std::unique_ptr<EmbeddedWorkerStartParams> params = | 141 std::unique_ptr<EmbeddedWorkerStartParams> params = |
110 base::MakeUnique<EmbeddedWorkerStartParams>(); | 142 base::MakeUnique<EmbeddedWorkerStartParams>(); |
111 params->service_worker_version_id = version_id; | 143 params->service_worker_version_id = version_id; |
112 params->scope = scope; | 144 params->scope = scope; |
113 params->script_url = script_url; | 145 params->script_url = script_url; |
114 params->pause_after_download = false; | 146 params->pause_after_download = false; |
115 params->is_installed = false; | 147 params->is_installed = false; |
| 148 mock_providers_.emplace_back( |
| 149 base::MakeUnique<MockServiceWorkerProviderHost>( |
| 150 helper_->context()->AsWeakPtr())); |
| 151 params->controller_provider = mock_providers_.back().get(); |
116 return params; | 152 return params; |
117 } | 153 } |
118 | 154 |
119 mojom::ServiceWorkerEventDispatcherRequest CreateEventDispatcher() { | 155 mojom::ServiceWorkerEventDispatcherRequest CreateEventDispatcher() { |
120 dispatchers_.emplace_back(); | 156 dispatchers_.emplace_back(); |
121 return mojo::MakeRequest(&dispatchers_.back()); | 157 return mojo::MakeRequest(&dispatchers_.back()); |
122 } | 158 } |
123 | 159 |
124 ServiceWorkerContextCore* context() { return helper_->context(); } | 160 ServiceWorkerContextCore* context() { return helper_->context(); } |
125 | 161 |
126 EmbeddedWorkerRegistry* embedded_worker_registry() { | 162 EmbeddedWorkerRegistry* embedded_worker_registry() { |
127 DCHECK(context()); | 163 DCHECK(context()); |
128 return context()->embedded_worker_registry(); | 164 return context()->embedded_worker_registry(); |
129 } | 165 } |
130 | 166 |
131 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 167 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
132 | 168 |
133 std::vector<std::unique_ptr< | 169 std::vector<std::unique_ptr< |
134 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient>>* | 170 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient>>* |
135 mock_instance_clients() { | 171 mock_instance_clients() { |
136 return helper_->mock_instance_clients(); | 172 return helper_->mock_instance_clients(); |
137 } | 173 } |
138 | 174 |
139 std::vector<mojom::ServiceWorkerEventDispatcherPtr> dispatchers_; | 175 std::vector<mojom::ServiceWorkerEventDispatcherPtr> dispatchers_; |
| 176 std::vector<std::unique_ptr<MockServiceWorkerProviderHost>> mock_providers_; |
140 | 177 |
141 TestBrowserThreadBundle thread_bundle_; | 178 TestBrowserThreadBundle thread_bundle_; |
142 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 179 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
143 std::vector<EventLog> events_; | 180 std::vector<EventLog> events_; |
144 | 181 |
145 private: | 182 private: |
146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 183 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
147 }; | 184 }; |
148 | 185 |
149 // A helper to simulate the start worker sequence is stalled in a worker | 186 // A helper to simulate the start worker sequence is stalled in a worker |
150 // process. | 187 // process. |
151 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { | 188 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { |
152 public: | 189 public: |
153 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 190 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
154 ~StalledInStartWorkerHelper() override {} | 191 ~StalledInStartWorkerHelper() override {} |
155 | 192 |
156 void OnStartWorker(int embedded_worker_id, | 193 void OnStartWorker( |
157 int64_t service_worker_version_id, | 194 int embedded_worker_id, |
158 const GURL& scope, | 195 int64_t service_worker_version_id, |
159 const GURL& script_url, | 196 const GURL& scope, |
160 bool pause_after_download, | 197 const GURL& script_url, |
161 mojom::ServiceWorkerEventDispatcherRequest request, | 198 bool pause_after_download, |
162 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo | 199 mojom::ServiceWorkerEventDispatcherRequest request, |
163 instance_host) override { | 200 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, |
| 201 mojom::ServiceWorkerProviderClientInfoPtr provider_client_info) override { |
164 if (force_stall_in_start_) { | 202 if (force_stall_in_start_) { |
165 // Prepare for OnStopWorker(). | 203 // Prepare for OnStopWorker(). |
166 instance_host_ptr_map_[embedded_worker_id].Bind(std::move(instance_host)); | 204 instance_host_ptr_map_[embedded_worker_id].Bind(std::move(instance_host)); |
167 // Do nothing to simulate a stall in the worker process. | 205 // Do nothing to simulate a stall in the worker process. |
168 return; | 206 return; |
169 } | 207 } |
170 EmbeddedWorkerTestHelper::OnStartWorker( | 208 EmbeddedWorkerTestHelper::OnStartWorker( |
171 embedded_worker_id, service_worker_version_id, scope, script_url, | 209 embedded_worker_id, service_worker_version_id, scope, script_url, |
172 pause_after_download, std::move(request), std::move(instance_host)); | 210 pause_after_download, std::move(request), std::move(instance_host), |
| 211 std::move(provider_client_info)); |
173 } | 212 } |
174 | 213 |
175 void OnStopWorker(int embedded_worker_id) override { | 214 void OnStopWorker(int embedded_worker_id) override { |
176 if (instance_host_ptr_map_[embedded_worker_id]) { | 215 if (instance_host_ptr_map_[embedded_worker_id]) { |
177 instance_host_ptr_map_[embedded_worker_id]->OnStopped(); | 216 instance_host_ptr_map_[embedded_worker_id]->OnStopped(); |
178 base::RunLoop().RunUntilIdle(); | 217 base::RunLoop().RunUntilIdle(); |
179 return; | 218 return; |
180 } | 219 } |
181 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); | 220 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); |
182 } | 221 } |
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 } | 755 } |
717 | 756 |
718 class FailEmbeddedWorkerInstanceClientImpl | 757 class FailEmbeddedWorkerInstanceClientImpl |
719 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient { | 758 : public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient { |
720 public: | 759 public: |
721 explicit FailEmbeddedWorkerInstanceClientImpl( | 760 explicit FailEmbeddedWorkerInstanceClientImpl( |
722 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 761 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
723 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} | 762 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} |
724 | 763 |
725 private: | 764 private: |
726 void StartWorker( | 765 void StartWorker(const EmbeddedWorkerStartParams&, |
727 const EmbeddedWorkerStartParams& /* unused */, | 766 mojom::ServiceWorkerEventDispatcherRequest, |
728 mojom::ServiceWorkerEventDispatcherRequest /* unused */, | 767 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo, |
729 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo /* unused */) | 768 mojom::ServiceWorkerProviderClientInfoPtr) override { |
730 override { | |
731 helper_->mock_instance_clients()->clear(); | 769 helper_->mock_instance_clients()->clear(); |
732 } | 770 } |
733 }; | 771 }; |
734 | 772 |
735 TEST_F(EmbeddedWorkerInstanceTest, RemoveRemoteInterface) { | 773 TEST_F(EmbeddedWorkerInstanceTest, RemoveRemoteInterface) { |
736 const int64_t version_id = 55L; | 774 const int64_t version_id = 55L; |
737 const GURL pattern("http://example.com/"); | 775 const GURL pattern("http://example.com/"); |
738 const GURL url("http://example.com/worker.js"); | 776 const GURL url("http://example.com/worker.js"); |
739 | 777 |
740 // Let StartWorker fail; binding is discarded in the middle of IPC | 778 // Let StartWorker fail; binding is discarded in the middle of IPC |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 ASSERT_EQ(1UL, instance_client_rawptr->message().size()); | 867 ASSERT_EQ(1UL, instance_client_rawptr->message().size()); |
830 EXPECT_EQ(test_message, instance_client_rawptr->message()[0]); | 868 EXPECT_EQ(test_message, instance_client_rawptr->message()[0]); |
831 | 869 |
832 // Ensure the worker is stopped. | 870 // Ensure the worker is stopped. |
833 worker->Stop(); | 871 worker->Stop(); |
834 base::RunLoop().RunUntilIdle(); | 872 base::RunLoop().RunUntilIdle(); |
835 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 873 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
836 } | 874 } |
837 | 875 |
838 } // namespace content | 876 } // namespace content |
OLD | NEW |