Commit a49b0b14 authored by Tony de Luna's avatar Tony de Luna Committed by Commit Bot

Create an observer to purge network icons

This cl moves network icon cache purging to a new observer that is owned
by the unified system tray.

Currently there are five subclasses of
TrayNetworkStateObserver::Delegate. Every time we get a network list
update the global network icon cache is purged.
This means that for every update the cache is purged at least five
times.

With this cl TrayNetworkStateObserver::Delegate can still be used as a
simple way of subscribing to all network events without excessive icon
purging.

Bug: 902409
Change-Id: I078ac8dbdee473fc285ee8e91f735c99524c74a7
Reviewed-on: https://chromium-review.googlesource.com/c/1334835
Commit-Queue: Tony De Luna <tonydeluna@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608230}
parent 6d50e57f
......@@ -762,6 +762,8 @@ component("ash") {
"system/network/network_icon_animation.cc",
"system/network/network_icon_animation.h",
"system/network/network_icon_animation_observer.h",
"system/network/network_icon_purger.cc",
"system/network/network_icon_purger.h",
"system/network/network_info.cc",
"system/network/network_info.h",
"system/network/network_list.cc",
......
// Copyright 2018 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.
#include "ash/system/network/network_icon_purger.h"
#include "ash/system/network/network_icon.h"
#include "base/bind.h"
#include "chromeos/network/network_state_handler.h"
using chromeos::NetworkHandler;
namespace {
const int kPurgeDelayMs = 300;
} // namespace
namespace ash {
NetworkIconPurger::NetworkIconPurger() {
// NetworkHandler may not be initialized in tests.
if (NetworkHandler::IsInitialized()) {
auto* network_handler = NetworkHandler::Get();
network_handler->network_state_handler()->AddObserver(this, FROM_HERE);
}
}
NetworkIconPurger::~NetworkIconPurger() {
// NetworkHandler may not be initialized in tests.
if (NetworkHandler::IsInitialized()) {
auto* network_handler = NetworkHandler::Get();
network_handler->network_state_handler()->RemoveObserver(this, FROM_HERE);
}
}
void NetworkIconPurger::NetworkListChanged() {
if (timer_.IsRunning())
return;
timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPurgeDelayMs),
base::BindOnce(&network_icon::PurgeNetworkIconCache));
}
} // namespace ash
// Copyright 2018 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.
#ifndef ASH_SYSTEM_NETWORK_NETWORK_ICON_PURGER_H_
#define ASH_SYSTEM_NETWORK_NETWORK_ICON_PURGER_H_
#include "base/macros.h"
#include "base/timer/timer.h"
#include "chromeos/network/network_state_handler_observer.h"
namespace ash {
// Purges network icon caches when the network list is updated.
// Introduces a small delay before purging the cache. This prevents
// us from excessively purging icon caches when receiving updates
// in quick succession.
class NetworkIconPurger : public chromeos::NetworkStateHandlerObserver {
public:
NetworkIconPurger();
~NetworkIconPurger() override;
// chromeos::NetworkStateHandlerObserver
void NetworkListChanged() override;
private:
base::OneShotTimer timer_;
DISALLOW_COPY_AND_ASSIGN(NetworkIconPurger);
};
} // namespace ash
#endif // ASH_SYSTEM_NETWORK_NETWORK_ICON_PURGER_H_
......@@ -7,7 +7,6 @@
#include <set>
#include <string>
#include "ash/system/network/network_icon.h"
#include "base/location.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
......@@ -31,7 +30,6 @@ namespace ash {
TrayNetworkStateObserver::TrayNetworkStateObserver(Delegate* delegate)
: delegate_(delegate),
purge_icons_(false),
update_frequency_(kUpdateFrequencyMs) {
if (ui::ScopedAnimationDurationScaleMode::duration_scale_mode() !=
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION) {
......@@ -58,7 +56,6 @@ TrayNetworkStateObserver::~TrayNetworkStateObserver() {
}
void TrayNetworkStateObserver::NetworkListChanged() {
purge_icons_ = true;
SignalUpdate(false /* notify_a11y */);
}
......@@ -119,10 +116,6 @@ void TrayNetworkStateObserver::SignalUpdate(bool notify_a11y) {
void TrayNetworkStateObserver::SendNetworkStateChanged(bool notify_a11y) {
delegate_->NetworkStateChanged(notify_a11y);
if (purge_icons_) {
network_icon::PurgeNetworkIconCache();
purge_icons_ = false;
}
}
} // namespace ash
......@@ -52,9 +52,6 @@ class TrayNetworkStateObserver
// Unowned Delegate pointer (must outlive this instance).
Delegate* delegate_;
// Set to true when we should purge stale icons in the cache.
bool purge_icons_;
// Frequency at which to push NetworkStateChanged updates. This avoids
// unnecessarily frequent UI updates (which can be expensive). We set this
// to 0 for tests to eliminate timing variance.
......
......@@ -14,6 +14,7 @@
#include "ash/system/message_center/message_center_ui_delegate.h"
#include "ash/system/model/clock_model.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/network/network_icon_purger.h"
#include "ash/system/network/network_tray_view.h"
#include "ash/system/power/tray_power.h"
#include "ash/system/status_area_widget.h"
......@@ -118,6 +119,7 @@ UnifiedSystemTray::UnifiedSystemTray(Shelf* shelf)
model_(std::make_unique<UnifiedSystemTrayModel>()),
slider_bubble_controller_(
std::make_unique<UnifiedSliderBubbleController>(this)),
network_icon_purger_(std::make_unique<NetworkIconPurger>()),
ime_mode_view_(new ImeModeView(shelf)),
managed_device_view_(new ManagedDeviceView(shelf)),
notification_counter_item_(new NotificationCounterView(shelf)),
......
......@@ -25,6 +25,7 @@ class QuietModeView;
class UnifiedSliderBubbleController;
class UnifiedSystemTrayBubble;
class UnifiedSystemTrayModel;
class NetworkIconPurger;
// UnifiedSystemTray is system menu of Chromium OS, which is typically
// accessible from the button on the right bottom of the screen (Status Area).
......@@ -123,6 +124,8 @@ class ASH_EXPORT UnifiedSystemTray : public TrayBackgroundView {
const std::unique_ptr<UnifiedSliderBubbleController>
slider_bubble_controller_;
const std::unique_ptr<NetworkIconPurger> network_icon_purger_;
ImeModeView* const ime_mode_view_;
ManagedDeviceView* const managed_device_view_;
NotificationCounterView* const notification_counter_item_;
......
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