1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/status/network_menu_button.h"
#include <algorithm>
#include <limits>
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/login/helper.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/options/network_config_view.h"
#include "chrome/browser/chromeos/sim_dialog_delegate.h"
#include "chrome/browser/chromeos/status/status_area_host.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/pref_names.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas_skia.h"
#include "views/window/window.h"
namespace {
// Time in milliseconds to delay showing of promo
// notification when Chrome window is not on screen.
const int kPromoShowDelayMs = 10000;
const int kNotificationCountPrefDefault = -1;
bool GetBooleanPref(const char* pref_name) {
Browser* browser = BrowserList::GetLastActive();
// Default to safe value which is false (not to show bubble).
if (!browser || !browser->profile())
return false;
PrefService* prefs = browser->profile()->GetPrefs();
return prefs->GetBoolean(pref_name);
}
int GetIntegerLocalPref(const char* pref_name) {
PrefService* prefs = g_browser_process->local_state();
return prefs->GetInteger(pref_name);
}
void SetBooleanPref(const char* pref_name, bool value) {
Browser* browser = BrowserList::GetLastActive();
if (!browser || !browser->profile())
return;
PrefService* prefs = browser->profile()->GetPrefs();
prefs->SetBoolean(pref_name, value);
}
void SetIntegerLocalPref(const char* pref_name, int value) {
PrefService* prefs = g_browser_process->local_state();
prefs->SetInteger(pref_name, value);
}
// Returns prefs::kShow3gPromoNotification or false
// if there's no active browser.
bool ShouldShow3gPromoNotification() {
return GetBooleanPref(prefs::kShow3gPromoNotification);
}
void SetShow3gPromoNotification(bool value) {
SetBooleanPref(prefs::kShow3gPromoNotification, value);
}
// Returns prefs::kCarrierDealPromoShown which is number of times
// carrier deal notification has been shown to users on this machine.
int GetCarrierDealPromoShown() {
return GetIntegerLocalPref(prefs::kCarrierDealPromoShown);
}
void SetCarrierDealPromoShown(int value) {
SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value);
}
} // namespace
namespace chromeos {
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton
// static
const int NetworkMenuButton::kThrobDuration = 750;
NetworkMenuButton::NetworkMenuButton(StatusAreaHost* host)
: StatusAreaButton(host, this),
NetworkMenu(),
icon_(NULL),
right_badge_(NULL),
left_badge_(NULL),
mobile_data_bubble_(NULL),
check_for_promo_(true),
was_sim_locked_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
last_network_type_(TYPE_WIFI) {
animation_connecting_.SetThrobDuration(kThrobDuration);
animation_connecting_.SetTweenType(ui::Tween::LINEAR);
NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary();
OnNetworkManagerChanged(network_library);
network_library->AddNetworkManagerObserver(this);
network_library->AddCellularDataPlanObserver(this);
const NetworkDevice* cellular = network_library->FindCellularDevice();
if (cellular) {
cellular_device_path_ = cellular->device_path();
was_sim_locked_ = cellular->is_sim_locked();
network_library->AddNetworkDeviceObserver(cellular_device_path_, this);
}
}
NetworkMenuButton::~NetworkMenuButton() {
NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
netlib->RemoveNetworkManagerObserver(this);
netlib->RemoveObserverForAllNetworks(this);
netlib->RemoveCellularDataPlanObserver(this);
if (!cellular_device_path_.empty())
netlib->RemoveNetworkDeviceObserver(cellular_device_path_, this);
if (mobile_data_bubble_)
mobile_data_bubble_->Close();
}
// static
void NetworkMenuButton::RegisterPrefs(PrefService* local_state) {
// Carrier deal notification shown count defaults to 0.
local_state->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0);
}
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, ui::AnimationDelegate implementation:
void NetworkMenuButton::AnimationProgressed(const ui::Animation* animation) {
if (animation == &animation_connecting_) {
SetIconOnly(IconForNetworkConnecting(
animation_connecting_.GetCurrentValue(), last_network_type_));
// No need to set the badge here, because it should already be set.
SchedulePaint();
} else {
MenuButton::AnimationProgressed(animation);
}
}
////////////////////////////////////////////////////////////////////////////////
// NetworkLibrary::NetworkDeviceObserver implementation:
void NetworkMenuButton::OnNetworkDeviceChanged(NetworkLibrary* cros,
const NetworkDevice* device) {
// Device status, such as SIMLock may have changed.
OnNetworkChanged(cros, cros->active_network());
const NetworkDevice* cellular = cros->FindCellularDevice();
if (cellular) {
// We make an assumption (which is valid for now) that the SIM
// unlock dialog is put up only when the user is trying to enable
// mobile data. So if the SIM is now unlocked, initiate the
// enable operation that the user originally requested.
if (was_sim_locked_ && !cellular->is_sim_locked() &&
!cros->cellular_enabled()) {
cros->EnableCellularNetworkDevice(true);
}
was_sim_locked_ = cellular->is_sim_locked();
}
}
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, NetworkLibrary::NetworkManagerObserver implementation:
void NetworkMenuButton::OnNetworkManagerChanged(NetworkLibrary* cros) {
OnNetworkChanged(cros, cros->active_network());
ShowOptionalMobileDataPromoNotification(cros);
}
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, NetworkLibrary::NetworkObserver implementation:
void NetworkMenuButton::OnNetworkChanged(NetworkLibrary* cros,
const Network* network) {
// This gets called on initialization, so any changes should be reflected
// in CrosMock::SetNetworkLibraryStatusAreaExpectations().
SetNetworkIcon(cros, network);
RefreshNetworkObserver(cros);
RefreshNetworkDeviceObserver(cros);
SchedulePaint();
UpdateMenu();
}
void NetworkMenuButton::OnCellularDataPlanChanged(NetworkLibrary* cros) {
// Call OnNetworkManagerChanged which will update the icon.
OnNetworkManagerChanged(cros);
}
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, NetworkMenu implementation:
bool NetworkMenuButton::IsBrowserMode() const {
return host_->GetScreenMode() == StatusAreaHost::kBrowserMode;
}
views::MenuButton* NetworkMenuButton::GetMenuButton() {
return this;
}
gfx::NativeWindow NetworkMenuButton::GetNativeWindow() const {
return host_->GetNativeWindow();
}
void NetworkMenuButton::OpenButtonOptions() {
host_->OpenButtonOptions(this);
}
bool NetworkMenuButton::ShouldOpenButtonOptions() const {
return host_->ShouldOpenButtonOptions(this);
}
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, views::View implementation:
void NetworkMenuButton::OnLocaleChanged() {
NetworkLibrary* lib = CrosLibrary::Get()->GetNetworkLibrary();
SetNetworkIcon(lib, lib->active_network());
}
////////////////////////////////////////////////////////////////////////////////
// MessageBubbleDelegate implementation:
void NetworkMenuButton::BubbleClosing(Bubble* bubble, bool closed_by_escape) {
mobile_data_bubble_ = NULL;
deal_info_url_.clear();
deal_topup_url_.clear();
}
bool NetworkMenuButton::CloseOnEscape() {
return true;
}
bool NetworkMenuButton::FadeInOnShow() {
return false;
}
void NetworkMenuButton::OnLinkActivated(size_t index) {
// If we have deal info URL defined that means that there're
// 2 links in bubble. Let the user close it manually then thus giving ability
// to navigate to second link.
// mobile_data_bubble_ will be set to NULL in BubbleClosing callback.
if (deal_info_url_.empty() && mobile_data_bubble_)
mobile_data_bubble_->Close();
std::string deal_url_to_open;
if (index == 0) {
if (!deal_topup_url_.empty()) {
deal_url_to_open = deal_topup_url_;
} else {
const Network* cellular =
CrosLibrary::Get()->GetNetworkLibrary()->cellular_network();
if (!cellular)
return;
ShowTabbedNetworkSettings(cellular);
return;
}
} else if (index == 1) {
deal_url_to_open = deal_info_url_;
}
if (!deal_url_to_open.empty()) {
Browser* browser = BrowserList::GetLastActive();
if (!browser)
return;
browser->ShowSingletonTab(GURL(deal_url_to_open));
}
}
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, private methods
const ServicesCustomizationDocument::CarrierDeal*
NetworkMenuButton::GetCarrierDeal(
NetworkLibrary* cros) {
std::string carrier_id = cros->GetCellularHomeCarrierId();
if (carrier_id.empty()) {
LOG(ERROR) << "Empty carrier ID with a cellular connected.";
return NULL;
}
ServicesCustomizationDocument* customization =
ServicesCustomizationDocument::GetInstance();
if (!customization->IsReady())
return NULL;
const ServicesCustomizationDocument::CarrierDeal* deal =
customization->GetCarrierDeal(carrier_id, true);
if (deal) {
// Check deal for validity.
int carrier_deal_promo_pref = GetCarrierDealPromoShown();
if (carrier_deal_promo_pref >= deal->notification_count())
return NULL;
const std::string locale = g_browser_process->GetApplicationLocale();
std::string deal_text = deal->GetLocalizedString(locale,
"notification_text");
if (deal_text.empty())
return NULL;
}
return deal;
}
void NetworkMenuButton::SetIconAndBadges(const SkBitmap* icon,
const SkBitmap* right_badge,
const SkBitmap* top_left_badge,
const SkBitmap* left_badge) {
icon_ = icon;
right_badge_ = right_badge;
top_left_badge_ = top_left_badge;
left_badge_ = left_badge;
SetIcon(IconForDisplay(icon_, right_badge_, top_left_badge_, left_badge_));
}
void NetworkMenuButton::SetIconOnly(const SkBitmap* icon) {
icon_ = icon;
SetIcon(IconForDisplay(icon_, right_badge_, top_left_badge_, left_badge_));
}
void NetworkMenuButton::SetBadgesOnly(const SkBitmap* right_badge,
const SkBitmap* top_left_badge,
const SkBitmap* left_badge) {
right_badge_ = right_badge;
top_left_badge_ = top_left_badge;
left_badge_ = left_badge;
SetIcon(IconForDisplay(icon_, right_badge_, top_left_badge_, left_badge_));
}
void NetworkMenuButton::SetNetworkIcon(NetworkLibrary* cros,
const Network* network) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
if (!cros || !CrosLibrary::Get()->EnsureLoaded()) {
SetIconAndBadges(rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0),
rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING),
NULL, NULL);
SetTooltipText(UTF16ToWide(l10n_util::GetStringUTF16(
IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)));
return;
}
if (!cros->Connected() && !cros->Connecting()) {
animation_connecting_.Stop();
if (last_network_type_ == TYPE_WIFI) {
SetIconAndBadges(
rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_ARCS0), NULL, NULL, NULL);
} else {
SetIconAndBadges(
rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0),
rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_DISCONNECTED),
NULL, NULL);
}
SetTooltipText(UTF16ToWide(l10n_util::GetStringUTF16(
IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)));
return;
}
// Stash last network type away so we can show the right icon if we lose
// signal.
last_network_type_ = NetworkMenu::TypeForNetwork(network);
if (cros->wifi_connecting() || cros->cellular_connecting()) {
// Start the connecting animation if not running.
if (!animation_connecting_.is_animating()) {
animation_connecting_.Reset();
animation_connecting_.StartThrobbing(-1);
SetIconOnly(IconForNetworkConnecting(0, last_network_type_));
}
const WirelessNetwork* wireless = NULL;
if (cros->wifi_connecting()) {
wireless = cros->wifi_network();
SetBadgesOnly(NULL, NULL, NULL);
} else { // cellular_connecting
wireless = cros->cellular_network();
SetBadgesOnly(BadgeForNetworkTechnology(cros->cellular_network()),
BadgeForRoamingStatus(cros->cellular_network()),
NULL);
}
SetTooltipText(UTF16ToWide(l10n_util::GetStringFUTF16(
wireless->configuring() ? IDS_STATUSBAR_NETWORK_CONFIGURING_TOOLTIP
: IDS_STATUSBAR_NETWORK_CONNECTING_TOOLTIP,
UTF8ToUTF16(wireless->name()))));
} else {
// Stop connecting animation since we are not connecting.
animation_connecting_.Stop();
// Only set the icon, if it is an active network that changed.
if (network && network->is_active()) {
const SkBitmap* right_badge(NULL);
const SkBitmap* top_left_badge(NULL);
const SkBitmap* left_badge(NULL);
if (cros->virtual_network())
left_badge = rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_SECURE);
if (network->type() == TYPE_ETHERNET) {
SetIconAndBadges(rb.GetBitmapNamed(IDR_STATUSBAR_WIRED),
right_badge, top_left_badge, left_badge);
SetTooltipText(
UTF16ToWide(l10n_util::GetStringFUTF16(
IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP,
l10n_util::GetStringUTF16(
IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET))));
} else if (network->type() == TYPE_WIFI) {
const WifiNetwork* wifi = static_cast<const WifiNetwork*>(network);
SetIconAndBadges(IconForNetworkStrength(wifi),
right_badge, top_left_badge, left_badge);
SetTooltipText(UTF16ToWide(l10n_util::GetStringFUTF16(
IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP,
UTF8ToUTF16(wifi->name()))));
} else if (network->type() == TYPE_CELLULAR) {
const CellularNetwork* cellular =
static_cast<const CellularNetwork*>(network);
right_badge = BadgeForNetworkTechnology(cellular);
top_left_badge = BadgeForRoamingStatus(cellular);
SetIconAndBadges(IconForNetworkStrength(cellular),
right_badge, top_left_badge, left_badge);
SetTooltipText(UTF16ToWide(l10n_util::GetStringFUTF16(
IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP,
UTF8ToUTF16(cellular->name()))));
}
}
}
}
void NetworkMenuButton::RefreshNetworkObserver(NetworkLibrary* cros) {
const Network* network = cros->active_network();
std::string new_network = network ? network->service_path() : std::string();
if (active_network_ != new_network) {
if (!active_network_.empty()) {
cros->RemoveNetworkObserver(active_network_, this);
}
if (!new_network.empty()) {
cros->AddNetworkObserver(new_network, this);
}
active_network_ = new_network;
}
}
void NetworkMenuButton::RefreshNetworkDeviceObserver(NetworkLibrary* cros) {
const NetworkDevice* cellular = cros->FindCellularDevice();
std::string new_cellular_device_path = cellular ?
cellular->device_path() : std::string();
if (cellular_device_path_ != new_cellular_device_path) {
if (!cellular_device_path_.empty()) {
cros->RemoveNetworkDeviceObserver(cellular_device_path_, this);
}
if (!new_cellular_device_path.empty()) {
was_sim_locked_ = cellular->is_sim_locked();
cros->AddNetworkDeviceObserver(new_cellular_device_path, this);
}
cellular_device_path_ = new_cellular_device_path;
}
}
void NetworkMenuButton::ShowOptionalMobileDataPromoNotification(
NetworkLibrary* cros) {
// Display one-time notification for non-Guest users on first use
// of Mobile Data connection or if there's a carrier deal defined
// show that even if user has already seen generic promo.
if (IsBrowserMode() && !UserManager::Get()->IsLoggedInAsGuest() &&
check_for_promo_ && BrowserList::GetLastActive() &&
cros->cellular_connected() && !cros->ethernet_connected() &&
!cros->wifi_connected()) {
const ServicesCustomizationDocument::CarrierDeal* deal =
GetCarrierDeal(cros);
std::string deal_text;
int carrier_deal_promo_pref = -1;
if (deal) {
carrier_deal_promo_pref = GetCarrierDealPromoShown();
const std::string locale = g_browser_process->GetApplicationLocale();
deal_text = deal->GetLocalizedString(locale, "notification_text");
deal_info_url_ = deal->info_url();
deal_topup_url_ = deal->top_up_url();
} else if (!ShouldShow3gPromoNotification()) {
check_for_promo_ = false;
return;
}
gfx::Rect button_bounds = GetScreenBounds();
// StatusArea button Y position is usually -1, fix it so that
// Contains() method for screen bounds works correctly.
button_bounds.set_y(button_bounds.y() + 1);
gfx::Rect screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
// Chrome window is initialized in visible state off screen and then is
// moved into visible screen area. Make sure that we're on screen
// so that bubble is shown correctly.
if (!screen_bounds.Contains(button_bounds)) {
// If we're not on screen yet, delay notification display.
// It may be shown earlier, on next NetworkLibrary callback processing.
if (method_factory_.empty()) {
MessageLoop::current()->PostDelayedTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&NetworkMenuButton::ShowOptionalMobileDataPromoNotification,
cros),
kPromoShowDelayMs);
}
return;
}
// Add deal text if it's defined.
std::wstring notification_text;
std::wstring default_text =
UTF16ToWide(l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE));
if (!deal_text.empty()) {
notification_text = StringPrintf(L"%ls\n\n%ls",
UTF8ToWide(deal_text).c_str(),
default_text.c_str());
} else {
notification_text = default_text;
}
// Use deal URL if it's defined or general "Network Settings" URL.
int link_message_id;
if (deal_topup_url_.empty())
link_message_id = IDS_OFFLINE_NETWORK_SETTINGS;
else
link_message_id = IDS_STATUSBAR_NETWORK_VIEW_ACCOUNT;
std::vector<std::wstring> links;
links.push_back(UTF16ToWide(l10n_util::GetStringUTF16(link_message_id)));
if (!deal_topup_url_.empty())
links.push_back(UTF16ToWide(l10n_util::GetStringUTF16(IDS_LEARN_MORE)));
mobile_data_bubble_ = MessageBubble::ShowWithLinks(
GetWidget(),
button_bounds,
BubbleBorder::TOP_RIGHT ,
ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_NOTIFICATION_3G),
notification_text,
links,
this);
check_for_promo_ = false;
SetShow3gPromoNotification(false);
if (carrier_deal_promo_pref != kNotificationCountPrefDefault)
SetCarrierDealPromoShown(carrier_deal_promo_pref + 1);
}
}
} // namespace chromeos
|