• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/chromeos/status/data_promo_notification.h"
6 
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"
38 
39 namespace chromeos {
40 
41 namespace {
42 
43 const int kNotificationCountPrefDefault = -1;
44 
GetBooleanPref(const char * pref_name)45 bool GetBooleanPref(const char* pref_name) {
46   Profile* profile = ProfileManager::GetPrimaryUserProfileOrOffTheRecord();
47   PrefService* prefs = profile->GetPrefs();
48   return prefs->GetBoolean(pref_name);
49 }
50 
GetIntegerLocalPref(const char * pref_name)51 int GetIntegerLocalPref(const char* pref_name) {
52   PrefService* prefs = g_browser_process->local_state();
53   return prefs->GetInteger(pref_name);
54 }
55 
SetBooleanPref(const char * pref_name,bool value)56 void SetBooleanPref(const char* pref_name, bool value) {
57   Profile* profile = ProfileManager::GetPrimaryUserProfileOrOffTheRecord();
58   PrefService* prefs = profile->GetPrefs();
59   prefs->SetBoolean(pref_name, value);
60 }
61 
SetIntegerLocalPref(const char * pref_name,int value)62 void SetIntegerLocalPref(const char* pref_name, int value) {
63   PrefService* prefs = g_browser_process->local_state();
64   prefs->SetInteger(pref_name, value);
65 }
66 
67 // Returns prefs::kShow3gPromoNotification or false if no active browser.
ShouldShow3gPromoNotification()68 bool ShouldShow3gPromoNotification() {
69   return GetBooleanPref(prefs::kShow3gPromoNotification);
70 }
71 
SetShow3gPromoNotification(bool value)72 void SetShow3gPromoNotification(bool value) {
73   SetBooleanPref(prefs::kShow3gPromoNotification, value);
74 }
75 
76 // Returns prefs::kCarrierDealPromoShown which is number of times
77 // carrier deal notification has been shown to users on this machine.
GetCarrierDealPromoShown()78 int GetCarrierDealPromoShown() {
79   return GetIntegerLocalPref(prefs::kCarrierDealPromoShown);
80 }
81 
SetCarrierDealPromoShown(int value)82 void SetCarrierDealPromoShown(int value) {
83   SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value);
84 }
85 
GetCarrier(const NetworkState * cellular)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");
94     return NULL;
95   }
96 
97   chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance();
98   if (!config->IsReady())
99     return NULL;
100 
101   return config->GetCarrier(carrier_id);
102 }
103 
GetCarrierDeal(const chromeos::MobileConfig::Carrier * carrier)104 const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal(
105     const chromeos::MobileConfig::Carrier* carrier) {
106   const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal();
107   if (deal) {
108     // Check deal for validity.
109     int carrier_deal_promo_pref = GetCarrierDealPromoShown();
110     if (carrier_deal_promo_pref >= deal->notification_count())
111       return NULL;
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())
117       return NULL;
118   }
119   return deal;
120 }
121 
NotificationClicked(const std::string & service_path,const std::string & info_url)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);
126 
127   chrome::ScopedTabbedBrowserDisplayer displayer(
128       ProfileManager::GetPrimaryUserProfileOrOffTheRecord(),
129       chrome::HOST_DESKTOP_TYPE_ASH);
130   chrome::ShowSingletonTab(displayer.browser(), GURL(info_url));
131 }
132 
133 }  // namespace
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 // DataPromoNotification
137 
DataPromoNotification()138 DataPromoNotification::DataPromoNotification()
139     : check_for_promo_(true),
140       weak_ptr_factory_(this) {
141   NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
142 }
143 
~DataPromoNotification()144 DataPromoNotification::~DataPromoNotification() {
145   if (NetworkHandler::IsInitialized()) {
146     NetworkHandler::Get()->network_state_handler()->RemoveObserver(
147         this, FROM_HERE);
148   }
149 }
150 
RegisterPrefs(PrefRegistrySimple * registry)151 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) {
152   // Carrier deal notification shown count defaults to 0.
153   registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0);
154 }
155 
NetworkPropertiesUpdated(const NetworkState * network)156 void DataPromoNotification::NetworkPropertiesUpdated(
157     const NetworkState* network) {
158   if (!network || network->type() != shill::kTypeCellular)
159     return;
160   ShowOptionalMobileDataPromoNotification();
161 }
162 
DefaultNetworkChanged(const NetworkState * network)163 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) {
164   // Call NetworkPropertiesUpdated in case the Cellular network became the
165   // default network.
166   NetworkPropertiesUpdated(network);
167 }
168 
ShowOptionalMobileDataPromoNotification()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())
174     return;
175   const NetworkState* default_network =
176       NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
177   if (!default_network || default_network->type() != shill::kTypeCellular)
178     return;
179   // When requesting a network connection, do not show the notification.
180   if (NetworkHandler::Get()->network_connection_handler()->
181       HasPendingConnectRequest())
182     return;
183 
184   int carrier_deal_promo_pref = kNotificationCountPrefDefault;
185   const MobileConfig::CarrierDeal* deal = NULL;
186   const MobileConfig::Carrier* carrier = GetCarrier(default_network);
187   if (carrier)
188     deal = GetCarrierDeal(carrier);
189 
190   base::string16 message = l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE);
191   std::string info_url;
192   if (deal) {
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 = 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;
203     return;
204   }
205 
206   int icon_id;
207   if (default_network->network_technology() == shill::kNetworkTechnologyLte)
208     icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
209   else
210     icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
211   const gfx::Image& icon =
212       ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
213 
214   message_center::MessageCenter::Get()->AddNotification(
215       message_center::Notification::CreateSystemNotification(
216           ash::network_connect::kNetworkActivateNotificationId,
217           base::string16() /* title */,
218           message,
219           icon,
220           ash::system_notifier::kNotifierNetwork,
221           base::Bind(&NotificationClicked,
222                      default_network->path(), info_url)));
223 
224   check_for_promo_ = false;
225   SetShow3gPromoNotification(false);
226   if (carrier_deal_promo_pref != kNotificationCountPrefDefault)
227     SetCarrierDealPromoShown(carrier_deal_promo_pref + 1);
228 }
229 
230 }  // namespace chromeos
231