Commit 78800866 authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Commit Bot

Convert WebUIURLFetcher to use SimpleURLLoader so that it can work with the network service.

Bug: 844927
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: Ib0b437f76822800bc393253250ed2660527f276b
Reviewed-on: https://chromium-review.googlesource.com/1110666Reviewed-by: default avatarXi Han <hanxi@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570169}
parent b8f425da
...@@ -119,6 +119,7 @@ void ReadData(scoped_refptr<network::ResourceResponse> headers, ...@@ -119,6 +119,7 @@ void ReadData(scoped_refptr<network::ResourceResponse> headers,
network::URLLoaderCompletionStatus status(net::OK); network::URLLoaderCompletionStatus status(net::OK);
status.encoded_data_length = output_size; status.encoded_data_length = output_size;
status.encoded_body_length = output_size; status.encoded_body_length = output_size;
status.decoded_body_length = output_size;
client->OnComplete(status); client->OnComplete(status);
} }
......
...@@ -462,7 +462,7 @@ bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( ...@@ -462,7 +462,7 @@ bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI(
GURL file_url(owner_base_url.Resolve(file_src)); GURL file_url(owner_base_url.Resolve(file_src));
url_fetcher_ = std::make_unique<WebUIURLFetcher>( url_fetcher_ = std::make_unique<WebUIURLFetcher>(
this->browser_context(), render_frame_host()->GetProcess()->GetID(), render_frame_host()->GetProcess()->GetID(),
render_frame_host()->GetRoutingID(), file_url, std::move(callback)); render_frame_host()->GetRoutingID(), file_url, std::move(callback));
url_fetcher_->Start(); url_fetcher_->Start();
return true; return true;
......
...@@ -4,20 +4,18 @@ ...@@ -4,20 +4,18 @@
#include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h" #include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/web_ui_url_loader_factory.h"
#include "content/public/common/url_fetcher.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_fetcher.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
WebUIURLFetcher::WebUIURLFetcher(content::BrowserContext* context, WebUIURLFetcher::WebUIURLFetcher(int render_process_id,
int render_process_id,
int render_frame_id, int render_frame_id,
const GURL& url, const GURL& url,
WebUILoadFileCallback callback) WebUILoadFileCallback callback)
: context_(context), : render_process_id_(render_process_id),
render_process_id_(render_process_id),
render_frame_id_(render_frame_id), render_frame_id_(render_frame_id),
url_(url), url_(url),
callback_(std::move(callback)) {} callback_(std::move(callback)) {}
...@@ -26,6 +24,16 @@ WebUIURLFetcher::~WebUIURLFetcher() { ...@@ -26,6 +24,16 @@ WebUIURLFetcher::~WebUIURLFetcher() {
} }
void WebUIURLFetcher::Start() { void WebUIURLFetcher::Start() {
content::RenderFrameHost* rfh =
content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
if (!rfh) {
std::move(callback_).Run(false, nullptr);
return;
}
auto factory = content::CreateWebUIURLLoader(rfh, url_.scheme(),
base::flat_set<std::string>());
net::NetworkTrafficAnnotationTag traffic_annotation = net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("webui_content_scripts_download", R"( net::DefineNetworkTrafficAnnotation("webui_content_scripts_download", R"(
semantics { semantics {
...@@ -45,29 +53,26 @@ void WebUIURLFetcher::Start() { ...@@ -45,29 +53,26 @@ void WebUIURLFetcher::Start() {
"Not Implemented, considered not useful as the request doesn't " "Not Implemented, considered not useful as the request doesn't "
"go to the network." "go to the network."
})"); })");
fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this, auto resource_request = std::make_unique<network::ResourceRequest>();
traffic_annotation); resource_request->url = url_;
fetcher_->SetRequestContext( resource_request->load_flags =
content::BrowserContext::GetDefaultStoragePartition(context_)-> net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES;
GetURLRequestContext()); fetcher_ = network::SimpleURLLoader::Create(std::move(resource_request),
fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | traffic_annotation);
net::LOAD_DO_NOT_SEND_COOKIES); fetcher_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
factory.get(), base::BindOnce(&WebUIURLFetcher::OnURLLoaderComplete,
content::AssociateURLFetcherWithRenderFrame( base::Unretained(this)));
fetcher_.get(), url::Origin::Create(url_), render_process_id_,
render_frame_id_);
fetcher_->Start();
} }
void WebUIURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { void WebUIURLFetcher::OnURLLoaderComplete(
CHECK_EQ(fetcher_.get(), source); std::unique_ptr<std::string> response_body) {
int response_code = 0;
if (fetcher_->ResponseInfo() && fetcher_->ResponseInfo()->headers)
response_code = fetcher_->ResponseInfo()->headers->response_code();
std::unique_ptr<std::string> data(new std::string());
bool result = false;
if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
result = fetcher_->GetResponseAsString(data.get());
DCHECK(result);
}
fetcher_.reset(); fetcher_.reset();
std::move(callback_).Run(result, std::move(data)); std::unique_ptr<std::string> data(new std::string());
if (response_body)
data = std::move(response_body);
std::move(callback_).Run(response_code == 200, std::move(data));
} }
...@@ -9,21 +9,16 @@ ...@@ -9,21 +9,16 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace content { namespace network {
class BrowserContext; class SimpleURLLoader;
}
namespace net {
class URLFetcher;
} }
// WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
// Each WebUIURLFetcher is associated with a given |render_process_id, // Each WebUIURLFetcher is associated with a given |render_process_id,
// render_view_id| pair. // render_view_id| pair.
class WebUIURLFetcher : public net::URLFetcherDelegate { class WebUIURLFetcher {
public: public:
// Called when a file URL request is complete. // Called when a file URL request is complete.
// Parameters: // Parameters:
...@@ -32,25 +27,22 @@ class WebUIURLFetcher : public net::URLFetcherDelegate { ...@@ -32,25 +27,22 @@ class WebUIURLFetcher : public net::URLFetcherDelegate {
using WebUILoadFileCallback = using WebUILoadFileCallback =
base::OnceCallback<void(bool, std::unique_ptr<std::string>)>; base::OnceCallback<void(bool, std::unique_ptr<std::string>)>;
WebUIURLFetcher(content::BrowserContext* context, WebUIURLFetcher(int render_process_id,
int render_process_id,
int render_frame_id, int render_frame_id,
const GURL& url, const GURL& url,
WebUILoadFileCallback callback); WebUILoadFileCallback callback);
~WebUIURLFetcher() override; ~WebUIURLFetcher();
void Start(); void Start();
private: private:
// net::URLFetcherDelegate: void OnURLLoaderComplete(std::unique_ptr<std::string> response_body);
void OnURLFetchComplete(const net::URLFetcher* source) override;
content::BrowserContext* context_;
int render_process_id_; int render_process_id_;
int render_frame_id_; int render_frame_id_;
GURL url_; GURL url_;
WebUILoadFileCallback callback_; WebUILoadFileCallback callback_;
std::unique_ptr<net::URLFetcher> fetcher_; std::unique_ptr<network::SimpleURLLoader> fetcher_;
DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher); DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher);
}; };
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h" #include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h"
namespace { namespace {
...@@ -91,22 +90,10 @@ void WebUIUserScriptLoader::LoadScripts( ...@@ -91,22 +90,10 @@ void WebUIUserScriptLoader::LoadScripts(
int render_process_id = iter->second.render_process_id; int render_process_id = iter->second.render_process_id;
int render_frame_id = iter->second.render_frame_id; int render_frame_id = iter->second.render_frame_id;
content::RenderProcessHost* render_process_host = CreateWebUIURLFetchers(script->js_scripts(), render_process_id,
content::RenderProcessHost::FromID(render_process_id); render_frame_id);
CreateWebUIURLFetchers(script->css_scripts(), render_process_id,
// LoadScripts may not be synchronous with AddScripts. Hence the render_frame_id);
// |render_process_host| may no longer be alive. This should fix
// crbug.com/720331. TODO(karandeepb): Investigate if there are any side
// effects of the render process host no longer being alive and add a test.
if (render_process_host) {
content::BrowserContext* browser_context =
render_process_host->GetBrowserContext();
CreateWebUIURLFetchers(script->js_scripts(), browser_context,
render_process_id, render_frame_id);
CreateWebUIURLFetchers(script->css_scripts(), browser_context,
render_process_id, render_frame_id);
}
script_render_info_map_.erase(script->id()); script_render_info_map_.erase(script->id());
} }
...@@ -122,7 +109,6 @@ void WebUIUserScriptLoader::LoadScripts( ...@@ -122,7 +109,6 @@ void WebUIUserScriptLoader::LoadScripts(
void WebUIUserScriptLoader::CreateWebUIURLFetchers( void WebUIUserScriptLoader::CreateWebUIURLFetchers(
const extensions::UserScript::FileList& script_files, const extensions::UserScript::FileList& script_files,
content::BrowserContext* browser_context,
int render_process_id, int render_process_id,
int render_frame_id) { int render_frame_id) {
for (const std::unique_ptr<extensions::UserScript::File>& script_file : for (const std::unique_ptr<extensions::UserScript::File>& script_file :
...@@ -135,8 +121,7 @@ void WebUIUserScriptLoader::CreateWebUIURLFetchers( ...@@ -135,8 +121,7 @@ void WebUIUserScriptLoader::CreateWebUIURLFetchers(
// being loaded, so passing a raw pointer to |script_file| below to // being loaded, so passing a raw pointer to |script_file| below to
// WebUIUserScriptLoader is also safe. // WebUIUserScriptLoader is also safe.
std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher(
browser_context, render_process_id, render_frame_id, render_process_id, render_frame_id, script_file->url(),
script_file->url(),
base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete,
base::Unretained(this), script_file.get()))); base::Unretained(this), script_file.get())));
fetchers_.push_back(std::move(fetcher)); fetchers_.push_back(std::move(fetcher));
......
...@@ -51,7 +51,6 @@ class WebUIUserScriptLoader : public extensions::UserScriptLoader { ...@@ -51,7 +51,6 @@ class WebUIUserScriptLoader : public extensions::UserScriptLoader {
// Creates WebUiURLFetchers for the given |script_files|. // Creates WebUiURLFetchers for the given |script_files|.
void CreateWebUIURLFetchers( void CreateWebUIURLFetchers(
const extensions::UserScript::FileList& script_files, const extensions::UserScript::FileList& script_files,
content::BrowserContext* browser_context,
int render_process_id, int render_process_id,
int render_frame_id); int render_frame_id);
......
...@@ -172,17 +172,6 @@ ...@@ -172,17 +172,6 @@
-UpdateServiceTest.TwoUpdateCheckErrors -UpdateServiceTest.TwoUpdateCheckErrors
-UpdateServiceTest.UpdateCheckError -UpdateServiceTest.UpdateCheckError
# extensions\browser\guest_view\web_view\web_ui\web_ui_url_fetcher.cc uses a
# URLFetcher to fetch chrome:// URLs.
# https://crbug.com/844927
-WebUIWebViewBrowserTest.AddAndRemoveContentScripts
-WebUIWebViewBrowserTest.AddContentScript
-WebUIWebViewBrowserTest.AddContentScriptsWithNewWindowAPI
-WebUIWebViewBrowserTest.AddContentScriptWithSameNameShouldOverwriteTheExistingOne
-WebUIWebViewBrowserTest.AddMultiContentScripts
-WebUIWebViewBrowserTest.ContentScriptExistsAsLongAsWebViewTagExists
-WebUIWebViewBrowserTest.ExecuteScriptCodeFromFile
# about:net-internals should be largely removed before shipping the network # about:net-internals should be largely removed before shipping the network
# service. # service.
# https://crbug.com/678391 # https://crbug.com/678391
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment