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

Side by Side Diff: content/browser/service_worker/embedded_worker_instance.cc

Issue 118103002: Add IPC stubs between browser and ServiceWorker's worker context in the child process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/browser/service_worker/embedded_worker_instance.h" 5 #include "content/browser/service_worker/embedded_worker_instance.h"
6 6
7 #include "content/browser/service_worker/embedded_worker_registry.h" 7 #include "content/browser/service_worker/embedded_worker_registry.h"
8 #include "content/common/service_worker_messages.h"
8 #include "url/gurl.h" 9 #include "url/gurl.h"
9 10
10 namespace content { 11 namespace content {
11 12
12 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { 13 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() {
13 registry_->RemoveWorker(embedded_worker_id_); 14 registry_->RemoveWorker(process_id_, embedded_worker_id_);
14 } 15 }
15 16
16 bool EmbeddedWorkerInstance::Start( 17 bool EmbeddedWorkerInstance::Start(
17 int64 service_worker_version_id, 18 int64 service_worker_version_id,
18 const GURL& script_url) { 19 const GURL& script_url) {
19 DCHECK(status_ == STOPPED); 20 DCHECK(status_ == STOPPED);
20 if (!ChooseProcess()) 21 if (!ChooseProcess())
21 return false; 22 return false;
22 status_ = STARTING; 23 status_ = STARTING;
23 bool success = registry_->StartWorker( 24 bool success = registry_->StartWorker(
24 process_id_, 25 process_id_,
25 embedded_worker_id_, 26 embedded_worker_id_,
26 service_worker_version_id, 27 service_worker_version_id,
27 script_url); 28 script_url);
28 if (!success) { 29 if (!success) {
29 status_ = STOPPED; 30 status_ = STOPPED;
30 process_id_ = -1; 31 process_id_ = -1;
31 } 32 }
32 return success; 33 return success;
33 } 34 }
34 35
35 bool EmbeddedWorkerInstance::Stop() { 36 bool EmbeddedWorkerInstance::Stop() {
36 DCHECK(status_ == STARTING || status_ == RUNNING); 37 DCHECK(status_ == STARTING || status_ == RUNNING);
37 const bool success = registry_->StopWorker(process_id_, embedded_worker_id_); 38 const bool success = registry_->StopWorker(process_id_, embedded_worker_id_);
38 if (success) 39 if (success)
39 status_ = STOPPING; 40 status_ = STOPPING;
40 return success; 41 return success;
41 } 42 }
42 43
44 bool EmbeddedWorkerInstance::SendFetchRequest(
45 const ServiceWorkerFetchRequest& request) {
46 DCHECK(status_ == RUNNING);
47 // Note to reviewers: the code around FetchEvent is very rough, it's here
48 // basically to show how I'm planning to establish bidirectional channel
49 // between embedded worker and browser. (I can remove this part from this
50 // patch if this part looks too rough)
alecflett 2013/12/19 00:59:11 I say leave it - this gives us something to build
kinuko 2013/12/20 08:01:09 Ok, changed it to a TODO comment
51 return registry_->Send(process_id_,
52 new ServiceWorkerContextMsg_FetchEvent(
53 thread_id_, embedded_worker_id_, request));
54 }
55
43 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { 56 void EmbeddedWorkerInstance::AddProcessReference(int process_id) {
44 ProcessRefMap::iterator found = process_refs_.find(process_id); 57 ProcessRefMap::iterator found = process_refs_.find(process_id);
45 if (found == process_refs_.end()) 58 if (found == process_refs_.end())
46 found = process_refs_.insert(std::make_pair(process_id, 0)).first; 59 found = process_refs_.insert(std::make_pair(process_id, 0)).first;
47 ++found->second; 60 ++found->second;
48 } 61 }
49 62
50 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { 63 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) {
51 ProcessRefMap::iterator found = process_refs_.find(process_id); 64 ProcessRefMap::iterator found = process_refs_.find(process_id);
52 if (found == process_refs_.end()) { 65 if (found == process_refs_.end()) {
53 NOTREACHED() << "Releasing unknown process ref " << process_id; 66 NOTREACHED() << "Releasing unknown process ref " << process_id;
54 return; 67 return;
55 } 68 }
56 if (--found->second == 0) 69 if (--found->second == 0)
57 process_refs_.erase(found); 70 process_refs_.erase(found);
58 } 71 }
59 72
60 EmbeddedWorkerInstance::EmbeddedWorkerInstance( 73 EmbeddedWorkerInstance::EmbeddedWorkerInstance(
61 EmbeddedWorkerRegistry* registry, 74 EmbeddedWorkerRegistry* registry,
62 int embedded_worker_id) 75 int embedded_worker_id)
63 : registry_(registry), 76 : registry_(registry),
64 embedded_worker_id_(embedded_worker_id), 77 embedded_worker_id_(embedded_worker_id),
65 status_(STOPPED), 78 status_(STOPPED),
66 process_id_(-1), 79 process_id_(-1),
67 thread_id_(-1) { 80 thread_id_(-1) {
68 } 81 }
69 82
70 void EmbeddedWorkerInstance::OnStarted(int thread_id) { 83 void EmbeddedWorkerInstance::OnStarted(int thread_id) {
84 // Stop is requested before OnStarted is sent back from the worker.
85 if (status_ == STOPPING)
86 return;
71 DCHECK(status_ == STARTING); 87 DCHECK(status_ == STARTING);
72 status_ = RUNNING; 88 status_ = RUNNING;
73 thread_id_ = thread_id; 89 thread_id_ = thread_id;
74 } 90 }
75 91
76 void EmbeddedWorkerInstance::OnStopped() { 92 void EmbeddedWorkerInstance::OnStopped() {
77 status_ = STOPPED; 93 status_ = STOPPED;
78 process_id_ = -1; 94 process_id_ = -1;
79 thread_id_ = -1; 95 thread_id_ = -1;
80 } 96 }
81 97
82 bool EmbeddedWorkerInstance::ChooseProcess() { 98 bool EmbeddedWorkerInstance::ChooseProcess() {
83 DCHECK_EQ(-1, process_id_); 99 DCHECK_EQ(-1, process_id_);
84 // Naive implementation; chooses a process which has the biggest number of 100 // Naive implementation; chooses a process which has the biggest number of
85 // associated providers (so that hopefully likely live longer). 101 // associated providers (so that hopefully likely live longer).
86 ProcessRefMap::iterator max_ref_iter = process_refs_.end(); 102 ProcessRefMap::iterator max_ref_iter = process_refs_.end();
87 for (ProcessRefMap::iterator iter = process_refs_.begin(); 103 for (ProcessRefMap::iterator iter = process_refs_.begin();
88 iter != process_refs_.end(); ++iter) { 104 iter != process_refs_.end(); ++iter) {
89 if (max_ref_iter == process_refs_.end() || 105 if (max_ref_iter == process_refs_.end() ||
90 max_ref_iter->second < iter->second) 106 max_ref_iter->second < iter->second)
91 max_ref_iter = iter; 107 max_ref_iter = iter;
92 } 108 }
93 if (max_ref_iter == process_refs_.end()) 109 if (max_ref_iter == process_refs_.end())
94 return false; 110 return false;
95 process_id_ = max_ref_iter->first; 111 process_id_ = max_ref_iter->first;
96 return true; 112 return true;
97 } 113 }
98 114
99 } // namespace content 115 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698