Commit 5847aa1b authored by Junbo Ke's avatar Junbo Ke Committed by Commit Bot

[chromecast] Add concrete implementation for CastURLLoaderThrottle.

Bug: b/120907590
Merge-With: eureka-internal/260345
Test: Manual test on device
Change-Id: I5c2063815cbeae78761496871db1bf2e804c82a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1554901Reviewed-by: default avatarZhongyi Shi <zhongyi@chromium.org>
Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Commit-Queue: Junbo Ke <juke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#649637}
parent b5c5e5fe
...@@ -7,8 +7,9 @@ include_rules = [ ...@@ -7,8 +7,9 @@ include_rules = [
"+extensions/shell/common/api", "+extensions/shell/common/api",
"+extensions/shell/grit", "+extensions/shell/grit",
"+mojo/public/cpp/bindings", "+mojo/public/cpp/bindings",
"+services/service_manager/public/cpp", "+net/http",
"+services/network/public/cpp", "+services/network/public/cpp",
"+services/service_manager/public/cpp",
"+ui/accessibility", "+ui/accessibility",
"+ui/base", "+ui/base",
"+ui/gfx", "+ui/gfx",
......
...@@ -4,19 +4,51 @@ ...@@ -4,19 +4,51 @@
#include "chromecast/common/cast_url_loader_throttle.h" #include "chromecast/common/cast_url_loader_throttle.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "services/network/public/cpp/resource_request.h" #include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/resource_response.h" #include "services/network/public/cpp/resource_response.h"
namespace chromecast { namespace chromecast {
CastURLLoaderThrottle::CastURLLoaderThrottle() = default; CastURLLoaderThrottle::CastURLLoaderThrottle(
Delegate* delegate,
const std::string& session_id)
: settings_delegate_(delegate),
session_id_(session_id),
weak_factory_(this) {
DCHECK(settings_delegate_);
weak_this_ = weak_factory_.GetWeakPtr();
}
CastURLLoaderThrottle::~CastURLLoaderThrottle() = default; CastURLLoaderThrottle::~CastURLLoaderThrottle() = default;
void CastURLLoaderThrottle::DetachFromCurrentSequence() {} void CastURLLoaderThrottle::DetachFromCurrentSequence() {}
void CastURLLoaderThrottle::WillStartRequest( void CastURLLoaderThrottle::WillStartRequest(
network::ResourceRequest* /* request */, network::ResourceRequest* request,
bool* defer) {} bool* defer) {
int error = settings_delegate_->WillStartResourceRequest(
request, session_id_,
base::BindOnce(&CastURLLoaderThrottle::ResumeRequest, weak_this_));
if (error == net::ERR_IO_PENDING) {
deferred_ = true;
*defer = true;
}
}
void CastURLLoaderThrottle::ResumeRequest(int error,
net::HttpRequestHeaders headers) {
DCHECK(deferred_);
if (error != net::OK) {
NOTREACHED() << "Trying to resume a request with unexpected error: "
<< error;
return;
}
deferred_ = false;
delegate_->UpdateDeferredRequestHeaders(headers);
delegate_->Resume();
}
} // namespace chromecast } // namespace chromecast
...@@ -5,22 +5,50 @@ ...@@ -5,22 +5,50 @@
#ifndef CHROMECAST_COMMON_CAST_URL_LOADER_THROTTLE_H_ #ifndef CHROMECAST_COMMON_CAST_URL_LOADER_THROTTLE_H_
#define CHROMECAST_COMMON_CAST_URL_LOADER_THROTTLE_H_ #define CHROMECAST_COMMON_CAST_URL_LOADER_THROTTLE_H_
#include <string>
#include "base/callback_forward.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "content/public/common/url_loader_throttle.h" #include "content/public/common/url_loader_throttle.h"
#include "net/http/http_request_headers.h"
namespace chromecast { namespace chromecast {
class CastURLLoaderThrottle : public content::URLLoaderThrottle { class CastURLLoaderThrottle : public content::URLLoaderThrottle {
public: public:
CastURLLoaderThrottle(); // An interface for CastURLLoaderThrottle to modify the resource request,
// possibly also defer the request (by returning net::IO_PENDING) in some
// scenarios where blocking operations are needed.
class Delegate {
public:
virtual int WillStartResourceRequest(
network::ResourceRequest* request,
const std::string& session_id,
base::OnceCallback<void(int, net::HttpRequestHeaders)> callback) = 0;
protected:
virtual ~Delegate() = default;
};
CastURLLoaderThrottle(Delegate* delegate, const std::string& session_id);
~CastURLLoaderThrottle() override; ~CastURLLoaderThrottle() override;
private:
// content::URLLoaderThrottle implementation: // content::URLLoaderThrottle implementation:
void DetachFromCurrentSequence() override; void DetachFromCurrentSequence() override;
void WillStartRequest(network::ResourceRequest* request, void WillStartRequest(network::ResourceRequest* request,
bool* defer) override; bool* defer) override;
private: void ResumeRequest(int error, net::HttpRequestHeaders headers);
bool deferred_ = false;
Delegate* const settings_delegate_;
const std::string session_id_;
base::WeakPtr<CastURLLoaderThrottle> weak_this_;
base::WeakPtrFactory<CastURLLoaderThrottle> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(CastURLLoaderThrottle); DISALLOW_COPY_AND_ASSIGN(CastURLLoaderThrottle);
}; };
......
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