OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/webui/network_menu_ui.h" | |
6 | |
7 #include "base/string_number_conversions.h" | |
8 #include "base/string_util.h" | |
9 #include "base/values.h" | |
10 #include "chrome/app/chrome_command_ids.h" | |
11 #include "chrome/browser/chromeos/status/network_menu.h" | |
12 #include "chrome/browser/chromeos/views/native_menu_webui.h" | |
13 #include "chrome/browser/chromeos/views/webui_menu_widget.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/webui/theme_source.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "content/browser/browser_thread.h" | |
18 #include "content/browser/tab_contents/tab_contents.h" | |
19 #include "googleurl/src/gurl.h" | |
20 #include "grit/browser_resources.h" | |
21 #include "grit/generated_resources.h" | |
22 #include "ui/base/l10n/l10n_util.h" | |
23 #include "views/controls/menu/menu_2.h" | |
24 | |
25 namespace { | |
26 | |
27 class NetworkMenuSourceDelegate : public chromeos::MenuSourceDelegate { | |
28 public: | |
29 virtual void AddLocalizedStrings(DictionaryValue* localized_strings) const { | |
30 DCHECK(localized_strings); | |
31 | |
32 localized_strings->SetString("reconnect", l10n_util::GetStringUTF16( | |
33 IDS_NETWORK_RECONNECT_TITLE)); | |
34 localized_strings->SetString("auto_connect_this_network", | |
35 l10n_util::GetStringUTF16( | |
36 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_AUTO_CONNECT)); | |
37 localized_strings->SetString("ssid_prompt", | |
38 l10n_util::GetStringUTF16(IDS_NETWORK_SSID_HINT)); | |
39 localized_strings->SetString("pass_prompt", | |
40 l10n_util::GetStringUTF16(IDS_NETWORK_PASSWORD_HINT)); | |
41 } | |
42 }; | |
43 | |
44 //////////////////////////////////////////////////////////////////////////////// | |
45 // | |
46 // MenuHandler | |
47 // | |
48 //////////////////////////////////////////////////////////////////////////////// | |
49 | |
50 // The handler for Javascript messages related to the "system" view. | |
51 class NetworkMenuHandler : public chromeos::MenuHandlerBase, | |
52 public base::SupportsWeakPtr<NetworkMenuHandler> { | |
53 public: | |
54 NetworkMenuHandler(); | |
55 virtual ~NetworkMenuHandler(); | |
56 | |
57 // WebUIMessageHandler implementation. | |
58 virtual void RegisterMessages(); | |
59 | |
60 private: | |
61 void HandleAction(const ListValue* values); | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(NetworkMenuHandler); | |
64 }; | |
65 | |
66 void NetworkMenuHandler::RegisterMessages() { | |
67 web_ui_->RegisterMessageCallback( | |
68 "action", | |
69 NewCallback(this, | |
70 &NetworkMenuHandler::HandleAction)); | |
71 } | |
72 | |
73 void NetworkMenuHandler::HandleAction(const ListValue* values) { | |
74 ui::MenuModel* model = GetMenuModel(); | |
75 if (model) { | |
76 chromeos::NetworkMenuUI* network_menu_ui = | |
77 static_cast<chromeos::NetworkMenuUI*>(web_ui_); | |
78 bool close_menu = network_menu_ui->ModelAction(model, values); | |
79 if (close_menu) { | |
80 chromeos::WebUIMenuWidget* widget | |
81 = chromeos::WebUIMenuWidget::FindWebUIMenuWidget( | |
82 web_ui_->tab_contents()->GetNativeView()); | |
83 if (widget) { | |
84 chromeos::NativeMenuWebUI* webui_menu = widget->webui_menu(); | |
85 if (webui_menu) | |
86 webui_menu->Hide(); | |
87 } | |
88 } | |
89 } | |
90 } | |
91 | |
92 NetworkMenuHandler::NetworkMenuHandler() { | |
93 } | |
94 | |
95 NetworkMenuHandler::~NetworkMenuHandler() { | |
96 } | |
97 | |
98 } // namespace | |
99 | |
100 namespace chromeos { | |
101 | |
102 //////////////////////////////////////////////////////////////////////////////// | |
103 // | |
104 // NetworkMenuUI | |
105 // | |
106 //////////////////////////////////////////////////////////////////////////////// | |
107 | |
108 NetworkMenuUI::NetworkMenuUI(TabContents* contents) | |
109 : chromeos::MenuUI( | |
110 contents, | |
111 ALLOW_THIS_IN_INITIALIZER_LIST( | |
112 CreateMenuUIHTMLSource(new NetworkMenuSourceDelegate(), | |
113 chrome::kChromeUINetworkMenu, | |
114 "NetworkMenu", | |
115 IDR_NETWORK_MENU_JS, | |
116 IDR_NETWORK_MENU_CSS))) { | |
117 NetworkMenuHandler* handler = new NetworkMenuHandler(); | |
118 AddMessageHandler((handler)->Attach(this)); | |
119 | |
120 // Set up chrome://theme/ source. | |
121 ThemeSource* theme = new ThemeSource(contents->profile()); | |
122 contents->profile()->GetChromeURLDataManager()->AddDataSource(theme); | |
123 } | |
124 | |
125 bool NetworkMenuUI::ModelAction(const ui::MenuModel* model, | |
126 const ListValue* values) { | |
127 const NetworkMenu* network_menu = static_cast<const NetworkMenu*>(model); | |
128 std::string action; | |
129 bool success = values->GetString(0, &action); | |
130 bool close_menu = true; | |
131 if (!success) { | |
132 LOG(WARNING) << "ModelAction called with no arguments from: " | |
133 << chrome::kChromeUINetworkMenu; | |
134 return close_menu; | |
135 } | |
136 int index; | |
137 std::string index_str; | |
138 success = values->GetString(1, &index_str); | |
139 success = success && base::StringToInt(index_str, &index); | |
140 if (!success) { | |
141 LOG(WARNING) << "ModelAction called with no index from: " | |
142 << chrome::kChromeUINetworkMenu; | |
143 return close_menu; | |
144 } | |
145 std::string passphrase; | |
146 values->GetString(2, &passphrase); // Optional | |
147 std::string ssid; | |
148 values->GetString(3, &ssid); // Optional | |
149 int auto_connect = -1; // -1 indicates default action (auto connect) | |
150 std::string auto_connect_str; | |
151 if (values->GetString(4, &auto_connect_str)) // Optional | |
152 base::StringToInt(auto_connect_str, &auto_connect); | |
153 | |
154 if (action == "connect" || action == "reconnect") { | |
155 close_menu = network_menu->ConnectToNetworkAt(index, passphrase, ssid, | |
156 auto_connect); | |
157 } else { | |
158 LOG(WARNING) << "Unrecognized action: " << action | |
159 << " from: " << chrome::kChromeUINetworkMenu; | |
160 } | |
161 return close_menu; | |
162 } | |
163 | |
164 DictionaryValue* NetworkMenuUI::CreateMenuItem(const ui::MenuModel* model, | |
165 int index, | |
166 const char* type, | |
167 int* max_icon_width, | |
168 bool* has_accel) const { | |
169 // Create a MenuUI menu item, then append network specific values. | |
170 DictionaryValue* item = MenuUI::CreateMenuItem(model, | |
171 index, | |
172 type, | |
173 max_icon_width, | |
174 has_accel); | |
175 // Network menu specific values. | |
176 const NetworkMenu* network_menu = static_cast<const NetworkMenu*>(model); | |
177 NetworkMenu::NetworkInfo info; | |
178 bool found = network_menu->GetNetworkAt(index, &info); | |
179 | |
180 item->SetBoolean("visible", found); | |
181 item->SetString("network_type", info.network_type); | |
182 item->SetString("status", info.status); | |
183 item->SetString("message", info.message); | |
184 item->SetString("ip_address", info.ip_address); | |
185 item->SetString("passphrase", info.passphrase); | |
186 item->SetBoolean("need_passphrase", info.need_passphrase); | |
187 item->SetBoolean("remembered", info.remembered); | |
188 return item; | |
189 } | |
190 | |
191 views::Menu2* NetworkMenuUI::CreateMenu2(ui::MenuModel* model) { | |
192 views::Menu2* menu = new views::Menu2(model); | |
193 NativeMenuWebUI::SetMenuURL( | |
194 menu, GURL(StringPrintf("chrome://%s", chrome::kChromeUINetworkMenu))); | |
195 return menu; | |
196 } | |
197 | |
198 } // namespace chromeos | |
OLD | NEW |