| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/cancelable_callback.h" | 6 #include "base/cancelable_callback.h" |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/debugger/devtools_window.h" | 14 #include "chrome/browser/debugger/devtools_window.h" |
| 15 #include "chrome/browser/extensions/extension_host.h" | 15 #include "chrome/browser/extensions/extension_host.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/unpacked_installer.h" | 17 #include "chrome/browser/extensions/unpacked_installer.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 21 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 22 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
| 26 #include "content/browser/renderer_host/render_view_host.h" | 26 #include "content/browser/renderer_host/render_view_host.h" |
| 27 #include "content/browser/worker_host/worker_process_host.h" | |
| 28 #include "content/public/browser/browser_child_process_host_iterator.h" | |
| 29 #include "content/public/browser/child_process_data.h" | 27 #include "content/public/browser/child_process_data.h" |
| 30 #include "content/public/browser/content_browser_client.h" | 28 #include "content/public/browser/content_browser_client.h" |
| 31 #include "content/public/browser/devtools_agent_host_registry.h" | 29 #include "content/public/browser/devtools_agent_host_registry.h" |
| 32 #include "content/public/browser/devtools_client_host.h" | 30 #include "content/public/browser/devtools_client_host.h" |
| 33 #include "content/public/browser/devtools_manager.h" | 31 #include "content/public/browser/devtools_manager.h" |
| 34 #include "content/public/browser/notification_registrar.h" | 32 #include "content/public/browser/notification_registrar.h" |
| 35 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
| 36 #include "content/public/browser/render_view_host_delegate.h" | 34 #include "content/public/browser/render_view_host_delegate.h" |
| 37 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/browser/worker_service.h" | 36 #include "content/public/browser/worker_service.h" |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 class WorkerCreationObserver : public WorkerServiceObserver { | 283 class WorkerCreationObserver : public WorkerServiceObserver { |
| 286 public: | 284 public: |
| 287 explicit WorkerCreationObserver(WorkerData* worker_data) | 285 explicit WorkerCreationObserver(WorkerData* worker_data) |
| 288 : worker_data_(worker_data) { | 286 : worker_data_(worker_data) { |
| 289 } | 287 } |
| 290 | 288 |
| 291 private: | 289 private: |
| 292 virtual ~WorkerCreationObserver() {} | 290 virtual ~WorkerCreationObserver() {} |
| 293 | 291 |
| 294 virtual void WorkerCreated ( | 292 virtual void WorkerCreated ( |
| 295 WorkerProcessHost* process, | 293 const GURL& url, |
| 296 const WorkerProcessHost::WorkerInstance& instance) OVERRIDE { | 294 const string16& name, |
| 297 worker_data_->worker_process_id = process->GetData().id; | 295 int process_id, |
| 298 worker_data_->worker_route_id = instance.worker_route_id(); | 296 int route_id) OVERRIDE { |
| 297 worker_data_->worker_process_id = process_id; |
| 298 worker_data_->worker_route_id = route_id; |
| 299 WorkerService::GetInstance()->RemoveObserver(this); | 299 WorkerService::GetInstance()->RemoveObserver(this); |
| 300 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 300 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 301 MessageLoop::QuitClosure()); | 301 MessageLoop::QuitClosure()); |
| 302 delete this; | 302 delete this; |
| 303 } | 303 } |
| 304 virtual void WorkerDestroyed( | |
| 305 WorkerProcessHost*, | |
| 306 int worker_route_id) OVERRIDE {} | |
| 307 virtual void WorkerContextStarted( | |
| 308 WorkerProcessHost*, | |
| 309 int worker_route_id) OVERRIDE {} | |
| 310 scoped_refptr<WorkerData> worker_data_; | 304 scoped_refptr<WorkerData> worker_data_; |
| 311 }; | 305 }; |
| 312 | 306 |
| 313 class WorkerTerminationObserver : public WorkerServiceObserver { | 307 class WorkerTerminationObserver : public WorkerServiceObserver { |
| 314 public: | 308 public: |
| 315 explicit WorkerTerminationObserver(WorkerData* worker_data) | 309 explicit WorkerTerminationObserver(WorkerData* worker_data) |
| 316 : worker_data_(worker_data) { | 310 : worker_data_(worker_data) { |
| 317 } | 311 } |
| 318 | 312 |
| 319 private: | 313 private: |
| 320 virtual ~WorkerTerminationObserver() {} | 314 virtual ~WorkerTerminationObserver() {} |
| 321 | 315 |
| 322 virtual void WorkerCreated ( | 316 virtual void WorkerDestroyed(int process_id, int route_id) OVERRIDE { |
| 323 WorkerProcessHost* process, | 317 ASSERT_EQ(worker_data_->worker_process_id, process_id); |
| 324 const WorkerProcessHost::WorkerInstance& instance) OVERRIDE {} | 318 ASSERT_EQ(worker_data_->worker_route_id, route_id); |
| 325 virtual void WorkerDestroyed( | |
| 326 WorkerProcessHost* process, | |
| 327 int worker_route_id) OVERRIDE { | |
| 328 ASSERT_EQ(worker_data_->worker_process_id, process->GetData().id); | |
| 329 ASSERT_EQ(worker_data_->worker_route_id, worker_route_id); | |
| 330 WorkerService::GetInstance()->RemoveObserver(this); | 319 WorkerService::GetInstance()->RemoveObserver(this); |
| 331 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 320 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 332 MessageLoop::QuitClosure()); | 321 MessageLoop::QuitClosure()); |
| 333 delete this; | 322 delete this; |
| 334 } | 323 } |
| 335 virtual void WorkerContextStarted( | |
| 336 WorkerProcessHost*, | |
| 337 int worker_route_id) OVERRIDE {} | |
| 338 scoped_refptr<WorkerData> worker_data_; | 324 scoped_refptr<WorkerData> worker_data_; |
| 339 }; | 325 }; |
| 340 | 326 |
| 341 void RunTest(const char* test_name, const char* test_page) { | 327 void RunTest(const char* test_name, const char* test_page) { |
| 342 ASSERT_TRUE(test_server()->Start()); | 328 ASSERT_TRUE(test_server()->Start()); |
| 343 GURL url = test_server()->GetURL(test_page); | 329 GURL url = test_server()->GetURL(test_page); |
| 344 ui_test_utils::NavigateToURL(browser(), url); | 330 ui_test_utils::NavigateToURL(browser(), url); |
| 345 | 331 |
| 346 scoped_refptr<WorkerData> worker_data = WaitForFirstSharedWorker(); | 332 scoped_refptr<WorkerData> worker_data = WaitForFirstSharedWorker(); |
| 347 OpenDevToolsWindowForSharedWorker(worker_data.get()); | 333 OpenDevToolsWindowForSharedWorker(worker_data.get()); |
| 348 RunTestFunction(window_, test_name); | 334 RunTestFunction(window_, test_name); |
| 349 CloseDevToolsWindow(); | 335 CloseDevToolsWindow(); |
| 350 } | 336 } |
| 351 | 337 |
| 352 static void TerminateWorkerOnIOThread( | 338 static void TerminateWorkerOnIOThread(scoped_refptr<WorkerData> worker_data) { |
| 353 scoped_refptr<WorkerData> worker_data) { | 339 if (WorkerService::GetInstance()->TerminateWorker( |
| 354 for (WorkerProcessHostIterator iter; !iter.Done(); ++iter) { | 340 worker_data->worker_process_id, worker_data->worker_route_id)) { |
| 355 if (iter.GetData().id == worker_data->worker_process_id) { | 341 WorkerService::GetInstance()->AddObserver( |
| 356 iter->TerminateWorker(worker_data->worker_route_id); | 342 new WorkerTerminationObserver(worker_data)); |
| 357 WorkerService::GetInstance()->AddObserver( | 343 return; |
| 358 new WorkerTerminationObserver(worker_data)); | |
| 359 return; | |
| 360 } | |
| 361 } | 344 } |
| 362 FAIL() << "Failed to terminate worker.\n"; | 345 FAIL() << "Failed to terminate worker.\n"; |
| 363 } | 346 } |
| 364 | 347 |
| 365 static void TerminateWorker(scoped_refptr<WorkerData> worker_data) { | 348 static void TerminateWorker(scoped_refptr<WorkerData> worker_data) { |
| 366 BrowserThread::PostTask( | 349 BrowserThread::PostTask( |
| 367 BrowserThread::IO, FROM_HERE, | 350 BrowserThread::IO, FROM_HERE, |
| 368 base::Bind(&TerminateWorkerOnIOThread, worker_data)); | 351 base::Bind(&TerminateWorkerOnIOThread, worker_data)); |
| 369 ui_test_utils::RunMessageLoop(); | 352 ui_test_utils::RunMessageLoop(); |
| 370 } | 353 } |
| 371 | 354 |
| 372 static void WaitForFirstSharedWorkerOnIOThread( | 355 static void WaitForFirstSharedWorkerOnIOThread( |
| 373 scoped_refptr<WorkerData> worker_data) { | 356 scoped_refptr<WorkerData> worker_data) { |
| 374 for (WorkerProcessHostIterator iter; !iter.Done(); ++iter) { | 357 std::vector<WorkerService::WorkerInfo> worker_info = |
| 375 const WorkerProcessHost::Instances& instances = iter->instances(); | 358 WorkerService::GetInstance()->GetWorkers(); |
| 376 for (WorkerProcessHost::Instances::const_iterator i = instances.begin(); | 359 if (!worker_info.empty()) { |
| 377 i != instances.end(); ++i) { | 360 worker_data->worker_process_id = worker_info[0].process_id; |
| 378 | 361 worker_data->worker_route_id = worker_info[0].route_id; |
| 379 worker_data->worker_process_id = iter.GetData().id; | 362 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 380 worker_data->worker_route_id = i->worker_route_id(); | 363 MessageLoop::QuitClosure()); |
| 381 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 364 return; |
| 382 MessageLoop::QuitClosure()); | |
| 383 return; | |
| 384 } | |
| 385 } | 365 } |
| 386 | 366 |
| 387 WorkerService::GetInstance()->AddObserver( | 367 WorkerService::GetInstance()->AddObserver( |
| 388 new WorkerCreationObserver(worker_data.get())); | 368 new WorkerCreationObserver(worker_data.get())); |
| 389 } | 369 } |
| 390 | 370 |
| 391 static scoped_refptr<WorkerData> WaitForFirstSharedWorker() { | 371 static scoped_refptr<WorkerData> WaitForFirstSharedWorker() { |
| 392 scoped_refptr<WorkerData> worker_data(new WorkerData()); | 372 scoped_refptr<WorkerData> worker_data(new WorkerData()); |
| 393 BrowserThread::PostTask( | 373 BrowserThread::PostTask( |
| 394 BrowserThread::IO, FROM_HERE, | 374 BrowserThread::IO, FROM_HERE, |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 | 567 |
| 588 // Reload page to restart the worker. | 568 // Reload page to restart the worker. |
| 589 ui_test_utils::NavigateToURL(browser(), url); | 569 ui_test_utils::NavigateToURL(browser(), url); |
| 590 | 570 |
| 591 // Wait until worker script is paused on the debugger statement. | 571 // Wait until worker script is paused on the debugger statement. |
| 592 RunTestFunction(window_, "testPauseInSharedWorkerInitialization"); | 572 RunTestFunction(window_, "testPauseInSharedWorkerInitialization"); |
| 593 CloseDevToolsWindow(); | 573 CloseDevToolsWindow(); |
| 594 } | 574 } |
| 595 | 575 |
| 596 } // namespace | 576 } // namespace |
| OLD | NEW |