Remove CRWJSInjectionEvaluator use from MojoFacade.
Bug: 944385 Change-Id: I4a32f4a8db04162db77285ec25ef4472b59acf8e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1533367 Auto-Submit: Mike Dougherty <michaeldo@chromium.org> Commit-Queue: Mike Dougherty <michaeldo@chromium.org> Reviewed-by: Eugene But <eugenebut@chromium.org> Cr-Commit-Position: refs/heads/master@{#645490}
This commit is contained in:

committed by
Commit Bot

parent
7cad0f5c98
commit
f85de4b1e4
ios/web
@ -914,7 +914,8 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
|
||||
if (!_mojoFacade) {
|
||||
service_manager::mojom::InterfaceProvider* interfaceProvider =
|
||||
self.webStateImpl->GetWebStateInterfaceProvider();
|
||||
_mojoFacade.reset(new web::MojoFacade(interfaceProvider, self));
|
||||
_mojoFacade =
|
||||
std::make_unique<web::MojoFacade>(interfaceProvider, self.webState);
|
||||
}
|
||||
return _mojoFacade.get();
|
||||
}
|
||||
@ -1033,6 +1034,7 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
|
||||
self.webStateImpl->CancelDialogs();
|
||||
|
||||
_SSLStatusUpdater = nil;
|
||||
_mojoFacade.reset();
|
||||
|
||||
self.nativeProvider = nil;
|
||||
self.swipeRecognizerProvider = nil;
|
||||
|
@ -12,8 +12,6 @@
|
||||
#include "base/values.h"
|
||||
#include "mojo/public/cpp/system/simple_watcher.h"
|
||||
|
||||
@protocol CRWJSInjectionEvaluator;
|
||||
|
||||
namespace service_manager {
|
||||
namespace mojom {
|
||||
class InterfaceProvider;
|
||||
@ -22,15 +20,17 @@ class InterfaceProvider;
|
||||
|
||||
namespace web {
|
||||
|
||||
class WebState;
|
||||
|
||||
// Facade class for Mojo. All inputs and outputs are optimized for communication
|
||||
// with WebUI pages and hence use JSON format. Must be created used and
|
||||
// destroyed on UI thread.
|
||||
class MojoFacade {
|
||||
public:
|
||||
// Constructs MojoFacade. The calling code must retain the ownership of
|
||||
// |interface_provider| and |script_evaluator|, both can not be null.
|
||||
// |interface_provider| and |web_state|, both can not be null.
|
||||
MojoFacade(service_manager::mojom::InterfaceProvider* interface_provider,
|
||||
id<CRWJSInjectionEvaluator> script_evaluator);
|
||||
WebState* web_state);
|
||||
~MojoFacade();
|
||||
|
||||
// Handles Mojo message received from WebUI page. Returns a valid JSON string
|
||||
@ -115,7 +115,8 @@ class MojoFacade {
|
||||
// Provides interfaces.
|
||||
service_manager::mojom::InterfaceProvider* interface_provider_;
|
||||
// Runs JavaScript on WebUI page.
|
||||
__weak id<CRWJSInjectionEvaluator> script_evaluator_ = nil;
|
||||
WebState* web_state_ = nil;
|
||||
// __weak id<CRWJSInjectionEvaluator> script_evaluator_ = nil;
|
||||
// Id of the last created watch.
|
||||
int last_watch_id_ = 0;
|
||||
// Currently active watches created through this facade.
|
||||
|
@ -17,8 +17,9 @@
|
||||
#include "base/json/json_reader.h"
|
||||
#include "base/json/json_writer.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#include "base/values.h"
|
||||
#import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
|
||||
#include "ios/web/public/web_state/web_state.h"
|
||||
#include "ios/web/public/web_thread.h"
|
||||
#include "mojo/public/cpp/system/core.h"
|
||||
#include "services/service_manager/public/mojom/interface_provider.mojom.h"
|
||||
@ -31,12 +32,11 @@ namespace web {
|
||||
|
||||
MojoFacade::MojoFacade(
|
||||
service_manager::mojom::InterfaceProvider* interface_provider,
|
||||
id<CRWJSInjectionEvaluator> script_evaluator)
|
||||
: interface_provider_(interface_provider),
|
||||
script_evaluator_(script_evaluator) {
|
||||
WebState* web_state)
|
||||
: interface_provider_(interface_provider), web_state_(web_state) {
|
||||
DCHECK_CURRENTLY_ON(WebThread::UI);
|
||||
DCHECK(interface_provider_);
|
||||
DCHECK(script_evaluator_);
|
||||
DCHECK(web_state_);
|
||||
}
|
||||
|
||||
MojoFacade::~MojoFacade() {
|
||||
@ -223,7 +223,7 @@ base::Value MojoFacade::HandleMojoHandleWatch(base::Value args) {
|
||||
stringWithFormat:
|
||||
@"Mojo.internal.watchCallbacksHolder.callCallback(%d, %d)",
|
||||
callback_id, result];
|
||||
[script_evaluator_ executeJavaScript:script completionHandler:nil];
|
||||
web_state_->ExecuteJavaScript(base::SysNSStringToUTF16(script));
|
||||
},
|
||||
*callback_id);
|
||||
auto watcher = std::make_unique<mojo::SimpleWatcher>(
|
||||
|
@ -7,22 +7,25 @@
|
||||
#include <memory>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/run_loop.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#import "base/test/ios/wait_util.h"
|
||||
#import "ios/web/public/test/fakes/test_web_state.h"
|
||||
#include "ios/web/public/test/web_test.h"
|
||||
#import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
|
||||
#include "ios/web/public/web_state/web_state_interface_provider.h"
|
||||
#include "ios/web/test/mojo_test.mojom.h"
|
||||
#include "ios/web/web_state/web_state_impl.h"
|
||||
#include "mojo/public/cpp/bindings/binding_set.h"
|
||||
#import "testing/gtest_mac.h"
|
||||
#import "third_party/ocmock/OCMock/OCMock.h"
|
||||
|
||||
#if !defined(__has_feature) || !__has_feature(objc_arc)
|
||||
#error "This file requires ARC support."
|
||||
#endif
|
||||
|
||||
using base::test::ios::kWaitForJSCompletionTimeout;
|
||||
using base::test::ios::WaitUntilConditionOrTimeout;
|
||||
|
||||
namespace web {
|
||||
|
||||
namespace {
|
||||
@ -46,6 +49,31 @@ id GetObject(const std::string& json) {
|
||||
error:nil];
|
||||
}
|
||||
|
||||
class FakeWebState : public TestWebState {
|
||||
public:
|
||||
void SetWatchId(int watch_id) { watch_id_ = watch_id; }
|
||||
|
||||
void SetFacade(MojoFacade* facade) { facade_ = facade; }
|
||||
|
||||
void ExecuteJavaScript(const base::string16& javascript) override {
|
||||
TestWebState::ExecuteJavaScript(javascript);
|
||||
// Cancel the watch immediately to ensure there are no additional
|
||||
// notifications.
|
||||
// NOTE: This must be done as a side effect of executing the JavaScript.
|
||||
NSDictionary* cancel_watch = @{
|
||||
@"name" : @"MojoWatcher.cancel",
|
||||
@"args" : @{
|
||||
@"watchId" : @(watch_id_),
|
||||
},
|
||||
};
|
||||
EXPECT_TRUE(facade_->HandleMojoMessage(GetJson(cancel_watch)).empty());
|
||||
}
|
||||
|
||||
private:
|
||||
int watch_id_;
|
||||
MojoFacade* facade_; // weak
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
// A test fixture to test MojoFacade class.
|
||||
@ -55,14 +83,12 @@ class MojoFacadeTest : public WebTest {
|
||||
interface_provider_ = std::make_unique<WebStateInterfaceProvider>();
|
||||
interface_provider_->registry()->AddInterface(base::Bind(
|
||||
&MojoFacadeTest::BindTestUIHandlerMojoRequest, base::Unretained(this)));
|
||||
evaluator_ =
|
||||
[OCMockObject mockForProtocol:@protocol(CRWJSInjectionEvaluator)];
|
||||
facade_ = std::make_unique<MojoFacade>(
|
||||
interface_provider_.get(),
|
||||
static_cast<id<CRWJSInjectionEvaluator>>(evaluator_));
|
||||
facade_ =
|
||||
std::make_unique<MojoFacade>(interface_provider_.get(), &web_state_);
|
||||
web_state_.SetFacade(facade_.get());
|
||||
}
|
||||
|
||||
OCMockObject* evaluator() { return evaluator_; }
|
||||
FakeWebState* web_state() { return &web_state_; }
|
||||
MojoFacade* facade() { return facade_.get(); }
|
||||
|
||||
void CreateMessagePipe(uint32_t* handle0, uint32_t* handle1) {
|
||||
@ -97,7 +123,7 @@ class MojoFacadeTest : public WebTest {
|
||||
void BindTestUIHandlerMojoRequest(TestUIHandlerMojoRequest request) {}
|
||||
|
||||
std::unique_ptr<WebStateInterfaceProvider> interface_provider_;
|
||||
OCMockObject* evaluator_;
|
||||
FakeWebState web_state_;
|
||||
std::unique_ptr<MojoFacade> facade_;
|
||||
};
|
||||
|
||||
@ -151,27 +177,7 @@ TEST_F(MojoFacadeTest, Watch) {
|
||||
int watch_id = 0;
|
||||
EXPECT_TRUE(base::StringToInt(watch_id_as_string, &watch_id));
|
||||
|
||||
// Start waiting for the watch callback.
|
||||
__block bool callback_received = false;
|
||||
NSString* expected_script =
|
||||
[NSString stringWithFormat:
|
||||
@"Mojo.internal.watchCallbacksHolder.callCallback(%d, %d)",
|
||||
callback_id, MOJO_RESULT_OK];
|
||||
[[[evaluator() expect] andDo:^(NSInvocation*) {
|
||||
callback_received = true;
|
||||
|
||||
// Cancel the watch immediately to ensure there are no additional
|
||||
// notifications.
|
||||
NSDictionary* cancel_watch = @{
|
||||
@"name" : @"MojoWatcher.cancel",
|
||||
@"args" : @{
|
||||
@"watchId" : @(watch_id),
|
||||
},
|
||||
};
|
||||
std::string result_as_string =
|
||||
facade()->HandleMojoMessage(GetJson(cancel_watch));
|
||||
EXPECT_TRUE(result_as_string.empty());
|
||||
}] executeJavaScript:expected_script completionHandler:nil];
|
||||
web_state()->SetWatchId(watch_id);
|
||||
|
||||
// Write to the other end of the pipe.
|
||||
NSDictionary* write = @{
|
||||
@ -185,11 +191,18 @@ TEST_F(MojoFacadeTest, Watch) {
|
||||
EXPECT_TRUE(base::StringToInt(result_as_string, &result));
|
||||
EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result));
|
||||
|
||||
base::test::ios::WaitUntilCondition(
|
||||
^{
|
||||
return callback_received;
|
||||
},
|
||||
true, base::TimeDelta());
|
||||
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^bool {
|
||||
base::RunLoop().RunUntilIdle();
|
||||
return !web_state()->GetLastExecutedJavascript().empty();
|
||||
}));
|
||||
|
||||
NSString* expected_script =
|
||||
[NSString stringWithFormat:
|
||||
@"Mojo.internal.watchCallbacksHolder.callCallback(%d, %d)",
|
||||
callback_id, MOJO_RESULT_OK];
|
||||
|
||||
EXPECT_EQ(base::SysNSStringToUTF16(expected_script),
|
||||
web_state()->GetLastExecutedJavascript());
|
||||
|
||||
CloseHandle(handle0);
|
||||
CloseHandle(handle1);
|
||||
|
Reference in New Issue
Block a user