Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / serviceworkers / WaitUntilObserver.cpp
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 "config.h"
6 #include "modules/serviceworkers/WaitUntilObserver.h"
7
8 #include "bindings/core/v8/ScriptFunction.h"
9 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptValue.h"
11 #include "bindings/core/v8/V8Binding.h"
12 #include "core/dom/ExecutionContext.h"
13 #include "platform/NotImplemented.h"
14 #include "public/platform/WebServiceWorkerEventResult.h"
15 #include "wtf/Assertions.h"
16 #include "wtf/RefCounted.h"
17 #include "wtf/RefPtr.h"
18 #include <v8.h>
19
20 namespace blink {
21
22 class WaitUntilObserver::ThenFunction FINAL : public ScriptFunction {
23 public:
24     enum ResolveType {
25         Fulfilled,
26         Rejected,
27     };
28
29     static PassOwnPtr<ScriptFunction> create(PassRefPtr<WaitUntilObserver> observer, ResolveType type)
30     {
31         ExecutionContext* executionContext = observer->executionContext();
32         return adoptPtr(new ThenFunction(toIsolate(executionContext), observer, type));
33     }
34
35 private:
36     ThenFunction(v8::Isolate* isolate, PassRefPtr<WaitUntilObserver> observer, ResolveType type)
37         : ScriptFunction(isolate)
38         , m_observer(observer)
39         , m_resolveType(type)
40     {
41     }
42
43     virtual ScriptValue call(ScriptValue value) OVERRIDE
44     {
45         ASSERT(m_observer);
46         ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
47         if (m_resolveType == Rejected)
48             m_observer->reportError(value);
49         m_observer->decrementPendingActivity();
50         m_observer = nullptr;
51         return value;
52     }
53
54     RefPtr<WaitUntilObserver> m_observer;
55     ResolveType m_resolveType;
56 };
57
58 PassRefPtr<WaitUntilObserver> WaitUntilObserver::create(ExecutionContext* context, EventType type, int eventID)
59 {
60     return adoptRef(new WaitUntilObserver(context, type, eventID));
61 }
62
63 WaitUntilObserver::~WaitUntilObserver()
64 {
65 }
66
67 void WaitUntilObserver::willDispatchEvent()
68 {
69     incrementPendingActivity();
70 }
71
72 void WaitUntilObserver::didDispatchEvent()
73 {
74     decrementPendingActivity();
75 }
76
77 void WaitUntilObserver::waitUntil(ScriptState* scriptState, const ScriptValue& value)
78 {
79     incrementPendingActivity();
80     ScriptPromise::cast(scriptState, value).then(
81         ThenFunction::create(this, ThenFunction::Fulfilled),
82         ThenFunction::create(this, ThenFunction::Rejected));
83 }
84
85 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, EventType type, int eventID)
86     : ContextLifecycleObserver(context)
87     , m_type(type)
88     , m_eventID(eventID)
89     , m_pendingActivity(0)
90     , m_hasError(false)
91 {
92 }
93
94 void WaitUntilObserver::reportError(const ScriptValue& value)
95 {
96     // FIXME: Propagate error message to the client for onerror handling.
97     notImplemented();
98
99     m_hasError = true;
100 }
101
102 void WaitUntilObserver::incrementPendingActivity()
103 {
104     ++m_pendingActivity;
105 }
106
107 void WaitUntilObserver::decrementPendingActivity()
108 {
109     ASSERT(m_pendingActivity > 0);
110     if (!executionContext() || (!m_hasError && --m_pendingActivity))
111         return;
112
113     ServiceWorkerGlobalScopeClient* client = ServiceWorkerGlobalScopeClient::from(executionContext());
114     WebServiceWorkerEventResult result = m_hasError ? WebServiceWorkerEventResultRejected : WebServiceWorkerEventResultCompleted;
115     switch (m_type) {
116     case Activate:
117         client->didHandleActivateEvent(m_eventID, result);
118         break;
119     case Install:
120         client->didHandleInstallEvent(m_eventID, result);
121         break;
122     }
123     observeContext(0);
124 }
125
126 } // namespace blink