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

Side by Side Diff: ash/system/network/tray_sms.cc

Issue 10582028: Re-factor TraySms to use ash::SmsObserver instead of NetoworkSmsHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « ash/system/network/tray_sms.h ('k') | ash/system/tray/system_tray.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/system/network/tray_sms.h" 5 #include "ash/system/network/tray_sms.h"
6 6
7 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "ash/shell.h" 8 #include "ash/shell.h"
9 #include "ash/system/tray/system_tray.h" 9 #include "ash/system/tray/system_tray.h"
10 #include "ash/system/tray/tray_constants.h" 10 #include "ash/system/tray/tray_constants.h"
11 #include "ash/system/tray/tray_details_view.h" 11 #include "ash/system/tray/tray_details_view.h"
12 #include "ash/system/tray/tray_item_more.h" 12 #include "ash/system/tray/tray_item_more.h"
13 #include "ash/system/tray/tray_item_view.h" 13 #include "ash/system/tray/tray_item_view.h"
14 #include "ash/system/tray/tray_views.h" 14 #include "ash/system/tray/tray_views.h"
15 #include "base/command_line.h" 15 #include "base/command_line.h"
16 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "grit/ash_strings.h" 18 #include "grit/ash_strings.h"
20 #include "grit/ui_resources_standard.h" 19 #include "grit/ui_resources_standard.h"
21 #include "grit/ui_resources_standard.h" 20 #include "grit/ui_resources_standard.h"
22 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/views/controls/image_view.h" 23 #include "ui/views/controls/image_view.h"
25 #include "ui/views/controls/label.h" 24 #include "ui/views/controls/label.h"
26 #include "ui/views/layout/box_layout.h" 25 #include "ui/views/layout/box_layout.h"
27 #include "ui/views/layout/fill_layout.h" 26 #include "ui/views/layout/fill_layout.h"
28 #include "ui/views/layout/grid_layout.h" 27 #include "ui/views/layout/grid_layout.h"
29 #include "ui/views/view.h" 28 #include "ui/views/view.h"
30 29
31 #if defined(OS_CHROMEOS)
32 #include "chromeos/network/network_sms_handler.h"
33 #endif
34
35 namespace { 30 namespace {
36 31
37 // Min height of the list of messages in the popup. 32 // Min height of the list of messages in the popup.
38 const int kMessageListMinHeight = 200; 33 const int kMessageListMinHeight = 200;
39 // Top/bottom padding of the text items. 34 // Top/bottom padding of the text items.
40 const int kPaddingVertical = 10; 35 const int kPaddingVertical = 10;
41 36
37 bool GetMessageFromDictionary(const base::DictionaryValue* message,
38 std::string* number,
39 std::string* text) {
40 if (!message->GetStringWithoutPathExpansion(ash::kSmsNumberKey, number))
41 return false;
42 if (!message->GetStringWithoutPathExpansion(ash::kSmsTextKey, text))
43 return false;
44 return true;
45 }
46
42 } // namespace 47 } // namespace
43 48
44 namespace ash { 49 namespace ash {
45 namespace internal { 50 namespace internal {
46 51
47 class SmsObserverBase {
48 public:
49 explicit SmsObserverBase(TraySms* tray) : tray_(tray) {}
50 virtual ~SmsObserverBase() {}
51
52 virtual bool GetMessageFromDictionary(const base::DictionaryValue* message,
53 std::string* number,
54 std::string* text) {
55 return false;
56 }
57
58 virtual void RequestUpdate() {
59 }
60
61 void RemoveMessage(size_t index) {
62 if (index < messages_.GetSize())
63 messages_.Remove(index, NULL);
64 }
65
66 const base::ListValue& messages() const { return messages_; }
67
68 protected:
69 base::ListValue messages_;
70 TraySms* tray_;
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(SmsObserverBase);
74 };
75
76 #if defined(OS_CHROMEOS)
77
78 class TraySms::SmsObserver : public SmsObserverBase,
79 public chromeos::NetworkSmsHandler::Observer {
80 public:
81 explicit SmsObserver(TraySms* tray) : SmsObserverBase(tray) {
82 if (CommandLine::ForCurrentProcess()->HasSwitch(
83 switches::kAshNotifyDisabled))
84 return;
85 sms_handler_.reset(new chromeos::NetworkSmsHandler());
86 sms_handler_->AddObserver(this);
87 sms_handler_->Init();
88 }
89
90 virtual ~SmsObserver() {
91 if (sms_handler_.get())
92 sms_handler_->RemoveObserver(this);
93 }
94
95 // Overridden from SmsObserverBase
96 virtual bool GetMessageFromDictionary(const base::DictionaryValue* message,
97 std::string* number,
98 std::string* text) OVERRIDE {
99 if (!message->GetStringWithoutPathExpansion(
100 chromeos::NetworkSmsHandler::kNumberKey, number))
101 return false;
102 if (!message->GetStringWithoutPathExpansion(
103 chromeos::NetworkSmsHandler::kTextKey, text))
104 return false;
105 return true;
106 }
107
108 // Requests an immediate check for new messages.
109 virtual void RequestUpdate() OVERRIDE {
110 if (sms_handler_.get())
111 sms_handler_->RequestUpdate();
112 }
113
114 // Overridden from chromeos::NetworkSmsHandler::Observer
115 virtual void MessageReceived(const base::DictionaryValue& message) {
116 messages_.Append(message.DeepCopy());
117 tray_->Update(true);
118 }
119
120 private:
121 scoped_ptr<chromeos::NetworkSmsHandler> sms_handler_;
122
123 DISALLOW_COPY_AND_ASSIGN(SmsObserver);
124 };
125
126 #else // OS_CHROMEOS
127
128 class TraySms::SmsObserver : public SmsObserverBase {
129 public:
130 explicit SmsObserver(TraySms* tray) : SmsObserverBase(tray) {
131 }
132 virtual ~SmsObserver() {}
133
134 private:
135 DISALLOW_COPY_AND_ASSIGN(SmsObserver);
136 };
137
138 #endif // OS_CHROMEOS
139
140 class TraySms::SmsDefaultView : public TrayItemMore { 52 class TraySms::SmsDefaultView : public TrayItemMore {
141 public: 53 public:
142 explicit SmsDefaultView(TraySms* tray) 54 explicit SmsDefaultView(TraySms* tray)
143 : TrayItemMore(tray), 55 : TrayItemMore(tray),
144 tray_(tray) { 56 tray_(tray) {
145 SetImage(ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 57 SetImage(ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
146 IDR_AURA_UBER_TRAY_SMS)); 58 IDR_AURA_UBER_TRAY_SMS));
147 Update(); 59 Update();
148 } 60 }
149 61
150 virtual ~SmsDefaultView() {} 62 virtual ~SmsDefaultView() {}
151 63
152 void Update() { 64 void Update() {
153 int message_count = 65 int message_count = tray_->messages().GetSize();
154 static_cast<int>(tray_->sms_observer()->messages().GetSize());
155 string16 label = l10n_util::GetStringFUTF16( 66 string16 label = l10n_util::GetStringFUTF16(
156 IDS_ASH_STATUS_TRAY_SMS_MESSAGES, base::IntToString16(message_count)); 67 IDS_ASH_STATUS_TRAY_SMS_MESSAGES, base::IntToString16(message_count));
157 SetLabel(label); 68 SetLabel(label);
158 SetAccessibleName(label); 69 SetAccessibleName(label);
159 } 70 }
160 71
161 private: 72 private:
162 TraySms* tray_; 73 TraySms* tray_;
163 74
164 DISALLOW_COPY_AND_ASSIGN(SmsDefaultView); 75 DISALLOW_COPY_AND_ASSIGN(SmsDefaultView);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 else 107 else
197 LayoutNotificationView(); 108 LayoutNotificationView();
198 } 109 }
199 110
200 virtual ~SmsMessageView() { 111 virtual ~SmsMessageView() {
201 } 112 }
202 113
203 // Overridden from ButtonListener. 114 // Overridden from ButtonListener.
204 virtual void ButtonPressed(views::Button* sender, 115 virtual void ButtonPressed(views::Button* sender,
205 const views::Event& event) OVERRIDE { 116 const views::Event& event) OVERRIDE {
206 tray_->sms_observer()->RemoveMessage(index_); 117 tray_->RemoveMessage(index_);
207 tray_->Update(false); 118 tray_->Update(false);
208 } 119 }
209 120
210 private: 121 private:
211 void LayoutDetailedView() { 122 void LayoutDetailedView() {
212 views::ImageButton* close_button = new views::ImageButton(this); 123 views::ImageButton* close_button = new views::ImageButton(this);
213 close_button->SetImage(views::CustomButton::BS_NORMAL, 124 close_button->SetImage(views::CustomButton::BS_NORMAL,
214 ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 125 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
215 IDR_AURA_WINDOW_CLOSE)); 126 IDR_AURA_WINDOW_CLOSE));
216 127
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 // Overridden from views::View. 199 // Overridden from views::View.
289 gfx::Size GetPreferredSize() { 200 gfx::Size GetPreferredSize() {
290 gfx::Size preferred_size = TrayDetailsView::GetPreferredSize(); 201 gfx::Size preferred_size = TrayDetailsView::GetPreferredSize();
291 if (preferred_size.height() < kMessageListMinHeight) 202 if (preferred_size.height() < kMessageListMinHeight)
292 preferred_size.set_height(kMessageListMinHeight); 203 preferred_size.set_height(kMessageListMinHeight);
293 return preferred_size; 204 return preferred_size;
294 } 205 }
295 206
296 private: 207 private:
297 void UpdateMessageList() { 208 void UpdateMessageList() {
298 const base::ListValue& messages = tray_->sms_observer()->messages(); 209 const base::ListValue& messages = tray_->messages();
299 scroll_content()->RemoveAllChildViews(true); 210 scroll_content()->RemoveAllChildViews(true);
300 for (size_t index = 0; index < messages.GetSize(); ++index) { 211 for (size_t index = 0; index < messages.GetSize(); ++index) {
301 base::DictionaryValue* message = NULL; 212 base::DictionaryValue* message = NULL;
302 if (!messages.GetDictionary(index, &message)) { 213 if (!messages.GetDictionary(index, &message)) {
303 LOG(ERROR) << "SMS message not a dictionary at: " << index; 214 LOG(ERROR) << "SMS message not a dictionary at: " << index;
304 continue; 215 continue;
305 } 216 }
306 std::string number, text; 217 std::string number, text;
307 if (!tray_->sms_observer()->GetMessageFromDictionary( 218 if (!GetMessageFromDictionary(message, &number, &text)) {
308 message, &number, &text)) {
309 LOG(ERROR) << "Error parsing SMS message"; 219 LOG(ERROR) << "Error parsing SMS message";
310 continue; 220 continue;
311 } 221 }
312 SmsMessageView* msgview = new SmsMessageView( 222 SmsMessageView* msgview = new SmsMessageView(
313 tray_, SmsMessageView::VIEW_DETAILED, index, number, text); 223 tray_, SmsMessageView::VIEW_DETAILED, index, number, text);
314 scroll_content()->AddChildView(msgview); 224 scroll_content()->AddChildView(msgview);
315 } 225 }
316 scroller()->Layout(); 226 scroller()->Layout();
317 } 227 }
318 228
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 } 260 }
351 261
352 // Overridden from views::View. 262 // Overridden from views::View.
353 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { 263 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE {
354 tray_->PopupDetailedView(0, true); 264 tray_->PopupDetailedView(0, true);
355 return true; 265 return true;
356 } 266 }
357 267
358 // Overridden from TrayNotificationView: 268 // Overridden from TrayNotificationView:
359 virtual void OnClose() OVERRIDE { 269 virtual void OnClose() OVERRIDE {
360 tray_->sms_observer()->RemoveMessage(message_index_); 270 tray_->RemoveMessage(message_index_);
361 tray_->HideNotificationView(); 271 tray_->HideNotificationView();
362 } 272 }
363 273
364 private: 274 private:
365 TraySms* tray_; 275 TraySms* tray_;
366 size_t message_index_; 276 size_t message_index_;
367 277
368 DISALLOW_COPY_AND_ASSIGN(SmsNotificationView); 278 DISALLOW_COPY_AND_ASSIGN(SmsNotificationView);
369 }; 279 };
370 280
371 TraySms::TraySms() 281 TraySms::TraySms()
372 : default_(NULL), 282 : default_(NULL),
373 detailed_(NULL), 283 detailed_(NULL),
374 notification_(NULL) { 284 notification_(NULL) {
375 sms_observer_.reset(new SmsObserver(this));
376 } 285 }
377 286
378 TraySms::~TraySms() { 287 TraySms::~TraySms() {
379 } 288 }
380 289
381 views::View* TraySms::CreateDefaultView(user::LoginStatus status) { 290 views::View* TraySms::CreateDefaultView(user::LoginStatus status) {
382 CHECK(default_ == NULL); 291 CHECK(default_ == NULL);
383 sms_observer()->RequestUpdate();
384 default_ = new SmsDefaultView(this); 292 default_ = new SmsDefaultView(this);
385 default_->SetVisible(!sms_observer()->messages().empty()); 293 default_->SetVisible(!messages_.empty());
386 return default_; 294 return default_;
387 } 295 }
388 296
389 views::View* TraySms::CreateDetailedView(user::LoginStatus status) { 297 views::View* TraySms::CreateDetailedView(user::LoginStatus status) {
390 CHECK(detailed_ == NULL); 298 CHECK(detailed_ == NULL);
391 sms_observer()->RequestUpdate();
392 HideNotificationView(); 299 HideNotificationView();
393 if (sms_observer()->messages().empty()) 300 if (messages_.empty())
394 return NULL; 301 return NULL;
395 detailed_ = new SmsDetailedView(this); 302 detailed_ = new SmsDetailedView(this);
396 return detailed_; 303 return detailed_;
397 } 304 }
398 305
399 views::View* TraySms::CreateNotificationView(user::LoginStatus status) { 306 views::View* TraySms::CreateNotificationView(user::LoginStatus status) {
400 CHECK(notification_ == NULL); 307 CHECK(notification_ == NULL);
401 size_t index; 308 size_t index;
402 std::string number, text; 309 std::string number, text;
403 if (GetLatestMessage(&index, &number, &text)) 310 if (GetLatestMessage(&index, &number, &text))
404 notification_ = new SmsNotificationView(this, index, number, text); 311 notification_ = new SmsNotificationView(this, index, number, text);
405 return notification_; 312 return notification_;
406 } 313 }
407 314
408 void TraySms::DestroyDefaultView() { 315 void TraySms::DestroyDefaultView() {
409 default_ = NULL; 316 default_ = NULL;
410 } 317 }
411 318
412 void TraySms::DestroyDetailedView() { 319 void TraySms::DestroyDetailedView() {
413 detailed_ = NULL; 320 detailed_ = NULL;
414 } 321 }
415 322
416 void TraySms::DestroyNotificationView() { 323 void TraySms::DestroyNotificationView() {
417 notification_ = NULL; 324 notification_ = NULL;
418 } 325 }
419 326
327 void TraySms::AddMessage(const base::DictionaryValue& message) {
328 messages_.Append(message.DeepCopy());
329 Update(true);
330 }
331
420 bool TraySms::GetLatestMessage(size_t* index, 332 bool TraySms::GetLatestMessage(size_t* index,
421 std::string* number, 333 std::string* number,
422 std::string* text) { 334 std::string* text) {
423 const base::ListValue& messages = sms_observer()->messages(); 335 if (messages_.empty())
424 if (messages.empty())
425 return false; 336 return false;
426 DictionaryValue* message; 337 DictionaryValue* message;
427 size_t message_index = messages.GetSize() - 1; 338 size_t message_index = messages_.GetSize() - 1;
428 if (!messages.GetDictionary(message_index, &message)) 339 if (!messages_.GetDictionary(message_index, &message))
429 return false; 340 return false;
430 if (!sms_observer()->GetMessageFromDictionary(message, number, text)) 341 if (!GetMessageFromDictionary(message, number, text))
431 return false; 342 return false;
432 *index = message_index; 343 *index = message_index;
433 return true; 344 return true;
434 } 345 }
435 346
347 void TraySms::RemoveMessage(size_t index) {
348 if (index < messages_.GetSize())
349 messages_.Remove(index, NULL);
350 }
351
436 void TraySms::Update(bool notify) { 352 void TraySms::Update(bool notify) {
437 if (sms_observer()->messages().empty()) { 353 if (messages_.empty()) {
438 if (default_) 354 if (default_)
439 default_->SetVisible(false); 355 default_->SetVisible(false);
440 if (detailed_) 356 if (detailed_)
441 HideDetailedView(); 357 HideDetailedView();
442 HideNotificationView(); 358 HideNotificationView();
443 } else { 359 } else {
444 if (default_) { 360 if (default_) {
445 default_->SetVisible(true); 361 default_->SetVisible(true);
446 default_->Update(); 362 default_->Update();
447 } 363 }
448 if (detailed_) 364 if (detailed_)
449 detailed_->Update(); 365 detailed_->Update();
450 if (notification_) { 366 if (notification_) {
451 size_t index; 367 size_t index;
452 std::string number, text; 368 std::string number, text;
453 if (GetLatestMessage(&index, &number, &text)) 369 if (GetLatestMessage(&index, &number, &text))
454 notification_->Update(index, number, text); 370 notification_->Update(index, number, text);
455 } else if (notify) { 371 } else if (notify) {
456 ShowNotificationView(); 372 ShowNotificationView();
457 } 373 }
458 } 374 }
459 } 375 }
460 376
461 } // namespace internal 377 } // namespace internal
462 } // namespace ash 378 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/network/tray_sms.h ('k') | ash/system/tray/system_tray.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698