| OLD | NEW |
| (Empty) |
| 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/network_menu_button.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/message_loop.h" | |
| 14 #include "base/string_util.h" | |
| 15 #include "base/stringprintf.h" | |
| 16 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 17 #include "chrome/browser/chromeos/login/base_login_display_host.h" | |
| 18 #include "chrome/browser/chromeos/options/network_config_view.h" | |
| 19 #include "chrome/browser/chromeos/sim_dialog_delegate.h" | |
| 20 #include "chrome/browser/chromeos/status/data_promo_notification.h" | |
| 21 #include "chrome/browser/chromeos/view_ids.h" | |
| 22 #include "chrome/browser/prefs/pref_service.h" | |
| 23 #include "chrome/browser/ui/browser.h" | |
| 24 #include "chrome/browser/ui/browser_list.h" | |
| 25 #include "chrome/common/pref_names.h" | |
| 26 | |
| 27 namespace chromeos { | |
| 28 | |
| 29 //////////////////////////////////////////////////////////////////////////////// | |
| 30 // NetworkMenuButton | |
| 31 | |
| 32 NetworkMenuButton::NetworkMenuButton(StatusAreaButton::Delegate* delegate) | |
| 33 : StatusAreaButton(delegate, this), | |
| 34 data_promo_notification_(new DataPromoNotification()) { | |
| 35 set_id(VIEW_ID_STATUS_BUTTON_NETWORK_MENU); | |
| 36 network_menu_.reset(new NetworkMenu(this)); | |
| 37 network_icon_.reset( | |
| 38 new NetworkMenuIcon(this, NetworkMenuIcon::MENU_MODE)); | |
| 39 | |
| 40 NetworkLibrary* network_library = CrosLibrary::Get()->GetNetworkLibrary(); | |
| 41 OnNetworkManagerChanged(network_library); | |
| 42 network_library->AddNetworkManagerObserver(this); | |
| 43 network_library->AddCellularDataPlanObserver(this); | |
| 44 const NetworkDevice* cellular = network_library->FindCellularDevice(); | |
| 45 if (cellular) { | |
| 46 cellular_device_path_ = cellular->device_path(); | |
| 47 network_library->AddNetworkDeviceObserver(cellular_device_path_, this); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 NetworkMenuButton::~NetworkMenuButton() { | |
| 52 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); | |
| 53 netlib->RemoveNetworkManagerObserver(this); | |
| 54 netlib->RemoveObserverForAllNetworks(this); | |
| 55 netlib->RemoveCellularDataPlanObserver(this); | |
| 56 if (!cellular_device_path_.empty()) | |
| 57 netlib->RemoveNetworkDeviceObserver(cellular_device_path_, this); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 void NetworkMenuButton::RegisterPrefs(PrefService* local_state) { | |
| 62 // Carrier deal notification shown count defaults to 0. | |
| 63 local_state->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); | |
| 64 } | |
| 65 | |
| 66 //////////////////////////////////////////////////////////////////////////////// | |
| 67 // NetworkLibrary::NetworkDeviceObserver implementation: | |
| 68 | |
| 69 void NetworkMenuButton::OnNetworkDeviceChanged(NetworkLibrary* cros, | |
| 70 const NetworkDevice* device) { | |
| 71 // Device status, such as SIMLock may have changed. | |
| 72 SetNetworkIcon(); | |
| 73 network_menu_->UpdateMenu(); | |
| 74 } | |
| 75 | |
| 76 //////////////////////////////////////////////////////////////////////////////// | |
| 77 // NetworkMenuButton, NetworkLibrary::NetworkManagerObserver implementation: | |
| 78 | |
| 79 void NetworkMenuButton::OnNetworkManagerChanged(NetworkLibrary* cros) { | |
| 80 // This gets called on initialization, so any changes should be reflected | |
| 81 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | |
| 82 SetNetworkIcon(); | |
| 83 network_menu_->UpdateMenu(); | |
| 84 RefreshNetworkObserver(cros); | |
| 85 RefreshNetworkDeviceObserver(cros); | |
| 86 data_promo_notification_->ShowOptionalMobileDataPromoNotification(cros, this, | |
| 87 this); | |
| 88 } | |
| 89 | |
| 90 //////////////////////////////////////////////////////////////////////////////// | |
| 91 // NetworkMenuButton, NetworkLibrary::NetworkObserver implementation: | |
| 92 void NetworkMenuButton::OnNetworkChanged(NetworkLibrary* cros, | |
| 93 const Network* network) { | |
| 94 SetNetworkIcon(); | |
| 95 network_menu_->UpdateMenu(); | |
| 96 } | |
| 97 | |
| 98 void NetworkMenuButton::OnCellularDataPlanChanged(NetworkLibrary* cros) { | |
| 99 // Call OnNetworkManagerChanged which will update the icon. | |
| 100 SetNetworkIcon(); | |
| 101 network_menu_->UpdateMenu(); | |
| 102 } | |
| 103 | |
| 104 //////////////////////////////////////////////////////////////////////////////// | |
| 105 // NetworkMenuButton, NetworkMenu implementation: | |
| 106 | |
| 107 views::MenuButton* NetworkMenuButton::GetMenuButton() { | |
| 108 return this; | |
| 109 } | |
| 110 | |
| 111 gfx::NativeWindow NetworkMenuButton::GetNativeWindow() const { | |
| 112 if (BaseLoginDisplayHost::default_host()) { | |
| 113 // When not in browser mode i.e. login screen, status area is hosted in | |
| 114 // a separate widget. | |
| 115 return BaseLoginDisplayHost::default_host()->GetNativeWindow(); | |
| 116 } else { | |
| 117 // This must always have a parent, which must have a widget ancestor. | |
| 118 return parent()->GetWidget()->GetNativeWindow(); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void NetworkMenuButton::OpenButtonOptions() { | |
| 123 delegate()->ExecuteStatusAreaCommand( | |
| 124 this, StatusAreaButton::Delegate::SHOW_NETWORK_OPTIONS); | |
| 125 } | |
| 126 | |
| 127 bool NetworkMenuButton::ShouldOpenButtonOptions() const { | |
| 128 return delegate()->ShouldExecuteStatusAreaCommand( | |
| 129 this, StatusAreaButton::Delegate::SHOW_NETWORK_OPTIONS); | |
| 130 } | |
| 131 | |
| 132 //////////////////////////////////////////////////////////////////////////////// | |
| 133 // NetworkMenuButton, views::View implementation: | |
| 134 | |
| 135 void NetworkMenuButton::OnLocaleChanged() { | |
| 136 SetNetworkIcon(); | |
| 137 network_menu_->UpdateMenu(); | |
| 138 } | |
| 139 | |
| 140 //////////////////////////////////////////////////////////////////////////////// | |
| 141 // NetworkMenuButton, views::MenuButtonListener implementation: | |
| 142 void NetworkMenuButton::OnMenuButtonClicked(views::View* source, | |
| 143 const gfx::Point& point) { | |
| 144 network_menu_->RunMenu(source); | |
| 145 } | |
| 146 | |
| 147 //////////////////////////////////////////////////////////////////////////////// | |
| 148 // NetworkMenuButton, NetworkMenuIcon::Delegate implementation: | |
| 149 void NetworkMenuButton::NetworkMenuIconChanged() { | |
| 150 const SkBitmap bitmap = network_icon_->GetIconAndText(NULL); | |
| 151 SetIcon(bitmap); | |
| 152 SchedulePaint(); | |
| 153 } | |
| 154 | |
| 155 //////////////////////////////////////////////////////////////////////////////// | |
| 156 // MessageBubbleLinkListener implementation: | |
| 157 | |
| 158 void NetworkMenuButton::OnLinkActivated(size_t index) { | |
| 159 // If we have deal info URL defined that means that there're | |
| 160 // 2 links in bubble. Let the user close it manually then thus giving ability | |
| 161 // to navigate to second link. | |
| 162 // mobile_data_bubble_ will be set to NULL in BubbleClosing callback. | |
| 163 std::string deal_info_url = data_promo_notification_->deal_info_url(); | |
| 164 std::string deal_topup_url = data_promo_notification_->deal_topup_url(); | |
| 165 if (deal_info_url.empty()) | |
| 166 data_promo_notification_->CloseNotification(); | |
| 167 | |
| 168 std::string deal_url_to_open; | |
| 169 if (index == 0) { | |
| 170 if (!deal_topup_url.empty()) { | |
| 171 deal_url_to_open = deal_topup_url; | |
| 172 } else { | |
| 173 const Network* cellular = | |
| 174 CrosLibrary::Get()->GetNetworkLibrary()->cellular_network(); | |
| 175 if (!cellular) | |
| 176 return; | |
| 177 network_menu_->ShowTabbedNetworkSettings(cellular); | |
| 178 return; | |
| 179 } | |
| 180 } else if (index == 1) { | |
| 181 deal_url_to_open = deal_info_url; | |
| 182 } | |
| 183 | |
| 184 if (!deal_url_to_open.empty()) { | |
| 185 Browser* browser = BrowserList::GetLastActive(); | |
| 186 if (!browser) | |
| 187 return; | |
| 188 browser->ShowSingletonTab(GURL(deal_url_to_open)); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 //////////////////////////////////////////////////////////////////////////////// | |
| 193 // NetworkMenuButton, private methods: | |
| 194 | |
| 195 void NetworkMenuButton::SetNetworkIcon() { | |
| 196 string16 tooltip; | |
| 197 const SkBitmap bitmap = network_icon_->GetIconAndText(&tooltip); | |
| 198 SetIcon(bitmap); | |
| 199 SetTooltipAndAccessibleName(tooltip); | |
| 200 SchedulePaint(); | |
| 201 } | |
| 202 | |
| 203 void NetworkMenuButton::RefreshNetworkObserver(NetworkLibrary* cros) { | |
| 204 const Network* network = cros->active_network(); | |
| 205 std::string new_network = network ? network->service_path() : std::string(); | |
| 206 if (active_network_ != new_network) { | |
| 207 if (!active_network_.empty()) { | |
| 208 cros->RemoveNetworkObserver(active_network_, this); | |
| 209 } | |
| 210 if (!new_network.empty()) { | |
| 211 cros->AddNetworkObserver(new_network, this); | |
| 212 } | |
| 213 active_network_ = new_network; | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 void NetworkMenuButton::RefreshNetworkDeviceObserver(NetworkLibrary* cros) { | |
| 218 const NetworkDevice* cellular = cros->FindCellularDevice(); | |
| 219 std::string new_cellular_device_path = cellular ? | |
| 220 cellular->device_path() : std::string(); | |
| 221 if (cellular_device_path_ != new_cellular_device_path) { | |
| 222 if (!cellular_device_path_.empty()) { | |
| 223 cros->RemoveNetworkDeviceObserver(cellular_device_path_, this); | |
| 224 } | |
| 225 if (!new_cellular_device_path.empty()) { | |
| 226 cros->AddNetworkDeviceObserver(new_cellular_device_path, this); | |
| 227 } | |
| 228 cellular_device_path_ = new_cellular_device_path; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 void NetworkMenuButton::SetTooltipAndAccessibleName(const string16& label) { | |
| 233 SetTooltipText(label); | |
| 234 SetAccessibleName(label); | |
| 235 } | |
| 236 | |
| 237 } // namespace chromeos | |
| OLD | NEW |