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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/status/data_promo_notification.h"
7 #include "ash/system/chromeos/network/network_connect.h"
8 #include "ash/system/system_notifier.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/login/helper.h"
14 #include "chrome/browser/chromeos/mobile_config.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
20 #include "chrome/browser/ui/singleton_tabs.h"
21 #include "chrome/common/pref_names.h"
22 #include "chromeos/login/login_state.h"
23 #include "chromeos/network/device_state.h"
24 #include "chromeos/network/network_connection_handler.h"
25 #include "chromeos/network/network_event_log.h"
26 #include "chromeos/network/network_state.h"
27 #include "chromeos/network/network_state_handler.h"
28 #include "grit/ash_resources.h"
29 #include "grit/generated_resources.h"
30 #include "grit/theme_resources.h"
31 #include "third_party/cros_system_api/dbus/service_constants.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/resource/resource_bundle.h"
34 #include "ui/message_center/message_center.h"
35 #include "ui/message_center/notification.h"
36 #include "ui/views/view.h"
37 #include "ui/views/widget/widget.h"
43 const int kNotificationCountPrefDefault
= -1;
45 bool GetBooleanPref(const char* pref_name
) {
46 Profile
* profile
= ProfileManager::GetPrimaryUserProfile();
47 PrefService
* prefs
= profile
->GetPrefs();
48 return prefs
->GetBoolean(pref_name
);
51 int GetIntegerLocalPref(const char* pref_name
) {
52 PrefService
* prefs
= g_browser_process
->local_state();
53 return prefs
->GetInteger(pref_name
);
56 void SetBooleanPref(const char* pref_name
, bool value
) {
57 Profile
* profile
= ProfileManager::GetPrimaryUserProfile();
58 PrefService
* prefs
= profile
->GetPrefs();
59 prefs
->SetBoolean(pref_name
, value
);
62 void SetIntegerLocalPref(const char* pref_name
, int value
) {
63 PrefService
* prefs
= g_browser_process
->local_state();
64 prefs
->SetInteger(pref_name
, value
);
67 // Returns prefs::kShow3gPromoNotification or false if no active browser.
68 bool ShouldShow3gPromoNotification() {
69 return GetBooleanPref(prefs::kShow3gPromoNotification
);
72 void SetShow3gPromoNotification(bool value
) {
73 SetBooleanPref(prefs::kShow3gPromoNotification
, value
);
76 // Returns prefs::kCarrierDealPromoShown which is number of times
77 // carrier deal notification has been shown to users on this machine.
78 int GetCarrierDealPromoShown() {
79 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown
);
82 void SetCarrierDealPromoShown(int value
) {
83 SetIntegerLocalPref(prefs::kCarrierDealPromoShown
, value
);
86 const chromeos::MobileConfig::Carrier
* GetCarrier(
87 const NetworkState
* cellular
) {
88 const DeviceState
* device
= NetworkHandler::Get()->network_state_handler()->
89 GetDeviceState(cellular
->device_path());
90 std::string carrier_id
= device
? device
->home_provider_id() : "";
91 if (carrier_id
.empty()) {
92 NET_LOG_ERROR("Empty carrier ID for cellular network",
93 device
? device
->path(): "No device");
97 chromeos::MobileConfig
* config
= chromeos::MobileConfig::GetInstance();
98 if (!config
->IsReady())
101 return config
->GetCarrier(carrier_id
);
104 const chromeos::MobileConfig::CarrierDeal
* GetCarrierDeal(
105 const chromeos::MobileConfig::Carrier
* carrier
) {
106 const chromeos::MobileConfig::CarrierDeal
* deal
= carrier
->GetDefaultDeal();
108 // Check deal for validity.
109 int carrier_deal_promo_pref
= GetCarrierDealPromoShown();
110 if (carrier_deal_promo_pref
>= deal
->notification_count())
112 const std::string locale
= g_browser_process
->GetApplicationLocale();
113 std::string deal_text
= deal
->GetLocalizedString(locale
,
114 "notification_text");
115 NET_LOG_DEBUG("Carrier Deal Found", deal_text
);
116 if (deal_text
.empty())
122 void NotificationClicked(const std::string
& service_path
,
123 const std::string
& info_url
) {
124 if (info_url
.empty())
125 ash::network_connect::ShowNetworkSettings(service_path
);
127 chrome::ScopedTabbedBrowserDisplayer
displayer(
128 ProfileManager::GetPrimaryUserProfile(),
129 chrome::HOST_DESKTOP_TYPE_ASH
);
130 chrome::ShowSingletonTab(displayer
.browser(), GURL(info_url
));
135 ////////////////////////////////////////////////////////////////////////////////
136 // DataPromoNotification
138 DataPromoNotification::DataPromoNotification()
139 : check_for_promo_(true),
140 weak_ptr_factory_(this) {
141 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE
);
144 DataPromoNotification::~DataPromoNotification() {
145 if (NetworkHandler::IsInitialized()) {
146 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
151 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple
* registry
) {
152 // Carrier deal notification shown count defaults to 0.
153 registry
->RegisterIntegerPref(prefs::kCarrierDealPromoShown
, 0);
156 void DataPromoNotification::NetworkPropertiesUpdated(
157 const NetworkState
* network
) {
158 if (!network
|| network
->type() != shill::kTypeCellular
)
160 ShowOptionalMobileDataPromoNotification();
163 void DataPromoNotification::DefaultNetworkChanged(const NetworkState
* network
) {
164 // Call NetworkPropertiesUpdated in case the Cellular network became the
166 NetworkPropertiesUpdated(network
);
169 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() {
170 // Display a one-time notification for authenticated users on first use
171 // of Mobile Data connection or if there is a carrier deal defined
172 // show that even if user has already seen generic promo.
173 if (!check_for_promo_
|| !LoginState::Get()->IsUserAuthenticated())
175 const NetworkState
* default_network
=
176 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
177 if (!default_network
|| default_network
->type() != shill::kTypeCellular
)
179 // When requesting a network connection, do not show the notification.
180 if (NetworkHandler::Get()->network_connection_handler()->
181 HasPendingConnectRequest())
184 int carrier_deal_promo_pref
= kNotificationCountPrefDefault
;
185 const MobileConfig::CarrierDeal
* deal
= NULL
;
186 const MobileConfig::Carrier
* carrier
= GetCarrier(default_network
);
188 deal
= GetCarrierDeal(carrier
);
190 base::string16 message
= l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE
);
191 std::string info_url
;
193 carrier_deal_promo_pref
= GetCarrierDealPromoShown();
194 const std::string locale
= g_browser_process
->GetApplicationLocale();
195 std::string deal_text
=
196 deal
->GetLocalizedString(locale
, "notification_text");
197 message
= base::UTF8ToUTF16(deal_text
+ "\n\n") + message
;
198 info_url
= deal
->info_url();
199 if (info_url
.empty() && carrier
)
200 info_url
= carrier
->top_up_url();
201 } else if (!ShouldShow3gPromoNotification()) {
202 check_for_promo_
= false;
207 if (default_network
->network_technology() == shill::kNetworkTechnologyLte
)
208 icon_id
= IDR_AURA_UBER_TRAY_NOTIFICATION_LTE
;
210 icon_id
= IDR_AURA_UBER_TRAY_NOTIFICATION_3G
;
211 const gfx::Image
& icon
=
212 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id
);
214 message_center::MessageCenter::Get()->AddNotification(
215 message_center::Notification::CreateSystemNotification(
216 ash::network_connect::kNetworkActivateNotificationId
,
217 base::string16() /* title */,
220 ash::system_notifier::kNotifierNetwork
,
221 base::Bind(&NotificationClicked
,
222 default_network
->path(), info_url
)));
224 check_for_promo_
= false;
225 SetShow3gPromoNotification(false);
226 if (carrier_deal_promo_pref
!= kNotificationCountPrefDefault
)
227 SetCarrierDealPromoShown(carrier_deal_promo_pref
+ 1);
230 } // namespace chromeos