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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 10 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 class WorkerActivatedObserver | 138 class WorkerActivatedObserver |
139 : public ServiceWorkerContextObserver, | 139 : public ServiceWorkerContextObserver, |
140 public base::RefCountedThreadSafe<WorkerActivatedObserver> { | 140 public base::RefCountedThreadSafe<WorkerActivatedObserver> { |
141 public: | 141 public: |
142 explicit WorkerActivatedObserver(ServiceWorkerContextWrapper* context) | 142 explicit WorkerActivatedObserver(ServiceWorkerContextWrapper* context) |
143 : context_(context) {} | 143 : context_(context) {} |
144 void Init() { | 144 void Init() { |
145 RunOnIOThread(base::Bind(&WorkerActivatedObserver::InitOnIOThread, this)); | 145 RunOnIOThread(base::Bind(&WorkerActivatedObserver::InitOnIOThread, this)); |
146 } | 146 } |
147 // ServiceWorkerContextObserver overrides. | 147 // ServiceWorkerContextObserver overrides. |
148 virtual void OnVersionStateChanged(int64 version_id) override { | 148 void OnVersionStateChanged(int64 version_id) override { |
149 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 149 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
150 const ServiceWorkerVersion* version = | 150 const ServiceWorkerVersion* version = |
151 context_->context()->GetLiveVersion(version_id); | 151 context_->context()->GetLiveVersion(version_id); |
152 if (version->status() == ServiceWorkerVersion::ACTIVATED) { | 152 if (version->status() == ServiceWorkerVersion::ACTIVATED) { |
153 context_->RemoveObserver(this); | 153 context_->RemoveObserver(this); |
154 BrowserThread::PostTask(BrowserThread::UI, | 154 BrowserThread::PostTask(BrowserThread::UI, |
155 FROM_HERE, | 155 FROM_HERE, |
156 base::Bind(&WorkerActivatedObserver::Quit, this)); | 156 base::Bind(&WorkerActivatedObserver::Quit, this)); |
157 } | 157 } |
158 } | 158 } |
159 void Wait() { run_loop_.Run(); } | 159 void Wait() { run_loop_.Run(); } |
160 | 160 |
161 private: | 161 private: |
162 friend class base::RefCountedThreadSafe<WorkerActivatedObserver>; | 162 friend class base::RefCountedThreadSafe<WorkerActivatedObserver>; |
163 virtual ~WorkerActivatedObserver() {} | 163 ~WorkerActivatedObserver() override {} |
164 void InitOnIOThread() { context_->AddObserver(this); } | 164 void InitOnIOThread() { context_->AddObserver(this); } |
165 void Quit() { run_loop_.Quit(); } | 165 void Quit() { run_loop_.Quit(); } |
166 | 166 |
167 base::RunLoop run_loop_; | 167 base::RunLoop run_loop_; |
168 ServiceWorkerContextWrapper* context_; | 168 ServiceWorkerContextWrapper* context_; |
169 DISALLOW_COPY_AND_ASSIGN(WorkerActivatedObserver); | 169 DISALLOW_COPY_AND_ASSIGN(WorkerActivatedObserver); |
170 }; | 170 }; |
171 | 171 |
172 scoped_ptr<net::test_server::HttpResponse> VerifyServiceWorkerHeaderInRequest( | 172 scoped_ptr<net::test_server::HttpResponse> VerifyServiceWorkerHeaderInRequest( |
173 const net::test_server::HttpRequest& request) { | 173 const net::test_server::HttpRequest& request) { |
(...skipping 11 matching lines...) Expand all Loading... |
185 | 185 |
186 // The ImportsBustMemcache test requires that the imported script | 186 // The ImportsBustMemcache test requires that the imported script |
187 // would naturally be cached in blink's memcache, but the embedded | 187 // would naturally be cached in blink's memcache, but the embedded |
188 // test server doesn't produce headers that allow the blink's memcache | 188 // test server doesn't produce headers that allow the blink's memcache |
189 // to do that. This interceptor injects headers that give the import | 189 // to do that. This interceptor injects headers that give the import |
190 // an experiration far in the future. | 190 // an experiration far in the future. |
191 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { | 191 class LongLivedResourceInterceptor : public net::URLRequestInterceptor { |
192 public: | 192 public: |
193 LongLivedResourceInterceptor(const std::string& body) | 193 LongLivedResourceInterceptor(const std::string& body) |
194 : body_(body) {} | 194 : body_(body) {} |
195 virtual ~LongLivedResourceInterceptor() {} | 195 ~LongLivedResourceInterceptor() override {} |
196 | 196 |
197 // net::URLRequestInterceptor implementation | 197 // net::URLRequestInterceptor implementation |
198 virtual net::URLRequestJob* MaybeInterceptRequest( | 198 net::URLRequestJob* MaybeInterceptRequest( |
199 net::URLRequest* request, | 199 net::URLRequest* request, |
200 net::NetworkDelegate* network_delegate) const override { | 200 net::NetworkDelegate* network_delegate) const override { |
201 const char kHeaders[] = | 201 const char kHeaders[] = |
202 "HTTP/1.1 200 OK\0" | 202 "HTTP/1.1 200 OK\0" |
203 "Content-Type: text/javascript\0" | 203 "Content-Type: text/javascript\0" |
204 "Expires: Thu, 1 Jan 2100 20:00:00 GMT\0" | 204 "Expires: Thu, 1 Jan 2100 20:00:00 GMT\0" |
205 "\0"; | 205 "\0"; |
206 std::string headers(kHeaders, arraysize(kHeaders)); | 206 std::string headers(kHeaders, arraysize(kHeaders)); |
207 return new net::URLRequestTestJob( | 207 return new net::URLRequestTestJob( |
208 request, network_delegate, headers, body_, true); | 208 request, network_delegate, headers, body_, true); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 wrapper->context()->GetLiveVersion(version_id); | 255 wrapper->context()->GetLiveVersion(version_id); |
256 *num_resources = static_cast<int>(version->script_cache_map()->size()); | 256 *num_resources = static_cast<int>(version->script_cache_map()->size()); |
257 } | 257 } |
258 | 258 |
259 } // namespace | 259 } // namespace |
260 | 260 |
261 class ServiceWorkerBrowserTest : public ContentBrowserTest { | 261 class ServiceWorkerBrowserTest : public ContentBrowserTest { |
262 protected: | 262 protected: |
263 typedef ServiceWorkerBrowserTest self; | 263 typedef ServiceWorkerBrowserTest self; |
264 | 264 |
265 virtual void SetUpCommandLine(base::CommandLine* command_line) override { | 265 void SetUpCommandLine(base::CommandLine* command_line) override { |
266 command_line->AppendSwitch( | 266 command_line->AppendSwitch( |
267 switches::kEnableExperimentalWebPlatformFeatures); | 267 switches::kEnableExperimentalWebPlatformFeatures); |
268 } | 268 } |
269 | 269 |
270 virtual void SetUpOnMainThread() override { | 270 void SetUpOnMainThread() override { |
271 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 271 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
272 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition( | 272 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition( |
273 shell()->web_contents()->GetBrowserContext()); | 273 shell()->web_contents()->GetBrowserContext()); |
274 wrapper_ = static_cast<ServiceWorkerContextWrapper*>( | 274 wrapper_ = static_cast<ServiceWorkerContextWrapper*>( |
275 partition->GetServiceWorkerContext()); | 275 partition->GetServiceWorkerContext()); |
276 | 276 |
277 // Navigate to the page to set up a renderer page (where we can embed | 277 // Navigate to the page to set up a renderer page (where we can embed |
278 // a worker). | 278 // a worker). |
279 NavigateToURLBlockUntilNavigationsComplete( | 279 NavigateToURLBlockUntilNavigationsComplete( |
280 shell(), | 280 shell(), |
281 embedded_test_server()->GetURL("/service_worker/empty.html"), 1); | 281 embedded_test_server()->GetURL("/service_worker/empty.html"), 1); |
282 | 282 |
283 RunOnIOThread(base::Bind(&self::SetUpOnIOThread, this)); | 283 RunOnIOThread(base::Bind(&self::SetUpOnIOThread, this)); |
284 } | 284 } |
285 | 285 |
286 virtual void TearDownOnMainThread() override { | 286 void TearDownOnMainThread() override { |
287 RunOnIOThread(base::Bind(&self::TearDownOnIOThread, this)); | 287 RunOnIOThread(base::Bind(&self::TearDownOnIOThread, this)); |
288 wrapper_ = NULL; | 288 wrapper_ = NULL; |
289 } | 289 } |
290 | 290 |
291 virtual void SetUpOnIOThread() {} | 291 virtual void SetUpOnIOThread() {} |
292 virtual void TearDownOnIOThread() {} | 292 virtual void TearDownOnIOThread() {} |
293 | 293 |
294 ServiceWorkerContextWrapper* wrapper() { return wrapper_.get(); } | 294 ServiceWorkerContextWrapper* wrapper() { return wrapper_.get(); } |
295 ServiceWorkerContext* public_context() { return wrapper(); } | 295 ServiceWorkerContext* public_context() { return wrapper(); } |
296 | 296 |
297 void AssociateRendererProcessToPattern(const GURL& pattern) { | 297 void AssociateRendererProcessToPattern(const GURL& pattern) { |
298 wrapper_->process_manager()->AddProcessReferenceToPattern( | 298 wrapper_->process_manager()->AddProcessReferenceToPattern( |
299 pattern, shell()->web_contents()->GetRenderProcessHost()->GetID()); | 299 pattern, shell()->web_contents()->GetRenderProcessHost()->GetID()); |
300 } | 300 } |
301 | 301 |
302 private: | 302 private: |
303 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 303 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
304 }; | 304 }; |
305 | 305 |
306 class EmbeddedWorkerBrowserTest : public ServiceWorkerBrowserTest, | 306 class EmbeddedWorkerBrowserTest : public ServiceWorkerBrowserTest, |
307 public EmbeddedWorkerInstance::Listener { | 307 public EmbeddedWorkerInstance::Listener { |
308 public: | 308 public: |
309 typedef EmbeddedWorkerBrowserTest self; | 309 typedef EmbeddedWorkerBrowserTest self; |
310 | 310 |
311 EmbeddedWorkerBrowserTest() | 311 EmbeddedWorkerBrowserTest() |
312 : last_worker_status_(EmbeddedWorkerInstance::STOPPED), | 312 : last_worker_status_(EmbeddedWorkerInstance::STOPPED), |
313 pause_mode_(DONT_PAUSE) {} | 313 pause_mode_(DONT_PAUSE) {} |
314 virtual ~EmbeddedWorkerBrowserTest() {} | 314 virtual ~EmbeddedWorkerBrowserTest() {} |
315 | 315 |
316 virtual void TearDownOnIOThread() override { | 316 void TearDownOnIOThread() override { |
317 if (worker_) { | 317 if (worker_) { |
318 worker_->RemoveListener(this); | 318 worker_->RemoveListener(this); |
319 worker_.reset(); | 319 worker_.reset(); |
320 } | 320 } |
321 } | 321 } |
322 | 322 |
323 void StartOnIOThread() { | 323 void StartOnIOThread() { |
324 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 324 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
325 worker_ = wrapper()->context()->embedded_worker_registry()->CreateWorker(); | 325 worker_ = wrapper()->context()->embedded_worker_registry()->CreateWorker(); |
326 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status()); | 326 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 last_worker_status_ = worker_->status(); | 360 last_worker_status_ = worker_->status(); |
361 EXPECT_EQ(SERVICE_WORKER_OK, status); | 361 EXPECT_EQ(SERVICE_WORKER_OK, status); |
362 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, last_worker_status_); | 362 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, last_worker_status_); |
363 | 363 |
364 if (status != SERVICE_WORKER_OK && !done_closure_.is_null()) | 364 if (status != SERVICE_WORKER_OK && !done_closure_.is_null()) |
365 done_closure_.Run(); | 365 done_closure_.Run(); |
366 } | 366 } |
367 | 367 |
368 protected: | 368 protected: |
369 // EmbeddedWorkerInstance::Observer overrides: | 369 // EmbeddedWorkerInstance::Observer overrides: |
370 virtual void OnStarted() override { | 370 void OnStarted() override { |
371 ASSERT_TRUE(worker_ != NULL); | 371 ASSERT_TRUE(worker_ != NULL); |
372 ASSERT_FALSE(done_closure_.is_null()); | 372 ASSERT_FALSE(done_closure_.is_null()); |
373 last_worker_status_ = worker_->status(); | 373 last_worker_status_ = worker_->status(); |
374 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); | 374 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
375 } | 375 } |
376 virtual void OnStopped() override { | 376 void OnStopped() override { |
377 ASSERT_TRUE(worker_ != NULL); | 377 ASSERT_TRUE(worker_ != NULL); |
378 ASSERT_FALSE(done_closure_.is_null()); | 378 ASSERT_FALSE(done_closure_.is_null()); |
379 last_worker_status_ = worker_->status(); | 379 last_worker_status_ = worker_->status(); |
380 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); | 380 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
381 } | 381 } |
382 virtual void OnPausedAfterDownload() override { | 382 void OnPausedAfterDownload() override { |
383 if (pause_mode_ == PAUSE_THEN_RESUME) | 383 if (pause_mode_ == PAUSE_THEN_RESUME) |
384 worker_->ResumeAfterDownload(); | 384 worker_->ResumeAfterDownload(); |
385 else if (pause_mode_ == PAUSE_THEN_STOP) | 385 else if (pause_mode_ == PAUSE_THEN_STOP) |
386 worker_->Stop(); | 386 worker_->Stop(); |
387 else | 387 else |
388 ASSERT_TRUE(false); | 388 ASSERT_TRUE(false); |
389 } | 389 } |
390 virtual void OnReportException(const base::string16& error_message, | 390 void OnReportException(const base::string16& error_message, |
391 int line_number, | 391 int line_number, |
392 int column_number, | 392 int column_number, |
393 const GURL& source_url) override {} | 393 const GURL& source_url) override {} |
394 virtual void OnReportConsoleMessage(int source_identifier, | 394 void OnReportConsoleMessage(int source_identifier, |
395 int message_level, | 395 int message_level, |
396 const base::string16& message, | 396 const base::string16& message, |
397 int line_number, | 397 int line_number, |
398 const GURL& source_url) override {} | 398 const GURL& source_url) override {} |
399 virtual bool OnMessageReceived(const IPC::Message& message) override { | 399 bool OnMessageReceived(const IPC::Message& message) override { return false; } |
400 return false; | |
401 } | |
402 | 400 |
403 scoped_ptr<EmbeddedWorkerInstance> worker_; | 401 scoped_ptr<EmbeddedWorkerInstance> worker_; |
404 EmbeddedWorkerInstance::Status last_worker_status_; | 402 EmbeddedWorkerInstance::Status last_worker_status_; |
405 | 403 |
406 enum { | 404 enum { |
407 DONT_PAUSE, | 405 DONT_PAUSE, |
408 PAUSE_THEN_RESUME, | 406 PAUSE_THEN_RESUME, |
409 PAUSE_THEN_STOP, | 407 PAUSE_THEN_STOP, |
410 } pause_mode_; | 408 } pause_mode_; |
411 | 409 |
412 // Called by EmbeddedWorkerInstance::Observer overrides so that | 410 // Called by EmbeddedWorkerInstance::Observer overrides so that |
413 // test code can wait for the worker status notifications. | 411 // test code can wait for the worker status notifications. |
414 base::Closure done_closure_; | 412 base::Closure done_closure_; |
415 }; | 413 }; |
416 | 414 |
417 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest { | 415 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest { |
418 public: | 416 public: |
419 typedef ServiceWorkerVersionBrowserTest self; | 417 typedef ServiceWorkerVersionBrowserTest self; |
420 | 418 |
421 virtual ~ServiceWorkerVersionBrowserTest() {} | 419 virtual ~ServiceWorkerVersionBrowserTest() {} |
422 | 420 |
423 virtual void TearDownOnIOThread() override { | 421 void TearDownOnIOThread() override { |
424 registration_ = NULL; | 422 registration_ = NULL; |
425 version_ = NULL; | 423 version_ = NULL; |
426 } | 424 } |
427 | 425 |
428 void InstallTestHelper(const std::string& worker_url, | 426 void InstallTestHelper(const std::string& worker_url, |
429 ServiceWorkerStatusCode expected_status) { | 427 ServiceWorkerStatusCode expected_status) { |
430 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | 428 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, |
431 worker_url)); | 429 worker_url)); |
432 | 430 |
433 // Dispatch install on a worker. | 431 // Dispatch install on a worker. |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
934 RunOnIOThread( | 932 RunOnIOThread( |
935 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, | 933 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, |
936 this, | 934 this, |
937 embedded_test_server()->GetURL("/service_worker/empty.html"), | 935 embedded_test_server()->GetURL("/service_worker/empty.html"), |
938 &status)); | 936 &status)); |
939 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); | 937 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); |
940 } | 938 } |
941 } | 939 } |
942 | 940 |
943 } // namespace content | 941 } // namespace content |
OLD | NEW |