data_promo_notification.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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/system_notifier.h"
11#include "ash/system/tray/system_tray.h"
12#include "ash/system/tray/system_tray_notifier.h"
13#include "base/prefs/pref_registry_simple.h"
14#include "base/prefs/pref_service.h"
15#include "base/strings/utf_string_conversions.h"
16#include "chrome/browser/browser_process.h"
17#include "chrome/browser/chromeos/login/helper.h"
18#include "chrome/browser/chromeos/mobile_config.h"
19#include "chrome/browser/profiles/profile.h"
20#include "chrome/browser/profiles/profile_manager.h"
21#include "chrome/browser/ui/browser.h"
22#include "chrome/browser/ui/browser_finder.h"
23#include "chrome/browser/ui/browser_list.h"
24#include "chrome/browser/ui/singleton_tabs.h"
25#include "chrome/common/pref_names.h"
26#include "chromeos/login/login_state.h"
27#include "chromeos/network/device_state.h"
28#include "chromeos/network/network_connection_handler.h"
29#include "chromeos/network/network_event_log.h"
30#include "chromeos/network/network_state.h"
31#include "chromeos/network/network_state_handler.h"
32#include "grit/ash_resources.h"
33#include "grit/generated_resources.h"
34#include "grit/theme_resources.h"
35#include "third_party/cros_system_api/dbus/service_constants.h"
36#include "ui/base/l10n/l10n_util.h"
37#include "ui/base/resource/resource_bundle.h"
38#include "ui/message_center/message_center.h"
39#include "ui/message_center/notification.h"
40#include "ui/views/view.h"
41#include "ui/views/widget/widget.h"
42
43namespace chromeos {
44
45namespace {
46
47// Time in milliseconds to delay showing of promo
48// notification when Chrome window is not on screen.
49const int kPromoShowDelayMs = 10000;
50
51const int kNotificationCountPrefDefault = -1;
52
53bool GetBooleanPref(const char* pref_name) {
54  Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
55  PrefService* prefs = profile->GetPrefs();
56  return prefs->GetBoolean(pref_name);
57}
58
59int GetIntegerLocalPref(const char* pref_name) {
60  PrefService* prefs = g_browser_process->local_state();
61  return prefs->GetInteger(pref_name);
62}
63
64void SetBooleanPref(const char* pref_name, bool value) {
65  Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
66  PrefService* prefs = profile->GetPrefs();
67  prefs->SetBoolean(pref_name, value);
68}
69
70void SetIntegerLocalPref(const char* pref_name, int value) {
71  PrefService* prefs = g_browser_process->local_state();
72  prefs->SetInteger(pref_name, value);
73}
74
75// Returns prefs::kShow3gPromoNotification or false if no active browser.
76bool ShouldShow3gPromoNotification() {
77  return GetBooleanPref(prefs::kShow3gPromoNotification);
78}
79
80void SetShow3gPromoNotification(bool value) {
81  SetBooleanPref(prefs::kShow3gPromoNotification, value);
82}
83
84// Returns prefs::kCarrierDealPromoShown which is number of times
85// carrier deal notification has been shown to users on this machine.
86int GetCarrierDealPromoShown() {
87  return GetIntegerLocalPref(prefs::kCarrierDealPromoShown);
88}
89
90void SetCarrierDealPromoShown(int value) {
91  SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value);
92}
93
94const chromeos::MobileConfig::Carrier* GetCarrier(
95    const NetworkState* cellular) {
96  const DeviceState* device = NetworkHandler::Get()->network_state_handler()->
97      GetDeviceState(cellular->device_path());
98  std::string carrier_id = device ? device->home_provider_id() : "";
99  if (carrier_id.empty()) {
100    NET_LOG_ERROR("Empty carrier ID for cellular network",
101                  device ? device->path(): "No device");
102    return NULL;
103  }
104
105  chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance();
106  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::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