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/resources/grit/ash_resources.h" 8#include "ash/system/chromeos/network/network_connect.h" 9#include "ash/system/system_notifier.h" 10#include "base/prefs/pref_registry_simple.h" 11#include "base/prefs/pref_service.h" 12#include "base/strings/utf_string_conversions.h" 13#include "chrome/browser/browser_process.h" 14#include "chrome/browser/chromeos/login/helper.h" 15#include "chrome/browser/chromeos/mobile_config.h" 16#include "chrome/browser/profiles/profile.h" 17#include "chrome/browser/profiles/profile_manager.h" 18#include "chrome/browser/ui/browser.h" 19#include "chrome/browser/ui/browser_list.h" 20#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" 21#include "chrome/browser/ui/singleton_tabs.h" 22#include "chrome/common/pref_names.h" 23#include "chrome/grit/generated_resources.h" 24#include "chromeos/login/login_state.h" 25#include "chromeos/network/device_state.h" 26#include "chromeos/network/network_connection_handler.h" 27#include "chromeos/network/network_event_log.h" 28#include "chromeos/network/network_state.h" 29#include "chromeos/network/network_state_handler.h" 30#include "third_party/cros_system_api/dbus/service_constants.h" 31#include "ui/base/l10n/l10n_util.h" 32#include "ui/base/resource/resource_bundle.h" 33#include "ui/message_center/message_center.h" 34#include "ui/message_center/notification.h" 35#include "ui/views/view.h" 36#include "ui/views/widget/widget.h" 37 38namespace chromeos { 39 40namespace { 41 42const int kNotificationCountPrefDefault = -1; 43 44bool GetBooleanPref(const char* pref_name) { 45 Profile* profile = ProfileManager::GetPrimaryUserProfile(); 46 PrefService* prefs = profile->GetPrefs(); 47 return prefs->GetBoolean(pref_name); 48} 49 50int GetIntegerLocalPref(const char* pref_name) { 51 PrefService* prefs = g_browser_process->local_state(); 52 return prefs->GetInteger(pref_name); 53} 54 55void SetBooleanPref(const char* pref_name, bool value) { 56 Profile* profile = ProfileManager::GetPrimaryUserProfile(); 57 PrefService* prefs = profile->GetPrefs(); 58 prefs->SetBoolean(pref_name, value); 59} 60 61void SetIntegerLocalPref(const char* pref_name, int value) { 62 PrefService* prefs = g_browser_process->local_state(); 63 prefs->SetInteger(pref_name, value); 64} 65 66// Returns prefs::kShow3gPromoNotification or false if no active browser. 67bool ShouldShow3gPromoNotification() { 68 return GetBooleanPref(prefs::kShow3gPromoNotification); 69} 70 71void SetShow3gPromoNotification(bool value) { 72 SetBooleanPref(prefs::kShow3gPromoNotification, value); 73} 74 75// Returns prefs::kCarrierDealPromoShown which is number of times 76// carrier deal notification has been shown to users on this machine. 77int GetCarrierDealPromoShown() { 78 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); 79} 80 81void SetCarrierDealPromoShown(int value) { 82 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); 83} 84 85const chromeos::MobileConfig::Carrier* GetCarrier( 86 const NetworkState* cellular) { 87 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> 88 GetDeviceState(cellular->device_path()); 89 std::string carrier_id = device ? device->home_provider_id() : ""; 90 if (carrier_id.empty()) { 91 NET_LOG_ERROR("Empty carrier ID for cellular network", 92 device ? device->path(): "No device"); 93 return NULL; 94 } 95 96 chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance(); 97 if (!config->IsReady()) 98 return NULL; 99 100 return config->GetCarrier(carrier_id); 101} 102 103const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal( 104 const chromeos::MobileConfig::Carrier* carrier) { 105 const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal(); 106 if (deal) { 107 // Check deal for validity. 108 int carrier_deal_promo_pref = GetCarrierDealPromoShown(); 109 if (carrier_deal_promo_pref >= deal->notification_count()) 110 return NULL; 111 const std::string locale = g_browser_process->GetApplicationLocale(); 112 std::string deal_text = deal->GetLocalizedString(locale, 113 "notification_text"); 114 NET_LOG_DEBUG("Carrier Deal Found", deal_text); 115 if (deal_text.empty()) 116 return NULL; 117 } 118 return deal; 119} 120 121void NotificationClicked(const std::string& service_path, 122 const std::string& info_url) { 123 if (info_url.empty()) 124 ash::network_connect::ShowNetworkSettings(service_path); 125 126 chrome::ScopedTabbedBrowserDisplayer displayer( 127 ProfileManager::GetPrimaryUserProfile(), 128 chrome::HOST_DESKTOP_TYPE_ASH); 129 chrome::ShowSingletonTab(displayer.browser(), GURL(info_url)); 130} 131 132} // namespace 133 134//////////////////////////////////////////////////////////////////////////////// 135// DataPromoNotification 136 137DataPromoNotification::DataPromoNotification() 138 : check_for_promo_(true), 139 weak_ptr_factory_(this) { 140 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); 141} 142 143DataPromoNotification::~DataPromoNotification() { 144 if (NetworkHandler::IsInitialized()) { 145 NetworkHandler::Get()->network_state_handler()->RemoveObserver( 146 this, FROM_HERE); 147 } 148} 149 150void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { 151 // Carrier deal notification shown count defaults to 0. 152 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); 153} 154 155void DataPromoNotification::NetworkPropertiesUpdated( 156 const NetworkState* network) { 157 if (!network || network->type() != shill::kTypeCellular) 158 return; 159 ShowOptionalMobileDataPromoNotification(); 160} 161 162void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { 163 // Call NetworkPropertiesUpdated in case the Cellular network became the 164 // default network. 165 NetworkPropertiesUpdated(network); 166} 167 168void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { 169 // Display a one-time notification for authenticated users on first use 170 // of Mobile Data connection or if there is a carrier deal defined 171 // show that even if user has already seen generic promo. 172 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated()) 173 return; 174 const NetworkState* default_network = 175 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 176 if (!default_network || default_network->type() != shill::kTypeCellular) 177 return; 178 // When requesting a network connection, do not show the notification. 179 if (NetworkHandler::Get()->network_connection_handler()-> 180 HasPendingConnectRequest()) 181 return; 182 183 int carrier_deal_promo_pref = kNotificationCountPrefDefault; 184 const MobileConfig::CarrierDeal* deal = NULL; 185 const MobileConfig::Carrier* carrier = GetCarrier(default_network); 186 if (carrier) 187 deal = GetCarrierDeal(carrier); 188 189 base::string16 message = 190 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 = 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; 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