Commit 8e06a6c4 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Move NetworkIconPurger functionality to ActiveNetworkIcon

NetworkIconPurger was added before ActiveNetworkIcon was introduced.
It now makes sense to move the very limited functionality to
ActiveNetworkIcon which already implements the required observer and
interface.

Bug: 862420
Change-Id: I6aa1a28ae18050563786f592beb082f32117ddb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1611873
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661383}
parent 395e464e
...@@ -747,8 +747,6 @@ component("ash") { ...@@ -747,8 +747,6 @@ component("ash") {
"system/network/network_icon_animation.cc", "system/network/network_icon_animation.cc",
"system/network/network_icon_animation.h", "system/network/network_icon_animation.h",
"system/network/network_icon_animation_observer.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.cc",
"system/network/network_info.h", "system/network/network_info.h",
"system/network/network_list.cc", "system/network/network_list.cc",
......
...@@ -29,6 +29,8 @@ using network_icon::NetworkIconState; ...@@ -29,6 +29,8 @@ using network_icon::NetworkIconState;
namespace { namespace {
const int kPurgeDelayMs = 500;
bool IsTrayIcon(network_icon::IconType icon_type) { bool IsTrayIcon(network_icon::IconType icon_type) {
return icon_type == network_icon::ICON_TYPE_TRAY_REGULAR || return icon_type == network_icon::ICON_TYPE_TRAY_REGULAR ||
icon_type == network_icon::ICON_TYPE_TRAY_OOBE; icon_type == network_icon::ICON_TYPE_TRAY_OOBE;
...@@ -58,7 +60,8 @@ base::Optional<NetworkIconState> GetConnectingOrConnected( ...@@ -58,7 +60,8 @@ base::Optional<NetworkIconState> GetConnectingOrConnected(
} // namespace } // namespace
ActiveNetworkIcon::ActiveNetworkIcon(service_manager::Connector* connector) { ActiveNetworkIcon::ActiveNetworkIcon(service_manager::Connector* connector)
: weak_ptr_factory_(this) {
if (connector) // May be null in tests. if (connector) // May be null in tests.
BindCrosNetworkConfig(connector); BindCrosNetworkConfig(connector);
} }
...@@ -295,7 +298,14 @@ void ActiveNetworkIcon::OnActiveNetworksChanged( ...@@ -295,7 +298,14 @@ void ActiveNetworkIcon::OnActiveNetworksChanged(
SetCellularUninitializedMsg(); SetCellularUninitializedMsg();
} }
void ActiveNetworkIcon::OnNetworkStateListChanged() {} void ActiveNetworkIcon::OnNetworkStateListChanged() {
if (purge_timer_.IsRunning())
return;
purge_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kPurgeDelayMs),
base::BindOnce(&ActiveNetworkIcon::PurgeNetworkIconCache,
weak_ptr_factory_.GetWeakPtr()));
}
void ActiveNetworkIcon::OnDeviceStateListChanged() { void ActiveNetworkIcon::OnDeviceStateListChanged() {
cros_network_config_ptr_->GetDeviceStateList(base::BindOnce( cros_network_config_ptr_->GetDeviceStateList(base::BindOnce(
...@@ -323,4 +333,20 @@ DeviceStateProperties* ActiveNetworkIcon::GetDevice(NetworkType type) { ...@@ -323,4 +333,20 @@ DeviceStateProperties* ActiveNetworkIcon::GetDevice(NetworkType type) {
return iter->second.get(); return iter->second.get();
} }
void ActiveNetworkIcon::PurgeNetworkIconCache() {
cros_network_config_ptr_->GetNetworkStateList(
NetworkFilter::New(FilterType::kVisible, NetworkType::kAll,
/*limit=*/0),
base::BindOnce(
[](std::vector<
chromeos::network_config::mojom::NetworkStatePropertiesPtr>
networks) {
std::set<std::string> network_guids;
for (const auto& iter : networks) {
network_guids.insert(iter->guid);
}
network_icon::PurgeNetworkIconCache(network_guids);
}));
}
} // namespace ash } // namespace ash
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h" #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding.h"
...@@ -40,6 +41,7 @@ namespace ash { ...@@ -40,6 +41,7 @@ namespace ash {
// network_icon_unittest.cc, and partially in active_network_icon_unittest.cc. // network_icon_unittest.cc, and partially in active_network_icon_unittest.cc.
// TODO(stevenjb): Move all test coverage to active_network_icon_unittest.cc and // TODO(stevenjb): Move all test coverage to active_network_icon_unittest.cc and
// test Dual icon methods. // test Dual icon methods.
// This class is also responsible for periodically purging the icon cache.
class ASH_EXPORT ActiveNetworkIcon class ASH_EXPORT ActiveNetworkIcon
: public chromeos::network_config::mojom::CrosNetworkConfigObserver { : public chromeos::network_config::mojom::CrosNetworkConfigObserver {
public: public:
...@@ -91,6 +93,7 @@ class ASH_EXPORT ActiveNetworkIcon ...@@ -91,6 +93,7 @@ class ASH_EXPORT ActiveNetworkIcon
devices); devices);
chromeos::network_config::mojom::DeviceStateProperties* GetDevice( chromeos::network_config::mojom::DeviceStateProperties* GetDevice(
chromeos::network_config::mojom::NetworkType type); chromeos::network_config::mojom::NetworkType type);
void PurgeNetworkIconCache();
chromeos::network_config::mojom::CrosNetworkConfigPtr chromeos::network_config::mojom::CrosNetworkConfigPtr
cros_network_config_ptr_; cros_network_config_ptr_;
...@@ -106,6 +109,8 @@ class ASH_EXPORT ActiveNetworkIcon ...@@ -106,6 +109,8 @@ class ASH_EXPORT ActiveNetworkIcon
base::Optional<network_icon::NetworkIconState> active_vpn_; base::Optional<network_icon::NetworkIconState> active_vpn_;
int cellular_uninitialized_msg_ = 0; int cellular_uninitialized_msg_ = 0;
base::Time uninitialized_state_time_; base::Time uninitialized_state_time_;
base::OneShotTimer purge_timer_;
base::WeakPtrFactory<ActiveNetworkIcon> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ActiveNetworkIcon); DISALLOW_COPY_AND_ASSIGN(ActiveNetworkIcon);
}; };
......
// 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;
using chromeos::NetworkStateHandler;
namespace ash {
namespace {
const int kPurgeDelayMs = 300;
void PurgeNetworkIconCache() {
NetworkStateHandler::NetworkStateList networks;
NetworkHandler::Get()->network_state_handler()->GetVisibleNetworkList(
&networks);
std::set<std::string> network_guids;
for (NetworkStateHandler::NetworkStateList::iterator iter = networks.begin();
iter != networks.end(); ++iter) {
network_guids.insert((*iter)->guid());
}
network_icon::PurgeNetworkIconCache(network_guids);
}
} // namespace
NetworkIconPurger::NetworkIconPurger() {
// NetworkHandler may not be initialized in tests.
if (NetworkHandler::IsInitialized()) {
NetworkHandler::Get()->network_state_handler()->AddObserver(this,
FROM_HERE);
}
}
NetworkIconPurger::~NetworkIconPurger() {
// NetworkHandler may not be initialized in tests.
if (NetworkHandler::IsInitialized()) {
NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
FROM_HERE);
}
}
void NetworkIconPurger::NetworkListChanged() {
if (timer_.IsRunning())
return;
timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPurgeDelayMs),
base::BindOnce(&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_
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "ash/system/network/network_row_title_view.h" #include "ash/system/network/network_row_title_view.h"
#include "ash/system/tray/tray_popup_utils.h" #include "ash/system/tray/tray_popup_utils.h"
#include "ash/system/tray/tri_view.h" #include "ash/system/tray/tri_view.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chromeos/network/network_state_handler.h" #include "chromeos/network/network_state_handler.h"
#include "ui/views/controls/button/toggle_button.h" #include "ui/views/controls/button/toggle_button.h"
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "ash/system/message_center/message_center_ui_delegate.h" #include "ash/system/message_center/message_center_ui_delegate.h"
#include "ash/system/model/clock_model.h" #include "ash/system/model/clock_model.h"
#include "ash/system/model/system_tray_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/network/network_tray_view.h"
#include "ash/system/power/tray_power.h" #include "ash/system/power/tray_power.h"
#include "ash/system/status_area_widget.h" #include "ash/system/status_area_widget.h"
...@@ -123,7 +122,6 @@ UnifiedSystemTray::UnifiedSystemTray(Shelf* shelf) ...@@ -123,7 +122,6 @@ UnifiedSystemTray::UnifiedSystemTray(Shelf* shelf)
model_(std::make_unique<UnifiedSystemTrayModel>()), model_(std::make_unique<UnifiedSystemTrayModel>()),
slider_bubble_controller_( slider_bubble_controller_(
std::make_unique<UnifiedSliderBubbleController>(this)), std::make_unique<UnifiedSliderBubbleController>(this)),
network_icon_purger_(std::make_unique<NetworkIconPurger>()),
current_locale_view_(new CurrentLocaleView(shelf)), current_locale_view_(new CurrentLocaleView(shelf)),
ime_mode_view_(new ImeModeView(shelf)), ime_mode_view_(new ImeModeView(shelf)),
managed_device_view_(new ManagedDeviceView(shelf)), managed_device_view_(new ManagedDeviceView(shelf)),
......
...@@ -26,7 +26,6 @@ class QuietModeView; ...@@ -26,7 +26,6 @@ class QuietModeView;
class UnifiedSliderBubbleController; class UnifiedSliderBubbleController;
class UnifiedSystemTrayBubble; class UnifiedSystemTrayBubble;
class UnifiedSystemTrayModel; class UnifiedSystemTrayModel;
class NetworkIconPurger;
// The UnifiedSystemTray is the system menu of Chromium OS, which is a clickable // The UnifiedSystemTray is the system menu of Chromium OS, which is a clickable
// rounded rectangle typically located on the bottom right corner of the screen, // rounded rectangle typically located on the bottom right corner of the screen,
...@@ -130,8 +129,6 @@ class ASH_EXPORT UnifiedSystemTray : public TrayBackgroundView { ...@@ -130,8 +129,6 @@ class ASH_EXPORT UnifiedSystemTray : public TrayBackgroundView {
const std::unique_ptr<UnifiedSliderBubbleController> const std::unique_ptr<UnifiedSliderBubbleController>
slider_bubble_controller_; slider_bubble_controller_;
const std::unique_ptr<NetworkIconPurger> network_icon_purger_;
CurrentLocaleView* const current_locale_view_; CurrentLocaleView* const current_locale_view_;
ImeModeView* const ime_mode_view_; ImeModeView* const ime_mode_view_;
ManagedDeviceView* const managed_device_view_; ManagedDeviceView* const managed_device_view_;
......
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