Commit 299669f9 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Cleanup WebFrame class.

Fixes for comments on crrev.com/c/1192424 after merge.

Bug: 851636
Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: Id742b36e6f46a647b510f81307f371b51bac7b3f
Reviewed-on: https://chromium-review.googlesource.com/1208463
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589433}
parent eb1903ca
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/supports_user_data.h" #include "base/supports_user_data.h"
#include "base/time/time.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace base { namespace base {
class TimeDelta;
class Value; class Value;
} }
......
...@@ -36,17 +36,12 @@ class WebFrameImpl : public WebFrame, public web::WebStateObserver { ...@@ -36,17 +36,12 @@ class WebFrameImpl : public WebFrame, public web::WebStateObserver {
// The associated web state. // The associated web state.
WebState* GetWebState(); WebState* GetWebState();
// Detaches the receiver from the associated WebState.
void DetachFromWebState();
// WebFrame implementation // WebFrame implementation
std::string GetFrameId() const override; std::string GetFrameId() const override;
bool IsMainFrame() const override; bool IsMainFrame() const override;
GURL GetSecurityOrigin() const override; GURL GetSecurityOrigin() const override;
// WebStateObserver implementation
void WebStateDestroyed(web::WebState* web_state) override;
bool CallJavaScriptFunction( bool CallJavaScriptFunction(
const std::string& name, const std::string& name,
const std::vector<base::Value>& parameters) override; const std::vector<base::Value>& parameters) override;
...@@ -56,6 +51,9 @@ class WebFrameImpl : public WebFrame, public web::WebStateObserver { ...@@ -56,6 +51,9 @@ class WebFrameImpl : public WebFrame, public web::WebStateObserver {
base::OnceCallback<void(const base::Value*)> callback, base::OnceCallback<void(const base::Value*)> callback,
base::TimeDelta timeout) override; base::TimeDelta timeout) override;
// WebStateObserver implementation
void WebStateDestroyed(web::WebState* web_state) override;
private: private:
// Calls the JavaScript function |name| in the frame context in the same // Calls the JavaScript function |name| in the frame context in the same
// manner as the inherited CallJavaScriptFunction functions. If // manner as the inherited CallJavaScriptFunction functions. If
...@@ -65,6 +63,11 @@ class WebFrameImpl : public WebFrame, public web::WebStateObserver { ...@@ -65,6 +63,11 @@ class WebFrameImpl : public WebFrame, public web::WebStateObserver {
const std::vector<base::Value>& parameters, const std::vector<base::Value>& parameters,
bool reply_with_result); bool reply_with_result);
// Detaches the receiver from the associated WebState.
void DetachFromWebState();
// Returns the script command name to use for this WebFrame.
const std::string GetScriptCommandPrefix();
// A structure to store the callbacks associated with the // A structure to store the callbacks associated with the
// |CallJavaScriptFunction| requests. // |CallJavaScriptFunction| requests.
typedef base::CancelableOnceCallback<void(void)> TimeoutCallback; typedef base::CancelableOnceCallback<void(void)> TimeoutCallback;
......
...@@ -43,11 +43,10 @@ WebFrameImpl::WebFrameImpl(const std::string& frame_id, ...@@ -43,11 +43,10 @@ WebFrameImpl::WebFrameImpl(const std::string& frame_id,
DCHECK(web_state); DCHECK(web_state);
web_state->AddObserver(this); web_state->AddObserver(this);
const std::string command_prefix = kJavaScriptReplyCommandPrefix + frame_id;
web_state->AddScriptCommandCallback( web_state->AddScriptCommandCallback(
base::BindRepeating(&WebFrameImpl::OnJavaScriptReply, base::BindRepeating(&WebFrameImpl::OnJavaScriptReply,
base::Unretained(this), base::Unretained(web_state)), base::Unretained(this), base::Unretained(web_state)),
command_prefix); GetScriptCommandPrefix());
} }
WebFrameImpl::~WebFrameImpl() { WebFrameImpl::~WebFrameImpl() {
...@@ -55,16 +54,6 @@ WebFrameImpl::~WebFrameImpl() { ...@@ -55,16 +54,6 @@ WebFrameImpl::~WebFrameImpl() {
DetachFromWebState(); DetachFromWebState();
} }
void WebFrameImpl::DetachFromWebState() {
if (web_state_) {
const std::string command_prefix =
kJavaScriptReplyCommandPrefix + frame_id_;
web_state_->RemoveScriptCommandCallback(command_prefix);
web_state_->RemoveObserver(this);
web_state_ = nullptr;
}
}
WebState* WebFrameImpl::GetWebState() { WebState* WebFrameImpl::GetWebState() {
return web_state_; return web_state_;
} }
...@@ -126,7 +115,7 @@ bool WebFrameImpl::CallJavaScriptFunction( ...@@ -126,7 +115,7 @@ bool WebFrameImpl::CallJavaScriptFunction(
bool WebFrameImpl::CallJavaScriptFunction( bool WebFrameImpl::CallJavaScriptFunction(
const std::string& name, const std::string& name,
const std::vector<base::Value>& parameters) { const std::vector<base::Value>& parameters) {
return CallJavaScriptFunction(name, parameters, /*replyWithResult=*/false); return CallJavaScriptFunction(name, parameters, /*reply_with_result=*/false);
} }
bool WebFrameImpl::CallJavaScriptFunction( bool WebFrameImpl::CallJavaScriptFunction(
...@@ -145,7 +134,7 @@ bool WebFrameImpl::CallJavaScriptFunction( ...@@ -145,7 +134,7 @@ bool WebFrameImpl::CallJavaScriptFunction(
base::PostDelayedTaskWithTraits(FROM_HERE, {web::WebThread::UI}, base::PostDelayedTaskWithTraits(FROM_HERE, {web::WebThread::UI},
timeout_callback_ptr->callback(), timeout); timeout_callback_ptr->callback(), timeout);
return CallJavaScriptFunction(name, parameters, /*replyWithResult=*/true); return CallJavaScriptFunction(name, parameters, /*reply_with_result=*/true);
} }
void WebFrameImpl::CancelRequest(int message_id) { void WebFrameImpl::CancelRequest(int message_id) {
...@@ -182,8 +171,7 @@ bool WebFrameImpl::OnJavaScriptReply(web::WebState* web_state, ...@@ -182,8 +171,7 @@ bool WebFrameImpl::OnJavaScriptReply(web::WebState* web_state,
} }
const std::string command_string = command->GetString(); const std::string command_string = command->GetString();
if (command_string != if (command_string != (GetScriptCommandPrefix() + ".reply")) {
(std::string(kJavaScriptReplyCommandPrefix) + frame_id_ + ".reply")) {
NOTREACHED(); NOTREACHED();
return false; return false;
} }
...@@ -211,6 +199,18 @@ bool WebFrameImpl::OnJavaScriptReply(web::WebState* web_state, ...@@ -211,6 +199,18 @@ bool WebFrameImpl::OnJavaScriptReply(web::WebState* web_state,
return true; return true;
} }
void WebFrameImpl::DetachFromWebState() {
if (web_state_) {
web_state_->RemoveScriptCommandCallback(GetScriptCommandPrefix());
web_state_->RemoveObserver(this);
web_state_ = nullptr;
}
}
const std::string WebFrameImpl::GetScriptCommandPrefix() {
return kJavaScriptReplyCommandPrefix + frame_id_;
}
void WebFrameImpl::WebStateDestroyed(web::WebState* web_state) { void WebFrameImpl::WebStateDestroyed(web::WebState* web_state) {
CancelPendingRequests(); CancelPendingRequests();
DetachFromWebState(); DetachFromWebState();
...@@ -221,6 +221,6 @@ WebFrameImpl::RequestCallbacks::RequestCallbacks( ...@@ -221,6 +221,6 @@ WebFrameImpl::RequestCallbacks::RequestCallbacks(
std::unique_ptr<TimeoutCallback> timeout) std::unique_ptr<TimeoutCallback> timeout)
: completion(std::move(completion)), timeout_callback(std::move(timeout)) {} : completion(std::move(completion)), timeout_callback(std::move(timeout)) {}
WebFrameImpl::RequestCallbacks::~RequestCallbacks(){}; WebFrameImpl::RequestCallbacks::~RequestCallbacks() {}
} // namespace web } // namespace web
...@@ -165,7 +165,7 @@ TEST_F(WebFrameImplIntTest, PreventMessageReplay) { ...@@ -165,7 +165,7 @@ TEST_F(WebFrameImplIntTest, PreventMessageReplay) {
return; return;
} }
ASSERT_TRUE(LoadHtml("<p>>")); ASSERT_TRUE(LoadHtml("<p>"));
WebFrame* main_frame = GetMainWebFrameForWebState(web_state()); WebFrame* main_frame = GetMainWebFrameForWebState(web_state());
ASSERT_TRUE(main_frame); ASSERT_TRUE(main_frame);
...@@ -220,13 +220,13 @@ TEST_F(WebFrameImplIntTest, PreventMessageReplay) { ...@@ -220,13 +220,13 @@ TEST_F(WebFrameImplIntTest, PreventMessageReplay) {
return called; return called;
})); }));
EXPECT_EQ(1, [ExecuteJavaScript(@"sensitiveValue") intValue]); EXPECT_NSEQ(@1, ExecuteJavaScript(@"sensitiveValue"));
ExecuteJavaScript(@"replayInterceptedMessage()"); ExecuteJavaScript(@"replayInterceptedMessage()");
// Value should not increase because replaying message should not re-execute // Value should not increase because replaying message should not re-execute
// the called function. // the called function.
EXPECT_EQ(1, [ExecuteJavaScript(@"sensitiveValue") intValue]); EXPECT_NSEQ(@1, ExecuteJavaScript(@"sensitiveValue"));
} }
} // namespace web } // namespace web
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