| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/optional.h" | 18 #include "base/optional.h" |
| 19 #include "base/time/time.h" | |
| 20 #include "content/common/service_worker/embedded_worker.mojom.h" | 19 #include "content/common/service_worker/embedded_worker.mojom.h" |
| 21 #include "content/common/service_worker/service_worker_status_code.h" | |
| 22 #include "ipc/ipc_listener.h" | 20 #include "ipc/ipc_listener.h" |
| 23 #include "ipc/ipc_test_sink.h" | 21 #include "ipc/ipc_test_sink.h" |
| 24 #include "mojo/public/cpp/bindings/binding.h" | 22 #include "mojo/public/cpp/bindings/binding.h" |
| 25 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 23 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 28 | 26 |
| 29 class GURL; | 27 class GURL; |
| 30 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; | 28 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; |
| 31 | 29 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 58 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 56 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
| 59 // | 57 // |
| 60 // Alternatively consumers can subclass this helper and override On*() | 58 // Alternatively consumers can subclass this helper and override On*() |
| 61 // methods to add their own logic/verification code. | 59 // methods to add their own logic/verification code. |
| 62 // | 60 // |
| 63 // See embedded_worker_instance_unittest.cc for example usages. | 61 // See embedded_worker_instance_unittest.cc for example usages. |
| 64 // | 62 // |
| 65 class EmbeddedWorkerTestHelper : public IPC::Sender, | 63 class EmbeddedWorkerTestHelper : public IPC::Sender, |
| 66 public IPC::Listener { | 64 public IPC::Listener { |
| 67 public: | 65 public: |
| 68 using FetchCallback = | |
| 69 base::Callback<void(ServiceWorkerStatusCode, | |
| 70 base::Time /* dispatch_event_time */)>; | |
| 71 | |
| 72 class MockEmbeddedWorkerInstanceClient | 66 class MockEmbeddedWorkerInstanceClient |
| 73 : public mojom::EmbeddedWorkerInstanceClient { | 67 : public mojom::EmbeddedWorkerInstanceClient { |
| 74 public: | 68 public: |
| 75 explicit MockEmbeddedWorkerInstanceClient( | 69 explicit MockEmbeddedWorkerInstanceClient( |
| 76 base::WeakPtr<EmbeddedWorkerTestHelper> helper); | 70 base::WeakPtr<EmbeddedWorkerTestHelper> helper); |
| 77 ~MockEmbeddedWorkerInstanceClient() override; | 71 ~MockEmbeddedWorkerInstanceClient() override; |
| 78 | 72 |
| 79 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, | 73 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, |
| 80 mojom::EmbeddedWorkerInstanceClientRequest request); | 74 mojom::EmbeddedWorkerInstanceClientRequest request); |
| 81 | 75 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 const GURL& script_url, | 143 const GURL& script_url, |
| 150 bool pause_after_download); | 144 bool pause_after_download); |
| 151 virtual void OnResumeAfterDownload(int embedded_worker_id); | 145 virtual void OnResumeAfterDownload(int embedded_worker_id); |
| 152 virtual void OnStopWorker(int embedded_worker_id); | 146 virtual void OnStopWorker(int embedded_worker_id); |
| 153 virtual bool OnMessageToWorker(int thread_id, | 147 virtual bool OnMessageToWorker(int thread_id, |
| 154 int embedded_worker_id, | 148 int embedded_worker_id, |
| 155 const IPC::Message& message); | 149 const IPC::Message& message); |
| 156 | 150 |
| 157 // Called to setup mojo for a new embedded worker. Override to register | 151 // Called to setup mojo for a new embedded worker. Override to register |
| 158 // interfaces the worker should expose to the browser. | 152 // interfaces the worker should expose to the browser. |
| 159 virtual void OnSetupMojo(int thread_id, | 153 virtual void OnSetupMojo(shell::InterfaceRegistry* interface_registry); |
| 160 shell::InterfaceRegistry* interface_registry); | |
| 161 | 154 |
| 162 // On*Event handlers. Called by the default implementation of | 155 // On*Event handlers. Called by the default implementation of |
| 163 // OnMessageToWorker when events are sent to the embedded | 156 // OnMessageToWorker when events are sent to the embedded |
| 164 // worker. By default they just return success via | 157 // worker. By default they just return success via |
| 165 // SimulateSendReplyToBrowser. | 158 // SimulateSendReplyToBrowser. |
| 166 virtual void OnActivateEvent(int embedded_worker_id, int request_id); | 159 virtual void OnActivateEvent(int embedded_worker_id, int request_id); |
| 167 virtual void OnExtendableMessageEvent(int embedded_worker_id, int request_id); | 160 virtual void OnExtendableMessageEvent(int embedded_worker_id, int request_id); |
| 168 virtual void OnInstallEvent(int embedded_worker_id, int request_id); | 161 virtual void OnInstallEvent(int embedded_worker_id, int request_id); |
| 169 virtual void OnFetchEvent(int embedded_worker_id, | 162 virtual void OnFetchEvent(int embedded_worker_id, |
| 170 int response_id, | 163 int response_id, |
| 171 const ServiceWorkerFetchRequest& request, | 164 int event_finish_id, |
| 172 const FetchCallback& callback); | 165 const ServiceWorkerFetchRequest& request); |
| 173 virtual void OnPushEvent(int embedded_worker_id, | 166 virtual void OnPushEvent(int embedded_worker_id, |
| 174 int request_id, | 167 int request_id, |
| 175 const PushEventPayload& payload); | 168 const PushEventPayload& payload); |
| 176 | 169 |
| 177 // These functions simulate sending an EmbeddedHostMsg message to the | 170 // These functions simulate sending an EmbeddedHostMsg message to the |
| 178 // browser. | 171 // browser. |
| 179 void SimulateWorkerReadyForInspection(int embedded_worker_id); | 172 void SimulateWorkerReadyForInspection(int embedded_worker_id); |
| 180 void SimulateWorkerScriptCached(int embedded_worker_id); | 173 void SimulateWorkerScriptCached(int embedded_worker_id); |
| 181 void SimulateWorkerScriptLoaded(int embedded_worker_id); | 174 void SimulateWorkerScriptLoaded(int embedded_worker_id); |
| 182 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); | 175 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); |
| 183 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); | 176 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); |
| 184 void SimulateWorkerStarted(int embedded_worker_id); | 177 void SimulateWorkerStarted(int embedded_worker_id); |
| 185 void SimulateWorkerStopped(int embedded_worker_id); | 178 void SimulateWorkerStopped(int embedded_worker_id); |
| 186 void SimulateSend(IPC::Message* message); | 179 void SimulateSend(IPC::Message* message); |
| 187 | 180 |
| 188 EmbeddedWorkerRegistry* registry(); | 181 EmbeddedWorkerRegistry* registry(); |
| 189 | 182 |
| 190 private: | 183 private: |
| 191 using InterfaceRegistryAndProvider = | 184 using InterfaceRegistryAndProvider = |
| 192 std::pair<std::unique_ptr<shell::InterfaceRegistry>, | 185 std::pair<std::unique_ptr<shell::InterfaceRegistry>, |
| 193 std::unique_ptr<shell::InterfaceProvider>>; | 186 std::unique_ptr<shell::InterfaceProvider>>; |
| 194 | 187 |
| 195 class MockEmbeddedWorkerSetup; | 188 class MockEmbeddedWorkerSetup; |
| 196 class MockFetchEventDispatcher; | |
| 197 | 189 |
| 198 void OnStartWorkerStub(const EmbeddedWorkerStartParams& params); | 190 void OnStartWorkerStub(const EmbeddedWorkerStartParams& params); |
| 199 void OnResumeAfterDownloadStub(int embedded_worker_id); | 191 void OnResumeAfterDownloadStub(int embedded_worker_id); |
| 200 void OnStopWorkerStub(int embedded_worker_id); | 192 void OnStopWorkerStub(int embedded_worker_id); |
| 201 void OnMessageToWorkerStub(int thread_id, | 193 void OnMessageToWorkerStub(int thread_id, |
| 202 int embedded_worker_id, | 194 int embedded_worker_id, |
| 203 const IPC::Message& message); | 195 const IPC::Message& message); |
| 204 void OnActivateEventStub(int request_id); | 196 void OnActivateEventStub(int request_id); |
| 205 void OnExtendableMessageEventStub( | 197 void OnExtendableMessageEventStub( |
| 206 int request_id, | 198 int request_id, |
| 207 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); | 199 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); |
| 208 void OnInstallEventStub(int request_id); | 200 void OnInstallEventStub(int request_id); |
| 209 void OnFetchEventStub(int thread_id, | 201 void OnFetchEventStub(int response_id, |
| 210 int response_id, | 202 int event_finish_id, |
| 211 const ServiceWorkerFetchRequest& request, | 203 const ServiceWorkerFetchRequest& request); |
| 212 const FetchCallback& callback); | |
| 213 void OnPushEventStub(int request_id, const PushEventPayload& payload); | 204 void OnPushEventStub(int request_id, const PushEventPayload& payload); |
| 214 void OnSetupMojoStub(int thread_id, | 205 void OnSetupMojoStub(int thread_id, |
| 215 shell::mojom::InterfaceProviderRequest services, | 206 shell::mojom::InterfaceProviderRequest services, |
| 216 shell::mojom::InterfaceProviderPtr exposed_services); | 207 shell::mojom::InterfaceProviderPtr exposed_services); |
| 217 | 208 |
| 218 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 209 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
| 219 | 210 |
| 220 std::unique_ptr<shell::InterfaceRegistry> CreateInterfaceRegistry( | 211 std::unique_ptr<shell::InterfaceRegistry> CreateInterfaceRegistry( |
| 221 MockRenderProcessHost* rph); | 212 MockRenderProcessHost* rph); |
| 222 | 213 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 235 | 226 |
| 236 int next_thread_id_; | 227 int next_thread_id_; |
| 237 int mock_render_process_id_; | 228 int mock_render_process_id_; |
| 238 int new_mock_render_process_id_; | 229 int new_mock_render_process_id_; |
| 239 | 230 |
| 240 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; | 231 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; |
| 241 std::unique_ptr<shell::InterfaceRegistry> | 232 std::unique_ptr<shell::InterfaceRegistry> |
| 242 new_render_process_interface_registry_; | 233 new_render_process_interface_registry_; |
| 243 | 234 |
| 244 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 235 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
| 245 std::map<int /* thread_id */, int /* embedded_worker_id */> | |
| 246 thread_id_embedded_worker_id_map_; | |
| 247 | 236 |
| 248 // Stores the InterfaceRegistry/InterfaceProviders that are associated with | 237 // Stores the InterfaceRegistry/InterfaceProviders that are associated with |
| 249 // each individual service worker. | 238 // each individual service worker. |
| 250 base::hash_map<int, InterfaceRegistryAndProvider> | 239 base::hash_map<int, InterfaceRegistryAndProvider> |
| 251 thread_id_service_registry_map_; | 240 thread_id_service_registry_map_; |
| 252 | 241 |
| 253 // Updated each time MessageToWorker message is received. | 242 // Updated each time MessageToWorker message is received. |
| 254 int current_embedded_worker_id_; | 243 int current_embedded_worker_id_; |
| 255 | 244 |
| 256 std::vector<scoped_refptr<MessagePortMessageFilter>> | 245 std::vector<scoped_refptr<MessagePortMessageFilter>> |
| 257 message_port_message_filters_; | 246 message_port_message_filters_; |
| 258 | 247 |
| 259 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 248 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
| 260 | 249 |
| 261 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 250 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
| 262 }; | 251 }; |
| 263 | 252 |
| 264 } // namespace content | 253 } // namespace content |
| 265 | 254 |
| 266 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 255 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
| OLD | NEW |