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

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

Issue 85023003: EmbeddedWorker, browser side code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: build fix 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/embedded_worker_instance.h"
6
7 #include "content/browser/service_worker/embedded_worker_registry.h"
8 #include "url/gurl.h"
9
10 namespace content {
11
12 EmbeddedWorkerInstance::EmbeddedWorkerInstance(
13 EmbeddedWorkerRegistry* registry,
14 int embedded_worker_id)
15 : registry_(registry),
16 embedded_worker_id_(embedded_worker_id),
17 status_(STOPPED),
18 process_id_(-1),
19 thread_id_(-1) {
20 }
21
22 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() {
23 DCHECK(status_ == STOPPED);
24 registry_->RemoveWorker(embedded_worker_id_);
25 }
26
27 void EmbeddedWorkerInstance::Start(
28 int64 service_worker_version_id,
29 const GURL& script_url) {
30 DCHECK(status_ == STOPPED);
31 process_id_ = ChooseProcess();
32 status_ = STARTING;
33 bool success = registry_->StartWorker(
34 process_id_,
35 embedded_worker_id_,
36 service_worker_version_id,
37 script_url);
38 if (!success)
39 OnStopped();
40 }
41
42 void EmbeddedWorkerInstance::Stop() {
43 DCHECK(status_ == STARTING || status_ == RUNNING);
44 registry_->StopWorker(process_id_, embedded_worker_id_);
alecflett 2013/12/05 05:18:42 set status to STOPPING?
kinuko 2013/12/09 14:07:42 Yup, done.
45 }
46
47 void EmbeddedWorkerInstance::OnStarted(int thread_id) {
48 DCHECK(status_ == STARTING);
49 status_ = RUNNING;
50 thread_id_ = thread_id;
51 }
52
53 void EmbeddedWorkerInstance::OnStopped() {
54 status_ = STOPPED;
alecflett 2013/12/05 05:18:42 DCHECK(status_ == STOPPING)?
kinuko 2013/12/09 14:07:42 This could also happen in STARTING or RUNNING (i.e
55 process_id_ = -1;
56 thread_id_ = -1;
57 }
58
59 void EmbeddedWorkerInstance::AddProcessReference(int process_id) {
60 ProcessRefMap::iterator found = process_refs_.find(process_id);
61 if (found == process_refs_.end())
62 found = process_refs_.insert(std::make_pair(process_id, 0)).first;
63 ++found->second;
64 }
65
66 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) {
67 ProcessRefMap::iterator found = process_refs_.find(process_id);
68 if (found == process_refs_.end()) {
69 NOTREACHED() << "Releasing unknown process ref " << process_id;
70 return;
71 }
72 if (--found->second == 0)
73 process_refs_.erase(found);
74 }
75
76 int EmbeddedWorkerInstance::ChooseProcess() {
77 // Naive implementation; chooses a process which has the biggest number of
78 // associated providers (so that hopefully likely live longer).
79 ProcessRefMap::iterator max_ref_iter = process_refs_.end();
80 for (ProcessRefMap::iterator iter = process_refs_.begin();
81 iter != process_refs_.end(); ++iter) {
82 if (max_ref_iter == process_refs_.end() ||
83 max_ref_iter->second < iter->second)
84 max_ref_iter = iter;
85 }
86 DCHECK(max_ref_iter != process_refs_.end());
alecflett 2013/12/05 05:18:42 Won't this be true if there are no entries in proc
kinuko 2013/12/09 14:07:42 Currently this instance code has some assumptions
87 return max_ref_iter->first;
88 }
89
90 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698