summaryrefslogtreecommitdiffstats
path: root/content/browser/service_worker/service_worker_handle.cc
blob: e47dc97b3e5c7db0a3616484d0491d54cfbe6108 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2014 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_handle.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"
#include "ipc/ipc_sender.h"

namespace content {

namespace {

blink::WebServiceWorkerState
GetWebServiceWorkerState(ServiceWorkerVersion* version) {
  DCHECK(version);
  switch (version->status()) {
    case ServiceWorkerVersion::NEW:
      if (version->running_status() == ServiceWorkerVersion::RUNNING)
        return blink::WebServiceWorkerStateParsed;
      else
        return blink::WebServiceWorkerStateUnknown;
    case ServiceWorkerVersion::INSTALLING:
      return blink::WebServiceWorkerStateInstalling;
    case ServiceWorkerVersion::INSTALLED:
      return blink::WebServiceWorkerStateInstalled;
    case ServiceWorkerVersion::ACTIVATING:
      return blink::WebServiceWorkerStateActivating;
    case ServiceWorkerVersion::ACTIVATED:
      return blink::WebServiceWorkerStateActivated;
    case ServiceWorkerVersion::REDUNDANT:
      return blink::WebServiceWorkerStateRedundant;
  }
  NOTREACHED() << version->status();
  return blink::WebServiceWorkerStateUnknown;
}

}  // namespace

scoped_ptr<ServiceWorkerHandle> ServiceWorkerHandle::Create(
    base::WeakPtr<ServiceWorkerContextCore> context,
    IPC::Sender* sender,
    int thread_id,
    int provider_id,
    ServiceWorkerVersion* version) {
  if (!context || !version)
    return scoped_ptr<ServiceWorkerHandle>();
  ServiceWorkerRegistration* registration =
      context->GetLiveRegistration(version->registration_id());
  return make_scoped_ptr(new ServiceWorkerHandle(
      context, sender, thread_id, provider_id, registration, version));
}

ServiceWorkerHandle::ServiceWorkerHandle(
    base::WeakPtr<ServiceWorkerContextCore> context,
    IPC::Sender* sender,
    int thread_id,
    int provider_id,
    ServiceWorkerRegistration* registration,
    ServiceWorkerVersion* version)
    : context_(context),
      sender_(sender),
      thread_id_(thread_id),
      provider_id_(provider_id),
      handle_id_(context.get() ? context->GetNewServiceWorkerHandleId() : -1),
      ref_count_(1),
      registration_(registration),
      version_(version) {
  version_->AddListener(this);
}

ServiceWorkerHandle::~ServiceWorkerHandle() {
  version_->RemoveListener(this);
  // TODO(kinuko): At this point we can discard the registration if
  // all documents/handles that have a reference to the registration is
  // closed or freed up, but could also keep it alive in cache
  // (e.g. in context_) for a while with some timer so that we don't
  // need to re-load the same registration from disk over and over.
}

void ServiceWorkerHandle::OnWorkerStarted(ServiceWorkerVersion* version) {
}

void ServiceWorkerHandle::OnWorkerStopped(ServiceWorkerVersion* version) {
}

void ServiceWorkerHandle::OnErrorReported(ServiceWorkerVersion* version,
                                          const base::string16& error_message,
                                          int line_number,
                                          int column_number,
                                          const GURL& source_url) {
}

void ServiceWorkerHandle::OnReportConsoleMessage(ServiceWorkerVersion* version,
                                                 int source_identifier,
                                                 int message_level,
                                                 const base::string16& message,
                                                 int line_number,
                                                 const GURL& source_url) {
}

void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion* version) {
  sender_->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
      thread_id_, handle_id_, GetWebServiceWorkerState(version)));
}

ServiceWorkerObjectInfo ServiceWorkerHandle::GetObjectInfo() {
  ServiceWorkerObjectInfo info;
  info.handle_id = handle_id_;
  info.scope = registration_->pattern();
  info.url = version_->script_url();
  info.state = GetWebServiceWorkerState(version_.get());
  return info;
}

void ServiceWorkerHandle::IncrementRefCount() {
  DCHECK_GT(ref_count_, 0);
  ++ref_count_;
}

void ServiceWorkerHandle::DecrementRefCount() {
  DCHECK_GE(ref_count_, 0);
  --ref_count_;
}

}  // namespace content