2016-09-19 09:21:09 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
|
|
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
|
|
|
|
#define ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-09-30 12:17:29 +00:00
|
|
|
#include "atom/browser/api/atom_api_url_request.h"
|
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2016-09-19 09:21:09 +00:00
|
|
|
#include "base/memory/ref_counted.h"
|
2016-09-30 12:17:29 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
|
|
|
#include "net/base/auth.h"
|
2016-09-26 12:03:49 +00:00
|
|
|
#include "net/base/chunked_upload_data_stream.h"
|
2016-09-30 12:17:29 +00:00
|
|
|
#include "net/base/io_buffer.h"
|
|
|
|
#include "net/base/upload_element_reader.h"
|
|
|
|
#include "net/http/http_response_headers.h"
|
2016-09-29 09:31:08 +00:00
|
|
|
#include "net/url_request/url_request.h"
|
2016-10-14 15:37:39 +00:00
|
|
|
#include "net/url_request/url_request_context_getter_observer.h"
|
2016-09-19 09:21:09 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
|
2016-10-14 15:37:39 +00:00
|
|
|
public net::URLRequest::Delegate,
|
|
|
|
public net::URLRequestContextGetterObserver {
|
2016-09-29 09:31:08 +00:00
|
|
|
public:
|
2016-09-21 15:35:03 +00:00
|
|
|
static scoped_refptr<AtomURLRequest> Create(
|
2016-10-14 08:58:16 +00:00
|
|
|
AtomBrowserContext* browser_context,
|
|
|
|
const std::string& method,
|
|
|
|
const std::string& url,
|
|
|
|
base::WeakPtr<api::URLRequest> delegate);
|
2016-10-14 15:37:39 +00:00
|
|
|
void Terminate();
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-10-14 09:50:47 +00:00
|
|
|
bool Write(scoped_refptr<const net::IOBufferWithSize> buffer, bool is_last);
|
2016-09-27 08:21:11 +00:00
|
|
|
void SetChunkedUpload(bool is_chunked_upload);
|
2016-10-14 15:37:39 +00:00
|
|
|
void Cancel();
|
2016-09-27 08:21:11 +00:00
|
|
|
void SetExtraHeader(const std::string& name, const std::string& value) const;
|
|
|
|
void RemoveExtraHeader(const std::string& name) const;
|
2016-09-21 15:35:03 +00:00
|
|
|
void PassLoginInformation(const base::string16& username,
|
2016-10-14 08:58:16 +00:00
|
|
|
const base::string16& password) const;
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
protected:
|
2016-09-19 09:21:09 +00:00
|
|
|
// Overrides of net::URLRequest::Delegate
|
2016-09-29 09:31:08 +00:00
|
|
|
void OnAuthRequired(net::URLRequest* request,
|
2016-10-14 08:58:16 +00:00
|
|
|
net::AuthChallengeInfo* auth_info) override;
|
2016-09-29 09:31:08 +00:00
|
|
|
void OnResponseStarted(net::URLRequest* request) override;
|
2016-10-14 08:58:16 +00:00
|
|
|
void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-10-14 15:37:39 +00:00
|
|
|
// Overrides of net::URLRequestContextGetterObserver
|
|
|
|
void OnContextShuttingDown() override;
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
private:
|
2016-09-19 09:21:09 +00:00
|
|
|
friend class base::RefCountedThreadSafe<AtomURLRequest>;
|
2016-09-30 12:17:29 +00:00
|
|
|
|
2016-10-04 15:12:17 +00:00
|
|
|
explicit AtomURLRequest(base::WeakPtr<api::URLRequest> delegate);
|
2016-10-14 09:50:47 +00:00
|
|
|
~AtomURLRequest() override;
|
2016-09-30 12:17:29 +00:00
|
|
|
|
2016-10-13 15:14:23 +00:00
|
|
|
void DoInitialize(scoped_refptr<net::URLRequestContextGetter>,
|
|
|
|
const std::string& method,
|
|
|
|
const std::string& url);
|
2016-10-14 15:37:39 +00:00
|
|
|
void DoTerminate();
|
2016-09-26 12:59:53 +00:00
|
|
|
void DoWriteBuffer(scoped_refptr<const net::IOBufferWithSize> buffer,
|
|
|
|
bool is_last);
|
2016-10-14 15:37:39 +00:00
|
|
|
void DoCancel();
|
2016-10-13 15:14:23 +00:00
|
|
|
void DoSetExtraHeader(const std::string& name,
|
|
|
|
const std::string& value) const;
|
|
|
|
void DoRemoveExtraHeader(const std::string& name) const;
|
2016-09-21 15:35:03 +00:00
|
|
|
void DoSetAuth(const base::string16& username,
|
2016-10-14 08:58:16 +00:00
|
|
|
const base::string16& password) const;
|
2016-09-21 15:35:03 +00:00
|
|
|
void DoCancelAuth() const;
|
2016-10-14 15:37:39 +00:00
|
|
|
void DoCancelWithError(const std::string& error, bool isRequestError);
|
2016-09-21 07:23:00 +00:00
|
|
|
|
|
|
|
void ReadResponse();
|
|
|
|
bool CopyAndPostBuffer(int bytes_read);
|
|
|
|
|
2016-09-21 15:35:03 +00:00
|
|
|
void InformDelegateAuthenticationRequired(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<net::AuthChallengeInfo> auth_info) const;
|
2016-10-04 15:33:34 +00:00
|
|
|
void InformDelegateResponseStarted(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<net::HttpResponseHeaders>) const;
|
2016-09-21 15:35:03 +00:00
|
|
|
void InformDelegateResponseData(
|
2016-10-14 08:58:16 +00:00
|
|
|
scoped_refptr<net::IOBufferWithSize> data) const;
|
2016-09-21 15:35:03 +00:00
|
|
|
void InformDelegateResponseCompleted() const;
|
2016-10-14 15:37:39 +00:00
|
|
|
void InformDelegateErrorOccured(const std::string& error,
|
|
|
|
bool isRequestError) const;
|
2016-09-19 09:21:09 +00:00
|
|
|
|
|
|
|
base::WeakPtr<api::URLRequest> delegate_;
|
2016-09-21 15:35:03 +00:00
|
|
|
std::unique_ptr<net::URLRequest> request_;
|
2016-10-14 15:37:39 +00:00
|
|
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
|
2016-09-26 12:03:49 +00:00
|
|
|
|
|
|
|
bool is_chunked_upload_;
|
|
|
|
std::unique_ptr<net::ChunkedUploadDataStream> chunked_stream_;
|
|
|
|
std::unique_ptr<net::ChunkedUploadDataStream::Writer> chunked_stream_writer_;
|
2016-09-29 09:31:08 +00:00
|
|
|
std::vector<std::unique_ptr<net::UploadElementReader>>
|
2016-10-14 09:50:47 +00:00
|
|
|
upload_element_readers_;
|
2016-09-26 12:03:49 +00:00
|
|
|
scoped_refptr<net::IOBuffer> response_read_buffer_;
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AtomURLRequest);
|
|
|
|
};
|
2016-09-19 09:21:09 +00:00
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
2016-09-29 09:31:08 +00:00
|
|
|
#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
|