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

Side by Side Diff: content/browser/service_worker/embedded_worker_test_helper.h

Issue 2381513002: Revert of ServiceWorker: Implement StartWorker by using mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 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"
19 #include "content/common/service_worker/embedded_worker.mojom.h"
20 #include "ipc/ipc_listener.h" 18 #include "ipc/ipc_listener.h"
21 #include "ipc/ipc_test_sink.h" 19 #include "ipc/ipc_test_sink.h"
22 #include "mojo/public/cpp/bindings/binding.h"
23 #include "services/shell/public/interfaces/interface_provider.mojom.h" 20 #include "services/shell/public/interfaces/interface_provider.mojom.h"
24 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
25 #include "url/gurl.h" 22 #include "url/gurl.h"
26 23
27 class GURL; 24 class GURL;
25 struct EmbeddedWorkerMsg_StartWorker_Params;
28 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; 26 struct ServiceWorkerMsg_ExtendableMessageEvent_Params;
29 27
30 namespace shell { 28 namespace shell {
31 class InterfaceProvider; 29 class InterfaceProvider;
32 class InterfaceRegistry; 30 class InterfaceRegistry;
33 } 31 }
34 32
35 namespace content { 33 namespace content {
36 34
37 class EmbeddedWorkerRegistry; 35 class EmbeddedWorkerRegistry;
38 class EmbeddedWorkerTestHelper; 36 class EmbeddedWorkerTestHelper;
39 class MessagePortMessageFilter; 37 class MessagePortMessageFilter;
40 class MockRenderProcessHost; 38 class MockRenderProcessHost;
41 class ServiceWorkerContextCore; 39 class ServiceWorkerContextCore;
42 class ServiceWorkerContextWrapper; 40 class ServiceWorkerContextWrapper;
43 class TestBrowserContext; 41 class TestBrowserContext;
44 struct EmbeddedWorkerStartParams;
45 struct PushEventPayload; 42 struct PushEventPayload;
46 struct ServiceWorkerFetchRequest; 43 struct ServiceWorkerFetchRequest;
47 44
48 // In-Process EmbeddedWorker test helper. 45 // In-Process EmbeddedWorker test helper.
49 // 46 //
50 // Usage: create an instance of this class to test browser-side embedded worker 47 // Usage: create an instance of this class to test browser-side embedded worker
51 // code without creating a child process. This class will create a 48 // code without creating a child process. This class will create a
52 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you. 49 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you.
53 // 50 //
54 // By default this class just notifies back WorkerStarted and WorkerStopped 51 // By default this class just notifies back WorkerStarted and WorkerStopped
55 // for StartWorker and StopWorker requests. The default implementation 52 // for StartWorker and StopWorker requests. The default implementation
56 // also returns success for event messages (e.g. InstallEvent, FetchEvent). 53 // also returns success for event messages (e.g. InstallEvent, FetchEvent).
57 // 54 //
58 // Alternatively consumers can subclass this helper and override On*() 55 // Alternatively consumers can subclass this helper and override On*()
59 // methods to add their own logic/verification code. 56 // methods to add their own logic/verification code.
60 // 57 //
61 // See embedded_worker_instance_unittest.cc for example usages. 58 // See embedded_worker_instance_unittest.cc for example usages.
62 // 59 //
63 class EmbeddedWorkerTestHelper : public IPC::Sender, 60 class EmbeddedWorkerTestHelper : public IPC::Sender,
64 public IPC::Listener { 61 public IPC::Listener {
65 public: 62 public:
66 class MockEmbeddedWorkerInstanceClient
67 : public mojom::EmbeddedWorkerInstanceClient {
68 public:
69 explicit MockEmbeddedWorkerInstanceClient(
70 base::WeakPtr<EmbeddedWorkerTestHelper> helper);
71 ~MockEmbeddedWorkerInstanceClient() override;
72
73 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper,
74 mojom::EmbeddedWorkerInstanceClientRequest request);
75
76 private:
77 // Implementation of mojo interfaces.
78 void StartWorker(const EmbeddedWorkerStartParams& params) override;
79
80 base::WeakPtr<EmbeddedWorkerTestHelper> helper_;
81 mojo::Binding<mojom::EmbeddedWorkerInstanceClient> binding_;
82
83 base::Optional<int> embedded_worker_id_;
84
85 DISALLOW_COPY_AND_ASSIGN(MockEmbeddedWorkerInstanceClient);
86 };
87
88 // If |user_data_directory| is empty, the context makes storage stuff in 63 // If |user_data_directory| is empty, the context makes storage stuff in
89 // memory. 64 // memory.
90 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); 65 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory);
91 ~EmbeddedWorkerTestHelper() override; 66 ~EmbeddedWorkerTestHelper() override;
92 67
93 // Call this to simulate add/associate a process to a pattern. 68 // Call this to simulate add/associate a process to a pattern.
94 // This also registers this sender for the process. 69 // This also registers this sender for the process.
95 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); 70 void SimulateAddProcessToPattern(const GURL& pattern, int process_id);
96 71
97 // IPC::Sender implementation. 72 // IPC::Sender implementation.
98 bool Send(IPC::Message* message) override; 73 bool Send(IPC::Message* message) override;
99 74
100 // IPC::Listener implementation. 75 // IPC::Listener implementation.
101 bool OnMessageReceived(const IPC::Message& msg) override; 76 bool OnMessageReceived(const IPC::Message& msg) override;
102 77
103 // IPC sink for EmbeddedWorker messages. 78 // IPC sink for EmbeddedWorker messages.
104 IPC::TestSink* ipc_sink() { return &sink_; } 79 IPC::TestSink* ipc_sink() { return &sink_; }
105 // Inner IPC sink for script context messages sent via EmbeddedWorker. 80 // Inner IPC sink for script context messages sent via EmbeddedWorker.
106 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } 81 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; }
107 82
108 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>>*
109 mock_instance_clients() {
110 return &mock_instance_clients_;
111 }
112
113 ServiceWorkerContextCore* context(); 83 ServiceWorkerContextCore* context();
114 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } 84 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); }
115 void ShutdownContext(); 85 void ShutdownContext();
116 86
117 int GetNextThreadId() { return next_thread_id_++; } 87 int GetNextThreadId() { return next_thread_id_++; }
118 88
119 int mock_render_process_id() const { return mock_render_process_id_; } 89 int mock_render_process_id() const { return mock_render_process_id_; }
120 MockRenderProcessHost* mock_render_process_host() { 90 MockRenderProcessHost* mock_render_process_host() {
121 return render_process_host_.get(); 91 return render_process_host_.get();
122 } 92 }
123 93
124 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map() { 94 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map() {
125 return embedded_worker_id_service_worker_version_id_map_; 95 return embedded_worker_id_service_worker_version_id_map_;
126 } 96 }
127 97
128 // Only used for tests that force creating a new render process. 98 // Only used for tests that force creating a new render process. There is no
129 int new_render_process_id() const { return new_mock_render_process_id_; } 99 // corresponding MockRenderProcessHost.
100 int new_render_process_id() const { return mock_render_process_id_ + 1; }
130 101
131 TestBrowserContext* browser_context() { return browser_context_.get(); } 102 TestBrowserContext* browser_context() { return browser_context_.get(); }
132 103
133 protected: 104 protected:
134 // Called when StartWorker, StopWorker and SendMessageToWorker message 105 // Called when StartWorker, StopWorker and SendMessageToWorker message
135 // is sent to the embedded worker. Override if necessary. By default 106 // is sent to the embedded worker. Override if necessary. By default
136 // they verify given parameters and: 107 // they verify given parameters and:
137 // - OnStartWorker calls SimulateWorkerStarted 108 // - OnStartWorker calls SimulateWorkerStarted
138 // - OnStopWorker calls SimulateWorkerStoped 109 // - OnStopWorker calls SimulateWorkerStoped
139 // - OnSendMessageToWorker calls the message's respective On*Event handler 110 // - OnSendMessageToWorker calls the message's respective On*Event handler
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 151
181 EmbeddedWorkerRegistry* registry(); 152 EmbeddedWorkerRegistry* registry();
182 153
183 private: 154 private:
184 using InterfaceRegistryAndProvider = 155 using InterfaceRegistryAndProvider =
185 std::pair<std::unique_ptr<shell::InterfaceRegistry>, 156 std::pair<std::unique_ptr<shell::InterfaceRegistry>,
186 std::unique_ptr<shell::InterfaceProvider>>; 157 std::unique_ptr<shell::InterfaceProvider>>;
187 158
188 class MockEmbeddedWorkerSetup; 159 class MockEmbeddedWorkerSetup;
189 160
190 void OnStartWorkerStub(const EmbeddedWorkerStartParams& params); 161 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params);
191 void OnResumeAfterDownloadStub(int embedded_worker_id); 162 void OnResumeAfterDownloadStub(int embedded_worker_id);
192 void OnStopWorkerStub(int embedded_worker_id); 163 void OnStopWorkerStub(int embedded_worker_id);
193 void OnMessageToWorkerStub(int thread_id, 164 void OnMessageToWorkerStub(int thread_id,
194 int embedded_worker_id, 165 int embedded_worker_id,
195 const IPC::Message& message); 166 const IPC::Message& message);
196 void OnActivateEventStub(int request_id); 167 void OnActivateEventStub(int request_id);
197 void OnExtendableMessageEventStub( 168 void OnExtendableMessageEventStub(
198 int request_id, 169 int request_id,
199 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); 170 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params);
200 void OnInstallEventStub(int request_id); 171 void OnInstallEventStub(int request_id);
201 void OnFetchEventStub(int response_id, 172 void OnFetchEventStub(int response_id,
202 int event_finish_id, 173 int event_finish_id,
203 const ServiceWorkerFetchRequest& request); 174 const ServiceWorkerFetchRequest& request);
204 void OnPushEventStub(int request_id, const PushEventPayload& payload); 175 void OnPushEventStub(int request_id, const PushEventPayload& payload);
205 void OnSetupMojoStub(int thread_id, 176 void OnSetupMojoStub(int thread_id,
206 shell::mojom::InterfaceProviderRequest services, 177 shell::mojom::InterfaceProviderRequest services,
207 shell::mojom::InterfaceProviderPtr exposed_services); 178 shell::mojom::InterfaceProviderPtr exposed_services);
208 179
209 MessagePortMessageFilter* NewMessagePortMessageFilter(); 180 MessagePortMessageFilter* NewMessagePortMessageFilter();
210 181
211 std::unique_ptr<shell::InterfaceRegistry> CreateInterfaceRegistry(
212 MockRenderProcessHost* rph);
213
214 std::unique_ptr<TestBrowserContext> browser_context_; 182 std::unique_ptr<TestBrowserContext> browser_context_;
215 std::unique_ptr<MockRenderProcessHost> render_process_host_; 183 std::unique_ptr<MockRenderProcessHost> render_process_host_;
216 std::unique_ptr<MockRenderProcessHost> new_render_process_host_;
217 184
218 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; 185 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
219 186
220 IPC::TestSink sink_; 187 IPC::TestSink sink_;
221 IPC::TestSink inner_sink_; 188 IPC::TestSink inner_sink_;
222 189
223 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>>
224 mock_instance_clients_;
225 size_t mock_instance_clients_next_index_;
226
227 int next_thread_id_; 190 int next_thread_id_;
228 int mock_render_process_id_; 191 int mock_render_process_id_;
229 int new_mock_render_process_id_;
230 192
231 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; 193 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_;
232 std::unique_ptr<shell::InterfaceRegistry>
233 new_render_process_interface_registry_;
234 194
235 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; 195 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_;
236 196
237 // Stores the InterfaceRegistry/InterfaceProviders that are associated with 197 // Stores the InterfaceRegistry/InterfaceProviders that are associated with
238 // each individual service worker. 198 // each individual service worker.
239 base::hash_map<int, InterfaceRegistryAndProvider> 199 base::hash_map<int, InterfaceRegistryAndProvider>
240 thread_id_service_registry_map_; 200 thread_id_service_registry_map_;
241 201
242 // Updated each time MessageToWorker message is received. 202 // Updated each time MessageToWorker message is received.
243 int current_embedded_worker_id_; 203 int current_embedded_worker_id_;
244 204
245 std::vector<scoped_refptr<MessagePortMessageFilter>> 205 std::vector<scoped_refptr<MessagePortMessageFilter>>
246 message_port_message_filters_; 206 message_port_message_filters_;
247 207
248 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; 208 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_;
249 209
250 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); 210 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper);
251 }; 211 };
252 212
253 } // namespace content 213 } // namespace content
254 214
255 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ 215 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698