Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_handle.cc
blobf39904e4035db9db50d9d4278219bc8435c4c7f0
1 // Copyright 2014 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.
5 #include "content/browser/service_worker/service_worker_handle.h"
7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_registration.h"
9 #include "content/common/service_worker/service_worker_messages.h"
10 #include "ipc/ipc_sender.h"
12 namespace content {
14 namespace {
16 blink::WebServiceWorkerState
17 GetWebServiceWorkerState(ServiceWorkerVersion* version) {
18 DCHECK(version);
19 switch (version->status()) {
20 case ServiceWorkerVersion::NEW:
21 return blink::WebServiceWorkerStateUnknown;
22 case ServiceWorkerVersion::INSTALLING:
23 return blink::WebServiceWorkerStateInstalling;
24 case ServiceWorkerVersion::INSTALLED:
25 return blink::WebServiceWorkerStateInstalled;
26 case ServiceWorkerVersion::ACTIVATING:
27 return blink::WebServiceWorkerStateActivating;
28 case ServiceWorkerVersion::ACTIVATED:
29 return blink::WebServiceWorkerStateActivated;
30 case ServiceWorkerVersion::REDUNDANT:
31 return blink::WebServiceWorkerStateRedundant;
33 NOTREACHED() << version->status();
34 return blink::WebServiceWorkerStateUnknown;
37 } // namespace
39 scoped_ptr<ServiceWorkerHandle> ServiceWorkerHandle::Create(
40 base::WeakPtr<ServiceWorkerContextCore> context,
41 IPC::Sender* sender,
42 int thread_id,
43 int provider_id,
44 ServiceWorkerVersion* version) {
45 if (!context || !version)
46 return scoped_ptr<ServiceWorkerHandle>();
47 ServiceWorkerRegistration* registration =
48 context->GetLiveRegistration(version->registration_id());
49 return make_scoped_ptr(new ServiceWorkerHandle(
50 context, sender, thread_id, provider_id, registration, version));
53 ServiceWorkerHandle::ServiceWorkerHandle(
54 base::WeakPtr<ServiceWorkerContextCore> context,
55 IPC::Sender* sender,
56 int thread_id,
57 int provider_id,
58 ServiceWorkerRegistration* registration,
59 ServiceWorkerVersion* version)
60 : context_(context),
61 sender_(sender),
62 thread_id_(thread_id),
63 provider_id_(provider_id),
64 handle_id_(context.get() ? context->GetNewServiceWorkerHandleId() : -1),
65 ref_count_(1),
66 registration_(registration),
67 version_(version) {
68 version_->AddListener(this);
71 ServiceWorkerHandle::~ServiceWorkerHandle() {
72 version_->RemoveListener(this);
73 // TODO(kinuko): At this point we can discard the registration if
74 // all documents/handles that have a reference to the registration is
75 // closed or freed up, but could also keep it alive in cache
76 // (e.g. in context_) for a while with some timer so that we don't
77 // need to re-load the same registration from disk over and over.
80 void ServiceWorkerHandle::OnWorkerStarted(ServiceWorkerVersion* version) {
83 void ServiceWorkerHandle::OnWorkerStopped(ServiceWorkerVersion* version) {
86 void ServiceWorkerHandle::OnErrorReported(ServiceWorkerVersion* version,
87 const base::string16& error_message,
88 int line_number,
89 int column_number,
90 const GURL& source_url) {
93 void ServiceWorkerHandle::OnReportConsoleMessage(ServiceWorkerVersion* version,
94 int source_identifier,
95 int message_level,
96 const base::string16& message,
97 int line_number,
98 const GURL& source_url) {
101 void ServiceWorkerHandle::OnVersionStateChanged(ServiceWorkerVersion* version) {
102 sender_->Send(new ServiceWorkerMsg_ServiceWorkerStateChanged(
103 thread_id_, handle_id_, GetWebServiceWorkerState(version)));
106 ServiceWorkerObjectInfo ServiceWorkerHandle::GetObjectInfo() {
107 ServiceWorkerObjectInfo info;
108 info.handle_id = handle_id_;
109 info.scope = registration_->pattern();
110 info.url = version_->script_url();
111 info.state = GetWebServiceWorkerState(version_.get());
112 info.version_id = version_->version_id();
113 return info;
116 void ServiceWorkerHandle::IncrementRefCount() {
117 DCHECK_GT(ref_count_, 0);
118 ++ref_count_;
121 void ServiceWorkerHandle::DecrementRefCount() {
122 DCHECK_GE(ref_count_, 0);
123 --ref_count_;
126 } // namespace content