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