diff --git a/shell/browser/api/atom_api_url_request_ns.cc b/shell/browser/api/atom_api_url_request_ns.cc index 546dfa87cdf9..29ffd145dc7e 100644 --- a/shell/browser/api/atom_api_url_request_ns.cc +++ b/shell/browser/api/atom_api_url_request_ns.cc @@ -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" @@ -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 factory_request = + mojo::MakeRequest(&network_factory); + + // Consult the embedder. + network::mojom::TrustedURLLoaderHeaderClientPtrInfo header_client; + static_cast(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( + std::move(network_factory)); InitWith(args->isolate(), args->GetThis()); } diff --git a/shell/browser/atom_browser_client.cc b/shell/browser/atom_browser_client.cc index ded6a3552aac..022fc01b4521 100644 --- a/shell/browser/atom_browser_client.cc +++ b/shell/browser/atom_browser_client.cc @@ -988,18 +988,11 @@ bool AtomBrowserClient::WillCreateURLLoaderFactory( mojo::PendingReceiver* 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); diff --git a/spec-main/api-net-spec.ts b/spec-main/api-net-spec.ts index 0ecf25250499..849ae960a0a9 100644 --- a/spec-main/api-net-spec.ts +++ b/spec-main/api-net-spec.ts @@ -628,7 +628,7 @@ describe('net module', () => { }) }) - describe.skip('webRequest', () => { + describe('webRequest', () => { afterEach(() => { session.defaultSession.webRequest.onBeforeRequest(null) })