summaryrefslogtreecommitdiffstats
path: root/content/browser/service_worker/service_worker_version.cc
blob: 03216e1a3c907fabe36195928d003b940bf39d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/service_worker/service_worker_version.h"

#include "base/stl_util.h"
#include "content/browser/service_worker/embedded_worker_instance.h"
#include "content/browser/service_worker/embedded_worker_registry.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/common/service_worker/service_worker_messages.h"

namespace content {

ServiceWorkerVersion::ServiceWorkerVersion(
    ServiceWorkerRegistration* registration,
    EmbeddedWorkerRegistry* worker_registry,
    int64 version_id)
    : version_id_(version_id),
      is_shutdown_(false),
      registration_(registration) {
  if (worker_registry)
    embedded_worker_ = worker_registry->CreateWorker();
}

ServiceWorkerVersion::~ServiceWorkerVersion() { DCHECK(is_shutdown_); }

void ServiceWorkerVersion::Shutdown() {
  is_shutdown_ = true;
  registration_ = NULL;
  embedded_worker_.reset();
}

void ServiceWorkerVersion::StartWorker() {
  DCHECK(!is_shutdown_);
  DCHECK(registration_);
  embedded_worker_->Start(version_id_, registration_->script_url());
}

void ServiceWorkerVersion::StopWorker() {
  DCHECK(!is_shutdown_);
  embedded_worker_->Stop();
}

bool ServiceWorkerVersion::DispatchFetchEvent(
    const ServiceWorkerFetchRequest& request) {
  if (embedded_worker_->status() != EmbeddedWorkerInstance::RUNNING)
    return false;
  return embedded_worker_->SendMessage(
      ServiceWorkerMsg_FetchEvent(request));
}

void ServiceWorkerVersion::OnAssociateProvider(
    ServiceWorkerProviderHost* provider_host) {
  DCHECK(!is_shutdown_);
  embedded_worker_->AddProcessReference(provider_host->process_id());
}

void ServiceWorkerVersion::OnUnassociateProvider(
    ServiceWorkerProviderHost* provider_host) {
  embedded_worker_->ReleaseProcessReference(provider_host->process_id());
}

}  // namespace content