Upstream version 5.34.104.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/v8/ScriptFunction.h"
9 #include "bindings/v8/ScriptPromise.h"
10 #include "bindings/v8/ScriptValue.h"
11 #include "core/dom/ExecutionContext.h"
12 #include "platform/NotImplemented.h"
13 #include "wtf/Assertions.h"
14 #include "wtf/RefCounted.h"
15 #include "wtf/RefPtr.h"
16
17 namespace WebCore {
18
19 class WaitUntilObserver::ThenFunction FINAL : public ScriptFunction {
20 public:
21     enum ResolveType {
22         Fulfilled,
23         Rejected,
24     };
25
26     static PassOwnPtr<ScriptFunction> create(PassRefPtr<WaitUntilObserver> observer, ResolveType type)
27     {
28         ExecutionContext* executionContext = observer->executionContext();
29         return adoptPtr(new ThenFunction(toIsolate(executionContext), observer, type));
30     }
31
32 private:
33     ThenFunction(v8::Isolate* isolate, PassRefPtr<WaitUntilObserver> observer, ResolveType type)
34         : ScriptFunction(isolate)
35         , m_observer(observer)
36         , m_resolveType(type)
37     {
38     }
39
40     virtual ScriptValue call(ScriptValue value) OVERRIDE
41     {
42         ASSERT(m_observer);
43         ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
44         if (m_resolveType == Rejected)
45             m_observer->reportError(value);
46         m_observer->decrementPendingActivity();
47         m_observer = 0;
48         return value;
49     }
50
51     RefPtr<WaitUntilObserver> m_observer;
52     ResolveType m_resolveType;
53 };
54
55 PassRefPtr<WaitUntilObserver> WaitUntilObserver::create(ExecutionContext* context, int eventID)
56 {
57     return adoptRef(new WaitUntilObserver(context, eventID));
58 }
59
60 WaitUntilObserver::~WaitUntilObserver()
61 {
62     ASSERT(!m_pendingActivity);
63 }
64
65 void WaitUntilObserver::willDispatchEvent()
66 {
67     incrementPendingActivity();
68 }
69
70 void WaitUntilObserver::didDispatchEvent()
71 {
72     decrementPendingActivity();
73 }
74
75 void WaitUntilObserver::waitUntil(const ScriptValue& value)
76 {
77     incrementPendingActivity();
78     ScriptPromise(value).then(
79         ThenFunction::create(this, ThenFunction::Fulfilled),
80         ThenFunction::create(this, ThenFunction::Rejected));
81 }
82
83 WaitUntilObserver::WaitUntilObserver(ExecutionContext* context, int eventID)
84     : ContextLifecycleObserver(context)
85     , m_eventID(eventID)
86     , m_pendingActivity(0)
87 {
88 }
89
90 void WaitUntilObserver::reportError(const ScriptValue& value)
91 {
92     // FIXME: Propagate error message to the client for onerror handling.
93     notImplemented();
94 }
95
96 void WaitUntilObserver::incrementPendingActivity()
97 {
98     ++m_pendingActivity;
99 }
100
101 void WaitUntilObserver::decrementPendingActivity()
102 {
103     ASSERT(m_pendingActivity > 0);
104     if (--m_pendingActivity || !executionContext())
105         return;
106
107     ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleInstallEvent(m_eventID);
108     observeContext(0);
109 }
110
111 } // namespace WebCore