File: XMLHttpRequestWorker.h

package info (click to toggle)
thunderbird 1:60.8.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,339,208 kB
  • sloc: cpp: 5,456,704; ansic: 2,360,384; python: 596,095; asm: 340,963; java: 326,291; xml: 258,664; sh: 84,366; makefile: 23,702; perl: 17,317; objc: 3,768; yacc: 1,766; ada: 1,681; lex: 1,364; pascal: 1,264; cs: 879; exp: 527; php: 436; lisp: 258; ruby: 153; awk: 152; sed: 53; csh: 27
file content (248 lines) | stat: -rw-r--r-- 7,791 bytes parent folder | download | duplicates (7)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_XMLHttpRequestWorker_h
#define mozilla_dom_XMLHttpRequestWorker_h

#include "XMLHttpRequest.h"
#include "XMLHttpRequestString.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/WorkerHolder.h"

namespace mozilla {
namespace dom {

class Proxy;
class SendRunnable;
class DOMString;
class WorkerPrivate;

class XMLHttpRequestWorker final : public XMLHttpRequest, public WorkerHolder {
 public:
  struct StateData {
    XMLHttpRequestStringSnapshot mResponseText;
    nsString mResponseURL;
    uint32_t mStatus;
    nsCString mStatusText;
    uint16_t mReadyState;
    bool mFlagSend;
    JS::Heap<JS::Value> mResponse;
    nsresult mResponseTextResult;
    nsresult mStatusResult;
    nsresult mResponseResult;

    StateData()
        : mStatus(0),
          mReadyState(0),
          mFlagSend(false),
          mResponse(JS::UndefinedValue()),
          mResponseTextResult(NS_OK),
          mStatusResult(NS_OK),
          mResponseResult(NS_OK) {}

    void trace(JSTracer* trc);
  };

 private:
  RefPtr<XMLHttpRequestUpload> mUpload;
  WorkerPrivate* mWorkerPrivate;
  RefPtr<Proxy> mProxy;
  XMLHttpRequestResponseType mResponseType;
  StateData mStateData;

  uint32_t mTimeout;

  bool mRooted;
  bool mBackgroundRequest;
  bool mWithCredentials;
  bool mCanceled;

  bool mMozAnon;
  bool mMozSystem;

 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequestWorker,
                                                         XMLHttpRequest)

  static already_AddRefed<XMLHttpRequest> Construct(
      const GlobalObject& aGlobal, const MozXMLHttpRequestParameters& aParams,
      ErrorResult& aRv);

  void Unpin();

  bool Notify(WorkerStatus aStatus) override;

  virtual uint16_t ReadyState() const override {
    return mStateData.mReadyState;
  }

  virtual void Open(const nsACString& aMethod, const nsAString& aUrl,
                    ErrorResult& aRv) override {
    Open(aMethod, aUrl, true, Optional<nsAString>(), Optional<nsAString>(),
         aRv);
  }

  virtual void Open(const nsACString& aMethod, const nsAString& aUrl,
                    bool aAsync, const nsAString& aUsername,
                    const nsAString& aPassword, ErrorResult& aRv) override {
    Optional<nsAString> username;
    username = &aUsername;
    Optional<nsAString> password;
    password = &aPassword;
    Open(aMethod, aUrl, aAsync, username, password, aRv);
  }

  void Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync,
            const Optional<nsAString>& aUser,
            const Optional<nsAString>& aPassword, ErrorResult& aRv);

  virtual void SetRequestHeader(const nsACString& aHeader,
                                const nsACString& aValue,
                                ErrorResult& aRv) override;

  virtual uint32_t Timeout() const override { return mTimeout; }

  virtual void SetTimeout(uint32_t aTimeout, ErrorResult& aRv) override;

  virtual bool WithCredentials() const override { return mWithCredentials; }

  virtual void SetWithCredentials(bool aWithCredentials,
                                  ErrorResult& aRv) override;

  virtual bool MozBackgroundRequest() const override {
    return mBackgroundRequest;
  }

  virtual void SetMozBackgroundRequest(bool aBackgroundRequest,
                                       ErrorResult& aRv) override;

