Commit b436fc7b authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Allow WebStateObserver to observe N WebStates [19/N].

Convert InfoBarManagerImpl to directly track registration with
the observed WebState instead of relying on the deprecated
code in WebStateObserver.

Remove unused method InfoBarManagerImpl::WebStateFromInfoBar
as it accessed deprecated web_state() method.

Bug: 775684
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I55574e425107a3306bc0ba2085d699d4008c3f44
Reviewed-on: https://chromium-review.googlesource.com/766750
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516296}
parent 0a8e88e5
...@@ -29,11 +29,6 @@ class InfoBarManagerImpl : public infobars::InfoBarManager, ...@@ -29,11 +29,6 @@ class InfoBarManagerImpl : public infobars::InfoBarManager,
public: public:
~InfoBarManagerImpl() override; ~InfoBarManagerImpl() override;
// This function must only be called on infobars that are owned by an
// InfoBarManagerImpl instance (or not owned at all, in which case this
// returns null).
static web::WebState* WebStateFromInfoBar(infobars::InfoBar* infobar);
private: private:
friend class web::WebStateUserData<InfoBarManagerImpl>; friend class web::WebStateUserData<InfoBarManagerImpl>;
...@@ -53,6 +48,10 @@ class InfoBarManagerImpl : public infobars::InfoBarManager, ...@@ -53,6 +48,10 @@ class InfoBarManagerImpl : public infobars::InfoBarManager,
// Opens a URL according to the specified |disposition|. // Opens a URL according to the specified |disposition|.
void OpenURL(const GURL& url, WindowOpenDisposition disposition) override; void OpenURL(const GURL& url, WindowOpenDisposition disposition) override;
// The WebState this instance is observing. Will be null after
// WebStateDestroyed has been called.
web::WebState* web_state_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(InfoBarManagerImpl); DISALLOW_COPY_AND_ASSIGN(InfoBarManagerImpl);
}; };
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <utility> #include <utility>
#include "base/logging.h"
#include "components/infobars/core/confirm_infobar_delegate.h" #include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h" #include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_delegate.h" #include "components/infobars/core/infobar_delegate.h"
...@@ -43,26 +44,26 @@ NavigationDetailsFromLoadCommittedDetails( ...@@ -43,26 +44,26 @@ NavigationDetailsFromLoadCommittedDetails(
} // namespace } // namespace
// static
web::WebState* InfoBarManagerImpl::WebStateFromInfoBar(
infobars::InfoBar* infobar) {
if (!infobar || !infobar->owner())
return nullptr;
return static_cast<InfoBarManagerImpl*>(infobar->owner())->web_state();
}
InfoBarManagerImpl::InfoBarManagerImpl(web::WebState* web_state) InfoBarManagerImpl::InfoBarManagerImpl(web::WebState* web_state)
: web::WebStateObserver(web_state) { : web_state_(web_state) {
DCHECK(web_state); web_state_->AddObserver(this);
} }
InfoBarManagerImpl::~InfoBarManagerImpl() { InfoBarManagerImpl::~InfoBarManagerImpl() {
ShutDown(); ShutDown();
// As the object can commit suicide, it is possible that its destructor
// is called before WebStateDestroyed. In that case stop observing the
// WebState.
if (web_state_) {
web_state_->RemoveObserver(this);
web_state_ = nullptr;
}
} }
int InfoBarManagerImpl::GetActiveEntryID() { int InfoBarManagerImpl::GetActiveEntryID() {
web::NavigationItem* visible_item = web::NavigationItem* visible_item =
web_state()->GetNavigationManager()->GetVisibleItem(); web_state_->GetNavigationManager()->GetVisibleItem();
return visible_item ? visible_item->GetUniqueID() : 0; return visible_item ? visible_item->GetUniqueID() : 0;
} }
...@@ -74,16 +75,17 @@ std::unique_ptr<infobars::InfoBar> InfoBarManagerImpl::CreateConfirmInfoBar( ...@@ -74,16 +75,17 @@ std::unique_ptr<infobars::InfoBar> InfoBarManagerImpl::CreateConfirmInfoBar(
void InfoBarManagerImpl::NavigationItemCommitted( void InfoBarManagerImpl::NavigationItemCommitted(
web::WebState* web_state, web::WebState* web_state,
const web::LoadCommittedDetails& load_details) { const web::LoadCommittedDetails& load_details) {
DCHECK_EQ(web_state_, web_state);
OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details)); OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details));
} }
void InfoBarManagerImpl::WebStateDestroyed(web::WebState* web_state) { void InfoBarManagerImpl::WebStateDestroyed(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
// The WebState is going away; be aggressively paranoid and delete this // The WebState is going away; be aggressively paranoid and delete this
// InfoBarManagerImpl lest other parts of the system attempt to add infobars // InfoBarManagerImpl lest other parts of the system attempt to add infobars
// or use it otherwise during the destruction. // or use it otherwise during the destruction. As this is the equivalent of
web_state->RemoveUserData(UserDataKey()); // "delete this", returning from this function is the only safe thing to do.
// That was the equivalent of "delete this". This object is now destroyed; web_state_->RemoveUserData(UserDataKey());
// returning from this function is the only safe thing to do.
} }
void InfoBarManagerImpl::OpenURL(const GURL& url, void InfoBarManagerImpl::OpenURL(const GURL& url,
......
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