2019-04-23 21:39:21 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// 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_LOADER_FACTORY_H_
|
|
|
|
#define ATOM_BROWSER_NET_ATOM_URL_LOADER_FACTORY_H_
|
|
|
|
|
2019-04-29 02:37:45 +00:00
|
|
|
#include <string>
|
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
#include "mojo/public/cpp/bindings/binding_set.h"
|
2019-05-03 00:48:51 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2019-04-23 21:39:21 +00:00
|
|
|
#include "net/url_request/url_request_job_factory.h"
|
|
|
|
#include "services/network/public/mojom/url_loader_factory.mojom.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2019-04-29 02:37:45 +00:00
|
|
|
// Old Protocol API can only serve one type of response for one scheme.
|
|
|
|
enum class ProtocolType {
|
|
|
|
kBuffer,
|
|
|
|
kString,
|
|
|
|
kFile,
|
|
|
|
kHttp,
|
|
|
|
kStream,
|
|
|
|
kFree, // special type for returning arbitrary type of response.
|
|
|
|
};
|
|
|
|
|
2019-05-03 00:48:51 +00:00
|
|
|
using StartLoadingCallback =
|
|
|
|
base::OnceCallback<void(v8::Local<v8::Value>, mate::Arguments*)>;
|
2019-04-29 02:37:45 +00:00
|
|
|
using ProtocolHandler =
|
2019-05-03 00:48:51 +00:00
|
|
|
base::Callback<void(const network::ResourceRequest&, StartLoadingCallback)>;
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
// Implementation of URLLoaderFactory.
|
|
|
|
class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
|
|
|
|
public:
|
2019-04-29 02:37:45 +00:00
|
|
|
AtomURLLoaderFactory(ProtocolType type, const ProtocolHandler& handler);
|
2019-04-23 21:39:21 +00:00
|
|
|
~AtomURLLoaderFactory() override;
|
|
|
|
|
|
|
|
// network::mojom::URLLoaderFactory:
|
|
|
|
void CreateLoaderAndStart(network::mojom::URLLoaderRequest loader,
|
|
|
|
int32_t routing_id,
|
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
|
|
|
network::mojom::URLLoaderClientPtr client,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag&
|
|
|
|
traffic_annotation) override;
|
|
|
|
void Clone(network::mojom::URLLoaderFactoryRequest request) override;
|
|
|
|
|
|
|
|
private:
|
2019-05-03 00:48:51 +00:00
|
|
|
static void StartLoading(
|
|
|
|
network::mojom::URLLoaderRequest loader,
|
|
|
|
int32_t routing_id,
|
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
|
|
|
network::mojom::URLLoaderClientPtr client,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
|
|
|
ProtocolType type,
|
|
|
|
v8::Local<v8::Value> response,
|
|
|
|
mate::Arguments* args);
|
|
|
|
static void StartLoadingBuffer(network::mojom::URLLoaderClientPtr client,
|
|
|
|
const mate::Dictionary& dict);
|
|
|
|
static void StartLoadingString(network::mojom::URLLoaderClientPtr client,
|
|
|
|
const mate::Dictionary& dict,
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> response);
|
|
|
|
static void StartLoadingFile(network::mojom::URLLoaderRequest loader,
|
|
|
|
network::ResourceRequest request,
|
|
|
|
network::mojom::URLLoaderClientPtr client,
|
|
|
|
const mate::Dictionary& dict,
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> response);
|
|
|
|
static void StartLoadingHttp(
|
2019-04-30 00:47:04 +00:00
|
|
|
network::mojom::URLLoaderRequest loader,
|
|
|
|
int32_t routing_id,
|
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& original_request,
|
|
|
|
network::mojom::URLLoaderClientPtr client,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
2019-05-03 00:48:51 +00:00
|
|
|
const mate::Dictionary& dict);
|
|
|
|
static void StartLoadingStream(network::mojom::URLLoaderRequest loader,
|
|
|
|
network::mojom::URLLoaderClientPtr client,
|
|
|
|
const mate::Dictionary& dict);
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-05-03 00:48:51 +00:00
|
|
|
// Helper to send string as response.
|
|
|
|
static void SendContents(network::mojom::URLLoaderClientPtr client,
|
|
|
|
network::ResourceResponseHead head,
|
2019-05-07 02:33:05 +00:00
|
|
|
std::string data);
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
// TODO(zcbenz): This comes from extensions/browser/extension_protocols.cc
|
|
|
|
// but I don't know what it actually does, find out the meanings of |Clone|
|
|
|
|
// and |bindings_| and add comments for them.
|
|
|
|
mojo::BindingSet<network::mojom::URLLoaderFactory> bindings_;
|
|
|
|
|
2019-04-29 02:37:45 +00:00
|
|
|
ProtocolType type_;
|
|
|
|
ProtocolHandler handler_;
|
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(AtomURLLoaderFactory);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
#endif // ATOM_BROWSER_NET_ATOM_URL_LOADER_FACTORY_H_
|