Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: chrome/browser/chromeos/status/network_menu.cc

Issue 7016042: Convert NetworkMenu from Menu2 to MenuItemView. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Undo undesired power menu edits. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/status/network_menu.h" 5 #include "chrome/browser/chromeos/status/network_menu.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h" 13 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h"
14 #include "chrome/browser/chromeos/cros/cros_library.h" 14 #include "chrome/browser/chromeos/cros/cros_library.h"
15 #include "chrome/browser/chromeos/customization_document.h" 15 #include "chrome/browser/chromeos/customization_document.h"
16 #include "chrome/browser/chromeos/sim_dialog_delegate.h" 16 #include "chrome/browser/chromeos/sim_dialog_delegate.h"
17 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list.h" 18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/views/window.h" 19 #include "chrome/browser/ui/views/window.h"
20 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
21 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
22 #include "content/browser/browser_thread.h"
22 #include "grit/generated_resources.h" 23 #include "grit/generated_resources.h"
23 #include "grit/theme_resources.h" 24 #include "grit/theme_resources.h"
24 #include "net/base/escape.h" 25 #include "net/base/escape.h"
25 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h" 27 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/canvas_skia.h" 28 #include "ui/gfx/canvas_skia.h"
28 #include "ui/gfx/skbitmap_operations.h" 29 #include "ui/gfx/skbitmap_operations.h"
29 #include "views/controls/menu/menu_2.h" 30 #include "views/controls/menu/menu_item_view.h"
31 #include "views/controls/menu/submenu_view.h"
30 #include "views/window/window.h" 32 #include "views/window/window.h"
31 33
32 namespace { 34 namespace {
33 35
34 // Replace '&' in a string with "&&" to allow it to be a menu item label. 36 // Replace '&' in a string with "&&" to allow it to be a menu item label.
35 std::string EscapeAmpersands(const std::string& input) { 37 std::string EscapeAmpersands(const std::string& input) {
36 std::string str = input; 38 std::string str = input;
37 size_t found = str.find('&'); 39 size_t found = str.find('&');
38 while (found != std::string::npos) { 40 while (found != std::string::npos) {
39 str.replace(found, 1, "&&"); 41 str.replace(found, 1, "&&");
40 found = str.find('&', found + 2); 42 found = str.find('&', found + 2);
41 } 43 }
42 return str; 44 return str;
43 } 45 }
44 46
47 // Offsets for views menu ids (main menu and submenu ids use the same
48 // namespace).
49 const int kMainIndexOffset = 1000;
50 const int kVPNIndexOffset = 2000;
51 const int kMoreIndexOffset = 3000;
52
53 // Default minimum width in pixels of the menu to prevent unnecessary
54 // resizing as networks are updated.
55 const int kDefaultMinimumWidth = 280;
56
45 } // namespace 57 } // namespace
46 58
47 namespace chromeos { 59 namespace chromeos {
48 60
61 class NetworkMenuModel : public views::MenuDelegate {
62 public:
63 struct NetworkInfo {
64 NetworkInfo();
65 ~NetworkInfo();
66
67 // "ethernet" | "wifi" | "cellular" | "other".
68 std::string network_type;
69 // "connected" | "connecting" | "disconnected" | "error".
70 std::string status;
71 // status message or error message, empty if unknown status.
72 std::string message;
73 // IP address (if network is active, empty otherwise)
74 std::string ip_address;
75 // Remembered passphrase.
76 std::string passphrase;
77 // true if the network requires a passphrase.
78 bool need_passphrase;
79 // true if the network is currently remembered.
80 bool remembered;
81 // true if the network is auto connect (meaningful for Wifi only).
82 bool auto_connect;
83 };
84
85 explicit NetworkMenuModel(NetworkMenu* owner);
86 virtual ~NetworkMenuModel();
87
88 // Connect or reconnect to the network at |index|.
89 // If remember >= 0, set the favorite state of the network.
90 void ConnectToNetworkAt(int index,
91 const std::string& passphrase,
92 const std::string& ssid,
93 int remember) const;
94
95 // Called by NetworkMenu::RunMenu to initialize list of menu items.
96 virtual void InitMenuItems(bool is_browser_mode,
97 bool should_open_button_options) = 0;
98
99 // PopulateMenu() clears and reinstalls the menu items defined in this
100 // instance by calling PopulateMenuItem() on each one. Subclasses override
101 // PopulateMenuItem(), transform command_id into the correct range for
102 // the menu, and call the base class PopulateMenuItem().
103 virtual void PopulateMenu(views::MenuItemView* menu);
104 virtual void PopulateMenuItem(views::MenuItemView* menu,
105 int index,
106 int command_id);
107
108 // Menu item field accessors.
109 int GetItemCount() const;
110 ui::MenuModel::ItemType GetTypeAt(int index) const;
111 string16 GetLabelAt(int index) const;
112 const gfx::Font* GetLabelFontAt(int index) const;
113 bool IsItemCheckedAt(int index) const;
114 bool GetIconAt(int index, SkBitmap* icon);
115 bool IsEnabledAt(int index) const;
116 NetworkMenuModel* GetSubmenuModelAt(int index) const;
117 void ActivatedAt(int index);
118
119 protected:
120 enum MenuItemFlags {
121 FLAG_NONE = 0,
122 FLAG_DISABLED = 1 << 0,
123 FLAG_TOGGLE_ETHERNET = 1 << 1,
124 FLAG_TOGGLE_WIFI = 1 << 2,
125 FLAG_TOGGLE_CELLULAR = 1 << 3,
126 FLAG_TOGGLE_OFFLINE = 1 << 4,
127 FLAG_ASSOCIATED = 1 << 5,
128 FLAG_ETHERNET = 1 << 6,
129 FLAG_WIFI = 1 << 7,
130 FLAG_CELLULAR = 1 << 8,
131 FLAG_OPTIONS = 1 << 9,
132 FLAG_ADD_WIFI = 1 << 10,
133 FLAG_ADD_CELLULAR = 1 << 11,
134 FLAG_VPN = 1 << 12,
135 FLAG_ADD_VPN = 1 << 13,
136 FLAG_DISCONNECT_VPN = 1 << 14,
137 FLAG_VIEW_ACCOUNT = 1 << 15,
138 };
139
140 struct MenuItem {
141 MenuItem()
142 : type(ui::MenuModel::TYPE_SEPARATOR),
143 sub_menu_model(NULL),
144 flags(0) {}
145 MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon,
146 const std::string& service_path, int flags)
147 : type(type),
148 label(label),
149 icon(icon),
150 service_path(service_path),
151 sub_menu_model(NULL),
152 flags(flags) {}
153 MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon,
154 NetworkMenuModel* sub_menu_model, int flags)
155 : type(type),
156 label(label),
157 icon(icon),
158 sub_menu_model(sub_menu_model),
159 flags(flags) {}
160
161 ui::MenuModel::ItemType type;
162 string16 label;
163 SkBitmap icon;
164 std::string service_path;
165 NetworkMenuModel* sub_menu_model; // Weak.
166 int flags;
167 };
168 typedef std::vector<MenuItem> MenuItemVector;
169
170 // Our menu items.
171 MenuItemVector menu_items_;
172
173 NetworkMenu* owner_; // Weak pointer to NetworkMenu that owns this MenuModel.
174
175 // Top up URL of the current carrier on empty string if there's none.
176 std::string top_up_url_;
177
178 // Carrier ID which top up URL is initialized for.
179 // Used to update top up URL only when cellular carrier has changed.
180 std::string carrier_id_;
181
182 private:
183 // Show a NetworkConfigView modal dialog instance.
184 void ShowNetworkConfigView(NetworkConfigView* view) const;
185
186 void ActivateCellular(const CellularNetwork* cellular) const;
187 void ShowOther(ConnectionType type) const;
188 void ShowOtherCellular() const;
189
190 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel);
191 };
192
193
49 class MoreMenuModel : public NetworkMenuModel { 194 class MoreMenuModel : public NetworkMenuModel {
50 public: 195 public:
51 explicit MoreMenuModel(NetworkMenu* owner); 196 explicit MoreMenuModel(NetworkMenu* owner);
52 virtual ~MoreMenuModel() {} 197 virtual ~MoreMenuModel() {}
53 198
54 // NetworkMenuModel implementation. 199 // NetworkMenuModel implementation.
55 virtual void InitMenuItems(bool is_browser_mode, 200 virtual void InitMenuItems(bool is_browser_mode,
56 bool should_open_button_options); 201 bool should_open_button_options) OVERRIDE;
202 virtual void PopulateMenuItem(views::MenuItemView* menu,
203 int index,
204 int command_id) OVERRIDE;
57 205
58 private: 206 private:
59 friend class MainMenuModel; 207 friend class MainMenuModel;
60 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); 208 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel);
61 }; 209 };
62 210
63 class VPNMenuModel : public NetworkMenuModel { 211 class VPNMenuModel : public NetworkMenuModel {
64 public: 212 public:
65 explicit VPNMenuModel(NetworkMenu* owner); 213 explicit VPNMenuModel(NetworkMenu* owner);
66 virtual ~VPNMenuModel() {} 214 virtual ~VPNMenuModel() {}
67 215
68 // NetworkMenuModel implementation. 216 // NetworkMenuModel implementation.
69 virtual void InitMenuItems(bool is_browser_mode, 217 virtual void InitMenuItems(bool is_browser_mode,
70 bool should_open_button_options); 218 bool should_open_button_options) OVERRIDE;
219 virtual void PopulateMenuItem(views::MenuItemView* menu,
220 int index,
221 int command_id) OVERRIDE;
71 222
72 static SkBitmap IconForDisplay(const Network* network); 223 static SkBitmap IconForDisplay(const Network* network);
73 224
74 private: 225 private:
75 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); 226 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel);
76 }; 227 };
77 228
78 class MainMenuModel : public NetworkMenuModel { 229 class MainMenuModel : public NetworkMenuModel {
79 public: 230 public:
80 explicit MainMenuModel(NetworkMenu* owner); 231 explicit MainMenuModel(NetworkMenu* owner);
81 virtual ~MainMenuModel() {} 232 virtual ~MainMenuModel() {}
82 233
83 // NetworkMenuModel implementation. 234 // NetworkMenuModel implementation.
84 virtual void InitMenuItems(bool is_browser_mode, 235 virtual void InitMenuItems(bool is_browser_mode,
85 bool should_open_button_options); 236 bool should_open_button_options) OVERRIDE;
237 virtual void PopulateMenuItem(views::MenuItemView* menu,
238 int index,
239 int command_id) OVERRIDE;
240
241 // views::MenuDelegate implementation.
242 virtual const gfx::Font& GetLabelFont(int id) const OVERRIDE;
243 virtual bool IsItemChecked(int id) const OVERRIDE;
244 virtual bool IsCommandEnabled(int id) const OVERRIDE;
245 virtual void ExecuteCommand(int id) OVERRIDE;
86 246
87 private: 247 private:
88 scoped_ptr<NetworkMenuModel> vpn_menu_model_; 248 scoped_ptr<NetworkMenuModel> vpn_menu_model_;
89 scoped_ptr<MoreMenuModel> more_menu_model_; 249 scoped_ptr<MoreMenuModel> more_menu_model_;
90 250
91 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); 251 DISALLOW_COPY_AND_ASSIGN(MainMenuModel);
92 }; 252 };
93 253
94 //////////////////////////////////////////////////////////////////////////////// 254 ////////////////////////////////////////////////////////////////////////////////
95 // NetworkMenuModel::NetworkInfo 255 // NetworkMenuModel::NetworkInfo
96 256
97 NetworkMenuModel::NetworkInfo::NetworkInfo() 257 NetworkMenuModel::NetworkInfo::NetworkInfo()
98 : need_passphrase(false), remembered(true), auto_connect(true) { 258 : need_passphrase(false), remembered(true), auto_connect(true) {
99 } 259 }
100 260
101 NetworkMenuModel::NetworkInfo::~NetworkInfo() {} 261 NetworkMenuModel::NetworkInfo::~NetworkInfo() {}
102 262
103 //////////////////////////////////////////////////////////////////////////////// 263 ////////////////////////////////////////////////////////////////////////////////
104 // NetworkMenuModel, public methods: 264 // NetworkMenuModel, public methods:
105 265
106 NetworkMenuModel::NetworkMenuModel(NetworkMenu* owner) : owner_(owner) {} 266 NetworkMenuModel::NetworkMenuModel(NetworkMenu* owner) : owner_(owner) {}
107 267
108 NetworkMenuModel::~NetworkMenuModel() {} 268 NetworkMenuModel::~NetworkMenuModel() {}
109 269
110 bool NetworkMenuModel::ConnectToNetworkAt(int index, 270 void NetworkMenuModel::PopulateMenu(views::MenuItemView* menu) {
271 if (menu->HasSubmenu()) {
272 const int old_count = menu->GetSubmenu()->child_count();
273 for (int i = 0; i < old_count; ++i)
274 menu->RemoveMenuItemAt(0);
275 }
276
277 const int menu_items_count = GetItemCount();
278 for (int i = 0; i < menu_items_count; ++i)
279 PopulateMenuItem(menu, i, i);
280 }
281
282 void NetworkMenuModel::PopulateMenuItem(
283 views::MenuItemView* menu,
284 int index,
285 int command_id) {
286 DCHECK_GT(GetItemCount(), index);
287 switch (GetTypeAt(index)) {
288 case ui::MenuModel::TYPE_SEPARATOR:
289 menu->AppendSeparator();
290 break;
291 case ui::MenuModel::TYPE_COMMAND: {
292 SkBitmap icon;
stevenjb 2011/05/19 20:13:05 nit: 2 space indentation even with {} (see below)
rhashimoto 2011/05/20 00:36:41 Done.
293 if (GetIconAt(index, &icon)) {
294 menu->AppendMenuItemWithIcon(command_id,
295 UTF16ToWide(GetLabelAt(index)),
296 icon);
297 } else {
298 menu->AppendMenuItemWithLabel(command_id,
299 UTF16ToWide(GetLabelAt(index)));
300 }
301 break;
302 }
303 case ui::MenuModel::TYPE_SUBMENU: {
304 views::MenuItemView* submenu = NULL;
305 SkBitmap icon;
306 if (GetIconAt(index, &icon)) {
307 submenu = menu->AppendSubMenuWithIcon(command_id,
308 UTF16ToWide(GetLabelAt(index)),
309 icon);
310 } else {
311 submenu = menu->AppendSubMenu(command_id,
312 UTF16ToWide(GetLabelAt(index)));
313 }
314 GetSubmenuModelAt(index)->PopulateMenu(submenu);
315 break;
316 }
317 default:
318 NOTREACHED();
319 }
320 }
321
322 void NetworkMenuModel::ConnectToNetworkAt(int index,
111 const std::string& passphrase, 323 const std::string& passphrase,
112 const std::string& ssid, 324 const std::string& ssid,
113 int auto_connect) const { 325 int auto_connect) const {
114 int flags = menu_items_[index].flags; 326 int flags = menu_items_[index].flags;
115 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); 327 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
116 const std::string& service_path = menu_items_[index].service_path; 328 const std::string& service_path = menu_items_[index].service_path;
117 if (flags & FLAG_WIFI) { 329 if (flags & FLAG_WIFI) {
118 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); 330 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path);
119 if (wifi) { 331 if (wifi) {
120 // Connect or reconnect. 332 // Connect or reconnect.
121 if (auto_connect >= 0) 333 if (auto_connect >= 0)
122 wifi->SetAutoConnect(auto_connect ? true : false); 334 wifi->SetAutoConnect(auto_connect ? true : false);
123 if (wifi->connecting_or_connected()) { 335 if (wifi->connecting_or_connected()) {
124 // Show the config settings for the active network. 336 // Show the config settings for the active network.
125 owner_->ShowTabbedNetworkSettings(wifi); 337 owner_->ShowTabbedNetworkSettings(wifi);
126 return true; 338 } else if (wifi->IsPassphraseRequired()) {
127 }
128 if (wifi->IsPassphraseRequired()) {
129 // Show the connection UI if we require a passphrase. 339 // Show the connection UI if we require a passphrase.
130 ShowNetworkConfigView(new NetworkConfigView(wifi)); 340 ShowNetworkConfigView(new NetworkConfigView(wifi));
131 return true;
132 } else { 341 } else {
133 cros->ConnectToWifiNetwork(wifi); 342 cros->ConnectToWifiNetwork(wifi);
134 // Connection failures are responsible for updating the UI, including 343 // Connection failures are responsible for updating the UI, including
135 // reopening dialogs. 344 // reopening dialogs.
136 return true;
137 } 345 }
138 } else { 346 } else {
139 // If we are attempting to connect to a network that no longer exists, 347 // If we are attempting to connect to a network that no longer exists,
140 // display a notification. 348 // display a notification.
141 LOG(WARNING) << "Wi-fi network does not exist to connect to: " 349 LOG(WARNING) << "Wi-fi network does not exist to connect to: "
142 << service_path; 350 << service_path;
143 // TODO(stevenjb): Show notification. 351 // TODO(stevenjb): Show notification.
144 } 352 }
145 } else if (flags & FLAG_CELLULAR) { 353 } else if (flags & FLAG_CELLULAR) {
146 CellularNetwork* cellular = cros->FindCellularNetworkByPath( 354 CellularNetwork* cellular = cros->FindCellularNetworkByPath(
147 service_path); 355 service_path);
148 if (cellular) { 356 if (cellular) {
149 if ((cellular->activation_state() != ACTIVATION_STATE_ACTIVATED && 357 if ((cellular->activation_state() != ACTIVATION_STATE_ACTIVATED &&
150 cellular->activation_state() != ACTIVATION_STATE_UNKNOWN) || 358 cellular->activation_state() != ACTIVATION_STATE_UNKNOWN) ||
151 cellular->needs_new_plan()) { 359 cellular->needs_new_plan()) {
152 ActivateCellular(cellular); 360 ActivateCellular(cellular);
153 return true;
154 } else if (cellular->connecting_or_connected()) { 361 } else if (cellular->connecting_or_connected()) {
155 // Cellular network is connecting or connected, 362 // Cellular network is connecting or connected,
156 // so we show the config settings for the cellular network. 363 // so we show the config settings for the cellular network.
157 owner_->ShowTabbedNetworkSettings(cellular); 364 owner_->ShowTabbedNetworkSettings(cellular);
158 return true; 365 } else {
366 // Clicked on a disconnected cellular network, so connect to it.
367 cros->ConnectToCellularNetwork(cellular);
159 } 368 }
160 // Clicked on a disconnected cellular network, so connect to it.
161 cros->ConnectToCellularNetwork(cellular);
162 } else { 369 } else {
163 // If we are attempting to connect to a network that no longer exists, 370 // If we are attempting to connect to a network that no longer exists,
164 // display a notification. 371 // display a notification.
165 LOG(WARNING) << "Cellular network does not exist to connect to: " 372 LOG(WARNING) << "Cellular network does not exist to connect to: "
166 << service_path; 373 << service_path;
167 // TODO(stevenjb): Show notification. 374 // TODO(stevenjb): Show notification.
168 } 375 }
169 } else if (flags & FLAG_ADD_WIFI) { 376 } else if (flags & FLAG_ADD_WIFI) {
170 ShowOther(TYPE_WIFI); 377 ShowOther(TYPE_WIFI);
171 } else if (flags & FLAG_ADD_CELLULAR) { 378 } else if (flags & FLAG_ADD_CELLULAR) {
172 ShowOtherCellular(); 379 ShowOtherCellular();
173 } else if (flags & FLAG_ADD_VPN) { 380 } else if (flags & FLAG_ADD_VPN) {
174 ShowOther(TYPE_VPN); 381 ShowOther(TYPE_VPN);
175 } else if (flags & FLAG_VPN) { 382 } else if (flags & FLAG_VPN) {
176 VirtualNetwork* vpn = cros->FindVirtualNetworkByPath(service_path); 383 VirtualNetwork* vpn = cros->FindVirtualNetworkByPath(service_path);
177 if (vpn) { 384 if (vpn) {
178 // Connect or reconnect. 385 // Connect or reconnect.
179 if (vpn->connecting_or_connected()) { 386 if (vpn->connecting_or_connected()) {
180 // Show the config settings for the connected network. 387 // Show the config settings for the connected network.
181 if (cros->connected_network()) 388 if (cros->connected_network())
182 owner_->ShowTabbedNetworkSettings(cros->connected_network()); 389 owner_->ShowTabbedNetworkSettings(cros->connected_network());
183 return true; 390 } else if (vpn->NeedMoreInfoToConnect()) {
391 // Show the connection UI if info for a field is missing.
392 ShowNetworkConfigView(new NetworkConfigView(vpn));
393 } else {
394 cros->ConnectToVirtualNetwork(vpn);
395 // Connection failures are responsible for updating the UI, including
396 // reopening dialogs.
184 } 397 }
185 // Show the connection UI if info for a field is missing.
186 if (vpn->NeedMoreInfoToConnect()) {
187 ShowNetworkConfigView(new NetworkConfigView(vpn));
188 return true;
189 }
190 cros->ConnectToVirtualNetwork(vpn);
191 // Connection failures are responsible for updating the UI, including
192 // reopening dialogs.
193 return true;
194 } else { 398 } else {
195 // If we are attempting to connect to a network that no longer exists, 399 // If we are attempting to connect to a network that no longer exists,
196 // display a notification. 400 // display a notification.
197 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; 401 LOG(WARNING) << "VPN does not exist to connect to: " << service_path;
198 // TODO(stevenjb): Show notification. 402 // TODO(stevenjb): Show notification.
199 } 403 }
200 } 404 }
201 return true;
202 } 405 }
203 406
204 //////////////////////////////////////////////////////////////////////////////// 407 ////////////////////////////////////////////////////////////////////////////////
205 // NetworkMenuModel, ui::MenuModel implementation: 408 // NetworkMenuModel, ui::MenuModel implementation:
206 409
207 bool NetworkMenuModel::HasIcons() const {
208 return true;
209 }
210
211 int NetworkMenuModel::GetItemCount() const { 410 int NetworkMenuModel::GetItemCount() const {
212 return static_cast<int>(menu_items_.size()); 411 return static_cast<int>(menu_items_.size());
213 } 412 }
214 413
215 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const { 414 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const {
216 return menu_items_[index].type; 415 return menu_items_[index].type;
217 } 416 }
218 417
219 int NetworkMenuModel::GetCommandIdAt(int index) const {
220 return index;
221 }
222
223 string16 NetworkMenuModel::GetLabelAt(int index) const { 418 string16 NetworkMenuModel::GetLabelAt(int index) const {
224 return menu_items_[index].label; 419 return menu_items_[index].label;
225 } 420 }
226 421
227 bool NetworkMenuModel::IsItemDynamicAt(int index) const {
228 return true;
229 }
230
231 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const { 422 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const {
232 return (menu_items_[index].flags & FLAG_ASSOCIATED) ? 423 return (menu_items_[index].flags & FLAG_ASSOCIATED) ?
233 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : 424 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) :
234 NULL; 425 NULL;
235 } 426 }
236 427
237 bool NetworkMenuModel::GetAcceleratorAt(
238 int index, ui::Accelerator* accelerator) const {
239 return false;
240 }
241
242 bool NetworkMenuModel::IsItemCheckedAt(int index) const { 428 bool NetworkMenuModel::IsItemCheckedAt(int index) const {
243 // All ui::MenuModel::TYPE_CHECK menu items are checked. 429 // All ui::MenuModel::TYPE_CHECK menu items are checked.
244 return true; 430 return true;
245 } 431 }
246 432
247 int NetworkMenuModel::GetGroupIdAt(int index) const {
248 return 0;
249 }
250
251 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) { 433 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) {
252 if (!menu_items_[index].icon.empty()) { 434 if (!menu_items_[index].icon.empty()) {
253 *icon = menu_items_[index].icon; 435 *icon = menu_items_[index].icon;
254 return true; 436 return true;
255 } 437 }
256 return false; 438 return false;
257 } 439 }
258 440
259 ui::ButtonMenuItemModel* NetworkMenuModel::GetButtonMenuItemAt(
260 int index) const {
261 return NULL;
262 }
263
264 bool NetworkMenuModel::IsEnabledAt(int index) const { 441 bool NetworkMenuModel::IsEnabledAt(int index) const {
265 return !(menu_items_[index].flags & FLAG_DISABLED); 442 return !(menu_items_[index].flags & FLAG_DISABLED);
266 } 443 }
267 444
268 ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const { 445 NetworkMenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const {
269 return menu_items_[index].sub_menu_model; 446 return menu_items_[index].sub_menu_model;
270 } 447 }
271 448
272 void NetworkMenuModel::HighlightChangedTo(int index) {}
273
274 void NetworkMenuModel::ActivatedAt(int index) { 449 void NetworkMenuModel::ActivatedAt(int index) {
275 // When we are refreshing the menu, ignore menu item activation. 450 // When we are refreshing the menu, ignore menu item activation.
276 if (owner_->refreshing_menu_) 451 if (owner_->refreshing_menu_)
277 return; 452 return;
278 453
279 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); 454 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
280 int flags = menu_items_[index].flags; 455 int flags = menu_items_[index].flags;
281 if (flags & FLAG_OPTIONS) { 456 if (flags & FLAG_OPTIONS) {
282 owner_->OpenButtonOptions(); 457 owner_->OpenButtonOptions();
283 } else if (flags & FLAG_TOGGLE_ETHERNET) { 458 } else if (flags & FLAG_TOGGLE_ETHERNET) {
(...skipping 28 matching lines...) Expand all
312 const VirtualNetwork* active_vpn = cros->virtual_network(); 487 const VirtualNetwork* active_vpn = cros->virtual_network();
313 if (active_vpn) 488 if (active_vpn)
314 cros->DisconnectFromNetwork(active_vpn); 489 cros->DisconnectFromNetwork(active_vpn);
315 } else if (flags & FLAG_VIEW_ACCOUNT) { 490 } else if (flags & FLAG_VIEW_ACCOUNT) {
316 Browser* browser = BrowserList::GetLastActive(); 491 Browser* browser = BrowserList::GetLastActive();
317 if (browser) 492 if (browser)
318 browser->ShowSingletonTab(GURL(top_up_url_)); 493 browser->ShowSingletonTab(GURL(top_up_url_));
319 } 494 }
320 } 495 }
321 496
322 void NetworkMenuModel::MenuWillShow() {}
323
324 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {}
325
326 //////////////////////////////////////////////////////////////////////////////// 497 ////////////////////////////////////////////////////////////////////////////////
327 // NetworkMenuModel, private methods: 498 // NetworkMenuModel, private methods:
328 499
329 // TODO(stevenjb): deprecate this once we've committed to tabbed settings 500 // TODO(stevenjb): deprecate this once we've committed to tabbed settings
330 // and the embedded menu UI (and fully deprecated NetworkConfigView). 501 // and the embedded menu UI (and fully deprecated NetworkConfigView).
331 // Meanwhile, if MenuUI::IsEnabled() is true, always show the settings UI, 502 // Meanwhile, if MenuUI::IsEnabled() is true, always show the settings UI,
332 // otherwise show NetworkConfigView only to get passwords when not connected. 503 // otherwise show NetworkConfigView only to get passwords when not connected.
333 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { 504 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const {
334 views::Window* window = browser::CreateViewsWindow( 505 views::Window* window = browser::CreateViewsWindow(
335 owner_->GetNativeWindow(), gfx::Rect(), view); 506 owner_->GetNativeWindow(), gfx::Rect(), view);
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 } else { 854 } else {
684 if (!more_menu_model_->menu_items_.empty()) { 855 if (!more_menu_model_->menu_items_.empty()) {
685 menu_items_.push_back(MenuItem( 856 menu_items_.push_back(MenuItem(
686 ui::MenuModel::TYPE_SUBMENU, 857 ui::MenuModel::TYPE_SUBMENU,
687 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), 858 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE),
688 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); 859 SkBitmap(), more_menu_model_.get(), FLAG_NONE));
689 } 860 }
690 } 861 }
691 } 862 }
692 863
864 void MainMenuModel::PopulateMenuItem(
865 views::MenuItemView* menu,
866 int index,
867 int command_id) {
868 NetworkMenuModel::PopulateMenuItem(menu, index,
869 command_id + kMainIndexOffset);
870 }
871
872 // views::MenuDelegate implementation.
873
874 const gfx::Font& MainMenuModel::GetLabelFont(int id) const {
875 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset);
876 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset);
877 const gfx::Font* font = NULL;
878 if (id >= kMoreIndexOffset)
879 font = more_menu_model_->GetLabelFontAt(id - kMoreIndexOffset);
880 else if (id >= kVPNIndexOffset)
881 font = vpn_menu_model_->GetLabelFontAt(id - kVPNIndexOffset);
882 else if (id >= kMainIndexOffset)
883 font = GetLabelFontAt(id - kMainIndexOffset);
884
885 return font ? *font : views::MenuDelegate::GetLabelFont(id);
886 }
887
888
889 bool MainMenuModel::IsItemChecked(int id) const {
890 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset);
891 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset);
892 if (id >= kMoreIndexOffset)
893 return more_menu_model_->IsItemCheckedAt(id - kMoreIndexOffset);
894 else if (id >= kVPNIndexOffset)
895 return vpn_menu_model_->IsItemCheckedAt(id - kVPNIndexOffset);
896 else if (id >= kMainIndexOffset)
897 return IsItemCheckedAt(id - kMainIndexOffset);
898
899 return views::MenuDelegate::IsItemChecked(id);
900 }
901
902 bool MainMenuModel::IsCommandEnabled(int id) const {
903 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset);
904 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset);
905 if (id >= kMoreIndexOffset)
906 return more_menu_model_->IsEnabledAt(id - kMoreIndexOffset);
907 else if (id >= kVPNIndexOffset)
908 return vpn_menu_model_->IsEnabledAt(id - kVPNIndexOffset);
909 else if (id >= kMainIndexOffset)
910 return IsEnabledAt(id - kMainIndexOffset);
911
912 return views::MenuDelegate::IsCommandEnabled(id);
913 }
914
915 void MainMenuModel::ExecuteCommand(int id) {
916 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset);
917 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset);
918 if (id >= kMoreIndexOffset)
919 more_menu_model_->ActivatedAt(id - kMoreIndexOffset);
920 else if (id >= kVPNIndexOffset)
921 vpn_menu_model_->ActivatedAt(id - kVPNIndexOffset);
922 else if (id >= kMainIndexOffset)
923 ActivatedAt(id - kMainIndexOffset);
924 }
925
693 //////////////////////////////////////////////////////////////////////////////// 926 ////////////////////////////////////////////////////////////////////////////////
694 // VPNMenuModel 927 // VPNMenuModel
695 928
696 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) 929 VPNMenuModel::VPNMenuModel(NetworkMenu* owner)
697 : NetworkMenuModel(owner) { 930 : NetworkMenuModel(owner) {
698 } 931 }
699 932
700 void VPNMenuModel::InitMenuItems(bool is_browser_mode, 933 void VPNMenuModel::InitMenuItems(bool is_browser_mode,
701 bool should_open_button_options) { 934 bool should_open_button_options) {
702 // This gets called on initialization, so any changes should be reflected 935 // This gets called on initialization, so any changes should be reflected
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), 988 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN),
756 SkBitmap(), std::string(), FLAG_ADD_VPN)); 989 SkBitmap(), std::string(), FLAG_ADD_VPN));
757 if (active_vpn) { 990 if (active_vpn) {
758 menu_items_.push_back(MenuItem( 991 menu_items_.push_back(MenuItem(
759 ui::MenuModel::TYPE_COMMAND, 992 ui::MenuModel::TYPE_COMMAND,
760 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), 993 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN),
761 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); 994 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN));
762 } 995 }
763 } 996 }
764 997
998 void VPNMenuModel::PopulateMenuItem(
999 views::MenuItemView* menu,
1000 int index,
1001 int command_id) {
1002 NetworkMenuModel::PopulateMenuItem(menu, index,
1003 command_id + kVPNIndexOffset);
1004 }
1005
765 // static 1006 // static
766 SkBitmap VPNMenuModel::IconForDisplay(const Network* network) { 1007 SkBitmap VPNMenuModel::IconForDisplay(const Network* network) {
767 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 1008 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
768 const SkBitmap* icon = NULL; 1009 const SkBitmap* icon = NULL;
769 const SkBitmap* bottom_right_badge = NULL; 1010 const SkBitmap* bottom_right_badge = NULL;
770 const SkBitmap* top_left_badge = NULL; 1011 const SkBitmap* top_left_badge = NULL;
771 // We know for sure |network| is the active network, so no more checking 1012 // We know for sure |network| is the active network, so no more checking
772 // is needed by BadgeForPrivateNetworkStatus, hence pass in NULL. 1013 // is needed by BadgeForPrivateNetworkStatus, hence pass in NULL.
773 const SkBitmap* bottom_left_badge = 1014 const SkBitmap* bottom_left_badge =
774 NetworkMenu::BadgeForPrivateNetworkStatus(NULL); 1015 NetworkMenu::BadgeForPrivateNetworkStatus(NULL);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 } 1105 }
865 } 1106 }
866 1107
867 menu_items_ = link_items; 1108 menu_items_ = link_items;
868 if (!menu_items_.empty() && address_items.size() > 1) 1109 if (!menu_items_.empty() && address_items.size() > 1)
869 menu_items_.push_back(MenuItem()); // Separator 1110 menu_items_.push_back(MenuItem()); // Separator
870 menu_items_.insert(menu_items_.end(), 1111 menu_items_.insert(menu_items_.end(),
871 address_items.begin(), address_items.end()); 1112 address_items.begin(), address_items.end());
872 } 1113 }
873 1114
1115 void MoreMenuModel::PopulateMenuItem(
1116 views::MenuItemView* menu,
1117 int index,
1118 int command_id) {
1119 NetworkMenuModel::PopulateMenuItem(menu, index,
1120 command_id + kMoreIndexOffset);
1121 }
1122
874 //////////////////////////////////////////////////////////////////////////////// 1123 ////////////////////////////////////////////////////////////////////////////////
875 // NetworkMenu 1124 // NetworkMenu
876 1125
877 // static 1126 // static
878 const int NetworkMenu::kNumBarsImages = 4; 1127 const int NetworkMenu::kNumBarsImages = 4;
879 1128
880 // NOTE: Use an array rather than just calculating a resource number to avoid 1129 // NOTE: Use an array rather than just calculating a resource number to avoid
881 // creating implicit ordering dependencies on the resource values. 1130 // creating implicit ordering dependencies on the resource values.
882 // static 1131 // static
883 const int NetworkMenu::kBarsImages[kNumBarsImages] = { 1132 const int NetworkMenu::kBarsImages[kNumBarsImages] = {
(...skipping 29 matching lines...) Expand all
913 1162
914 // static 1163 // static
915 const int NetworkMenu::kNumAnimatingImages = 10; 1164 const int NetworkMenu::kNumAnimatingImages = 10;
916 1165
917 // static 1166 // static
918 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; 1167 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages];
919 1168
920 // static 1169 // static
921 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; 1170 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages];
922 1171
923 NetworkMenu::NetworkMenu() : min_width_(-1) { 1172 NetworkMenu::NetworkMenu() : min_width_(kDefaultMinimumWidth) {
924 main_menu_model_.reset(new MainMenuModel(this)); 1173 main_menu_model_.reset(new MainMenuModel(this));
925 network_menu_.reset(new views::Menu2(main_menu_model_.get())); 1174 network_menu_.reset(new views::MenuItemView(main_menu_model_.get()));
1175 network_menu_->set_has_icons(true);
926 } 1176 }
927 1177
928 NetworkMenu::~NetworkMenu() { 1178 NetworkMenu::~NetworkMenu() {
929 } 1179 }
930 1180
931 void NetworkMenu::SetFirstLevelMenuWidth(int width) { 1181 void NetworkMenu::SetFirstLevelMenuWidth(int width) {
932 min_width_ = width; 1182 min_width_ = width;
933 // This actually has no effect since menu is rebuilt before showing.
934 network_menu_->SetMinimumWidth(width);
935 } 1183 }
936 1184
937 void NetworkMenu::CancelMenu() { 1185 void NetworkMenu::CancelMenu() {
938 network_menu_->CancelMenu(); 1186 network_menu_->Cancel();
939 } 1187 }
940 1188
941 void NetworkMenu::UpdateMenu() { 1189 void NetworkMenu::UpdateMenu() {
1190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1191
942 refreshing_menu_ = true; 1192 refreshing_menu_ = true;
943 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); 1193 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions());
944 network_menu_->Rebuild(); 1194 main_menu_model_->PopulateMenu(network_menu_.get());
1195 network_menu_->ChildrenChanged();
945 refreshing_menu_ = false; 1196 refreshing_menu_ = false;
946 } 1197 }
947 1198
948 // static 1199 // static
949 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, 1200 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi,
950 bool black) { 1201 bool black) {
951 DCHECK(wifi); 1202 DCHECK(wifi);
952 if (wifi->strength() == 0) { 1203 if (wifi->strength() == 0) {
953 return ResourceBundle::GetSharedInstance().GetBitmapNamed( 1204 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
954 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : 1205 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK :
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 chrome::kInternetOptionsSubPage, 1400 chrome::kInternetOptionsSubPage,
1150 EscapeUrlEncodedData(network->service_path()).c_str(), 1401 EscapeUrlEncodedData(network->service_path()).c_str(),
1151 network->type()); 1402 network->type());
1152 browser->ShowOptionsTab(page); 1403 browser->ShowOptionsTab(page);
1153 } 1404 }
1154 1405
1155 //////////////////////////////////////////////////////////////////////////////// 1406 ////////////////////////////////////////////////////////////////////////////////
1156 // NetworkMenu, views::ViewMenuDelegate implementation: 1407 // NetworkMenu, views::ViewMenuDelegate implementation:
1157 1408
1158 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { 1409 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) {
1159 refreshing_menu_ = true;
1160 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); 1410 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
1161 cros->RequestNetworkScan(); 1411 cros->RequestNetworkScan();
1162 1412
1163 // Build initial menu items. They will be updated when UpdateMenu is 1413 UpdateMenu();
1164 // called from NetworkChanged.
1165 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions());
1166 network_menu_->Rebuild();
1167 1414
1168 // Restore menu width, if it was set up. 1415 // TODO(rhashimoto): Remove this workaround when WebUI provides a
1169 // NOTE: width isn't checked for correctness here since all width-related 1416 // top-level widget on the ChromeOS login screen that is a window.
1170 // logic implemented inside |network_menu_|. 1417 // The current BackgroundView class for the ChromeOS login screen
1171 if (min_width_ != -1) 1418 // creates a owning Widget that has a native GtkWindow but is not a
1172 network_menu_->SetMinimumWidth(min_width_); 1419 // Window. This makes it impossible to get the NativeWindow via
1173 refreshing_menu_ = false; 1420 // the views API. This workaround casts the top-level NativeWidget
1174 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); 1421 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt().
1422 gfx::NativeWindow window = GTK_WINDOW(source->GetWidget()->GetNativeView());
1423
1424 gfx::Point screen_loc;
1425 views::View::ConvertPointToScreen(source, &screen_loc);
1426 gfx::Rect bounds(screen_loc, source->size());
1427 network_menu_->GetSubmenu()->set_minimum_preferred_width(min_width_);
1428 network_menu_->RunMenuAt(window, GetMenuButton(), bounds,
1429 views::MenuItemView::TOPRIGHT, true);
1175 } 1430 }
1176 1431
1177 } // namespace chromeos 1432 } // namespace chromeos
1178
1179
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/status/network_menu.h ('k') | chrome/browser/chromeos/status/network_menu_button.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698