File: WaitUntilObserver.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (214 lines) | stat: -rw-r--r-- 7,265 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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 "modules/serviceworkers/WaitUntilObserver.h"

#include "bindings/core/v8/ScriptFunction.h"
#include "bindings/core/v8/ScriptPromise.h"
#include "bindings/core/v8/ScriptValue.h"
#include "bindings/core/v8/V8Binding.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "modules/serviceworkers/ServiceWorkerGlobalScope.h"
#include "platform/LayoutTestSupport.h"
#include "public/platform/Platform.h"
#include "public/platform/modules/serviceworker/WebServiceWorkerEventResult.h"
#include "wtf/Assertions.h"
#include <v8.h>

namespace blink {

namespace {

// Timeout before a service worker that was given window interaction
// permission loses them. The unit is seconds.
const unsigned kWindowInteractionTimeout = 10;
const unsigned kWindowInteractionTimeoutForTest = 1;

unsigned windowInteractionTimeout() {
  return LayoutTestSupport::isRunningLayoutTest()
             ? kWindowInteractionTimeoutForTest
             : kWindowInteractionTimeout;
}

}  // anonymous namespace

class WaitUntilObserver::ThenFunction final : public ScriptFunction {
 public:
  enum ResolveType {
    Fulfilled,
    Rejected,
  };

  static v8::Local<v8::Function> createFunction(ScriptState* scriptState,
                                                WaitUntilObserver* observer,
                                                ResolveType type) {
    ThenFunction* self = new ThenFunction(scriptState, observer, type);
    return self->bindToV8Function();
  }

  DEFINE_INLINE_VIRTUAL_TRACE() {
    visitor->trace(m_observer);
    ScriptFunction::trace(visitor);
  }

 private:
  ThenFunction(ScriptState* scriptState,
               WaitUntilObserver* observer,
               ResolveType type)
      : ScriptFunction(scriptState),
        m_observer(observer),
        m_resolveType(type) {}

  ScriptValue call(ScriptValue value) override {
    ASSERT(m_observer);
    ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
    if (m_resolveType == Rejected) {
      m_observer->reportError(value);
      value =
          ScriptPromise::reject(value.getScriptState(), value).getScriptValue();
    }
    m_observer->decrementPendingActivity();
    m_observer = nullptr;
    return value;
  }

  Member<WaitUntilObserver> m_observer;
  ResolveType m_resolveType;
};

WaitUntilObserver* WaitUntilObserver::create(ExecutionContext* context,
                                             EventType type,
                                             int eventID) {
  return new WaitUntilObserver(context, type, eventID);
}

void WaitUntilObserver::willDispatchEvent() {
  m_eventDispatchTime = WTF::currentTime();
  // When handling a notificationclick event, we want to allow one window to
  // be focused or opened. These calls are allowed between the call to
  // willDispatchEvent() and the last call to decrementPendingActivity(). If
  // waitUntil() isn't called, that means between willDispatchEvent() and
  // didDispatchEvent().
  if (m_type == NotificationClick)
    m_executionContext->allowWindowInteraction();

  incrementPendingActivity();
}

void WaitUntilObserver::didDispatchEvent(bool errorOccurred) {
  if (errorOccurred)
    m_hasError = true;
  decrementPendingActivity();
  m_eventDispatched = true;
}

void WaitUntilObserver::waitUntil(ScriptState* scriptState,
                                  ScriptPromise scriptPromise,
                                  ExceptionState& exceptionState) {
  if (m_eventDispatched) {
    exceptionState.throwDOMException(InvalidStateError,
                                     "The event handler is already finished.");
    return;
  }

  if (!m_executionContext)
    return;

  // When handling a notificationclick event, we want to allow one window to
  // be focused or opened. See comments in ::willDispatchEvent(). When
  // waitUntil() is being used, opening or closing a window must happen in a
  // timeframe specified by windowInteractionTimeout(), otherwise the calls
  // will fail.
  if (m_type == NotificationClick)
    m_consumeWindowInteractionTimer.startOneShot(windowInteractionTimeout(),
                                                 BLINK_FROM_HERE);

  incrementPendingActivity();
  scriptPromise.then(
      ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled),
      ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected));
}

WaitUntilObserver::WaitUntilObserver(ExecutionContext* context,
                                     EventType type,
                                     int eventID)
    : m_executionContext(context),
      m_type(type),
      m_eventID(eventID),
      m_consumeWindowInteractionTimer(
          Platform::current()->currentThread()->getWebTaskRunner(),
          this,
          &WaitUntilObserver::consumeWindowInteraction) {}

void WaitUntilObserver::reportError(const ScriptValue& value) {
  // FIXME: Propagate error message to the client for onerror handling.
  NOTIMPLEMENTED();

  m_hasError = true;
}

void WaitUntilObserver::incrementPendingActivity() {
  ++m_pendingActivity;
}

void WaitUntilObserver::decrementPendingActivity() {
  ASSERT(m_pendingActivity > 0);
  if (!m_executionContext || (!m_hasError && --m_pendingActivity))
    return;

  ServiceWorkerGlobalScopeClient* client =
      ServiceWorkerGlobalScopeClient::from(m_executionContext);
  WebServiceWorkerEventResult result =
      m_hasError ? WebServiceWorkerEventResultRejected
                 : WebServiceWorkerEventResultCompleted;
  switch (m_type) {
    case Activate:
      client->didHandleActivateEvent(m_eventID, result, m_eventDispatchTime);
      break;
    case Fetch:
      client->didHandleFetchEvent(m_eventID, result, m_eventDispatchTime);
      break;
    case Install:
      client->didHandleInstallEvent(m_eventID, result, m_eventDispatchTime);
      break;
    case Message:
      client->didHandleExtendableMessageEvent(m_eventID, result,
                                              m_eventDispatchTime);
      break;
    case NotificationClick:
      client->didHandleNotificationClickEvent(m_eventID, result,
                                              m_eventDispatchTime);
      m_consumeWindowInteractionTimer.stop();
      consumeWindowInteraction(nullptr);
      break;
    case NotificationClose:
      client->didHandleNotificationCloseEvent(m_eventID, result,
                                              m_eventDispatchTime);
      break;
    case Push:
      client->didHandlePushEvent(m_eventID, result, m_eventDispatchTime);
      break;
    case Sync:
      client->didHandleSyncEvent(m_eventID, result, m_eventDispatchTime);
      break;
    case PaymentRequest:
      client->didHandlePaymentRequestEvent(m_eventID, result,
                                           m_eventDispatchTime);
      break;
  }
  m_executionContext = nullptr;
}

void WaitUntilObserver::consumeWindowInteraction(TimerBase*) {
  if (!m_executionContext)
    return;
  m_executionContext->consumeWindowInteraction();
}

DEFINE_TRACE(WaitUntilObserver) {
  visitor->trace(m_executionContext);
}

}  // namespace blink