Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / system / locale / locale_notification_controller.cc
1 // Copyright 2013 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/system/locale/locale_notification_controller.h"
6
7 #include "ash/shell.h"
8 #include "ash/system/system_notifier.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "base/strings/string16.h"
11 #include "grit/ash_resources.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/message_center/message_center.h"
16 #include "ui/message_center/notification.h"
17 #include "ui/message_center/notification_delegate.h"
18 #include "ui/message_center/notification_types.h"
19
20 using message_center::Notification;
21
22 namespace ash {
23 namespace {
24
25 const char kLocaleChangeNotificationId[] = "chrome://settings/locale";
26
27 class LocaleNotificationDelegate : public message_center::NotificationDelegate {
28  public:
29   explicit LocaleNotificationDelegate(LocaleObserver::Delegate* delegate);
30
31  protected:
32   ~LocaleNotificationDelegate() override;
33
34   // message_center::NotificationDelegate overrides:
35   void Close(bool by_user) override;
36   bool HasClickedListener() override;
37   void Click() override;
38   void ButtonClick(int button_index) override;
39
40  private:
41   LocaleObserver::Delegate* delegate_;
42
43   DISALLOW_COPY_AND_ASSIGN(LocaleNotificationDelegate);
44 };
45
46 LocaleNotificationDelegate::LocaleNotificationDelegate(
47     LocaleObserver::Delegate* delegate)
48   : delegate_(delegate) {
49   DCHECK(delegate_);
50 }
51
52 LocaleNotificationDelegate::~LocaleNotificationDelegate() {
53 }
54
55 void LocaleNotificationDelegate::Close(bool by_user) {
56   delegate_->AcceptLocaleChange();
57 }
58
59 bool LocaleNotificationDelegate::HasClickedListener() {
60   return true;
61 }
62
63 void LocaleNotificationDelegate::Click() {
64   delegate_->AcceptLocaleChange();
65 }
66
67 void LocaleNotificationDelegate::ButtonClick(int button_index) {
68   DCHECK_EQ(0, button_index);
69   delegate_->RevertLocaleChange();
70 }
71
72 }  // namespace
73
74 LocaleNotificationController::LocaleNotificationController()
75     : delegate_(NULL) {
76   Shell::GetInstance()->system_tray_notifier()->AddLocaleObserver(this);
77 }
78
79 LocaleNotificationController::~LocaleNotificationController() {
80   Shell::GetInstance()->system_tray_notifier()->RemoveLocaleObserver(this);
81 }
82
83 void LocaleNotificationController::OnLocaleChanged(
84     LocaleObserver::Delegate* delegate,
85     const std::string& cur_locale,
86     const std::string& from_locale,
87     const std::string& to_locale) {
88   if (!delegate)
89     return;
90
91   base::string16 from = l10n_util::GetDisplayNameForLocale(
92       from_locale, cur_locale, true);
93   base::string16 to = l10n_util::GetDisplayNameForLocale(
94       to_locale, cur_locale, true);
95
96   message_center::RichNotificationData optional;
97   optional.buttons.push_back(message_center::ButtonInfo(
98       l10n_util::GetStringFUTF16(
99           IDS_ASH_STATUS_TRAY_LOCALE_REVERT_MESSAGE, from)));
100   optional.never_timeout = true;
101
102   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
103   scoped_ptr<Notification> notification(new Notification(
104       message_center::NOTIFICATION_TYPE_SIMPLE,
105       kLocaleChangeNotificationId,
106       base::string16()  /* title */,
107       l10n_util::GetStringFUTF16(
108           IDS_ASH_STATUS_TRAY_LOCALE_CHANGE_MESSAGE, from, to),
109       bundle.GetImageNamed(IDR_AURA_UBER_TRAY_LOCALE),
110       base::string16()  /* display_source */,
111       message_center::NotifierId(
112           message_center::NotifierId::SYSTEM_COMPONENT,
113           system_notifier::kNotifierLocale),
114       optional,
115       new LocaleNotificationDelegate(delegate)));
116   message_center::MessageCenter::Get()->AddNotification(notification.Pass());
117 }
118
119 }  // namespace ash