Commit 61ec9b3e authored by Eugene But's avatar Eugene But Committed by Commit Bot

Added WasShown/WasHidden WebState and WebStateObserver API.

WebState's WasShown/WasHidden is called from Tab, so WebStateObserver
callbacks are actually work.
WebStateObserver's WasShown/WasHidden callbacks will allow to move
code from Tab to TabHelpers.

Bug: 616244
Change-Id: I2d8f3eb0f5854dd7204b9aec6db2aac593309638
Reviewed-on: https://chromium-review.googlesource.com/596783Reviewed-by: default avatarKurt Horimoto <kkhorimoto@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491739}
parent 12c55129
......@@ -1794,14 +1794,16 @@ void TabInfoBarObserver::OnInfoBarReplaced(infobars::InfoBar* old_infobar,
- (void)wasShown {
_visible = YES;
[self updateFullscreenWithToolbarVisible:YES];
[self.webController wasShown];
if (self.webState)
self.webState->WasShown();
[_inputAccessoryViewController wasShown];
}
- (void)wasHidden {
_visible = NO;
[self updateFullscreenWithToolbarVisible:YES];
[self.webController wasHidden];
if (self.webState)
self.webState->WasHidden();
[_inputAccessoryViewController wasHidden];
}
......
......@@ -33,6 +33,8 @@ class TestWebState : public WebState {
bool ShouldSuppressDialogs() const override;
void SetShouldSuppressDialogs(bool should_suppress) override;
UIView* GetView() override;
void WasShown() override;
void WasHidden() override;
BrowserState* GetBrowserState() const override;
void OpenURL(const OpenURLParams& params) override {}
void Stop() override {}
......
......@@ -69,6 +69,16 @@ UIView* TestWebState::GetView() {
return view_;
}
void TestWebState::WasShown() {
for (auto& observer : observers_)
observer.WasShown();
}
void TestWebState::WasHidden() {
for (auto& observer : observers_)
observer.WasHidden();
}
const NavigationManager* TestWebState::GetNavigationManager() const {
return navigation_manager_.get();
}
......
......@@ -19,6 +19,10 @@ class TestWebStateObserver : public WebStateObserver {
TestWebStateObserver(WebState* web_state);
~TestWebStateObserver() override;
// Arguments passed to |WasShown|.
web::TestWasShownInfo* was_shown_info() { return was_shown_info_.get(); }
// Arguments passed to |WasHidden|.
web::TestWasHiddenInfo* was_hidden_info() { return was_hidden_info_.get(); }
// Arguments passed to |DidStartNavigation|.
web::TestDidStartNavigationInfo* did_start_navigation_info() {
return did_start_navigation_info_.get();
......@@ -94,6 +98,8 @@ class TestWebStateObserver : public WebStateObserver {
private:
// WebStateObserver implementation:
void WasShown() override;
void WasHidden() override;
void NavigationItemCommitted(const LoadCommittedDetails&) override;
void PageLoaded(PageLoadCompletionStatus load_completion_status) override;
void InterstitialDismissed() override;
......@@ -118,6 +124,8 @@ class TestWebStateObserver : public WebStateObserver {
void DidStartLoading() override;
void DidStopLoading() override;
std::unique_ptr<web::TestWasShownInfo> was_shown_info_;
std::unique_ptr<web::TestWasHiddenInfo> was_hidden_info_;
std::unique_ptr<web::TestCommitNavigationInfo> commit_navigation_info_;
std::unique_ptr<web::TestLoadPageInfo> load_page_info_;
std::unique_ptr<web::TestDismissInterstitialInfo> dismiss_interstitial_info_;
......
......@@ -21,6 +21,16 @@ TestWebStateObserver::TestWebStateObserver(WebState* web_state)
: WebStateObserver(web_state) {}
TestWebStateObserver::~TestWebStateObserver() = default;
void TestWebStateObserver::WasShown() {
was_shown_info_ = base::MakeUnique<web::TestWasShownInfo>();
was_shown_info_->web_state = web_state();
}
void TestWebStateObserver::WasHidden() {
was_hidden_info_ = base::MakeUnique<web::TestWasHiddenInfo>();
was_hidden_info_->web_state = web_state();
}
void TestWebStateObserver::NavigationItemCommitted(
const LoadCommittedDetails& load_details) {
commit_navigation_info_ = base::MakeUnique<web::TestCommitNavigationInfo>();
......
......@@ -16,6 +16,16 @@ namespace web {
class NavigationContext;
class WebState;
// Arguments passed to |WasShown|.
struct TestWasShownInfo {
WebState* web_state;
};
// Arguments passed to |WasHidden|.
struct TestWasHiddenInfo {
WebState* web_state;
};
// Arguments passed to |DidStartNavigation|.
struct TestDidStartNavigationInfo {
TestDidStartNavigationInfo();
......
......@@ -124,6 +124,10 @@ class WebState : public base::SupportsUserData {
// caller to size the view.
virtual UIView* GetView() = 0;
// Must be called when the WebState becomes shown/hidden.
virtual void WasShown() = 0;
virtual void WasHidden() = 0;
// Gets the BrowserState associated with this WebState. Can never return null.
virtual BrowserState* GetBrowserState() const = 0;
......
......@@ -30,6 +30,10 @@ class WebStateObserver {
// Returns the web state associated with this observer.
WebState* web_state() const { return web_state_; }
// These methods are invoked every time the WebState changes visibility.
virtual void WasShown() {}
virtual void WasHidden() {}
// This method is invoked when committed navigation items have been pruned.
virtual void NavigationItemsPruned(size_t pruned_item_count) {}
......
......@@ -175,6 +175,8 @@ class WebStateImpl : public WebState, public NavigationManagerDelegate {
bool ShouldSuppressDialogs() const override;
void SetShouldSuppressDialogs(bool should_suppress) override;
UIView* GetView() override;
void WasShown() override;
void WasHidden() override;
BrowserState* GetBrowserState() const override;
void OpenURL(const WebState::OpenURLParams& params) override;
void Stop() override;
......
......@@ -576,6 +576,18 @@ UIView* WebStateImpl::GetView() {
return [web_controller_ view];
}
void WebStateImpl::WasShown() {
[web_controller_ wasShown];
for (auto& observer : observers_)
observer.WasShown();
}
void WebStateImpl::WasHidden() {
[web_controller_ wasHidden];
for (auto& observer : observers_)
observer.WasHidden();
}
BrowserState* WebStateImpl::GetBrowserState() const {
return navigation_manager_->GetBrowserState();
}
......
......@@ -267,6 +267,18 @@ TEST_F(WebStateImplTest, ObserverTest) {
new TestWebStateObserver(web_state_.get()));
EXPECT_EQ(web_state_.get(), observer->web_state());
// Test that WasShown() is called.
ASSERT_FALSE(observer->was_shown_info());
web_state_->WasShown();
ASSERT_TRUE(observer->was_shown_info());
EXPECT_EQ(web_state_.get(), observer->was_shown_info()->web_state);
// Test that WasHidden() is called.
ASSERT_FALSE(observer->was_hidden_info());
web_state_->WasHidden();
ASSERT_TRUE(observer->was_hidden_info());
EXPECT_EQ(web_state_.get(), observer->was_hidden_info()->web_state);
// Test that LoadProgressChanged() is called.
ASSERT_FALSE(observer->change_loading_progress_info());
const double kTestLoadProgress = 0.75;
......
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