  virtual nsIChannel* GetChannel() const override {
    MOZ_CRASH("This method cannot be called on workers.");
  }

  virtual void GetNetworkInterfaceId(nsACString& aId) const override {
    MOZ_CRASH("This method cannot be called on workers.");
  }

  virtual void SetNetworkInterfaceId(const nsACString& aId) override {
    MOZ_CRASH("This method cannot be called on workers.");
  }

  virtual XMLHttpRequestUpload* GetUpload(ErrorResult& aRv) override;

  virtual void Send(
      JSContext* aCx,
      const Nullable<
          DocumentOrBlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString>&
          aData,
      ErrorResult& aRv) override;

  virtual void SendInputStream(nsIInputStream* aInputStream,
                               ErrorResult& aRv) override {
    MOZ_CRASH("nsIInputStream is not a valid argument for XHR in workers.");
  }

  virtual void Abort(ErrorResult& aRv) override;

  virtual void GetResponseURL(nsAString& aUrl) override {
    aUrl = mStateData.mResponseURL;
  }

  uint32_t GetStatus(ErrorResult& aRv) override {
    aRv = mStateData.mStatusResult;
    return mStateData.mStatus;
  }

  virtual void GetStatusText(nsACString& aStatusText,
                             ErrorResult& aRv) override {
    aStatusText = mStateData.mStatusText;
  }

  virtual void GetResponseHeader(const nsACString& aHeader,
                                 nsACString& aResponseHeader,
                                 ErrorResult& aRv) override;

  virtual void GetAllResponseHeaders(nsACString& aResponseHeaders,
                                     ErrorResult& aRv) override;

  virtual void OverrideMimeType(const nsAString& aMimeType,
                                ErrorResult& aRv) override;

  virtual XMLHttpRequestResponseType ResponseType() const override {
    return mResponseType;
  }

  virtual void SetResponseType(XMLHttpRequestResponseType aResponseType,
                               ErrorResult& aRv) override;

  virtual void GetResponse(JSContext* /* unused */,
                           JS::MutableHandle<JS::Value> aResponse,
                           ErrorResult& aRv) override;

  virtual void GetResponseText(DOMString& aResponseText,
                               ErrorResult& aRv) override;

  virtual nsIDocument* GetResponseXML(ErrorResult& aRv) override {
    MOZ_CRASH("This method should not be called.");
  }

  virtual void GetInterface(JSContext* aCx, nsIJSID* aIID,
                            JS::MutableHandle<JS::Value> aRetval,
                            ErrorResult& aRv) override {
    aRv.Throw(NS_ERROR_FAILURE);
  }

  virtual void SetOriginAttributes(
      const mozilla::dom::OriginAttributesDictionary& aAttrs) override {
    MOZ_CRASH("This method cannot be called on workers.");
  }

  XMLHttpRequestUpload* GetUploadObjectNoCreate() const { return mUpload; }

  void UpdateState(const StateData& aStateData,
                   bool aUseCachedArrayBufferResponse);

  void NullResponseText() {
    mStateData.mResponseText.SetVoid();
    mStateData.mResponse.setNull();
  }

  virtual uint16_t ErrorCode() const override {
    return 0;  // eOK
  }

  virtual bool MozAnon() const override { return mMozAnon; }

  virtual bool MozSystem() const override { return mMozSystem; }

  bool SendInProgress() const { return mRooted; }

 private:
  explicit XMLHttpRequestWorker(WorkerPrivate* aWorkerPrivate);
  ~XMLHttpRequestWorker();

  enum ReleaseType { Default, XHRIsGoingAway, WorkerIsGoingAway };

  void ReleaseProxy(ReleaseType aType = Default);

  void MaybePin(ErrorResult& aRv);

  void MaybeDispatchPrematureAbortEvents(ErrorResult& aRv);

  void DispatchPrematureAbortEvent(EventTarget* aTarget,
                                   const nsAString& aEventType,
                                   bool aUploadTarget, ErrorResult& aRv);

  void Send(JSContext* aCx, JS::Handle<JSObject*> aBody, ErrorResult& aRv);

  void SendInternal(SendRunnable* aRunnable, ErrorResult& aRv);
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_workers_xmlhttprequest_h__