Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / status / data_promo_notification.cc
blob821e95a6cfd00fbc83c03066ad036853057e7571
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/system_notifier.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/login/helper.h"
13 #include "chrome/browser/chromeos/mobile_config.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_list.h"
18 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
19 #include "chrome/browser/ui/singleton_tabs.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/grit/generated_resources.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/ui_chromeos_resources.h"
29 #include "third_party/cros_system_api/dbus/service_constants.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/chromeos/network/network_connect.h"
33 #include "ui/chromeos/network/network_state_notifier.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"
39 namespace chromeos {
41 namespace {
43 const char kDataPromoNotificationId[] = "chrome://settings/internet/data_promo";
45 const int kNotificationCountPrefDefault = -1;
47 bool GetBooleanPref(const char* pref_name) {
48 Profile* profile = ProfileManager::GetPrimaryUserProfile();
49 PrefService* prefs = profile->GetPrefs();
50 return prefs->GetBoolean(pref_name);
53 int GetIntegerLocalPref(const char* pref_name) {
54 PrefService* prefs = g_browser_process->local_state();
55 return prefs->GetInteger(pref_name);
58 void SetBooleanPref(const char* pref_name, bool value) {
59 Profile* profile = ProfileManager::GetPrimaryUserProfile();
60 PrefService* prefs = profile->GetPrefs();
61 prefs->SetBoolean(pref_name, value);
64 void SetIntegerLocalPref(const char* pref_name, int value) {
65 PrefService* prefs = g_browser_process->local_state();
66 prefs->SetInteger(pref_name, value);
69 // Returns prefs::kShow3gPromoNotification or false if no active browser.
70 bool ShouldShow3gPromoNotification() {
71 return GetBooleanPref(prefs::kShow3gPromoNotification);
74 void SetShow3gPromoNotification(bool value) {
75 SetBooleanPref(prefs::kShow3gPromoNotification, value);
78 // Returns prefs::kCarrierDealPromoShown which is number of times
79 // carrier deal notification has been shown to users on this machine.
80 int GetCarrierDealPromoShown() {
81 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown);
84 void SetCarrierDealPromoShown(int value) {
85 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value);
88 const chromeos::MobileConfig::Carrier* GetCarrier(
89 const NetworkState* cellular) {
90 const DeviceState* device = NetworkHandler::Get()->network_state_handler()->
91 GetDeviceState(cellular->device_path());
92 std::string carrier_id = device ? device->home_provider_id() : "";
93 if (carrier_id.empty()) {
94 NET_LOG_ERROR("Empty carrier ID for cellular network",
95 device ? device->path(): "No device");
96 return NULL;
99 chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance();
100 if (!config->IsReady())
101 return NULL;
103 return config->GetCarrier(carrier_id);
106 const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal(
107 const chromeos::MobileConfig::Carrier* carrier) {
108 const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal();
109 if (deal) {
110 // Check deal for validity.
111 int carrier_deal_promo_pref = GetCarrierDealPromoShown();
112 if (carrier_deal_promo_pref >= deal->notification_count())
113 return NULL;
114 const std::string locale = g_browser_process->GetApplicationLocale();
115 std::string deal_text = deal->GetLocalizedString(locale,
116 "notification_text");
117 NET_LOG_DEBUG("Carrier Deal Found", deal_text);
118 if (deal_text.empty())
119 return NULL;
121 return deal;
124 void NotificationClicked(const std::string& service_path,
125 const std::string& info_url) {
126 if (info_url.empty())
127 ui::NetworkConnect::Get()->ShowNetworkSettingsForPath(service_path);
129 chrome::ScopedTabbedBrowserDisplayer displayer(
130 ProfileManager::GetPrimaryUserProfile(),
131 chrome::HOST_DESKTOP_TYPE_ASH);
132 chrome::ShowSingletonTab(displayer.browser(), GURL(info_url));
135 } // namespace
137 ////////////////////////////////////////////////////////////////////////////////
138 // DataPromoNotification
140 DataPromoNotification::DataPromoNotification()
141 : check_for_promo_(true),
142 weak_ptr_factory_(this) {
143 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
146 DataPromoNotification::~DataPromoNotification() {
147 if (NetworkHandler::IsInitialized()) {
148 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
149 this, FROM_HERE);
153 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) {
154 // Carrier deal notification shown count defaults to 0.
155 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0);
158 void DataPromoNotification::NetworkPropertiesUpdated(
159 const NetworkState* network) {
160 if (!network || network->type() != shill::kTypeCellular)
161 return;
162 ShowOptionalMobileDataPromoNotification();
165 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) {
166 // Call NetworkPropertiesUpdated in case the Cellular network became the
167 // default network.
168 NetworkPropertiesUpdated(network);
171 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() {
172 // Display a one-time notification for authenticated users on first use
173 // of Mobile Data connection or if there is a carrier deal defined
174 // show that even if user has already seen generic promo.
175 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated())
176 return;
177 const NetworkState* default_network =
178 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
179 if (!default_network || default_network->type() != shill::kTypeCellular)
180 return;
181 // When requesting a network connection, do not show the notification.
182 if (NetworkHandler::Get()->network_connection_handler()->
183 HasPendingConnectRequest())
184 return;
186 int carrier_deal_promo_pref = kNotificationCountPrefDefault;
187 const MobileConfig::CarrierDeal* deal = NULL;
188 const MobileConfig::Carrier* carrier = GetCarrier(default_network);
189 if (carrier)
190 deal = GetCarrierDeal(carrier);
192 base::string16 message =
193 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE);
194 std::string info_url;
195 if (deal) {
196 carrier_deal_promo_pref = GetCarrierDealPromoShown();
197 const std::string locale = g_browser_process->GetApplicationLocale();
198 std::string deal_text =
199 deal->GetLocalizedString(locale, "notification_text");
200 message = base::UTF8ToUTF16(deal_text + "\n\n") + message;
201 info_url = deal->info_url();
202 if (info_url.empty() && carrier)
203 info_url = carrier->top_up_url();
204 } else if (!ShouldShow3gPromoNotification()) {
205 check_for_promo_ = false;
206 return;
209 int icon_id;
210 if (default_network->network_technology() == shill::kNetworkTechnologyLte)
211 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
212 else
213 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
214 const gfx::Image& icon =
215 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
217 message_center::MessageCenter::Get()->AddNotification(
218 message_center::Notification::CreateSystemNotification(
219 kDataPromoNotificationId, base::string16() /* title */, message, icon,
220 ui::NetworkStateNotifier::kNotifierNetwork,
221 base::Bind(&NotificationClicked, default_network->path(), info_url)));
223 check_for_promo_ = false;
224 SetShow3gPromoNotification(false);
225 if (carrier_deal_promo_pref != kNotificationCountPrefDefault)
226 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1);
229 } // namespace chromeos