| 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 "content/renderer/render_thread_impl.h" | 5 #include "content/renderer/render_thread_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 #include "services/shell/public/cpp/interface_registry.h" | 141 #include "services/shell/public/cpp/interface_registry.h" |
| 142 #include "skia/ext/event_tracer_impl.h" | 142 #include "skia/ext/event_tracer_impl.h" |
| 143 #include "skia/ext/skia_memory_dump_provider.h" | 143 #include "skia/ext/skia_memory_dump_provider.h" |
| 144 #include "third_party/WebKit/public/platform/WebImageGenerator.h" | 144 #include "third_party/WebKit/public/platform/WebImageGenerator.h" |
| 145 #include "third_party/WebKit/public/platform/WebMemoryCoordinator.h" | 145 #include "third_party/WebKit/public/platform/WebMemoryCoordinator.h" |
| 146 #include "third_party/WebKit/public/platform/WebString.h" | 146 #include "third_party/WebKit/public/platform/WebString.h" |
| 147 #include "third_party/WebKit/public/platform/WebThread.h" | 147 #include "third_party/WebKit/public/platform/WebThread.h" |
| 148 #include "third_party/WebKit/public/platform/scheduler/child/compositor_worker_s
cheduler.h" | 148 #include "third_party/WebKit/public/platform/scheduler/child/compositor_worker_s
cheduler.h" |
| 149 #include "third_party/WebKit/public/platform/scheduler/child/webthread_impl_for_
worker_scheduler.h" | 149 #include "third_party/WebKit/public/platform/scheduler/child/webthread_impl_for_
worker_scheduler.h" |
| 150 #include "third_party/WebKit/public/platform/scheduler/renderer/renderer_schedul
er.h" | 150 #include "third_party/WebKit/public/platform/scheduler/renderer/renderer_schedul
er.h" |
| 151 #include "third_party/WebKit/public/platform/modules/serviceworker/service_worke
r.mojom.h" |
| 152 #include "third_party/WebKit/public/web/modules/serviceworker/WebIsolatedWorkerC
ontextClient.h" |
| 153 #include "third_party/WebKit/public/web/modules/serviceworker/WebIsolatedWorker.
h" |
| 151 #include "third_party/WebKit/public/web/WebCache.h" | 154 #include "third_party/WebKit/public/web/WebCache.h" |
| 152 #include "third_party/WebKit/public/web/WebDatabase.h" | 155 #include "third_party/WebKit/public/web/WebDatabase.h" |
| 153 #include "third_party/WebKit/public/web/WebDocument.h" | 156 #include "third_party/WebKit/public/web/WebDocument.h" |
| 154 #include "third_party/WebKit/public/web/WebFrame.h" | 157 #include "third_party/WebKit/public/web/WebFrame.h" |
| 155 #include "third_party/WebKit/public/web/WebKit.h" | 158 #include "third_party/WebKit/public/web/WebKit.h" |
| 156 #include "third_party/WebKit/public/web/WebNetworkStateNotifier.h" | 159 #include "third_party/WebKit/public/web/WebNetworkStateNotifier.h" |
| 157 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | 160 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
| 158 #include "third_party/WebKit/public/web/WebScriptController.h" | 161 #include "third_party/WebKit/public/web/WebScriptController.h" |
| 159 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 162 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
| 160 #include "third_party/WebKit/public/web/WebView.h" | 163 #include "third_party/WebKit/public/web/WebView.h" |
| 161 #include "third_party/icu/source/i18n/unicode/timezone.h" | 164 #include "third_party/icu/source/i18n/unicode/timezone.h" |
| 162 #include "third_party/skia/include/core/SkGraphics.h" | 165 #include "third_party/skia/include/core/SkGraphics.h" |
| 163 #include "ui/base/layout.h" | 166 #include "ui/base/layout.h" |
| 164 #include "ui/base/ui_base_switches.h" | 167 #include "ui/base/ui_base_switches.h" |
| 168 #include "mojo/public/cpp/system/watcher.h" |
| 169 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
| 170 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 165 | 171 |
| 166 #if defined(OS_ANDROID) | 172 #if defined(OS_ANDROID) |
| 167 #include <cpu-features.h> | 173 #include <cpu-features.h> |
| 168 #include "content/renderer/android/synchronous_compositor_filter.h" | 174 #include "content/renderer/android/synchronous_compositor_filter.h" |
| 169 #include "content/renderer/android/synchronous_compositor_output_surface.h" | 175 #include "content/renderer/android/synchronous_compositor_output_surface.h" |
| 170 #include "content/renderer/media/android/renderer_demuxer_android.h" | 176 #include "content/renderer/media/android/renderer_demuxer_android.h" |
| 171 #include "content/renderer/media/android/stream_texture_factory.h" | 177 #include "content/renderer/media/android/stream_texture_factory.h" |
| 172 #include "media/base/android/media_codec_util.h" | 178 #include "media/base/android/media_codec_util.h" |
| 173 #endif | 179 #endif |
| 174 | 180 |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 gpu::kNullSurfaceHandle, | 433 gpu::kNullSurfaceHandle, |
| 428 GURL("chrome://gpu/RenderThreadImpl::CreateOffscreenContext"), | 434 GURL("chrome://gpu/RenderThreadImpl::CreateOffscreenContext"), |
| 429 automatic_flushes, support_locking, limits, attributes, nullptr, type)); | 435 automatic_flushes, support_locking, limits, attributes, nullptr, type)); |
| 430 } | 436 } |
| 431 | 437 |
| 432 bool IsRunningInMash() { | 438 bool IsRunningInMash() { |
| 433 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 439 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
| 434 return cmdline->HasSwitch(switches::kIsRunningInMash); | 440 return cmdline->HasSwitch(switches::kIsRunningInMash); |
| 435 } | 441 } |
| 436 | 442 |
| 443 class IsolatedWorkerContextClient |
| 444 : public blink::WebIsolatedWorkerContextClient { |
| 445 public: |
| 446 IsolatedWorkerContextClient( |
| 447 const std::string& scope, |
| 448 shell::mojom::InterfaceProviderRequest request, |
| 449 shell::mojom::InterfaceProviderPtr remote_interfaces, |
| 450 mojom::WorkerScriptListPtr worker_script_list, |
| 451 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner, |
| 452 base::Closure worker_destroyed_closure) |
| 453 : scope_(scope), |
| 454 interface_bind_callback_( |
| 455 base::Bind(&IsolatedWorkerContextClient::BindInterfaceProviders, |
| 456 base::Passed(&request), |
| 457 base::Passed(remote_interfaces.PassInterface()))), |
| 458 io_thread_task_runner_(io_thread_task_runner), |
| 459 worker_destroyed_closure_(worker_destroyed_closure), |
| 460 worker_script_list_(std::move(worker_script_list)), |
| 461 script_ready_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 462 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 463 weak_factory_(this) { |
| 464 TRACE_EVENT0("ServiceWorker", |
| 465 "IsolatedWorkerContextClient::IsolatedWorkerContextClient"); |
| 466 io_thread_task_runner_->PostTask( |
| 467 FROM_HERE, base::Bind(&IsolatedWorkerContextClient::StartScriptRead, |
| 468 weak_factory_.GetWeakPtr())); |
| 469 } |
| 470 ~IsolatedWorkerContextClient() override {} |
| 471 |
| 472 // Must be called in the worker thread. |
| 473 void initializeOnWorkerThread() override { |
| 474 TRACE_EVENT0("ServiceWorker", |
| 475 "IsolatedWorkerContextClient::initializeOnWorkerThread"); |
| 476 context_.reset(new WorkerContextData()); |
| 477 interface_bind_callback_.Run(context_.get()); |
| 478 interface_bind_callback_.Reset(); |
| 479 } |
| 480 // Must be called in the worker thread. |
| 481 void connectToRemoteService(const char* name, |
| 482 mojo::ScopedMessagePipeHandle handle) override { |
| 483 TRACE_EVENT0("ServiceWorker", |
| 484 "IsolatedWorkerContextClient::connectToRemoteService"); |
| 485 DCHECK(context_); |
| 486 context_->remote_interfaces.GetInterface(name, std::move(handle)); |
| 487 } |
| 488 |
| 489 // Must be called in the worker thread. |
| 490 std::unique_ptr<blink::WebServiceWorkerRegistration::Handle> |
| 491 takeAssociatedRegistration() override { |
| 492 ServiceWorkerRegistrationObjectInfo info; |
| 493 ServiceWorkerVersionAttributes attrs; |
| 494 info.scope = GURL(scope_); |
| 495 info.handle_id = 1; // test |
| 496 info.registration_id = 1; // test |
| 497 |
| 498 scoped_refptr<WebServiceWorkerRegistrationImpl> registration( |
| 499 new WebServiceWorkerRegistrationImpl( |
| 500 ServiceWorkerRegistrationHandleReference::Create(info, nullptr))); |
| 501 |
| 502 return WebServiceWorkerRegistrationImpl::CreateHandle(registration); |
| 503 } |
| 504 |
| 505 // Must be called in the worker thread. |
| 506 std::unique_ptr<std::vector<std::unique_ptr<WebScriptData>>> |
| 507 takeWorkerScriptList() override { |
| 508 TRACE_EVENT0("ServiceWorker", |
| 509 "IsolatedWorkerContextClient::takeWorkerScriptList"); |
| 510 LOG(ERROR) << "takeWorkerScriptList"; |
| 511 { |
| 512 TRACE_EVENT0("ServiceWorker", |
| 513 "IsolatedWorkerContextClient::takeWorkerScriptList wait"); |
| 514 LOG(ERROR) << "script_ready_.Wait();"; |
| 515 script_ready_.Wait(); |
| 516 LOG(ERROR) << "script_ready_.Wait(); done"; |
| 517 } |
| 518 std::unique_ptr<std::vector<std::unique_ptr<WebScriptData>>> list( |
| 519 new std::vector<std::unique_ptr<WebScriptData>>()); |
| 520 |
| 521 for (auto& it : data_map_) { |
| 522 std::unique_ptr<WebScriptData> script_data(new WebScriptData( |
| 523 it.first, std::move(it.second), std::move(metadata_map_[it.first]))); |
| 524 list->push_back(std::move(script_data)); |
| 525 } |
| 526 |
| 527 return list; |
| 528 } |
| 529 |
| 530 // Must be called in the worker thread. |
| 531 void workerDestroyed() override { |
| 532 context_.reset(); |
| 533 io_thread_task_runner_->PostTask(FROM_HERE, worker_destroyed_closure_); |
| 534 // |this| is deleted |
| 535 } |
| 536 |
| 537 private: |
| 538 struct WorkerContextData { |
| 539 WorkerContextData() : interface_registry() {} |
| 540 ~WorkerContextData() { DCHECK(thread_checker.CalledOnValidThread()); } |
| 541 base::ThreadChecker thread_checker; |
| 542 shell::InterfaceRegistry interface_registry; |
| 543 shell::InterfaceProvider remote_interfaces; |
| 544 }; |
| 545 class ScriptPump { |
| 546 public: |
| 547 ScriptPump(const std::string& url, |
| 548 mojo::ScopedDataPipeConsumerHandle pipe, |
| 549 base::Callback<void(ScriptPump*)> callback) |
| 550 : url_(url), |
| 551 pipe_(std::move(pipe)), |
| 552 data_vector(new DataVector), |
| 553 callback_(callback), |
| 554 weak_factory_(this) {} |
| 555 ~ScriptPump() {} |
| 556 void StartRead() { ReadNext(); } |
| 557 std::unique_ptr<DataVector> TakeData() { return std::move(data_vector); } |
| 558 std::string url() const { return url_; } |
| 559 |
| 560 private: |
| 561 void OnReadable(MojoResult unused) { |
| 562 LOG(ERROR) << "ScriptPump::OnReadable"; |
| 563 ReadNext(); |
| 564 } |
| 565 void ReadNext() { |
| 566 LOG(ERROR) << "ScriptPump::ReadNext"; |
| 567 TRACE_EVENT0("ServiceWorker", "ScriptPump::ReadNext"); |
| 568 |
| 569 const void* buffer = nullptr; |
| 570 uint32_t available = 0; |
| 571 MojoResult result = mojo::BeginReadDataRaw( |
| 572 pipe_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); |
| 573 // LOG(ERROR) << " mojo::BeginReadDataRaw: " << result; |
| 574 // LOG(ERROR) << " mojo::BeginReadDataRaw: available: " << available; |
| 575 |
| 576 // TODO: We need error handling code. |
| 577 if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 578 callback_.Run(this); |
| 579 // Deleted |
| 580 return; |
| 581 } |
| 582 if (result == MOJO_RESULT_OK) { |
| 583 if (available > 0) { |
| 584 std::unique_ptr<std::vector<char>> data(new std::vector<char>( |
| 585 static_cast<const char*>(buffer), |
| 586 static_cast<const char*>(buffer) + available)); |
| 587 data_vector->push_back(std::move(data)); |
| 588 } |
| 589 result = mojo::EndReadDataRaw(pipe_.get(), available); |
| 590 // LOG(ERROR) << " mojo::EndReadDataRaw: " << result; |
| 591 ReadNext(); |
| 592 return; |
| 593 } |
| 594 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 595 LOG(ERROR) << "handle_watcher_.Start"; |
| 596 handle_watcher_.Start( |
| 597 pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 598 base::Bind(&ScriptPump::OnReadable, weak_factory_.GetWeakPtr())); |
| 599 } |
| 600 } |
| 601 |
| 602 const std::string url_; |
| 603 mojo::ScopedDataPipeConsumerHandle pipe_; |
| 604 std::unique_ptr<DataVector> data_vector; |
| 605 base::Callback<void(ScriptPump*)> callback_; |
| 606 mojo::Watcher handle_watcher_; |
| 607 base::WeakPtrFactory<ScriptPump> weak_factory_; |
| 608 }; |
| 609 |
| 610 // Must be called in the worker thread. |
| 611 static void BindInterfaceProviders( |
| 612 shell::mojom::InterfaceProviderRequest request, |
| 613 shell::mojom::InterfaceProviderPtrInfo remote_interfaces, |
| 614 WorkerContextData* context) { |
| 615 DCHECK(context->thread_checker.CalledOnValidThread()); |
| 616 context->interface_registry.Bind(std::move(request)); |
| 617 context->remote_interfaces.Bind( |
| 618 mojo::MakeProxy(std::move(remote_interfaces))); |
| 619 } |
| 620 // Must be called in IO thread. |
| 621 void StartScriptRead() { |
| 622 LOG(ERROR) << "StartScriptRead: size: " |
| 623 << worker_script_list_->scripts.size(); |
| 624 std::vector<ScriptPump*> pump_list; |
| 625 for (size_t i = 0; i < worker_script_list_->scripts.size(); ++i) { |
| 626 std::unique_ptr<ScriptPump> pump( |
| 627 new ScriptPump(worker_script_list_->scripts[i]->url, |
| 628 std::move(worker_script_list_->scripts[i]->data_pipe), |
| 629 base::Bind(&IsolatedWorkerContextClient::PumpFinished, |
| 630 weak_factory_.GetWeakPtr()))); |
| 631 ScriptPump* ptr = pump.get(); |
| 632 pump_map_[ptr] = std::move(pump); |
| 633 pump_list.push_back(ptr); |
| 634 |
| 635 std::unique_ptr<ScriptPump> metadata_pump(new ScriptPump( |
| 636 worker_script_list_->scripts[i]->url, |
| 637 std::move(worker_script_list_->scripts[i]->meta_data_pipe), |
| 638 base::Bind(&IsolatedWorkerContextClient::MetadataPumpFinished, |
| 639 weak_factory_.GetWeakPtr()))); |
| 640 ScriptPump* metadata_pump_ptr = metadata_pump.get(); |
| 641 metadata_pump_map_[metadata_pump_ptr] = std::move(metadata_pump); |
| 642 pump_list.push_back(metadata_pump_ptr); |
| 643 } |
| 644 for (auto& pump : pump_list) { |
| 645 pump->StartRead(); |
| 646 } |
| 647 LOG(ERROR) << "pump_map_.size() " << pump_map_.size() |
| 648 << " metadata_pump_map_.size(): " << metadata_pump_map_.size(); |
| 649 } |
| 650 void PumpFinished(ScriptPump* pump) { |
| 651 TRACE_EVENT0("ServiceWorker", "ScriptPump::PumpFinished"); |
| 652 LOG(ERROR) << "ScriptPump::PumpFinished"; |
| 653 data_map_[pump->url()] = pump->TakeData(); |
| 654 pump_map_.erase(pump); |
| 655 LOG(ERROR) << "pump_map_.size() " << pump_map_.size() |
| 656 << " metadata_pump_map_.size(): " << metadata_pump_map_.size(); |
| 657 if (pump_map_.empty() && metadata_pump_map_.empty()) { |
| 658 TRACE_EVENT0("ServiceWorker", "ScriptPump::PumpFinished ALL"); |
| 659 LOG(ERROR) << "ScriptPump::PumpFinished ALL"; |
| 660 script_ready_.Signal(); |
| 661 } |
| 662 } |
| 663 void MetadataPumpFinished(ScriptPump* pump) { |
| 664 TRACE_EVENT0("ServiceWorker", "ScriptPump::MetadataPumpFinished"); |
| 665 LOG(ERROR) << "ScriptPump::MetadataPumpFinished"; |
| 666 metadata_map_[pump->url()] = pump->TakeData(); |
| 667 metadata_pump_map_.erase(pump); |
| 668 LOG(ERROR) << "pump_map_.size() " << pump_map_.size() |
| 669 << " metadata_pump_map_.size(): " << metadata_pump_map_.size(); |
| 670 if (pump_map_.empty() && metadata_pump_map_.empty()) { |
| 671 TRACE_EVENT0("ServiceWorker", "ScriptPump::MetadataPumpFinished ALL"); |
| 672 LOG(ERROR) << "ScriptPump::MetadataPumpFinished ALL"; |
| 673 script_ready_.Signal(); |
| 674 } |
| 675 } |
| 676 |
| 677 // Initialized on the worker thread in workerContextStarted and |
| 678 // destructed on the worker thread in willDestroyWorkerContext. |
| 679 std::unique_ptr<WorkerContextData> context_; |
| 680 const std::string scope_; |
| 681 base::Callback<void(WorkerContextData*)> interface_bind_callback_; |
| 682 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
| 683 base::Closure worker_destroyed_closure_; |
| 684 mojom::WorkerScriptListPtr worker_script_list_; |
| 685 base::WaitableEvent script_ready_; |
| 686 base::hash_map<ScriptPump*, std::unique_ptr<ScriptPump>> pump_map_; |
| 687 base::hash_map<ScriptPump*, std::unique_ptr<ScriptPump>> metadata_pump_map_; |
| 688 base::hash_map<std::string, std::unique_ptr<DataVector>> data_map_; |
| 689 base::hash_map<std::string, std::unique_ptr<DataVector>> metadata_map_; |
| 690 base::WeakPtrFactory<IsolatedWorkerContextClient> weak_factory_; |
| 691 }; |
| 692 |
| 437 } // namespace | 693 } // namespace |
| 438 | 694 |
| 695 class IsolatedWorkerManager |
| 696 : public base::RefCountedThreadSafe<IsolatedWorkerManager> { |
| 697 public: |
| 698 IsolatedWorkerManager( |
| 699 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
| 700 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner, |
| 701 mojom::IsolatedWorkerDispatcherRecieverPtr dispatcher_reciever) |
| 702 : main_thread_task_runner_(main_thread_task_runner), |
| 703 io_thread_task_runner_(io_thread_task_runner), |
| 704 weak_factory_(this) { |
| 705 TRACE_EVENT0("ServiceWorker", |
| 706 "IsolatedWorkerManager::IsolatedWorkerManager"); |
| 707 LOG(ERROR) << "IsolatedWorkerManager " << this; |
| 708 io_thread_task_runner->PostTask( |
| 709 FROM_HERE, |
| 710 base::Bind(&IsolatedWorkerManager::InitializeOnIO, this, |
| 711 base::Passed(dispatcher_reciever.PassInterface()))); |
| 712 } |
| 713 void InitializeOnIO( |
| 714 mojom::IsolatedWorkerDispatcherRecieverPtrInfo dispatcher_reciever_info) { |
| 715 TRACE_EVENT0("ServiceWorker", "IsolatedWorkerManager::InitializeOnIO"); |
| 716 mojom::IsolatedWorkerDispatcherRecieverPtr dispatcher_reciever; |
| 717 dispatcher_reciever.Bind(std::move(dispatcher_reciever_info)); |
| 718 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 719 LOG(ERROR) << "InitializeOnIO " << this; |
| 720 // pass |
| 721 LOG(ERROR) << "IsolatedWorkerDispatcherImpl "; |
| 722 dispatcher_.reset(new IsolatedWorkerDispatcherImpl(this)); |
| 723 mojom::IsolatedWorkerDispatcherPtr dispatcher_ptr; |
| 724 LOG(ERROR) << " dispatcher_->Bind "; |
| 725 dispatcher_->Bind(GetProxy(&dispatcher_ptr)); |
| 726 LOG(ERROR) << " dispatcher_reciever->SetDispatcher "; |
| 727 dispatcher_reciever->SetDispatcher(std::move(dispatcher_ptr)); |
| 728 LOG(ERROR) << " dispatcher_reciever->SetDispatcher done"; |
| 729 } |
| 730 |
| 731 void StartServiceWorker(int64_t version_id, |
| 732 const std::string& scope, |
| 733 const std::string& script_url, |
| 734 shell::mojom::InterfaceProviderRequest request, |
| 735 shell::mojom::InterfaceProviderPtr remote_interfaces, |
| 736 mojom::WorkerScriptListPtr worker_script_list) { |
| 737 TRACE_EVENT0("ServiceWorker", "IsolatedWorkerManager::StartServiceWorker"); |
| 738 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 739 int32_t isolated_worker_id = next_isolated_worker_id_++; |
| 740 std::unique_ptr<IsolatedWorkerContextClient> client( |
| 741 new IsolatedWorkerContextClient( |
| 742 scope, std::move(request), std::move(remote_interfaces), |
| 743 std::move(worker_script_list), io_thread_task_runner_, |
| 744 base::Bind(&IsolatedWorkerManager::WorkerDestroyed, this, |
| 745 isolated_worker_id))); |
| 746 std::unique_ptr<blink::WebIsolatedWorker> worker( |
| 747 blink::WebIsolatedWorker::create(client.release())); |
| 748 std::unique_ptr<WorkerWrapper> wrapper( |
| 749 new WorkerWrapper(std::move(worker), main_thread_task_runner_)); |
| 750 wrapper->worker()->startWorker(version_id, GURL(scope), GURL(script_url)); |
| 751 workers_.AddWithID(wrapper.release(), isolated_worker_id); |
| 752 } |
| 753 |
| 754 base::WeakPtr<IsolatedWorkerManager> AsWeakPtr() { |
| 755 return weak_factory_.GetWeakPtr(); |
| 756 } |
| 757 |
| 758 private: |
| 759 friend class base::RefCountedThreadSafe<IsolatedWorkerManager>; |
| 760 |
| 761 // Lives in |
| 762 class IsolatedWorkerDispatcherImpl : public mojom::IsolatedWorkerDispatcher { |
| 763 public: |
| 764 explicit IsolatedWorkerDispatcherImpl( |
| 765 scoped_refptr<IsolatedWorkerManager> manager) |
| 766 : manager_(manager), binding_(this) { |
| 767 TRACE_EVENT0( |
| 768 "ServiceWorker", |
| 769 "IsolatedWorkerDispatcherImpl::IsolatedWorkerDispatcherImpl"); |
| 770 LOG(ERROR) << "IsolatedWorkerDispatcherImpl" << this; |
| 771 } |
| 772 ~IsolatedWorkerDispatcherImpl() override { |
| 773 LOG(ERROR) << "~IsolatedWorkerDispatcherImpl" << this; |
| 774 } |
| 775 void Bind(mojom::IsolatedWorkerDispatcherRequest local_interfaces_request) { |
| 776 DCHECK(!binding_.is_bound()); |
| 777 binding_.Bind(std::move(local_interfaces_request)); |
| 778 } |
| 779 |
| 780 void StartServiceWorker( |
| 781 int64_t version_id, |
| 782 const std::string& scope, |
| 783 const std::string& script_url, |
| 784 ::shell::mojom::InterfaceProviderRequest request, |
| 785 ::shell::mojom::InterfaceProviderPtr remote_interfaces, |
| 786 mojom::WorkerScriptListPtr script_list) override { |
| 787 LOG(ERROR) << "IsolatedWorkerDispatcherImpl[[2]] :: StartServiceWorker"; |
| 788 if (!manager_) |
| 789 return; |
| 790 manager_->StartServiceWorker( |
| 791 version_id, scope, script_url, std::move(request), |
| 792 std::move(remote_interfaces), std::move(script_list)); |
| 793 } |
| 794 |
| 795 private: |
| 796 scoped_refptr<IsolatedWorkerManager> manager_; |
| 797 mojo::Binding<mojom::IsolatedWorkerDispatcher> binding_; |
| 798 }; |
| 799 class WorkerWrapper { |
| 800 public: |
| 801 WorkerWrapper( |
| 802 std::unique_ptr<blink::WebIsolatedWorker> worker, |
| 803 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
| 804 : worker_(std::move(worker)), |
| 805 main_thread_task_runner_(main_thread_task_runner) { |
| 806 main_thread_task_runner_->PostTask( |
| 807 FROM_HERE, base::Bind(WorkerWrapper::AddRefProcessOnMain)); |
| 808 } |
| 809 ~WorkerWrapper() { |
| 810 main_thread_task_runner_->PostTask( |
| 811 FROM_HERE, base::Bind(WorkerWrapper::ReleaseProcessOnMain)); |
| 812 } |
| 813 blink::WebIsolatedWorker* worker() { return worker_.get(); } |
| 814 |
| 815 private: |
| 816 static void AddRefProcessOnMain() { |
| 817 LOG(ERROR) << "AddRefProcessOnMain-------------"; |
| 818 // TODO: ChildProcess::current()->AddRefProcess(); |
| 819 } |
| 820 static void ReleaseProcessOnMain() { |
| 821 LOG(ERROR) << "ReleaseProcessOnMain-------------"; |
| 822 // TODO: ChildProcess::current()->ReleaseProcess(); |
| 823 } |
| 824 std::unique_ptr<blink::WebIsolatedWorker> worker_; |
| 825 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 826 }; |
| 827 |
| 828 ~IsolatedWorkerManager() { LOG(ERROR) << "~IsolatedWorkerManager " << this; } |
| 829 void WorkerDestroyed(int64_t isolated_worker_id) { |
| 830 workers_.Remove(isolated_worker_id); |
| 831 } |
| 832 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 833 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
| 834 |
| 835 std::unique_ptr<IsolatedWorkerDispatcherImpl> dispatcher_; |
| 836 |
| 837 int32_t next_isolated_worker_id_ = 0; |
| 838 IDMap<WorkerWrapper, IDMapOwnPointer> workers_; |
| 839 base::WeakPtrFactory<IsolatedWorkerManager> weak_factory_; |
| 840 |
| 841 DISALLOW_COPY_AND_ASSIGN(IsolatedWorkerManager); |
| 842 }; |
| 843 |
| 439 // For measuring memory usage after each task. Behind a command line flag. | 844 // For measuring memory usage after each task. Behind a command line flag. |
| 440 class MemoryObserver : public base::MessageLoop::TaskObserver { | 845 class MemoryObserver : public base::MessageLoop::TaskObserver { |
| 441 public: | 846 public: |
| 442 MemoryObserver() {} | 847 MemoryObserver() {} |
| 443 ~MemoryObserver() override {} | 848 ~MemoryObserver() override {} |
| 444 | 849 |
| 445 void WillProcessTask(const base::PendingTask& pending_task) override {} | 850 void WillProcessTask(const base::PendingTask& pending_task) override {} |
| 446 | 851 |
| 447 void DidProcessTask(const base::PendingTask& pending_task) override { | 852 void DidProcessTask(const base::PendingTask& pending_task) override { |
| 448 LOCAL_HISTOGRAM_MEMORY_KB("Memory.RendererUsed", GetMemoryUsageKB()); | 853 LOCAL_HISTOGRAM_MEMORY_KB("Memory.RendererUsed", GetMemoryUsageKB()); |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 // discardable memory manager, and should skip this if kSingleProcess. | 1249 // discardable memory manager, and should skip this if kSingleProcess. |
| 845 // See crbug.com/503724. | 1250 // See crbug.com/503724. |
| 846 base::DiscardableMemoryAllocator::SetInstance( | 1251 base::DiscardableMemoryAllocator::SetInstance( |
| 847 ChildThreadImpl::discardable_shared_memory_manager()); | 1252 ChildThreadImpl::discardable_shared_memory_manager()); |
| 848 | 1253 |
| 849 GetContentClient()->renderer()->ExposeInterfacesToBrowser( | 1254 GetContentClient()->renderer()->ExposeInterfacesToBrowser( |
| 850 GetInterfaceRegistry()); | 1255 GetInterfaceRegistry()); |
| 851 | 1256 |
| 852 GetInterfaceRegistry()->AddInterface(base::Bind(CreateFrameFactory)); | 1257 GetInterfaceRegistry()->AddInterface(base::Bind(CreateFrameFactory)); |
| 853 GetInterfaceRegistry()->AddInterface(base::Bind(CreateEmbeddedWorkerSetup)); | 1258 GetInterfaceRegistry()->AddInterface(base::Bind(CreateEmbeddedWorkerSetup)); |
| 1259 LOG(ERROR) << "GetInterfaceRegistry()->AddInterface"; |
| 1260 LOG(ERROR) << "base::ThreadTaskRunnerHandle::IsSet() " |
| 1261 << base::ThreadTaskRunnerHandle::IsSet(); |
| 1262 |
| 1263 mojom::IsolatedWorkerDispatcherRecieverPtr dispatcher_reciever; |
| 1264 LOG(ERROR) << "GetRemoteInterfaces()->GetInterface"; |
| 1265 GetRemoteInterfaces()->GetInterface(mojo::GetProxy(&dispatcher_reciever)); |
| 1266 |
| 1267 isolated_worker_manager_ = |
| 1268 new IsolatedWorkerManager(base::ThreadTaskRunnerHandle::Get(), |
| 1269 ChildProcess::current()->io_task_runner(), |
| 1270 std::move(dispatcher_reciever)); |
| 854 | 1271 |
| 855 GetRemoteInterfaces()->GetInterface( | 1272 GetRemoteInterfaces()->GetInterface( |
| 856 mojo::GetProxy(&storage_partition_service_)); | 1273 mojo::GetProxy(&storage_partition_service_)); |
| 857 | 1274 |
| 858 is_renderer_suspended_ = false; | 1275 is_renderer_suspended_ = false; |
| 859 } | 1276 } |
| 860 | 1277 |
| 861 RenderThreadImpl::~RenderThreadImpl() { | 1278 RenderThreadImpl::~RenderThreadImpl() { |
| 862 } | 1279 } |
| 863 | 1280 |
| (...skipping 1363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2227 if (blink::mainThreadIsolate()) { | 2644 if (blink::mainThreadIsolate()) { |
| 2228 blink::mainThreadIsolate()->MemoryPressureNotification( | 2645 blink::mainThreadIsolate()->MemoryPressureNotification( |
| 2229 v8::MemoryPressureLevel::kCritical); | 2646 v8::MemoryPressureLevel::kCritical); |
| 2230 blink::MemoryPressureNotificationToWorkerThreadIsolates( | 2647 blink::MemoryPressureNotificationToWorkerThreadIsolates( |
| 2231 v8::MemoryPressureLevel::kCritical); | 2648 v8::MemoryPressureLevel::kCritical); |
| 2232 } | 2649 } |
| 2233 } | 2650 } |
| 2234 | 2651 |
| 2235 | 2652 |
| 2236 } // namespace content | 2653 } // namespace content |
| OLD | NEW |