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

Side by Side Diff: chrome/browser/chromeos/arc/notification/arc_notification_item.cc

Issue 1477733002: Add ArcNotificationManager for new ARC notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a TODO comment about ArcServiceManager Created 5 years 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 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 #if defined(USE_ASH)
15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
16 #include "components/signin/core/account_id/account_id.h"
17 #endif
18
19 namespace {
20
21 // static
22 static const char* kNotificationIdPrefix = "ARC_NOTIFICATION_";
23
24 class ArcNotificationDelegate : public message_center::NotificationDelegate {
25 public:
26 explicit ArcNotificationDelegate(
27 base::WeakPtr<chromeos::ArcNotificationItem> item)
28 : item_(item) {}
29
30 void Close(bool by_user) override {
31 if (item_)
32 item_->Close(by_user);
33 }
34
35 // Indicates all notifications have a click handler. This changes the mouse
36 // cursor on hover.
37 // TODO(yoshiki): Return the corerct value according to the content intent
hidehiko 2015/12/11 05:18:00 nit: s/corerct/correct/
yoshiki 2015/12/14 15:19:29 Done.
38 // and the flags.
39 bool HasClickedListener() override { return true; }
40
41 void Click() override {
42 if (item_)
43 item_->Click();
44 }
45
46 private:
47 // The destractor is private since this class is ref-counted.
48 ~ArcNotificationDelegate() override {}
49
50 base::WeakPtr<chromeos::ArcNotificationItem> item_;
51 };
52
53 } // anonymous namespace
54
55 namespace chromeos {
56
57 ArcNotificationItem::ArcNotificationItem(
58 ArcNotificationManager* manager,
59 message_center::MessageCenter* message_center,
60 const arc::ArcNotificationData& data,
61 Profile* profile)
62 : manager_(manager),
63 message_center_(message_center),
64 profile_(profile),
65 weak_ptr_factory_(this) {
66 // This must be initialized after ArcBridgeService.
67 notification_key_ = data.key;
hidehiko 2015/12/11 05:18:00 Please move them to the initializer list.
yoshiki 2015/12/14 15:19:29 Done.
68 notification_id_ = kNotificationIdPrefix + notification_key_;
69
70 UpdateWithArcNotificationData(data);
hidehiko 2015/12/11 05:18:00 IMHO, you can defer the responsibility to call thi
yoshiki 2015/12/14 15:19:29 Done.
71 }
72
73 void ArcNotificationItem::UpdateWithArcNotificationData(
74 const arc::ArcNotificationData& data) {
75 DCHECK(notification_key_ == data.key);
76
77 message_center::RichNotificationData rich_data;
78 message_center::NotificationType type =
hidehiko 2015/12/11 05:18:00 I prefer move this to default:, as you set it in t
yoshiki 2015/12/14 15:19:29 I moved them into the switch block, since I remove
79 message_center::NOTIFICATION_TYPE_SIMPLE;
80 switch (data.type) {
81 case arc::ARC_NOTIFICATION_TYPE_PROGRESS:
82 type = message_center::NOTIFICATION_TYPE_PROGRESS;
83 rich_data.timestamp = base::Time::UnixEpoch() +
84 base::TimeDelta::FromMilliseconds(data.time);
85 rich_data.progress = std::floor(
86 static_cast<float>(data.progress_current) / data.progress_max * 100 +
87 0.5); // round off
hidehiko 2015/12/11 05:18:00 nit: std::round is your friend. BTW, the values,
yoshiki 2015/12/14 15:19:29 Thank you for letting me know round(), done.
88 break;
89 default:
90 // TODO(yoshiki): Implement other types.
91 break;
92 }
93
94 message_center::NotifierId notifier_id(
95 message_center::NotifierId::SYSTEM_COMPONENT, notification_id_);
96 #if defined(USE_ASH)
97 notifier_id.profile_id =
98 multi_user_util::GetAccountIdFromProfile(profile_).GetUserEmail();
99 #endif
100
101 DCHECK(!data.title.is_null());
102 DCHECK(!data.message.is_null());
103 notification_.reset(new message_center::Notification(
104 type, notification_id_, base::UTF8ToUTF16(data.title.get()),
105 base::UTF8ToUTF16(data.message.get()),
106 gfx::Image(), // Will be overriden by decODED IMAGE.
107 base::UTF8ToUTF16("arc"), // display source
108 GURL(), // origin url
109 notifier_id, rich_data,
110 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr())));
111
112 DCHECK(!data.icon_data.is_null());
113 DCHECK(data.icon_data.size() > 0);
114 std::string icon_data_str(data.icon_data.storage().begin(),
115 data.icon_data.storage().end()); // copy
116 ImageDecoder::Start(this, icon_data_str); // copy in the method.
117 }
118
119 ArcNotificationItem::~ArcNotificationItem() {}
120
121 void ArcNotificationItem::OnClosedFromAndroid() {
122 message_center_->RemoveNotification(notification_id_, true /* by_user */);
123 }
124
125 void ArcNotificationItem::Close(bool by_user) {
126 manager_->SendNotificationRemovedFromChrome(notification_key_);
127 }
128
129 void ArcNotificationItem::Click() {
130 manager_->SendNotificationClickedOnChrome(notification_key_);
131 }
132
133 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) {
134 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap);
135 notification_->set_icon(image);
136
137 message_center_->AddNotification(make_scoped_ptr(
hidehiko 2015/12/11 05:18:00 Looks race? - UpdateWithArcNotificationData(...).
yoshiki 2015/12/14 15:19:29 It shouldn't be occurred. Because this instance is
138 new message_center::Notification(*notification_))); // copy
hidehiko 2015/12/11 05:18:00 You do not need a copy here, instead you can move
yoshiki 2015/12/14 15:19:29 I thought I wanted to have the data stored to skip
139 }
140
141 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698