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 @@
#pragma mark - SadTabTabHelperDelegate
- (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper
presentSadTabForRepeatedFailure:(BOOL)repeatedFailure {
presentSadTabForWebState:(web::WebState*)webState
repeatedFailure:(BOOL)repeatedFailure {
// Create a SadTabView so |webstate| presents it.
SadTabView* sadTabview = [[SadTabView alloc]
initWithMode:repeatedFailure ? SadTabViewMode::FEEDBACK
: SadTabViewMode::RELOAD
navigationManager:tabHelper->web_state()->GetNavigationManager()];
navigationManager:webState->GetNavigationManager()];
sadTabview.dispatcher = static_cast<id<ApplicationCommands>>(self.dispatcher);
sadTabview.actionDelegate = self;
CRWContentView* contentView =
[[CRWGenericContentView alloc] initWithView:sadTabview];
tabHelper->web_state()->ShowTransientContentView(contentView);
webState->ShowTransientContentView(contentView);
}
@end
......@@ -78,6 +78,10 @@ class SadTabTabHelper : public web::WebStateUserData<SadTabTabHelper>,
// to be considered a repeat failure.
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,
// used to detect repeated crashes.
GURL last_failed_url_;
......
......@@ -7,6 +7,7 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/sys_string_conversions.h"
#include "ios/chrome/browser/chrome_url_constants.h"
......@@ -38,14 +39,16 @@ SadTabTabHelper::SadTabTabHelper(web::WebState* web_state,
SadTabTabHelper::SadTabTabHelper(web::WebState* web_state,
double repeat_failure_interval,
id<SadTabTabHelperDelegate> delegate)
: web::WebStateObserver(web_state),
: web_state_(web_state),
repeat_failure_interval_(repeat_failure_interval),
delegate_(delegate) {
web_state_->AddObserver(this);
AddApplicationDidBecomeActiveObserver();
}
SadTabTabHelper::~SadTabTabHelper() {
DCHECK(!application_did_become_active_observer_);
DCHECK(!web_state_);
}
void SadTabTabHelper::CreateForWebState(web::WebState* web_state,
......@@ -73,6 +76,7 @@ void SadTabTabHelper::SetDelegate(id<SadTabTabHelperDelegate> delegate) {
}
void SadTabTabHelper::WasShown(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
if (requires_reload_on_becoming_visible_) {
ReloadTab();
requires_reload_on_becoming_visible_ = false;
......@@ -80,6 +84,7 @@ void SadTabTabHelper::WasShown(web::WebState* web_state) {
}
void SadTabTabHelper::RenderProcessGone(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
if (!web_state->IsVisible()) {
requires_reload_on_becoming_visible_ = true;
return;
......@@ -99,6 +104,7 @@ void SadTabTabHelper::RenderProcessGone(web::WebState* web_state) {
void SadTabTabHelper::DidFinishNavigation(
web::WebState* web_state,
web::NavigationContext* navigation_context) {
DCHECK_EQ(web_state_, web_state);
if (navigation_context->GetUrl().host() == kChromeUICrashHost &&
navigation_context->GetUrl().scheme() == kChromeUIScheme) {
PresentSadTab(navigation_context->GetUrl());
......@@ -106,6 +112,9 @@ void SadTabTabHelper::DidFinishNavigation(
}
void SadTabTabHelper::WebStateDestroyed(web::WebState* web_state) {
DCHECK_EQ(web_state_, web_state);
web_state_->RemoveObserver(this);
web_state_ = nullptr;
RemoveApplicationDidBecomeActiveObserver();
}
......@@ -120,22 +129,23 @@ void SadTabTabHelper::PresentSadTab(const GURL& url_causing_failure) {
seconds_since_last_failure < repeat_failure_interval_);
[delegate_ sadTabTabHelper:this
presentSadTabForRepeatedFailure:repeated_failure];
presentSadTabForWebState:web_state_
repeatedFailure:repeated_failure];
last_failed_url_ = url_causing_failure;
last_failed_timer_ = base::MakeUnique<base::ElapsedTimer>();
}
void SadTabTabHelper::ReloadTab() {
PagePlaceholderTabHelper::FromWebState(web_state())
PagePlaceholderTabHelper::FromWebState(web_state_)
->AddPlaceholderForNextNavigation();
web_state()->GetNavigationManager()->LoadIfNecessary();
web_state_->GetNavigationManager()->LoadIfNecessary();
}
void SadTabTabHelper::OnAppDidBecomeActive() {
if (!requires_reload_on_becoming_active_)
return;
if (web_state()->IsVisible()) {
if (web_state_->IsVisible()) {
ReloadTab();
} else {
requires_reload_on_becoming_visible_ = true;
......
......@@ -9,12 +9,17 @@
class SadTabTabHelper;
namespace web {
class WebState;
}
// Delegate for SadTabTabHelper.
@protocol SadTabTabHelperDelegate<NSObject>
// Asks the delegate to present a SadTabView.
- (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper
presentSadTabForRepeatedFailure:(BOOL)repeatedFailure;
presentSadTabForWebState:(web::WebState*)webState
repeatedFailure:(BOOL)repeatedFailure;
@end
......
......@@ -33,11 +33,12 @@
@synthesize repeatedFailure = _repeatedFailure;
- (void)sadTabTabHelper:(SadTabTabHelper*)tabHelper
presentSadTabForRepeatedFailure:(BOOL)repeatedFailure {
presentSadTabForWebState:(web::WebState*)webState
repeatedFailure:(BOOL)repeatedFailure {
self.repeatedFailure = repeatedFailure;
CRWContentView* contentView = [[CRWGenericContentView alloc]
initWithView:[[UIView alloc] initWithFrame:CGRectZero]];
tabHelper->web_state()->ShowTransientContentView(contentView);
webState->ShowTransientContentView(contentView);
}
@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