data_promo_notification.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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_finder.h" 19#include "chrome/browser/ui/browser_list.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 39namespace chromeos { 40 41namespace { 42 43// Time in milliseconds to delay showing of promo 44// notification when Chrome window is not on screen. 45const int kPromoShowDelayMs = 10000; 46 47const int kNotificationCountPrefDefault = -1; 48 49bool GetBooleanPref(const char* pref_name) { 50 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 51 PrefService* prefs = profile->GetPrefs(); 52 return prefs->GetBoolean(pref_name); 53} 54 55int GetIntegerLocalPref(const char* pref_name) { 56 PrefService* prefs = g_browser_process->local_state(); 57 return prefs->GetInteger(pref_name); 58} 59 60void SetBooleanPref(const char* pref_name, bool value) { 61 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 62 PrefService* prefs = profile->GetPrefs(); 63 prefs->SetBoolean(pref_name, value); 64} 65 66void SetIntegerLocalPref(const char* pref_name, int value) { 67 PrefService* prefs = g_browser_process->local_state(); 68 prefs->SetInteger(pref_name, value); 69} 70 71// Returns prefs::kShow3gPromoNotification or false if no active browser. 72bool ShouldShow3gPromoNotification() { 73 return GetBooleanPref(prefs::kShow3gPromoNotification); 74} 75 76void SetShow3gPromoNotification(bool value) { 77 SetBooleanPref(prefs::kShow3gPromoNotification, value); 78} 79 80// Returns prefs::kCarrierDealPromoShown which is number of times 81// carrier deal notification has been shown to users on this machine. 82int GetCarrierDealPromoShown() { 83 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); 84} 85 86void SetCarrierDealPromoShown(int value) { 87 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); 88} 89 90const chromeos::MobileConfig::Carrier* GetCarrier( 91 const NetworkState* cellular) { 92 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> 93 GetDeviceState(cellular->device_path()); 94 std::string carrier_id = device ? device->home_provider_id() : ""; 95 if (carrier_id.empty()) { 96 NET_LOG_ERROR("Empty carrier ID for cellular network", 97 device ? device->path(): "No device"); 98 return NULL; 99 } 100 101 chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance(); 102 if (!config->IsReady()) 103 return NULL; 104 105 return config->GetCarrier(carrier_id); 106} 107 108const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal( 109 const chromeos::MobileConfig::Carrier* carrier) { 110 const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal(); 111 if (deal) { 112 // Check deal for validity. 113 int carrier_deal_promo_pref = GetCarrierDealPromoShown(); 114 if (carrier_deal_promo_pref >= deal->notification_count()) 115 return NULL; 116 const std::string locale = g_browser_process->GetApplicationLocale(); 117 std::string deal_text = deal->GetLocalizedString(locale, 118 "notification_text"); 119 NET_LOG_DEBUG("Carrier Deal Found", deal_text); 120 if (deal_text.empty()) 121 return NULL; 122 } 123 return deal; 124} 125 126void NotificationClicked(const std::string& service_path, 127 const std::string& info_url) { 128 if (info_url.empty()) 129 ash::network_connect::ShowNetworkSettings(service_path); 130 131 Browser* browser = chrome::FindOrCreateTabbedBrowser( 132 ProfileManager::GetDefaultProfileOrOffTheRecord(), 133 chrome::HOST_DESKTOP_TYPE_ASH); 134 if (!browser) 135 return; 136 chrome::ShowSingletonTab(browser, GURL(info_url)); 137} 138 139} // namespace 140 141//////////////////////////////////////////////////////////////////////////////// 142// DataPromoNotification 143 144DataPromoNotification::DataPromoNotification() 145 : check_for_promo_(true), 146 weak_ptr_factory_(this) { 147 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); 148} 149 150DataPromoNotification::~DataPromoNotification() { 151 if (NetworkHandler::IsInitialized()) { 152 NetworkHandler::Get()->network_state_handler()->RemoveObserver( 153 this, FROM_HERE); 154 } 155} 156 157void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { 158 // Carrier deal notification shown count defaults to 0. 159 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); 160} 161 162void DataPromoNotification::NetworkPropertiesUpdated( 163 const NetworkState* network) { 164 if (!network || network->type() != shill::kTypeCellular) 165 return; 166 ShowOptionalMobileDataPromoNotification(); 167} 168 169void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { 170 // Call NetworkPropertiesUpdated in case the Cellular network became the 171 // default network. 172 NetworkPropertiesUpdated(network); 173} 174 175void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { 176 // Display a one-time notification for authenticated users on first use 177 // of Mobile Data connection or if there is a carrier deal defined 178 // show that even if user has already seen generic promo. 179 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated()) 180 return; 181 const NetworkState* default_network = 182 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 183 if (!default_network || default_network->type() != shill::kTypeCellular) 184 return; 185 // When requesting a network connection, do not show the notification. 186 if (NetworkHandler::Get()->network_connection_handler()-> 187 HasPendingConnectRequest()) 188 return; 189 190 int carrier_deal_promo_pref = kNotificationCountPrefDefault; 191 const MobileConfig::CarrierDeal* deal = NULL; 192 const MobileConfig::Carrier* carrier = GetCarrier(default_network); 193 if (carrier) 194 deal = GetCarrierDeal(carrier); 195 196 string16 message = l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); 197 std::string info_url; 198 if (deal) { 199 carrier_deal_promo_pref = GetCarrierDealPromoShown(); 200 const std::string locale = g_browser_process->GetApplicationLocale(); 201 std::string deal_text = 202 deal->GetLocalizedString(locale, "notification_text"); 203 message = UTF8ToUTF16(deal_text + "\n\n") + message; 204 info_url = deal->info_url(); 205 if (info_url.empty() && carrier) 206 info_url = carrier->top_up_url(); 207 } else if (!ShouldShow3gPromoNotification()) { 208 check_for_promo_ = false; 209 return; 210 } 211 212 int icon_id; 213 if (default_network->network_technology() == shill::kNetworkTechnologyLte) 214 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; 215 else 216 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; 217 const gfx::Image& icon = 218 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); 219 220 message_center::MessageCenter::Get()->AddNotification( 221 message_center::Notification::CreateSystemNotification( 222 ash::network_connect::kNetworkActivateNotificationId, 223 base::string16() /* title */, 224 message, 225 icon, 226 ash::system_notifier::NOTIFIER_NETWORK, 227 base::Bind(&NotificationClicked, 228 default_network->path(), info_url))); 229 230 check_for_promo_ = false; 231 SetShow3gPromoNotification(false); 232 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) 233 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); 234} 235 236} // namespace chromeos 237