electron/atom/browser/net/atom_url_request.h

92 lines
3.3 KiB
C
Raw Normal View History

// 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>
#include "atom/browser/api/atom_api_url_request.h"
#include "atom/browser/atom_browser_context.h"
#include "base/memory/ref_counted.h"
#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"
#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"
namespace atom {
class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
public net::URLRequest::Delegate {
2016-09-29 09:31:08 +00:00
public:
2016-09-21 15:35:03 +00:00
static scoped_refptr<AtomURLRequest> Create(
AtomBrowserContext* browser_context,
2016-09-21 15:35:03 +00:00
const std::string& method,
const std::string& url,
base::WeakPtr<api::URLRequest> delegate);
bool Write(scoped_refptr<const net::IOBufferWithSize> buffer,
2016-09-26 12:59:53 +00:00
bool is_last);
void SetChunkedUpload(bool is_chunked_upload);
void Cancel() const;
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,
const base::string16& password) const;
2016-09-29 09:31:08 +00:00
protected:
// Overrides of net::URLRequest::Delegate
2016-09-29 09:31:08 +00:00
void OnAuthRequired(net::URLRequest* request,
2016-09-21 15:35:03 +00:00
net::AuthChallengeInfo* auth_info) override;
2016-09-29 09:31:08 +00:00
void OnResponseStarted(net::URLRequest* request) override;
void OnReadCompleted(net::URLRequest* request,
2016-09-21 15:35:03 +00:00
int bytes_read) override;
2016-09-29 09:31:08 +00:00
private:
friend class base::RefCountedThreadSafe<AtomURLRequest>;
explicit AtomURLRequest(base::WeakPtr<api::URLRequest> delegate);
~AtomURLRequest()override;
2016-09-26 12:59:53 +00:00
void DoWriteBuffer(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last);
void DoCancel() const;
2016-09-21 15:35:03 +00:00
void DoSetAuth(const base::string16& username,
const base::string16& password) const;
void DoCancelAuth() const;
void ReadResponse();
bool CopyAndPostBuffer(int bytes_read);
2016-09-21 15:35:03 +00:00
void InformDelegateAuthenticationRequired(
scoped_refptr<net::AuthChallengeInfo> auth_info) const;
void InformDelegateResponseStarted(
scoped_refptr<const net::HttpResponseHeaders>) const;
2016-09-21 15:35:03 +00:00
void InformDelegateResponseData(
scoped_refptr<net::IOBufferWithSize> data) const;
void InformDelegateResponseCompleted() const;
void InformDelegateRequestErrorOccured(const std::string& error) const;
void InformDelegateResponseErrorOccured(const std::string& error) const;
base::WeakPtr<api::URLRequest> delegate_;
2016-09-21 15:35:03 +00:00
std::unique_ptr<net::URLRequest> request_;
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>>
upload_element_readers_;
2016-09-26 12:03:49 +00:00
scoped_refptr<net::IOBuffer> response_read_buffer_;
2016-09-29 09:31:08 +00:00
DISALLOW_COPY_AND_ASSIGN(AtomURLRequest);
};
} // namespace atom
2016-09-29 09:31:08 +00:00
#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_