Skip to content

Commit

Permalink
feat: migrate webRequest module to NetworkService (Part 9) (#19976)
Browse files Browse the repository at this point in the history
* no need to get WebContents for URLLoaderFactory

* consult embedder for network_factory created in net module

* set disable_web_security to false

* re-enable webRequest tests in net module
  • Loading branch information
zcbenz authored and deepak1556 committed Aug 27, 2019
1 parent bdb20d5 commit 4eee71f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
33 changes: 31 additions & 2 deletions shell/browser/api/atom_api_url_request_ns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/chunked_data_pipe_getter.mojom.h"
#include "shell/browser/api/atom_api_session.h"
#include "shell/browser/atom_browser_client.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/common/native_mate_converters/gurl_converter.h"
#include "shell/common/native_mate_converters/net_converter.h"
Expand Down Expand Up @@ -180,9 +182,36 @@ URLRequestNS::URLRequestNS(mate::Arguments* args) : weak_factory_(this) {
}

auto* browser_context = session->browser_context();
auto* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser_context);

network::mojom::URLLoaderFactoryPtr network_factory;
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_request =
mojo::MakeRequest(&network_factory);

// Consult the embedder.
network::mojom::TrustedURLLoaderHeaderClientPtrInfo header_client;
static_cast<content::ContentBrowserClient*>(AtomBrowserClient::Get())
->WillCreateURLLoaderFactory(
browser_context, nullptr, -1,
content::ContentBrowserClient::URLLoaderFactoryType::kNavigation,
url::Origin(), &factory_request, &header_client, nullptr);

network::mojom::URLLoaderFactoryParamsPtr params =
network::mojom::URLLoaderFactoryParams::New();
params->header_client = std::move(header_client);
params->process_id = network::mojom::kBrowserProcessId;
params->is_trusted = true;
params->is_corb_enabled = false;
// The tests of net module would fail if this setting is true, it seems that
// the non-NetworkService implementation always has web security enabled.
params->disable_web_security = false;

storage_partition->GetNetworkContext()->CreateURLLoaderFactory(
std::move(factory_request), std::move(params));
url_loader_factory_ =
content::BrowserContext::GetDefaultStoragePartition(browser_context)
->GetURLLoaderFactoryForBrowserProcess();
base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
std::move(network_factory));

InitWith(args->isolate(), args->GetThis());
}
Expand Down
13 changes: 3 additions & 10 deletions shell/browser/atom_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -988,18 +988,11 @@ bool AtomBrowserClient::WillCreateURLLoaderFactory(
mojo::PendingReceiver<network::mojom::URLLoaderFactory>* factory_receiver,
network::mojom::TrustedURLLoaderHeaderClientPtrInfo* header_client,
bool* bypass_redirect_checks) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(frame_host);
// For service workers there might be no WebContents.
if (!web_contents)
return false;

v8::Isolate* isolate = v8::Isolate::GetCurrent();
api::ProtocolNS* protocol = api::ProtocolNS::FromWrappedClass(
isolate, web_contents->GetBrowserContext());
api::ProtocolNS* protocol =
api::ProtocolNS::FromWrappedClass(isolate, browser_context);
DCHECK(protocol);
auto web_request = api::WebRequestNS::FromOrCreate(
isolate, web_contents->GetBrowserContext());
auto web_request = api::WebRequestNS::FromOrCreate(isolate, browser_context);
DCHECK(web_request.get());

auto proxied_receiver = std::move(*factory_receiver);
Expand Down
2 changes: 1 addition & 1 deletion spec-main/api-net-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ describe('net module', () => {
})
})

describe.skip('webRequest', () => {
describe('webRequest', () => {
afterEach(() => {
session.defaultSession.webRequest.onBeforeRequest(null)
})
Expand Down

0 comments on commit 4eee71f

Please sign in to comment.