feat: migrate protocol module to NetworkService (Part 1) (#17899)

This commit is contained in:
Cheng Zhao 2019-04-24 06:39:21 +09:00 committed by Jeremy Apthorp
parent 341592119f
commit 132137081a
8 changed files with 326 additions and 2 deletions

View file

@ -0,0 +1,54 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/atom_url_loader_factory.h"
#include <string>
#include <utility>
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/url_loader.mojom.h"
using content::BrowserThread;
namespace atom {
AtomURLLoaderFactory::AtomURLLoaderFactory() {}
AtomURLLoaderFactory::~AtomURLLoaderFactory() = default;
void AtomURLLoaderFactory::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) {
std::string contents = "Not Implemented";
uint32_t size = base::saturated_cast<uint32_t>(contents.size());
mojo::DataPipe pipe(size);
MojoResult result = pipe.producer_handle->WriteData(
contents.data(), &size, MOJO_WRITE_DATA_FLAG_NONE);
if (result != MOJO_RESULT_OK || size < contents.size()) {
client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED));
return;
}
network::ResourceResponseHead head;
head.mime_type = "text/html";
head.charset = "utf-8";
client->OnReceiveResponse(head);
client->OnStartLoadingResponseBody(std::move(pipe.consumer_handle));
client->OnComplete(network::URLLoaderCompletionStatus(net::OK));
}
void AtomURLLoaderFactory::Clone(
network::mojom::URLLoaderFactoryRequest request) {
bindings_.AddBinding(this, std::move(request));
}
} // namespace atom

View file

@ -0,0 +1,42 @@
// 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_
#include "mojo/public/cpp/bindings/binding_set.h"
#include "net/url_request/url_request_job_factory.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
namespace atom {
// Implementation of URLLoaderFactory.
class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
public:
AtomURLLoaderFactory();
~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:
// 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_;
DISALLOW_COPY_AND_ASSIGN(AtomURLLoaderFactory);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_ATOM_URL_LOADER_FACTORY_H_