Home | History | Annotate | Download | only in serviceworkers
      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 #ifndef RespondWithObserver_h
      6 #define RespondWithObserver_h
      7 
      8 #include "core/dom/ContextLifecycleObserver.h"
      9 #include "wtf/Forward.h"
     10 #include "wtf/RefCounted.h"
     11 
     12 namespace WebCore {
     13 
     14 class ExecutionContext;
     15 class Response;
     16 class ScriptState;
     17 class ScriptValue;
     18 
     19 // This class observes the service worker's handling of a FetchEvent and
     20 // notifies the client.
     21 class RespondWithObserver FINAL : public ContextLifecycleObserver, public RefCounted<RespondWithObserver> {
     22 public:
     23     static PassRefPtr<RespondWithObserver> create(ExecutionContext*, int eventID);
     24     ~RespondWithObserver();
     25 
     26     virtual void contextDestroyed() OVERRIDE;
     27 
     28     void didDispatchEvent();
     29 
     30     // Observes the promise and delays calling didHandleFetchEvent() until the
     31     // given promise is resolved or rejected.
     32     void respondWith(ScriptState*, const ScriptValue&);
     33 
     34     void responseWasRejected();
     35     void responseWasFulfilled(const ScriptValue&);
     36 
     37 private:
     38     class ThenFunction;
     39 
     40     RespondWithObserver(ExecutionContext*, int eventID);
     41 
     42     // Sends a response back to the client. The null response means to fallback
     43     // to native.
     44     void sendResponse(PassRefPtr<Response>);
     45 
     46     int m_eventID;
     47 
     48     enum State { Initial, Pending, Done };
     49     State m_state;
     50 };
     51 
     52 } // namespace WebCore
     53 
     54 #endif // RespondWithObserver_h
     55