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 Body_h
      6 #define Body_h
      7 
      8 #include "bindings/core/v8/ScriptPromise.h"
      9 #include "bindings/core/v8/ScriptPromiseResolver.h"
     10 #include "bindings/core/v8/ScriptWrappable.h"
     11 #include "core/dom/ActiveDOMObject.h"
     12 #include "core/fileapi/FileReaderLoader.h"
     13 #include "core/fileapi/FileReaderLoaderClient.h"
     14 #include "platform/blob/BlobData.h"
     15 #include "platform/heap/Handle.h"
     16 #include "wtf/RefPtr.h"
     17 
     18 namespace blink {
     19 
     20 class ScriptState;
     21 
     22 class Body
     23     : public GarbageCollectedFinalized<Body>
     24     , public ScriptWrappable
     25     , public ActiveDOMObject
     26     , public FileReaderLoaderClient {
     27     DEFINE_WRAPPERTYPEINFO();
     28 public:
     29     explicit Body(ExecutionContext*);
     30     virtual ~Body() { }
     31     enum ResponseType {
     32         ResponseUnknown,
     33         ResponseAsArrayBuffer,
     34         ResponseAsBlob,
     35         ResponseAsFormData,
     36         ResponseAsJSON,
     37         ResponseAsText
     38     };
     39 
     40     ScriptPromise arrayBuffer(ScriptState*);
     41     ScriptPromise blob(ScriptState*);
     42     ScriptPromise formData(ScriptState*);
     43     ScriptPromise json(ScriptState*);
     44     ScriptPromise text(ScriptState*);
     45 
     46     bool bodyUsed() const;
     47 
     48     // ActiveDOMObject override.
     49     virtual void stop() OVERRIDE;
     50     virtual bool hasPendingActivity() const OVERRIDE;
     51 
     52     virtual void trace(Visitor*) { }
     53 
     54 protected:
     55     // Copy constructor for clone() implementations
     56     explicit Body(const Body&);
     57 
     58     // Sets the bodyUsed flag to true. This signifies that the contents of the
     59     // body have been consumed and cannot be accessed again.
     60     void setBodyUsed();
     61 
     62 private:
     63     ScriptPromise readAsync(ScriptState*, ResponseType);
     64     void resolveJSON();
     65 
     66     // FileReaderLoaderClient functions.
     67     virtual void didStartLoading() OVERRIDE;
     68     virtual void didReceiveData() OVERRIDE;
     69     virtual void didFinishLoading() OVERRIDE;
     70     virtual void didFail(FileError::ErrorCode) OVERRIDE;
     71 
     72     virtual PassRefPtr<BlobDataHandle> blobDataHandle() = 0;
     73 
     74     OwnPtr<FileReaderLoader> m_loader;
     75     bool m_bodyUsed;
     76     ResponseType m_responseType;
     77     RefPtr<ScriptPromiseResolver> m_resolver;
     78 };
     79 
     80 } // namespace blink
     81 
     82 #endif // Body_h
     83