data_promo_notification.cc revision 58537e28ecd584eab876aee8be7156509866d23a
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// Use of this source code is governed by a BSD-style license that can be 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// found in the LICENSE file. 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com#include "chrome/browser/chromeos/status/data_promo_notification.h" 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com#include "ash/shell.h" 8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com#include "ash/shell_window_ids.h" 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ash/system/chromeos/network/network_connect.h" 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ash/system/system_notifier.h" 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ash/system/tray/system_tray.h" 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ash/system/tray/system_tray_notifier.h" 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "base/prefs/pref_registry_simple.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "base/prefs/pref_service.h" 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "base/strings/utf_string_conversions.h" 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/browser_process.h" 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/chromeos/login/helper.h" 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/chromeos/mobile_config.h" 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/profiles/profile.h" 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/profiles/profile_manager.h" 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/ui/browser.h" 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/ui/browser_finder.h" 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/ui/browser_list.h" 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/browser/ui/singleton_tabs.h" 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chrome/common/pref_names.h" 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chromeos/login/login_state.h" 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chromeos/network/device_state.h" 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chromeos/network/network_connection_handler.h" 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chromeos/network/network_event_log.h" 30d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com#include "chromeos/network/network_state.h" 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "chromeos/network/network_state_handler.h" 328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "grit/ash_resources.h" 338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "grit/generated_resources.h" 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "grit/theme_resources.h" 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "third_party/cros_system_api/dbus/service_constants.h" 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ui/base/l10n/l10n_util.h" 378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ui/base/resource/resource_bundle.h" 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ui/message_center/message_center.h" 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ui/message_center/notification.h" 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ui/views/view.h" 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "ui/views/widget/widget.h" 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comnamespace chromeos { 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comnamespace { 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// Time in milliseconds to delay showing of promo 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// notification when Chrome window is not on screen. 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst int kPromoShowDelayMs = 10000; 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst int kNotificationCountPrefDefault = -1; 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool GetBooleanPref(const char* pref_name) { 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com PrefService* prefs = profile->GetPrefs(); 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prefs->GetBoolean(pref_name); 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint GetIntegerLocalPref(const char* pref_name) { 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com PrefService* prefs = g_browser_process->local_state(); 61d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com return prefs->GetInteger(pref_name); 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SetBooleanPref(const char* pref_name, bool value) { 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com PrefService* prefs = profile->GetPrefs(); 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com prefs->SetBoolean(pref_name, value); 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SetIntegerLocalPref(const char* pref_name, int value) { 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com PrefService* prefs = g_browser_process->local_state(); 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com prefs->SetInteger(pref_name, value); 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// Returns prefs::kShow3gPromoNotification or false if no active browser. 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool ShouldShow3gPromoNotification() { 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return GetBooleanPref(prefs::kShow3gPromoNotification); 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SetShow3gPromoNotification(bool value) { 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SetBooleanPref(prefs::kShow3gPromoNotification, value); 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// Returns prefs::kCarrierDealPromoShown which is number of times 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// carrier deal notification has been shown to users on this machine. 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint GetCarrierDealPromoShown() { 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SetCarrierDealPromoShown(int value) { 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst chromeos::MobileConfig::Carrier* GetCarrier( 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const NetworkState* cellular) { 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com GetDeviceState(cellular->device_path()); 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com std::string carrier_id = device ? device->home_provider_id() : ""; 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (carrier_id.empty()) { 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com NET_LOG_ERROR("Empty carrier ID for cellular network", 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com device ? device->path(): "No device"); 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return NULL; 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance(); 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (!config->IsReady()) 107 return NULL; 108 109 return config->GetCarrier(carrier_id); 110} 111 112const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal( 113 const chromeos::MobileConfig::Carrier* carrier) { 114 const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal(); 115 if (deal) { 116 // Check deal for validity. 117 int carrier_deal_promo_pref = GetCarrierDealPromoShown(); 118 if (carrier_deal_promo_pref >= deal->notification_count()) 119 return NULL; 120 const std::string locale = g_browser_process->GetApplicationLocale(); 121 std::string deal_text = deal->GetLocalizedString(locale, 122 "notification_text"); 123 NET_LOG_DEBUG("Carrier Deal Found", deal_text); 124 if (deal_text.empty()) 125 return NULL; 126 } 127 return deal; 128} 129 130void NotificationClicked(const std::string& service_path, 131 const std::string& info_url) { 132 if (info_url.empty()) 133 ash::network_connect::ShowNetworkSettings(service_path); 134 135 Browser* browser = chrome::FindOrCreateTabbedBrowser( 136 ProfileManager::GetDefaultProfileOrOffTheRecord(), 137 chrome::HOST_DESKTOP_TYPE_ASH); 138 if (!browser) 139 return; 140 chrome::ShowSingletonTab(browser, GURL(info_url)); 141} 142 143} // namespace 144 145//////////////////////////////////////////////////////////////////////////////// 146// DataPromoNotification 147 148DataPromoNotification::DataPromoNotification() 149 : check_for_promo_(true), 150 weak_ptr_factory_(this) { 151 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); 152} 153 154DataPromoNotification::~DataPromoNotification() { 155 if (NetworkHandler::IsInitialized()) { 156 NetworkHandler::Get()->network_state_handler()->RemoveObserver( 157 this, FROM_HERE); 158 } 159} 160 161void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { 162 // Carrier deal notification shown count defaults to 0. 163 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); 164} 165 166void DataPromoNotification::NetworkPropertiesUpdated( 167 const NetworkState* network) { 168 if (!network || network->type() != flimflam::kTypeCellular) 169 return; 170 ShowOptionalMobileDataPromoNotification(); 171} 172 173void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { 174 // Call NetworkPropertiesUpdated in case the Cellular network became the 175 // default network. 176 NetworkPropertiesUpdated(network); 177} 178 179void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { 180 // Display a one-time notification for authenticated users on first use 181 // of Mobile Data connection or if there is a carrier deal defined 182 // show that even if user has already seen generic promo. 183 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated()) 184 return; 185 const NetworkState* default_network = 186 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 187 if (!default_network || default_network->type() != flimflam::kTypeCellular) 188 return; 189 // When requesting a network connection, do not show the notification. 190 if (NetworkHandler::Get()->network_connection_handler()-> 191 HasPendingConnectRequest()) 192 return; 193 194 int carrier_deal_promo_pref = kNotificationCountPrefDefault; 195 const MobileConfig::CarrierDeal* deal = NULL; 196 const MobileConfig::Carrier* carrier = GetCarrier(default_network); 197 if (carrier) 198 deal = GetCarrierDeal(carrier); 199 200 string16 message = l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); 201 std::string info_url; 202 if (deal) { 203 carrier_deal_promo_pref = GetCarrierDealPromoShown(); 204 const std::string locale = g_browser_process->GetApplicationLocale(); 205 std::string deal_text = 206 deal->GetLocalizedString(locale, "notification_text"); 207 message = UTF8ToUTF16(deal_text + "\n\n") + message; 208 info_url = deal->info_url(); 209 if (info_url.empty() && carrier) 210 info_url = carrier->top_up_url(); 211 } else if (!ShouldShow3gPromoNotification()) { 212 check_for_promo_ = false; 213 return; 214 } 215 216 int icon_id; 217 if (default_network->network_technology() == flimflam::kNetworkTechnologyLte) 218 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; 219 else 220 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; 221 const gfx::Image& icon = 222 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); 223 224 message_center::MessageCenter::Get()->AddNotification( 225 message_center::Notification::CreateSystemNotification( 226 ash::network_connect::kNetworkActivateNotificationId, 227 base::string16() /* title */, 228 message, 229 icon, 230 ash::system_notifier::NOTIFIER_NETWORK, 231 base::Bind(&NotificationClicked, 232 default_network->path(), info_url))); 233 234 check_for_promo_ = false; 235 SetShow3gPromoNotification(false); 236 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) 237 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); 238} 239 240} // namespace chromeos 241