Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/system/chromeos/power/tray_power.h" | 5 #include "ash/common/system/chromeos/power/tray_power.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "ash/common/accessibility_delegate.h" | 9 #include "ash/common/accessibility_delegate.h" |
| 10 #include "ash/common/ash_switches.h" | 10 #include "ash/common/ash_switches.h" |
| 11 #include "ash/common/system/chromeos/devicetype_utils.h" | 11 #include "ash/common/system/chromeos/devicetype_utils.h" |
| 12 #include "ash/common/system/chromeos/power/battery_notification.h" | 12 #include "ash/common/system/chromeos/power/battery_notification.h" |
| 13 #include "ash/common/system/chromeos/power/dual_role_notification.h" | 13 #include "ash/common/system/chromeos/power/dual_role_notification.h" |
| 14 #include "ash/common/system/date/date_view.h" | 14 #include "ash/common/system/date/date_view.h" |
| 15 #include "ash/common/system/system_notifier.h" | 15 #include "ash/common/system/system_notifier.h" |
| 16 #include "ash/common/system/tray/system_tray_delegate.h" | 16 #include "ash/common/system/tray/system_tray_delegate.h" |
| 17 #include "ash/common/system/tray/tray_constants.h" | 17 #include "ash/common/system/tray/tray_constants.h" |
| 18 #include "ash/common/system/tray/tray_item_view.h" | 18 #include "ash/common/system/tray/tray_item_view.h" |
| 19 #include "ash/common/system/tray/tray_utils.h" | 19 #include "ash/common/system/tray/tray_utils.h" |
| 20 #include "ash/resources/grit/ash_resources.h" | 20 #include "ash/resources/grit/ash_resources.h" |
| 21 #include "ash/strings/grit/ash_strings.h" | 21 #include "ash/strings/grit/ash_strings.h" |
| 22 #include "base/command_line.h" | 22 #include "base/command_line.h" |
| 23 #include "base/logging.h" | 23 #include "base/logging.h" |
| 24 #include "base/metrics/histogram.h" | 24 #include "base/metrics/histogram.h" |
| 25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 26 #include "ui/accessibility/ax_node_data.h" | 26 #include "ui/accessibility/ax_node_data.h" |
| 27 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "ui/gfx/image/image_skia_source.h" | |
| 28 #include "ui/message_center/message_center.h" | 29 #include "ui/message_center/message_center.h" |
| 29 #include "ui/message_center/notification.h" | 30 #include "ui/message_center/notification.h" |
| 30 #include "ui/message_center/notification_delegate.h" | 31 #include "ui/message_center/notification_delegate.h" |
| 31 #include "ui/views/controls/image_view.h" | 32 #include "ui/views/controls/image_view.h" |
| 32 #include "ui/views/view.h" | 33 #include "ui/views/view.h" |
| 33 | 34 |
| 34 using message_center::MessageCenter; | 35 using message_center::MessageCenter; |
| 35 using message_center::Notification; | 36 using message_center::Notification; |
| 36 | 37 |
| 37 namespace ash { | 38 namespace ash { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 int remaining_minutes) { | 84 int remaining_minutes) { |
| 84 LOG(WARNING) << "Showing " << GetNotificationStateString(state) | 85 LOG(WARNING) << "Showing " << GetNotificationStateString(state) |
| 85 << " notification. No charger connected." | 86 << " notification. No charger connected." |
| 86 << " Remaining time: " << remaining_minutes << " minutes."; | 87 << " Remaining time: " << remaining_minutes << " minutes."; |
| 87 } | 88 } |
| 88 | 89 |
| 89 } // namespace | 90 } // namespace |
| 90 | 91 |
| 91 namespace tray { | 92 namespace tray { |
| 92 | 93 |
| 94 class PowerTrayImageSource : public gfx::ImageSkiaSource { | |
|
Peter Kasting
2017/03/25 00:22:30
Nit: I'm sorta inclined to declare this class insi
danakj
2017/03/31 20:49:04
More nesting :/ and it's in the .cc anyways. But d
| |
| 95 public: | |
| 96 PowerTrayImageSource(const PowerStatus::BatteryImageInfo& info) | |
|
Peter Kasting
2017/03/25 00:22:30
Nit: explicit
Maybe also pass by value and move i
danakj
2017/03/31 20:49:04
Done.
Peter Kasting
2017/03/31 22:07:20
Wouldn't these both be at worst one deep copy? In
danakj
2017/04/03 14:43:13
If we pass by const&
- the caller doesn't invoke a
Peter Kasting
2017/04/03 20:32:51
It sounds like pass-by-value + move-into-place is
| |
| 97 : info_(info) {} | |
| 98 | |
| 99 // gfx::ImageSkiaSource implementation. | |
| 100 gfx::ImageSkiaRep GetImageForScale(float scale) override { | |
| 101 return PowerStatus::Get()->GetBatteryImage(info_, scale); | |
| 102 } | |
| 103 | |
| 104 const PowerStatus::BatteryImageInfo& info() const { return info_; } | |
| 105 | |
| 106 private: | |
| 107 PowerStatus::BatteryImageInfo info_; | |
| 108 }; | |
|
Peter Kasting
2017/03/25 00:22:30
Nit: DISALLOW_COPY_AND_ASSIGN?
danakj
2017/03/31 20:49:04
Done.
| |
| 109 | |
| 93 // This view is used only for the tray. | 110 // This view is used only for the tray. |
| 94 class PowerTrayView : public TrayItemView { | 111 class PowerTrayView : public TrayItemView { |
| 95 public: | 112 public: |
| 96 explicit PowerTrayView(SystemTrayItem* owner) : TrayItemView(owner) { | 113 explicit PowerTrayView(SystemTrayItem* owner) : TrayItemView(owner) { |
| 97 CreateImageView(); | 114 CreateImageView(); |
| 98 UpdateImage(); | 115 UpdateImage(); |
| 99 } | 116 } |
| 100 | 117 |
| 101 ~PowerTrayView() override {} | 118 ~PowerTrayView() override {} |
| 102 | 119 |
| 103 // Overriden from views::View. | 120 // Overridden from views::View. |
| 104 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | 121 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| 105 node_data->SetName(accessible_name_); | 122 node_data->SetName(accessible_name_); |
| 106 node_data->role = ui::AX_ROLE_BUTTON; | 123 node_data->role = ui::AX_ROLE_BUTTON; |
| 107 } | 124 } |
| 108 | 125 |
| 109 void UpdateStatus(bool battery_alert) { | 126 void UpdateStatus(bool battery_alert) { |
| 110 UpdateImage(); | 127 UpdateImage(); |
| 111 SetVisible(PowerStatus::Get()->IsBatteryPresent()); | 128 SetVisible(PowerStatus::Get()->IsBatteryPresent()); |
| 112 | 129 |
| 113 if (battery_alert) { | 130 if (battery_alert) { |
| 114 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true); | 131 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true); |
| 115 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | 132 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); |
| 116 } | 133 } |
| 117 } | 134 } |
| 118 | 135 |
| 119 private: | 136 private: |
| 120 void UpdateImage() { | 137 void UpdateImage() { |
| 121 const PowerStatus::BatteryImageInfo info = | 138 const PowerStatus::BatteryImageInfo& info = |
| 122 PowerStatus::Get()->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); | 139 PowerStatus::Get()->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); |
| 123 if (info != previous_image_info_) { | 140 // Only change the image when the info changes. http://crbug.com/589348 |
| 124 image_view()->SetImage(PowerStatus::Get()->GetBatteryImage(info)); | 141 if (image_source_ && image_source_->info() == info) |
| 125 previous_image_info_ = info; | 142 return; |
| 126 } | 143 image_source_ = new PowerTrayImageSource(info); |
| 144 // gfx::ImageSkia takes ownership of image_source_. It will stay alive until | |
|
Peter Kasting
2017/03/25 00:22:30
Blargh, it'd sure be nice if it took a unique_ptr
danakj
2017/03/31 20:49:04
Yeeeeep. I also had a crash until I realized this
| |
| 145 // we call SetImage() again, which only happens here. | |
| 146 image_view()->SetImage( | |
| 147 gfx::ImageSkia(image_source_, PowerStatus::GetBatteryImageSizeInDip())); | |
| 127 } | 148 } |
| 128 | 149 |
| 129 base::string16 accessible_name_; | 150 base::string16 accessible_name_; |
| 130 | 151 PowerTrayImageSource* image_source_ = nullptr; |
| 131 // Information about the last-used image. Cached to avoid unnecessary updates | |
| 132 // (http://crbug.com/589348). | |
| 133 PowerStatus::BatteryImageInfo previous_image_info_; | |
| 134 | 152 |
| 135 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); | 153 DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| 136 }; | 154 }; |
| 137 | 155 |
| 138 } // namespace tray | 156 } // namespace tray |
| 139 | 157 |
| 140 const int TrayPower::kCriticalMinutes = 5; | 158 const int TrayPower::kCriticalMinutes = 5; |
| 141 const int TrayPower::kLowPowerMinutes = 15; | 159 const int TrayPower::kLowPowerMinutes = 15; |
| 142 const int TrayPower::kNoWarningMinutes = 30; | 160 const int TrayPower::kNoWarningMinutes = 30; |
| 143 const int TrayPower::kCriticalPercentage = 5; | 161 const int TrayPower::kCriticalPercentage = 5; |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 } | 364 } |
| 347 NOTREACHED(); | 365 NOTREACHED(); |
| 348 return false; | 366 return false; |
| 349 } | 367 } |
| 350 | 368 |
| 351 void TrayPower::NotifyUsbNotificationClosedByUser() { | 369 void TrayPower::NotifyUsbNotificationClosedByUser() { |
| 352 usb_notification_dismissed_ = true; | 370 usb_notification_dismissed_ = true; |
| 353 } | 371 } |
| 354 | 372 |
| 355 } // namespace ash | 373 } // namespace ash |
| OLD | NEW |