Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Side by Side Diff: ash/common/system/chromeos/network/tray_network.cc

Issue 2734653002: chromeos: Move files in //ash/common to //ash (Closed)
Patch Set: fix a11y tests, fix docs Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ash/common/system/chromeos/network/tray_network.h"
6
7 #include "ash/common/shelf/wm_shelf_util.h"
8 #include "ash/common/system/chromeos/network/network_icon.h"
9 #include "ash/common/system/chromeos/network/network_icon_animation.h"
10 #include "ash/common/system/chromeos/network/network_icon_animation_observer.h"
11 #include "ash/common/system/chromeos/network/network_state_list_detailed_view.h"
12 #include "ash/common/system/chromeos/network/tray_network_state_observer.h"
13 #include "ash/common/system/tray/system_tray.h"
14 #include "ash/common/system/tray/system_tray_delegate.h"
15 #include "ash/common/system/tray/system_tray_notifier.h"
16 #include "ash/common/system/tray/tray_constants.h"
17 #include "ash/common/system/tray/tray_item_more.h"
18 #include "ash/common/system/tray/tray_item_view.h"
19 #include "ash/common/system/tray/tray_popup_item_style.h"
20 #include "ash/common/system/tray/tray_utils.h"
21 #include "ash/common/wm_shell.h"
22 #include "ash/resources/grit/ash_resources.h"
23 #include "ash/strings/grit/ash_strings.h"
24 #include "base/command_line.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "chromeos/network/network_state.h"
27 #include "chromeos/network/network_state_handler.h"
28 #include "third_party/cros_system_api/dbus/service_constants.h"
29 #include "ui/accessibility/ax_node_data.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/views/controls/image_view.h"
33 #include "ui/views/controls/link.h"
34 #include "ui/views/controls/link_listener.h"
35 #include "ui/views/layout/box_layout.h"
36 #include "ui/views/widget/widget.h"
37
38 using chromeos::NetworkHandler;
39 using chromeos::NetworkState;
40 using chromeos::NetworkStateHandler;
41 using chromeos::NetworkTypePattern;
42
43 namespace ash {
44 namespace tray {
45
46 namespace {
47
48 // Returns the connected, non-virtual (aka VPN), network.
49 const NetworkState* GetConnectedNetwork() {
50 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
51 return handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual());
52 }
53
54 } // namespace
55
56 class NetworkTrayView : public TrayItemView,
57 public network_icon::AnimationObserver {
58 public:
59 explicit NetworkTrayView(TrayNetwork* network_tray)
60 : TrayItemView(network_tray) {
61 CreateImageView();
62 UpdateNetworkStateHandlerIcon();
63 }
64
65 ~NetworkTrayView() override {
66 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
67 }
68
69 const char* GetClassName() const override { return "NetworkTrayView"; }
70
71 void UpdateNetworkStateHandlerIcon() {
72 gfx::ImageSkia image;
73 base::string16 name;
74 bool animating = false;
75 network_icon::GetDefaultNetworkImageAndLabel(network_icon::ICON_TYPE_TRAY,
76 &image, &name, &animating);
77 bool show_in_tray = !image.isNull();
78 UpdateIcon(show_in_tray, image);
79 if (animating)
80 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
81 else
82 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
83 // Update accessibility.
84 const NetworkState* connected_network = GetConnectedNetwork();
85 if (connected_network) {
86 UpdateConnectionStatus(base::UTF8ToUTF16(connected_network->name()),
87 true);
88 } else {
89 UpdateConnectionStatus(base::string16(), false);
90 }
91 }
92
93 // views::View:
94 void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
95 node_data->SetName(connection_status_string_);
96 node_data->role = ui::AX_ROLE_BUTTON;
97 }
98
99 // network_icon::AnimationObserver:
100 void NetworkIconChanged() override { UpdateNetworkStateHandlerIcon(); }
101
102 private:
103 // Updates connection status and notifies accessibility event when necessary.
104 void UpdateConnectionStatus(const base::string16& network_name,
105 bool connected) {
106 base::string16 new_connection_status_string;
107 if (connected) {
108 new_connection_status_string = l10n_util::GetStringFUTF16(
109 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTED, network_name);
110 }
111 if (new_connection_status_string != connection_status_string_) {
112 connection_status_string_ = new_connection_status_string;
113 if (!connection_status_string_.empty())
114 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
115 }
116 }
117
118 void UpdateIcon(bool tray_icon_visible, const gfx::ImageSkia& image) {
119 image_view()->SetImage(image);
120 SetVisible(tray_icon_visible);
121 SchedulePaint();
122 }
123
124 base::string16 connection_status_string_;
125
126 DISALLOW_COPY_AND_ASSIGN(NetworkTrayView);
127 };
128
129 class NetworkDefaultView : public TrayItemMore,
130 public network_icon::AnimationObserver {
131 public:
132 explicit NetworkDefaultView(TrayNetwork* network_tray)
133 : TrayItemMore(network_tray) {
134 Update();
135 }
136
137 ~NetworkDefaultView() override {
138 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
139 }
140
141 void Update() {
142 gfx::ImageSkia image;
143 base::string16 label;
144 bool animating = false;
145 // TODO(bruthig): Update the image to use the proper color. See
146 // https://crbug.com/632027.
147 network_icon::GetDefaultNetworkImageAndLabel(
148 network_icon::ICON_TYPE_DEFAULT_VIEW, &image, &label, &animating);
149 if (animating)
150 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
151 else
152 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
153 SetImage(image);
154 SetLabel(label);
155 SetAccessibleName(label);
156 UpdateStyle();
157 }
158
159 // network_icon::AnimationObserver
160 void NetworkIconChanged() override { Update(); }
161
162 protected:
163 // TrayItemMore:
164 std::unique_ptr<TrayPopupItemStyle> HandleCreateStyle() const override {
165 std::unique_ptr<TrayPopupItemStyle> style =
166 TrayItemMore::HandleCreateStyle();
167 style->set_color_style(GetConnectedNetwork() != nullptr
168 ? TrayPopupItemStyle::ColorStyle::ACTIVE
169 : TrayPopupItemStyle::ColorStyle::INACTIVE);
170 return style;
171 }
172
173 private:
174 DISALLOW_COPY_AND_ASSIGN(NetworkDefaultView);
175 };
176
177 class NetworkWifiDetailedView : public NetworkDetailedView {
178 public:
179 explicit NetworkWifiDetailedView(SystemTrayItem* owner)
180 : NetworkDetailedView(owner) {
181 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
182 kTrayPopupPaddingHorizontal, 10,
183 kTrayPopupPaddingBetweenItems));
184 image_view_ = new views::ImageView;
185 AddChildView(image_view_);
186
187 label_view_ = new views::Label();
188 label_view_->SetMultiLine(true);
189 label_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
190 AddChildView(label_view_);
191
192 Update();
193 }
194
195 ~NetworkWifiDetailedView() override {}
196
197 // Overridden from NetworkDetailedView:
198
199 void Init() override {}
200
201 NetworkDetailedView::DetailedViewType GetViewType() const override {
202 return NetworkDetailedView::WIFI_VIEW;
203 }
204
205 void Layout() override {
206 // Center both views vertically.
207 views::View::Layout();
208 image_view_->SetY((height() - image_view_->GetPreferredSize().height()) /
209 2);
210 label_view_->SetY((height() - label_view_->GetPreferredSize().height()) /
211 2);
212 }
213
214 void Update() override {
215 bool wifi_enabled =
216 NetworkHandler::Get()->network_state_handler()->IsTechnologyEnabled(
217 NetworkTypePattern::WiFi());
218 const int image_id = wifi_enabled ? IDR_AURA_UBER_TRAY_WIFI_ENABLED
219 : IDR_AURA_UBER_TRAY_WIFI_DISABLED;
220 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
221 image_view_->SetImage(bundle.GetImageNamed(image_id).ToImageSkia());
222
223 const int string_id = wifi_enabled
224 ? IDS_ASH_STATUS_TRAY_NETWORK_WIFI_ENABLED
225 : IDS_ASH_STATUS_TRAY_NETWORK_WIFI_DISABLED;
226 label_view_->SetText(bundle.GetLocalizedString(string_id));
227 label_view_->SizeToFit(
228 kTrayPopupMinWidth - kTrayPopupPaddingHorizontal * 2 -
229 kTrayPopupPaddingBetweenItems - kTrayPopupDetailsIconWidth);
230 }
231
232 private:
233 views::ImageView* image_view_;
234 views::Label* label_view_;
235
236 DISALLOW_COPY_AND_ASSIGN(NetworkWifiDetailedView);
237 };
238
239 } // namespace tray
240
241 TrayNetwork::TrayNetwork(SystemTray* system_tray)
242 : SystemTrayItem(system_tray, UMA_NETWORK),
243 tray_(NULL),
244 default_(NULL),
245 detailed_(NULL),
246 request_wifi_view_(false) {
247 network_state_observer_.reset(new TrayNetworkStateObserver(this));
248 SystemTrayNotifier* notifier = WmShell::Get()->system_tray_notifier();
249 notifier->AddNetworkObserver(this);
250 notifier->AddNetworkPortalDetectorObserver(this);
251 }
252
253 TrayNetwork::~TrayNetwork() {
254 SystemTrayNotifier* notifier = WmShell::Get()->system_tray_notifier();
255 notifier->RemoveNetworkObserver(this);
256 notifier->RemoveNetworkPortalDetectorObserver(this);
257 }
258
259 views::View* TrayNetwork::CreateTrayView(LoginStatus status) {
260 CHECK(tray_ == NULL);
261 if (!chromeos::NetworkHandler::IsInitialized())
262 return NULL;
263 tray_ = new tray::NetworkTrayView(this);
264 return tray_;
265 }
266
267 views::View* TrayNetwork::CreateDefaultView(LoginStatus status) {
268 CHECK(default_ == NULL);
269 if (!chromeos::NetworkHandler::IsInitialized())
270 return NULL;
271 CHECK(tray_ != NULL);
272 default_ = new tray::NetworkDefaultView(this);
273 default_->SetEnabled(status != LoginStatus::LOCKED);
274 return default_;
275 }
276
277 views::View* TrayNetwork::CreateDetailedView(LoginStatus status) {
278 CHECK(detailed_ == NULL);
279 WmShell::Get()->RecordUserMetricsAction(
280 UMA_STATUS_AREA_DETAILED_NETWORK_VIEW);
281 if (!chromeos::NetworkHandler::IsInitialized())
282 return NULL;
283 if (request_wifi_view_) {
284 detailed_ = new tray::NetworkWifiDetailedView(this);
285 request_wifi_view_ = false;
286 } else {
287 detailed_ = new tray::NetworkStateListDetailedView(
288 this, tray::NetworkStateListDetailedView::LIST_TYPE_NETWORK, status);
289 detailed_->Init();
290 }
291 return detailed_;
292 }
293
294 void TrayNetwork::DestroyTrayView() {
295 tray_ = NULL;
296 }
297
298 void TrayNetwork::DestroyDefaultView() {
299 default_ = NULL;
300 }
301
302 void TrayNetwork::DestroyDetailedView() {
303 detailed_ = NULL;
304 }
305
306 void TrayNetwork::UpdateAfterLoginStatusChange(LoginStatus status) {}
307
308 void TrayNetwork::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
309 if (tray_)
310 SetTrayImageItemBorder(tray_, alignment);
311 }
312
313 void TrayNetwork::RequestToggleWifi() {
314 // This will always be triggered by a user action (e.g. keyboard shortcut)
315 if (!detailed_ ||
316 detailed_->GetViewType() == tray::NetworkDetailedView::WIFI_VIEW) {
317 request_wifi_view_ = true;
318 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false);
319 }
320 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
321 bool enabled = handler->IsTechnologyEnabled(NetworkTypePattern::WiFi());
322 WmShell::Get()->RecordUserMetricsAction(
323 enabled ? UMA_STATUS_AREA_DISABLE_WIFI : UMA_STATUS_AREA_ENABLE_WIFI);
324 handler->SetTechnologyEnabled(NetworkTypePattern::WiFi(), !enabled,
325 chromeos::network_handler::ErrorCallback());
326 }
327
328 void TrayNetwork::OnCaptivePortalDetected(const std::string& /* guid */) {
329 NetworkStateChanged();
330 }
331
332 void TrayNetwork::NetworkStateChanged() {
333 if (tray_)
334 tray_->UpdateNetworkStateHandlerIcon();
335 if (default_)
336 default_->Update();
337 if (detailed_)
338 detailed_->Update();
339 }
340
341 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/network/tray_network.h ('k') | ash/common/system/chromeos/network/tray_network_state_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698