OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/network_message_observer.h" | 5 #include "chrome/browser/chromeos/network_message_observer.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/browser_list.h" | 12 #include "chrome/browser/browser_list.h" |
13 #include "chrome/browser/browser_window.h" | |
14 #include "chrome/browser/chromeos/cros/cros_library.h" | 13 #include "chrome/browser/chromeos/cros/cros_library.h" |
15 #include "chrome/browser/chromeos/cros/network_library.h" | 14 #include "chrome/browser/chromeos/cros/network_library.h" |
16 #include "chrome/browser/chromeos/notifications/balloon_view_host.h" | 15 #include "chrome/browser/chromeos/notifications/balloon_view_host.h" |
17 #include "chrome/browser/chromeos/options/network_config_view.h" | |
18 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
19 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/views/window.h" | |
22 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
23 #include "chrome/common/time_format.h" | 20 #include "chrome/common/time_format.h" |
24 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
25 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
26 #include "views/window/dialog_delegate.h" | |
27 #include "views/window/window.h" | |
28 | 23 |
29 namespace { | 24 namespace { |
30 | 25 |
31 // Returns prefs::kShowPlanNotifications in the profile of the last active | 26 // Returns prefs::kShowPlanNotifications in the profile of the last active |
32 // browser. If there is no active browser, returns true. | 27 // browser. If there is no active browser, returns true. |
33 bool ShouldShowMobilePlanNotifications() { | 28 bool ShouldShowMobilePlanNotifications() { |
34 Browser* browser = BrowserList::GetLastActive(); | 29 Browser* browser = BrowserList::GetLastActive(); |
35 if (!browser || !browser->profile()) | 30 if (!browser || !browser->profile()) |
36 return true; | 31 return true; |
37 | 32 |
38 PrefService* prefs = browser->profile()->GetPrefs(); | 33 PrefService* prefs = browser->profile()->GetPrefs(); |
39 return prefs->GetBoolean(prefs::kShowPlanNotifications); | 34 return prefs->GetBoolean(prefs::kShowPlanNotifications); |
40 } | 35 } |
41 | 36 |
42 } // namespace | 37 } // namespace |
43 | 38 |
44 namespace chromeos { | 39 namespace chromeos { |
45 | 40 |
46 NetworkMessageObserver::NetworkMessageObserver(Profile* profile) | 41 NetworkMessageObserver::NetworkMessageObserver(Profile* profile) |
47 : initialized_(false), | 42 : notification_connection_error_(profile, "network_connection.chromeos", |
48 notification_connection_error_(profile, "network_connection.chromeos", | |
49 IDR_NOTIFICATION_NETWORK_FAILED, | 43 IDR_NOTIFICATION_NETWORK_FAILED, |
50 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE)), | 44 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE)), |
51 notification_low_data_(profile, "network_low_data.chromeos", | 45 notification_low_data_(profile, "network_low_data.chromeos", |
52 IDR_NOTIFICATION_BARS_CRITICAL, | 46 IDR_NOTIFICATION_BARS_CRITICAL, |
53 l10n_util::GetStringUTF16(IDS_NETWORK_LOW_DATA_TITLE)), | 47 l10n_util::GetStringUTF16(IDS_NETWORK_LOW_DATA_TITLE)), |
54 notification_no_data_(profile, "network_no_data.chromeos", | 48 notification_no_data_(profile, "network_no_data.chromeos", |
55 IDR_NOTIFICATION_BARS_EMPTY, | 49 IDR_NOTIFICATION_BARS_EMPTY, |
56 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { | 50 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { |
57 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); | 51 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); |
58 OnNetworkManagerChanged(netlib); | 52 OnNetworkManagerChanged(netlib); |
59 // Note that this gets added as a NetworkManagerObserver and a | 53 // Note that this gets added as a NetworkManagerObserver and a |
60 // CellularDataPlanObserver in browser_init.cc | 54 // CellularDataPlanObserver in browser_init.cc |
61 initialized_ = true; | |
62 } | 55 } |
63 | 56 |
64 NetworkMessageObserver::~NetworkMessageObserver() { | 57 NetworkMessageObserver::~NetworkMessageObserver() { |
65 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); | 58 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); |
66 netlib->RemoveNetworkManagerObserver(this); | 59 netlib->RemoveNetworkManagerObserver(this); |
67 netlib->RemoveCellularDataPlanObserver(this); | 60 netlib->RemoveCellularDataPlanObserver(this); |
68 notification_connection_error_.Hide(); | 61 notification_connection_error_.Hide(); |
69 notification_low_data_.Hide(); | 62 notification_low_data_.Hide(); |
70 notification_no_data_.Hide(); | 63 notification_no_data_.Hide(); |
71 STLDeleteValues(&cellular_networks_); | 64 STLDeleteValues(&cellular_networks_); |
72 STLDeleteValues(&wifi_networks_); | 65 STLDeleteValues(&wifi_networks_); |
73 } | 66 } |
74 | 67 |
75 void NetworkMessageObserver::CreateModalPopup(views::WindowDelegate* view) { | |
76 Browser* browser = BrowserList::GetLastActive(); | |
77 if (browser && browser->type() != Browser::TYPE_NORMAL) { | |
78 browser = BrowserList::FindBrowserWithType(browser->profile(), | |
79 Browser::TYPE_NORMAL, | |
80 true); | |
81 } | |
82 DCHECK(browser); | |
83 views::Window* window = browser::CreateViewsWindow( | |
84 browser->window()->GetNativeHandle(), gfx::Rect(), view); | |
85 window->SetIsAlwaysOnTop(true); | |
86 window->Show(); | |
87 } | |
88 | |
89 void NetworkMessageObserver::OpenMobileSetupPage(const ListValue* args) { | 68 void NetworkMessageObserver::OpenMobileSetupPage(const ListValue* args) { |
90 Browser* browser = BrowserList::GetLastActive(); | 69 Browser* browser = BrowserList::GetLastActive(); |
91 if (browser) | 70 if (browser) |
92 browser->OpenMobilePlanTabAndActivate(); | 71 browser->OpenMobilePlanTabAndActivate(); |
93 } | 72 } |
94 | 73 |
95 void NetworkMessageObserver::OpenMoreInfoPage(const ListValue* args) { | 74 void NetworkMessageObserver::OpenMoreInfoPage(const ListValue* args) { |
96 Browser* browser = BrowserList::GetLastActive(); | 75 Browser* browser = BrowserList::GetLastActive(); |
97 if (!browser) | 76 if (!browser) |
98 return; | 77 return; |
99 chromeos::NetworkLibrary* lib = | 78 chromeos::NetworkLibrary* lib = |
100 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); | 79 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); |
101 const chromeos::CellularNetwork* cellular = lib->cellular_network(); | 80 const chromeos::CellularNetwork* cellular = lib->cellular_network(); |
102 if (!cellular) | 81 if (!cellular) |
103 return; | 82 return; |
104 browser->ShowSingletonTab(GURL(cellular->payment_url()), false); | 83 browser->ShowSingletonTab(GURL(cellular->payment_url()), false); |
105 } | 84 } |
106 | 85 |
107 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { | 86 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { |
108 const WifiNetworkVector& wifi_networks = obj->wifi_networks(); | 87 const WifiNetworkVector& wifi_networks = obj->wifi_networks(); |
109 const CellularNetworkVector& cellular_networks = obj->cellular_networks(); | 88 const CellularNetworkVector& cellular_networks = obj->cellular_networks(); |
110 | 89 |
111 NetworkConfigView* view = NULL; | |
112 std::string new_failed_network; | 90 std::string new_failed_network; |
113 // Check to see if we have any newly failed wifi network. | 91 // Check to see if we have any newly failed wifi network. |
114 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); | 92 for (WifiNetworkVector::const_iterator it = wifi_networks.begin(); |
115 it < wifi_networks.end(); it++) { | 93 it < wifi_networks.end(); it++) { |
116 const WifiNetwork* wifi = *it; | 94 const WifiNetwork* wifi = *it; |
117 if (wifi->failed()) { | 95 if (wifi->failed()) { |
118 ServicePathWifiMap::iterator iter = | 96 ServicePathWifiMap::iterator iter = |
119 wifi_networks_.find(wifi->service_path()); | 97 wifi_networks_.find(wifi->service_path()); |
120 // If the network did not previously exist, then don't do anything. | 98 // If the network did not previously exist, then don't do anything. |
121 // For example, if the user travels to a location and finds a service | 99 // For example, if the user travels to a location and finds a service |
122 // that has previously failed, we don't want to show a notification. | 100 // that has previously failed, we don't want to show a notification. |
123 if (iter == wifi_networks_.end()) | 101 if (iter == wifi_networks_.end()) |
124 continue; | 102 continue; |
125 | 103 |
126 const WifiNetwork* wifi_old = iter->second; | 104 const WifiNetwork* wifi_old = iter->second; |
127 // If this network was in a failed state previously, then it's not new. | |
128 if (wifi_old->failed()) | |
129 continue; | |
130 | |
131 // Display login box again for bad_passphrase and bad_wepkey errors. | |
132 if (wifi->error() == ERROR_BAD_PASSPHRASE || | |
133 wifi->error() == ERROR_BAD_WEPKEY) { | |
134 // The NetworkConfigView will show the appropriate error message. | |
135 view = new NetworkConfigView(wifi); | |
136 // There should only be one wifi network that failed to connect. | |
137 // If for some reason, we have more than one failure, | |
138 // we only display the first one. So we break here. | |
139 break; | |
140 } | |
141 | |
142 // If network connection failed, display a notification. | 105 // If network connection failed, display a notification. |
143 // We only do this if we were trying to make a new connection. | 106 // We only do this if we were trying to make a new connection. |
144 // So if a previously connected network got disconnected for any reason, | 107 // So if a previously connected network got disconnected for any reason, |
145 // we don't display notification. | 108 // we don't display notification. |
146 if (wifi_old->connecting()) { | 109 if (wifi_old->connecting()) { |
147 new_failed_network = wifi->name(); | 110 new_failed_network = wifi->name(); |
148 // Like above, there should only be one newly failed network. | 111 // Like above, there should only be one newly failed network. |
149 break; | 112 break; |
150 } | 113 } |
151 } | 114 } |
(...skipping 24 matching lines...) Expand all Loading... |
176 | 139 |
177 // Show connection error notification if necessary. | 140 // Show connection error notification if necessary. |
178 if (!new_failed_network.empty()) { | 141 if (!new_failed_network.empty()) { |
179 // Hide if already shown to force show it in case user has closed it. | 142 // Hide if already shown to force show it in case user has closed it. |
180 if (notification_connection_error_.visible()) | 143 if (notification_connection_error_.visible()) |
181 notification_connection_error_.Hide(); | 144 notification_connection_error_.Hide(); |
182 notification_connection_error_.Show(l10n_util::GetStringFUTF16( | 145 notification_connection_error_.Show(l10n_util::GetStringFUTF16( |
183 IDS_NETWORK_CONNECTION_ERROR_MESSAGE, | 146 IDS_NETWORK_CONNECTION_ERROR_MESSAGE, |
184 ASCIIToUTF16(new_failed_network)), false, false); | 147 ASCIIToUTF16(new_failed_network)), false, false); |
185 } | 148 } |
186 | |
187 // Show login box if necessary. | |
188 if (view && initialized_) | |
189 CreateModalPopup(view); | |
190 } | 149 } |
191 | 150 |
192 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* obj) { | 151 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* obj) { |
193 const CellularNetwork* cellular = obj->cellular_network(); | 152 const CellularNetwork* cellular = obj->cellular_network(); |
194 if (!cellular) | 153 if (!cellular) |
195 return; | 154 return; |
196 const CellularDataPlan* plan = cellular->GetSignificantDataPlan(); | 155 const CellularDataPlan* plan = cellular->GetSignificantDataPlan(); |
197 std::string new_plan_name = plan ? plan->plan_name : ""; | 156 std::string new_plan_name = plan ? plan->plan_name : ""; |
198 CellularDataPlanType new_plan_type = plan ? plan->plan_type : | 157 CellularDataPlanType new_plan_type = plan ? plan->plan_type : |
199 CELLULAR_DATA_PLAN_UNKNOWN; | 158 CELLULAR_DATA_PLAN_UNKNOWN; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 NewCallback(this, &NetworkMessageObserver::OpenMoreInfoPage), | 236 NewCallback(this, &NetworkMessageObserver::OpenMoreInfoPage), |
278 false, false); | 237 false, false); |
279 } else { | 238 } else { |
280 // Got data, so hide notifications. | 239 // Got data, so hide notifications. |
281 notification_low_data_.Hide(); | 240 notification_low_data_.Hide(); |
282 notification_no_data_.Hide(); | 241 notification_no_data_.Hide(); |
283 } | 242 } |
284 } | 243 } |
285 | 244 |
286 } // namespace chromeos | 245 } // namespace chromeos |
OLD | NEW |