OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "chrome/browser/chromeos/arc/notification/arc_notification_item.h" | |
6 | |
7 #include "base/strings/string16.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "ui/gfx/image/image.h" | |
10 #include "ui/message_center/notification.h" | |
11 #include "ui/message_center/notification_types.h" | |
12 #include "ui/message_center/notifier_settings.h" | |
13 | |
14 namespace { | |
15 | |
16 // static | |
17 static const char* kNotificationIdPrefix = "ARC_NOTIFICATION_"; | |
18 | |
19 // static | |
20 const char kArcNotificationOrigin[] = "chrome://arc"; | |
21 | |
22 class ArcNotificationDelegate : public message_center::NotificationDelegate { | |
23 public: | |
24 explicit ArcNotificationDelegate( | |
25 base::WeakPtr<chromeos::ArcNotificationItem> item) | |
26 : item_(item) {} | |
27 | |
28 void Close(bool by_user) override { | |
29 if (item_) | |
30 item_->Close(by_user); | |
31 } | |
32 | |
33 bool HasClickedListener() override { return true; } | |
34 | |
35 void Click() override { | |
36 if (item_) | |
37 item_->Click(); | |
38 } | |
39 | |
40 private: | |
41 // The destractor is private since this classes is ref-counted. | |
42 ~ArcNotificationDelegate() override {} | |
43 | |
44 base::WeakPtr<chromeos::ArcNotificationItem> item_; | |
45 }; | |
46 | |
47 } // anonymous namespace | |
48 | |
49 namespace chromeos { | |
50 | |
51 ArcNotificationItem::ArcNotificationItem( | |
52 ArcNotificationManager* manager, | |
53 message_center::MessageCenter* message_center, | |
54 const arc::ArcNotificationData& data) | |
55 : manager_(manager), | |
56 message_center_(message_center), | |
57 data_(data), | |
58 weak_ptr_factory_(this) { | |
59 // This must be initialized after ArcBridgeService. | |
60 DCHECK(arc::ArcBridgeService::Get()); | |
61 | |
62 notification_id_ = kNotificationIdPrefix + data_.key; | |
63 | |
64 data_ = data; | |
65 std::string icon_data_str(data.icon_data.begin(), data.icon_data.end()); | |
66 ImageDecoder::Start(this, icon_data_str); | |
67 } | |
68 | |
69 ArcNotificationItem::~ArcNotificationItem() { | |
70 ImageDecoder::Cancel(this); // just in case | |
khmel1
2015/11/25 00:30:10
It is automatically canceled on DTOR
yoshiki
2015/11/25 09:40:34
Done.
| |
71 } | |
72 | |
73 void ArcNotificationItem::OnClosedFromAndroid() { | |
74 message_center_->RemoveNotification(notification_id_, true /* by_user */); | |
75 } | |
76 | |
77 void ArcNotificationItem::Close(bool by_user) { | |
78 manager_->NotifyNotificationRemovedFromChrome(data_.key); | |
79 } | |
80 | |
81 void ArcNotificationItem::Click() { | |
82 manager_->NotifyNotificationClickedFromChrome(data_.key); | |
83 } | |
84 | |
85 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { | |
86 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); | |
87 | |
88 message_center::RichNotificationData rich_data; | |
89 | |
90 message_center::NotificationType type = | |
91 message_center::NOTIFICATION_TYPE_SIMPLE; | |
92 switch (data_.type) { | |
93 case arc::NOTIFICATION_TYPE_PROGRESS: | |
94 type = message_center::NOTIFICATION_TYPE_PROGRESS; | |
95 rich_data.timestamp = data_.timestamp; | |
96 rich_data.progress = | |
97 std::floor(static_cast<float>(data_.progress_current) / | |
98 data_.progress_max * 100 + | |
99 0.5); | |
100 break; | |
101 default: | |
102 // TODO(yoshiki): Implement other types. | |
103 break; | |
104 } | |
105 | |
106 scoped_ptr<message_center::Notification> notification( | |
107 new message_center::Notification( | |
108 type, notification_id_, base::UTF8ToUTF16(data_.title), | |
109 base::UTF8ToUTF16(data_.message), image, | |
110 base::UTF8ToUTF16("arc"), // display source | |
111 GURL(kArcNotificationOrigin), // origin url | |
112 message_center::NotifierId( | |
113 message_center::NotifierId::SYSTEM_COMPONENT, notification_id_), | |
114 rich_data, | |
115 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); | |
116 message_center_->AddNotification(notification.Pass()); | |
117 } | |
118 | |
119 } // namespace chromeos | |
OLD | NEW |