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