Skip to content

Commit

Permalink
[ios] Expose API for "unrealized" WebState
Browse files Browse the repository at this point in the history
Add new methods to WebState and WebStateObserver necessary to
expose the new "unrealized" state of the WebState instances.
As the feature is not yet implemented, the methods are stubs,
but this allow converting client code.

Add unit tests for the new method of WebStateObserverBridge
(and also test for the few untested methods). Sort the methods
and properties of the test objects (in the same order as the
methods of WebStateObserver).

Bug: 1264453
Change-Id: I6a8b2334940a60e6e6311f2d9ee718b75ed46778
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3260613
Reviewed-by: Gauthier Ambard <[email protected]>
Commit-Queue: Sylvain Defresne <[email protected]>
Cr-Commit-Position: refs/heads/main@{#939340}
  • Loading branch information
sdefresne authored and Chromium LUCI CQ committed Nov 8, 2021
1 parent 51de5bf commit aa57c60
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 69 deletions.
19 changes: 14 additions & 5 deletions ios/web/public/test/fakes/crw_fake_web_state_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@
// Arguments passed to |webState:didStartNavigation:|.
@property(nonatomic, readonly)
web::TestDidStartNavigationInfo* didStartNavigationInfo;
// Arguments passed to |webState:didRedirectNavigation:|.
@property(nonatomic, readonly)
web::TestDidRedirectNavigationInfo* didRedirectNavigationInfo;
// Arguments passed to |webState:didFinishNavigation:|.
@property(nonatomic, readonly)
web::TestDidFinishNavigationInfo* didFinishNavigationInfo;
// Arguments passed to |webStateDidStartLoading:|.
@property(nonatomic, readonly) web::TestStartLoadingInfo* startLoadingInfo;
// Arguments passed to |webStateDidStopLoading:|.
@property(nonatomic, readonly) web::TestStopLoadingInfo* stopLoadingInfo;
// Arguments passed to |webState:didLoadPageWithSuccess:|.
@property(nonatomic, readonly) web::TestLoadPageInfo* loadPageInfo;
// Arguments passed to |webState:didChangeLoadingProgress:|.
@property(nonatomic, readonly)
web::TestChangeLoadingProgressInfo* changeLoadingProgressInfo;
// Arguments passed to |webStateDidChangeBackForwardState:|.
@property(nonatomic, readonly)
web::TestDidChangeBackForwardStateInfo* changeBackForwardStateInfo;
// Arguments passed to |webStateDidChangeTitle:|.
@property(nonatomic, readonly) web::TestTitleWasSetInfo* titleWasSetInfo;
// Arguments passed to |webStateDidChangeVisibleSecurityState:|.
Expand All @@ -34,16 +44,15 @@
// Arguments passed to |webState:didUpdateFaviconURLCandidates|.
@property(nonatomic, readonly)
web::TestUpdateFaviconUrlCandidatesInfo* updateFaviconUrlCandidatesInfo;
// Arguments passed to |webState:renderProcessGoneForWebState:|.
// Arguments passed to |renderProcessGoneForWebState:|.
@property(nonatomic, readonly)
web::TestRenderProcessGoneInfo* renderProcessGoneInfo;
// Arguments passed to |webStateRealized:|.
@property(nonatomic, readonly)
web::TestWebStateRealizedInfo* webStateRealizedInfo;
// Arguments passed to |webStateDestroyed:|.
@property(nonatomic, readonly)
web::TestWebStateDestroyedInfo* webStateDestroyedInfo;
// Arguments passed to |webStateDidStopLoading:|.
@property(nonatomic, readonly) web::TestStopLoadingInfo* stopLoadingInfo;
// Arguments passed to |webStateDidStartLoading:|.
@property(nonatomic, readonly) web::TestStartLoadingInfo* startLoadingInfo;

@end

Expand Down
97 changes: 69 additions & 28 deletions ios/web/public/test/fakes/crw_fake_web_state_observer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@
#error "This file requires ARC support."
#endif

namespace web {
TestUpdateFaviconUrlCandidatesInfo::TestUpdateFaviconUrlCandidatesInfo() {}
TestUpdateFaviconUrlCandidatesInfo::~TestUpdateFaviconUrlCandidatesInfo() =
default;
}

@implementation CRWFakeWebStateObserver {
// Arguments passed to |webStateWasShown:|.
std::unique_ptr<web::TestWasShownInfo> _wasShownInfo;
// Arguments passed to |webStateWasHidden:|.
std::unique_ptr<web::TestWasHiddenInfo> _wasHiddenInfo;
// Arguments passed to |webState:didStartNavigation:|.
std::unique_ptr<web::TestDidStartNavigationInfo> _didStartNavigationInfo;
// Arguments passed to |webState:didRedirectNavigation:|.
std::unique_ptr<web::TestDidRedirectNavigationInfo>
_didRedirectNavigationInfo;
// Arguments passed to |webState:didFinishNavigationForURL:|.
std::unique_ptr<web::TestDidFinishNavigationInfo> _didFinishNavigationInfo;
// Arguments passed to |webStateDidStartLoading:|.
std::unique_ptr<web::TestStartLoadingInfo> _startLoadingInfo;
// Arguments passed to |webStateDidStopLoading:|.
std::unique_ptr<web::TestStopLoadingInfo> _stopLoadingInfo;
// Arguments passed to |webState:didLoadPageWithSuccess:|.
std::unique_ptr<web::TestLoadPageInfo> _loadPageInfo;
// Arguments passed to |webState:didChangeLoadingProgress:|.
std::unique_ptr<web::TestChangeLoadingProgressInfo>
_changeLoadingProgressInfo;
// Arguments passed to |webStateDidChangeBackForwardState:|.
std::unique_ptr<web::TestDidChangeBackForwardStateInfo>
_changeBackForwardStateInfo;
// Arguments passed to |webStateDidChangeTitle:|.
std::unique_ptr<web::TestTitleWasSetInfo> _titleWasSetInfo;
// Arguments passed to |webStateDidChangeVisibleSecurityState:|.
Expand All @@ -45,12 +49,10 @@ @implementation CRWFakeWebStateObserver {
_updateFaviconUrlCandidatesInfo;
// Arguments passed to |webState:renderProcessGoneForWebState:|.
std::unique_ptr<web::TestRenderProcessGoneInfo> _renderProcessGoneInfo;
// Arguments passed to |webStateRealized:|.
std::unique_ptr<web::TestWebStateRealizedInfo> _webStateRealizedInfo;
// Arguments passed to |webStateDestroyed:|.
std::unique_ptr<web::TestWebStateDestroyedInfo> _webStateDestroyedInfo;
// Arguments passed to |webStateDidStopLoading:|.
std::unique_ptr<web::TestStopLoadingInfo> _stopLoadingInfo;
// Arguments passed to |webStateDidStartLoading:|.
std::unique_ptr<web::TestStartLoadingInfo> _startLoadingInfo;
}

- (web::TestWasShownInfo*)wasShownInfo {
Expand All @@ -65,10 +67,22 @@ @implementation CRWFakeWebStateObserver {
return _didStartNavigationInfo.get();
}

- (web::TestDidRedirectNavigationInfo*)didRedirectNavigationInfo {
return _didRedirectNavigationInfo.get();
}

- (web::TestDidFinishNavigationInfo*)didFinishNavigationInfo {
return _didFinishNavigationInfo.get();
}

- (web::TestStartLoadingInfo*)startLoadingInfo {
return _startLoadingInfo.get();
}

- (web::TestStopLoadingInfo*)stopLoadingInfo {
return _stopLoadingInfo.get();
}

- (web::TestLoadPageInfo*)loadPageInfo {
return _loadPageInfo.get();
}
Expand All @@ -77,6 +91,10 @@ @implementation CRWFakeWebStateObserver {
return _changeLoadingProgressInfo.get();
}

- (web::TestDidChangeBackForwardStateInfo*)changeBackForwardStateInfo {
return _changeBackForwardStateInfo.get();
}

- (web::TestTitleWasSetInfo*)titleWasSetInfo {
return _titleWasSetInfo.get();
}
Expand All @@ -94,16 +112,12 @@ @implementation CRWFakeWebStateObserver {
return _renderProcessGoneInfo.get();
}

- (web::TestWebStateDestroyedInfo*)webStateDestroyedInfo {
return _webStateDestroyedInfo.get();
- (web::TestWebStateRealizedInfo*)webStateRealizedInfo {
return _webStateRealizedInfo.get();
}

- (web::TestStopLoadingInfo*)stopLoadingInfo {
return _stopLoadingInfo.get();
}

- (web::TestStartLoadingInfo*)startLoadingInfo {
return _startLoadingInfo.get();
- (web::TestWebStateDestroyedInfo*)webStateDestroyedInfo {
return _webStateDestroyedInfo.get();
}

#pragma mark CRWWebStateObserver methods -
Expand Down Expand Up @@ -133,6 +147,22 @@ - (void)webState:(web::WebState*)webState
_didStartNavigationInfo->context = std::move(context);
}

- (void)webState:(web::WebState*)webState
didRedirectNavigation:(web::NavigationContext*)navigation {
ASSERT_TRUE(!navigation->GetError() || !navigation->IsSameDocument());
_didRedirectNavigationInfo =
std::make_unique<web::TestDidRedirectNavigationInfo>();
_didRedirectNavigationInfo->web_state = webState;
std::unique_ptr<web::NavigationContextImpl> context =
web::NavigationContextImpl::CreateNavigationContext(
navigation->GetWebState(), navigation->GetUrl(),
navigation->HasUserGesture(), navigation->GetPageTransition(),
navigation->IsRendererInitiated());
context->SetIsSameDocument(navigation->IsSameDocument());
context->SetError(navigation->GetError());
_didRedirectNavigationInfo->context = std::move(context);
}

- (void)webState:(web::WebState*)webState
didFinishNavigation:(web::NavigationContext*)navigation {
ASSERT_TRUE(!navigation->GetError() || !navigation->IsSameDocument());
Expand All @@ -149,6 +179,16 @@ - (void)webState:(web::WebState*)webState
_didFinishNavigationInfo->context = std::move(context);
}

- (void)webStateDidStartLoading:(web::WebState*)webState {
_startLoadingInfo = std::make_unique<web::TestStartLoadingInfo>();
_startLoadingInfo->web_state = webState;
}

- (void)webStateDidStopLoading:(web::WebState*)webState {
_stopLoadingInfo = std::make_unique<web::TestStopLoadingInfo>();
_stopLoadingInfo->web_state = webState;
}

- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
_loadPageInfo = std::make_unique<web::TestLoadPageInfo>();
_loadPageInfo->web_state = webState;
Expand All @@ -168,6 +208,12 @@ - (void)webStateDidChangeTitle:(web::WebState*)webState {
_titleWasSetInfo->web_state = webState;
}

- (void)webStateDidChangeBackForwardState:(web::WebState*)webState {
_changeBackForwardStateInfo =
std::make_unique<web::TestDidChangeBackForwardStateInfo>();
_changeBackForwardStateInfo->web_state = webState;
}

- (void)webStateDidChangeVisibleSecurityState:(web::WebState*)webState {
_didChangeVisibleSecurityStateInfo =
std::make_unique<web::TestDidChangeVisibleSecurityStateInfo>();
Expand All @@ -188,19 +234,14 @@ - (void)renderProcessGoneForWebState:(web::WebState*)webState {
_renderProcessGoneInfo->web_state = webState;
}

- (void)webStateRealized:(web::WebState*)webState {
_webStateRealizedInfo = std::make_unique<web::TestWebStateRealizedInfo>();
_webStateRealizedInfo->web_state = webState;
}

- (void)webStateDestroyed:(web::WebState*)webState {
_webStateDestroyedInfo = std::make_unique<web::TestWebStateDestroyedInfo>();
_webStateDestroyedInfo->web_state = webState;
}

- (void)webStateDidStopLoading:(web::WebState*)webState {
_stopLoadingInfo = std::make_unique<web::TestStopLoadingInfo>();
_stopLoadingInfo->web_state = webState;
}

- (void)webStateDidStartLoading:(web::WebState*)webState {
_startLoadingInfo = std::make_unique<web::TestStartLoadingInfo>();
_startLoadingInfo->web_state = webState;
}

@end
2 changes: 2 additions & 0 deletions ios/web/public/test/fakes/fake_web_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class FakeWebState : public WebState {
OnceGetter CreateDefaultOnceGetter() override;
WebStateDelegate* GetDelegate() override;
void SetDelegate(WebStateDelegate* delegate) override;
bool IsRealized() const final;
WebState* ForceRealized() final;
bool IsWebUsageEnabled() const override;
void SetWebUsageEnabled(bool enabled) override;
UIView* GetView() override;
Expand Down
9 changes: 9 additions & 0 deletions ios/web/public/test/fakes/fake_web_state.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@

void FakeWebState::SetDelegate(WebStateDelegate* delegate) {}

bool FakeWebState::IsRealized() const {
// FakeWebState cannot be unrealized.
return true;
}

WebState* FakeWebState::ForceRealized() {
return this;
}

BrowserState* FakeWebState::GetBrowserState() const {
return browser_state_;
}
Expand Down
40 changes: 29 additions & 11 deletions ios/web/public/test/fakes/fake_web_state_observer_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ struct TestDidStartNavigationInfo {
std::unique_ptr<web::NavigationContext> context;
};

// Arguments passed to |DidRedirectNavigation|.
struct TestDidRedirectNavigationInfo {
TestDidRedirectNavigationInfo();
~TestDidRedirectNavigationInfo();
WebState* web_state = nullptr;
std::unique_ptr<web::NavigationContext> context;
};

// Arguments passed to |DidFinishNavigation|.
struct TestDidFinishNavigationInfo {
TestDidFinishNavigationInfo();
Expand All @@ -43,16 +51,31 @@ struct TestDidFinishNavigationInfo {
std::unique_ptr<web::NavigationContext> context;
};

// Arguments passed to |DidStartLoading|.
struct TestStartLoadingInfo {
WebState* web_state = nullptr;
};

// Arguments passed to |DidStopLoading|.
struct TestStopLoadingInfo {
WebState* web_state = nullptr;
};

// Arguments passed to |PageLoaded|.
struct TestLoadPageInfo {
WebState* web_state = nullptr;
bool success;
bool success = false;
};

// Arguments passed to |LoadProgressChanged|.
struct TestChangeLoadingProgressInfo {
WebState* web_state = nullptr;
double progress;
double progress = 0.0;
};

// Arguments passed to |DidChangeBackForwardState|.
struct TestDidChangeBackForwardStateInfo {
WebState* web_state = nullptr;
};

// Arguments passed to |TitleWasSet|.
Expand Down Expand Up @@ -92,18 +115,13 @@ struct TestRenderProcessGoneInfo {
WebState* web_state = nullptr;
};

// Arguments passed to |WebStateDestroyed|.
struct TestWebStateDestroyedInfo {
WebState* web_state = nullptr;
};

// Arguments passed to |DidStartLoading|.
struct TestStartLoadingInfo {
// Arguments passed to |WebStateRealized|.
struct TestWebStateRealizedInfo {
WebState* web_state = nullptr;
};

// Arguments passed to |DidStopLoading|.
struct TestStopLoadingInfo {
// Arguments passed to |WebStateDestroyed|.
struct TestWebStateDestroyedInfo {
WebState* web_state = nullptr;
};

Expand Down
16 changes: 11 additions & 5 deletions ios/web/public/test/fakes/fake_web_state_observer_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
#endif

namespace web {
TestDidChangeVisibleSecurityStateInfo::TestDidChangeVisibleSecurityStateInfo() {
}
TestDidChangeVisibleSecurityStateInfo::TestDidChangeVisibleSecurityStateInfo() =
default;
TestDidChangeVisibleSecurityStateInfo::
~TestDidChangeVisibleSecurityStateInfo() {}
TestDidStartNavigationInfo::TestDidStartNavigationInfo() {}
~TestDidChangeVisibleSecurityStateInfo() = default;
TestDidStartNavigationInfo::TestDidStartNavigationInfo() = default;
TestDidStartNavigationInfo::~TestDidStartNavigationInfo() = default;
TestDidFinishNavigationInfo::TestDidFinishNavigationInfo() {}
TestDidRedirectNavigationInfo::TestDidRedirectNavigationInfo() = default;
TestDidRedirectNavigationInfo::~TestDidRedirectNavigationInfo() = default;
TestDidFinishNavigationInfo::TestDidFinishNavigationInfo() = default;
TestDidFinishNavigationInfo::~TestDidFinishNavigationInfo() = default;
TestUpdateFaviconUrlCandidatesInfo::TestUpdateFaviconUrlCandidatesInfo() =
default;
TestUpdateFaviconUrlCandidatesInfo::~TestUpdateFaviconUrlCandidatesInfo() =
default;
} // namespace web
33 changes: 33 additions & 0 deletions ios/web/public/web_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,39 @@ class WebState : public base::SupportsUserData {
virtual WebStateDelegate* GetDelegate() = 0;
virtual void SetDelegate(WebStateDelegate* delegate) = 0;

// Returns whether the WebState is realized.
//
// What does "realized" mean? When creating a WebState from session storage
// with |CreateWithStorageSession()|, it may not yet have been fully created.
// Instead, it has all information to fully instantiate it and its history
// available, but the underlying objects (WKWebView, NavigationManager, ...)
// have not been created.
//
// This is an optimisation to reduce the amount of memory consumed by tabs
// that have been restored after the browser has been shutdown. If the user
// has many tabs, but only consult a subset of them, then there is no point
// in creating them eagerly at startup. Instead, the creation is delayed
// until the tabs are activated by the user.
//
// When the WebState becomes realized, the WebStateRealized() event will be
// sent to all its WebStateObservers. They can listen to that event if they
// need to support this optimisation (by delaying the creation of their own
// state until the WebState is really used).
//
// See //docs/ios/unrealized_web_state.md for more information.
virtual bool IsRealized() const = 0;

// Forcefully bring the WebState in "realized" state. This method can safely
// be called multiple time on a WebState, though it should not be necessary
// to call it as the WebState will lazily switch to "realized" state when
// needed.
//
// Returns `this` so that the method can be chained such as:
//
// WebState* web_state = ...;
// web_state->ForceRealized()->SetDelegate(this);
virtual WebState* ForceRealized() = 0;

// Whether or not a web view is allowed to exist in this WebState. Defaults
// to false; this should be enabled before attempting to access the view.
virtual bool IsWebUsageEnabled() const = 0;
Expand Down
Loading

0 comments on commit aa57c60

Please sign in to comment.