blob: 2d2e89f125805534c7e9ac9ad7ddc5a246aa5a81 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/web/network_activity_indicator_tab_helper.h"
#include "base/memory/ptr_util.h"
#import "ios/chrome/browser/ui/network_activity_indicator_manager.h"
#import "ios/web/public/web_state/web_state.h"
DEFINE_WEB_STATE_USER_DATA_KEY(NetworkActivityIndicatorTabHelper);
// static
void NetworkActivityIndicatorTabHelper::CreateForWebState(
web::WebState* web_state,
NSString* tab_id) {
DCHECK(web_state);
if (!FromWebState(web_state)) {
web_state->SetUserData(
UserDataKey(), base::WrapUnique(new NetworkActivityIndicatorTabHelper(
web_state, tab_id)));
}
}
NetworkActivityIndicatorTabHelper::NetworkActivityIndicatorTabHelper(
web::WebState* web_state,
NSString* tab_id)
: web::WebStateObserver(web_state), network_activity_key_([tab_id copy]) {}
NetworkActivityIndicatorTabHelper::~NetworkActivityIndicatorTabHelper() {
Stop();
}
void NetworkActivityIndicatorTabHelper::DidStartLoading() {
NetworkActivityIndicatorManager* shared_manager =
[NetworkActivityIndicatorManager sharedInstance];
// Verifies that there are not any network tasks associated with this instance
// before starting another task, so that this method is idempotent.
if (![shared_manager numNetworkTasksForGroup:network_activity_key_])
[shared_manager startNetworkTaskForGroup:network_activity_key_];
}
void NetworkActivityIndicatorTabHelper::DidStopLoading() {
Stop();
}
void NetworkActivityIndicatorTabHelper::Stop() {
NetworkActivityIndicatorManager* shared_manager =
[NetworkActivityIndicatorManager sharedInstance];
// Verifies that there is a network task associated with this instance
// before stopping a task, so that this method is idempotent.
if ([shared_manager numNetworkTasksForGroup:network_activity_key_])
[shared_manager stopNetworkTaskForGroup:network_activity_key_];
}