Commit 9626a0a7 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

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

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

Pass the WebState to SadTabTabHelperTestDelegate to
fix the unit test (as WebStateObserver method web_state
always return nullptr).

Bug: 775684
Change-Id: I7908650e3612645eddf03eb7ea01f88dfba4d395
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Reviewed-on: https://chromium-review.googlesource.com/768724
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517791}
parent 3a2776a9
...@@ -27,17 +27,18 @@ ...@@ -27,17 +27,18 @@
#pragma mark - SadTabTabHelperDelegate #pragma mark - SadTabTabHelperDelegate
- (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper - (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper
presentSadTabForRepeatedFailure:(BOOL)repeatedFailure { presentSadTabForWebState:(web::WebState*)webState
repeatedFailure:(BOOL)repeatedFailure {
// Create a SadTabView so |webstate| presents it. // Create a SadTabView so |webstate| presents it.
SadTabView* sadTabview = [[SadTabView alloc] SadTabView* sadTabview = [[SadTabView alloc]
initWithMode:repeatedFailure ? SadTabViewMode::FEEDBACK initWithMode:repeatedFailure ? SadTabViewMode::FEEDBACK
: SadTabViewMode::RELOAD : SadTabViewMode::RELOAD
navigationManager:tabHelper->web_state()->GetNavigationManager()]; navigationManager:webState->GetNavigationManager()];
sadTabview.dispatcher = static_cast<id<ApplicationCommands>>(self.dispatcher); sadTabview.dispatcher = static_cast<id<ApplicationCommands>>(self.dispatcher);
sadTabview.actionDelegate = self; sadTabview.actionDelegate = self;
CRWContentView* contentView = CRWContentView* contentView =
[[CRWGenericContentView alloc] initWithView:sadTabview]; [[CRWGenericContentView alloc] initWithView:sadTabview];
tabHelper->web_state()->ShowTransientContentView(contentView); webState->ShowTransientContentView(contentView);
} }
@end @end
...@@ -78,6 +78,10 @@ class SadTabTabHelper : public web::WebStateUserData<SadTabTabHelper>, ...@@ -78,6 +78,10 @@ class SadTabTabHelper : public web::WebStateUserData<SadTabTabHelper>,
// to be considered a repeat failure. // to be considered a repeat failure.
static const double kDefaultRepeatFailureInterval; static const double kDefaultRepeatFailureInterval;
// The WebState this instance is observing. Will be null after
// WebStateDestroyed has been called.
web::WebState* web_state_ = nullptr;
// Stores the last URL that caused a renderer crash, // Stores the last URL that caused a renderer crash,
// used to detect repeated crashes. // used to detect repeated crashes.
GURL last_failed_url_; GURL last_failed_url_;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "ios/chrome/browser/chrome_url_constants.h" #include "ios/chrome/browser/chrome_url_constants.h"
...@@ -38,14 +39,16 @@ SadTabTabHelper::SadTabTabHelper(web::WebState* web_state, ...@@ -38,14 +39,16 @@ SadTabTabHelper::SadTabTabHelper(web::WebState* web_state,
SadTabTabHelper::SadTabTabHelper(web::WebState* web_state, SadTabTabHelper::SadTabTabHelper(web::WebState* web_state,
double repeat_failure_interval, double repeat_failure_interval,
id<SadTabTabHelperDelegate> delegate) id<SadTabTabHelperDelegate> delegate)
: web::WebStateObserver(web_state), : web_state_(web_state),
repeat_failure_interval_(repeat_failure_interval), repeat_failure_interval_(repeat_failure_interval),
delegate_(delegate) { delegate_(delegate) {
web_state_->AddObserver(this);
AddApplicationDidBecomeActiveObserver(); AddApplicationDidBecomeActiveObserver();
} }
SadTabTabHelper::~SadTabTabHelper() { SadTabTabHelper::~SadTabTabHelper() {
DCHECK(!application_did_become_active_observer_); DCHECK(!application_did_become_active_observer_);
DCHECK(!web_state_);
} }
void SadTabTabHelper::CreateForWebState(web::WebState* web_state, void SadTabTabHelper::CreateForWebState(web::WebState* web_state,
...@@ -73,6 +76,7 @@ void SadTabTabHelper::SetDelegate(id<SadTabTabHelperDelegate> delegate) { ...@@ -73,6 +76,7 @@ void SadTabTabHelper::SetDelegate(id<SadTabTabHelperDelegate> delegate) {
} }
void SadTabTabHelper::WasShown(web::WebState* web_state) { void SadTabTabHelper::WasShown(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
if (requires_reload_on_becoming_visible_) { if (requires_reload_on_becoming_visible_) {
ReloadTab(); ReloadTab();
requires_reload_on_becoming_visible_ = false; requires_reload_on_becoming_visible_ = false;
...@@ -80,6 +84,7 @@ void SadTabTabHelper::WasShown(web::WebState* web_state) { ...@@ -80,6 +84,7 @@ void SadTabTabHelper::WasShown(web::WebState* web_state) {
} }
void SadTabTabHelper::RenderProcessGone(web::WebState* web_state) { void SadTabTabHelper::RenderProcessGone(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
if (!web_state->IsVisible()) { if (!web_state->IsVisible()) {
requires_reload_on_becoming_visible_ = true; requires_reload_on_becoming_visible_ = true;
return; return;
...@@ -99,6 +104,7 @@ void SadTabTabHelper::RenderProcessGone(web::WebState* web_state) { ...@@ -99,6 +104,7 @@ void SadTabTabHelper::RenderProcessGone(web::WebState* web_state) {
void SadTabTabHelper::DidFinishNavigation( void SadTabTabHelper::DidFinishNavigation(
web::WebState* web_state, web::WebState* web_state,
web::NavigationContext* navigation_context) { web::NavigationContext* navigation_context) {
DCHECK_EQ(web_state_, web_state);
if (navigation_context->GetUrl().host() == kChromeUICrashHost && if (navigation_context->GetUrl().host() == kChromeUICrashHost &&
navigation_context->GetUrl().scheme() == kChromeUIScheme) { navigation_context->GetUrl().scheme() == kChromeUIScheme) {
PresentSadTab(navigation_context->GetUrl()); PresentSadTab(navigation_context->GetUrl());
...@@ -106,6 +112,9 @@ void SadTabTabHelper::DidFinishNavigation( ...@@ -106,6 +112,9 @@ void SadTabTabHelper::DidFinishNavigation(
} }
void SadTabTabHelper::WebStateDestroyed(web::WebState* web_state) { void SadTabTabHelper::WebStateDestroyed(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
web_state_->RemoveObserver(this);
web_state_ = nullptr;
RemoveApplicationDidBecomeActiveObserver(); RemoveApplicationDidBecomeActiveObserver();
} }
...@@ -120,22 +129,23 @@ void SadTabTabHelper::PresentSadTab(const GURL& url_causing_failure) { ...@@ -120,22 +129,23 @@ void SadTabTabHelper::PresentSadTab(const GURL& url_causing_failure) {
seconds_since_last_failure < repeat_failure_interval_); seconds_since_last_failure < repeat_failure_interval_);
[delegate_ sadTabTabHelper:this [delegate_ sadTabTabHelper:this
presentSadTabForRepeatedFailure:repeated_failure]; presentSadTabForWebState:web_state_
repeatedFailure:repeated_failure];
last_failed_url_ = url_causing_failure; last_failed_url_ = url_causing_failure;
last_failed_timer_ = base::MakeUnique<base::ElapsedTimer>(); last_failed_timer_ = base::MakeUnique<base::ElapsedTimer>();
} }
void SadTabTabHelper::ReloadTab() { void SadTabTabHelper::ReloadTab() {
PagePlaceholderTabHelper::FromWebState(web_state()) PagePlaceholderTabHelper::FromWebState(web_state_)
->AddPlaceholderForNextNavigation(); ->AddPlaceholderForNextNavigation();
web_state()->GetNavigationManager()->LoadIfNecessary(); web_state_->GetNavigationManager()->LoadIfNecessary();
} }
void SadTabTabHelper::OnAppDidBecomeActive() { void SadTabTabHelper::OnAppDidBecomeActive() {
if (!requires_reload_on_becoming_active_) if (!requires_reload_on_becoming_active_)
return; return;
if (web_state()->IsVisible()) { if (web_state_->IsVisible()) {
ReloadTab(); ReloadTab();
} else { } else {
requires_reload_on_becoming_visible_ = true; requires_reload_on_becoming_visible_ = true;
......
...@@ -9,12 +9,17 @@ ...@@ -9,12 +9,17 @@
class SadTabTabHelper; class SadTabTabHelper;
namespace web {
class WebState;
}
// Delegate for SadTabTabHelper. // Delegate for SadTabTabHelper.
@protocol SadTabTabHelperDelegate<NSObject> @protocol SadTabTabHelperDelegate<NSObject>
// Asks the delegate to present a SadTabView. // Asks the delegate to present a SadTabView.
- (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper - (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper
presentSadTabForRepeatedFailure:(BOOL)repeatedFailure; presentSadTabForWebState:(web::WebState*)webState
repeatedFailure:(BOOL)repeatedFailure;
@end @end
......
...@@ -33,11 +33,12 @@ ...@@ -33,11 +33,12 @@
@synthesize repeatedFailure = _repeatedFailure; @synthesize repeatedFailure = _repeatedFailure;
- (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper - (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper
presentSadTabForRepeatedFailure:(BOOL)repeatedFailure { presentSadTabForWebState:(web::WebState*)webState
repeatedFailure:(BOOL)repeatedFailure {
self.repeatedFailure = repeatedFailure; self.repeatedFailure = repeatedFailure;
CRWContentView* contentView = [[CRWGenericContentView alloc] CRWContentView* contentView = [[CRWGenericContentView alloc]
initWithView:[[UIView alloc] initWithFrame:CGRectZero]]; initWithView:[[UIView alloc] initWithFrame:CGRectZero]];
tabHelper->web_state()->ShowTransientContentView(contentView); webState->ShowTransientContentView(contentView);
} }
@end @end
......
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