- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / login / network_dropdown.cc
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/ui/webui/chromeos/login/network_dropdown.h"
6
7 #include <string>
8
9 #include "ash/system/chromeos/network/network_icon.h"
10 #include "ash/system/chromeos/network/network_icon_animation.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/login/login_display_host.h"
14 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
15 #include "chromeos/network/network_state_handler.h"
16 #include "content/public/browser/web_ui.h"
17 #include "ui/base/models/menu_model.h"
18 #include "ui/base/webui/web_ui_util.h"
19 #include "ui/gfx/font.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/image/image_skia.h"
22
23 namespace {
24
25 // Timeout between consecutive requests to network library for network
26 // scan.
27 const int kNetworkScanIntervalSecs = 60;
28
29 }  // namespace
30
31 namespace chromeos {
32
33 // WebUI specific implementation of the NetworkMenu class.
34 class NetworkMenuWebUI : public NetworkMenu {
35  public:
36   NetworkMenuWebUI(NetworkMenu::Delegate* delegate, content::WebUI* web_ui);
37
38   // NetworkMenu override:
39   virtual void UpdateMenu() OVERRIDE;
40
41   // Called when item with command |id| is chosen.
42   void OnItemChosen(int id);
43
44  private:
45   // Converts menu model into the ListValue, ready for passing to WebUI.
46   base::ListValue* ConvertMenuModel(ui::MenuModel* model);
47
48   // WebUI where network menu is located.
49   content::WebUI* web_ui_;
50
51   DISALLOW_COPY_AND_ASSIGN(NetworkMenuWebUI);
52 };
53
54 // NetworkMenuWebUI ------------------------------------------------------------
55
56 NetworkMenuWebUI::NetworkMenuWebUI(NetworkMenu::Delegate* delegate,
57                                    content::WebUI* web_ui)
58     : NetworkMenu(delegate),
59       web_ui_(web_ui) {
60 }
61
62 void NetworkMenuWebUI::UpdateMenu() {
63   NetworkMenu::UpdateMenu();
64   if (web_ui_) {
65     scoped_ptr<base::ListValue> list(ConvertMenuModel(GetMenuModel()));
66     web_ui_->CallJavascriptFunction("cr.ui.DropDown.updateNetworks", *list);
67   }
68 }
69
70 void NetworkMenuWebUI::OnItemChosen(int id) {
71   int index;
72   ui::MenuModel* model = GetMenuModel();
73    if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
74      return;
75   model->ActivatedAt(index);
76 }
77
78 base::ListValue* NetworkMenuWebUI::ConvertMenuModel(ui::MenuModel* model) {
79   base::ListValue* list = new base::ListValue();
80   for (int i = 0; i < model->GetItemCount(); ++i) {
81     ui::MenuModel::ItemType type = model->GetTypeAt(i);
82     int id;
83     if (type == ui::MenuModel::TYPE_SEPARATOR)
84       id = -2;
85     else
86       id = model->GetCommandIdAt(i);
87     base::DictionaryValue* item = new base::DictionaryValue();
88     item->SetInteger("id", id);
89     item->SetString("label", model->GetLabelAt(i));
90     gfx::Image icon;
91     if (model->GetIconAt(i, &icon)) {
92       SkBitmap icon_bitmap = icon.ToImageSkia()->GetRepresentation(
93           ui::GetImageScale(
94               web_ui_->GetDeviceScaleFactor())).sk_bitmap();
95       item->SetString("icon", webui::GetBitmapDataUrl(icon_bitmap));
96     }
97     if (id >= 0) {
98       item->SetBoolean("enabled", model->IsEnabledAt(i));
99       const gfx::Font* font = model->GetLabelFontAt(i);
100       if (font) {
101         item->SetBoolean("bold", font->GetStyle() == gfx::Font::BOLD);
102       }
103     }
104     if (type == ui::MenuModel::TYPE_SUBMENU)
105       item->Set("sub", ConvertMenuModel(model->GetSubmenuModelAt(i)));
106     list->Append(item);
107   }
108   return list;
109 }
110
111 // NetworkDropdown -------------------------------------------------------------
112
113 NetworkDropdown::NetworkDropdown(Actor* actor,
114                                  content::WebUI* web_ui,
115                                  bool oobe)
116     : actor_(actor),
117       web_ui_(web_ui),
118       oobe_(oobe) {
119   DCHECK(actor_);
120   network_menu_.reset(new NetworkMenuWebUI(this, web_ui));
121   DCHECK(NetworkHandler::IsInitialized());
122   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
123   handler->RequestScan();
124   handler->AddObserver(this, FROM_HERE);
125   Refresh();
126   network_scan_timer_.Start(
127       FROM_HERE,
128       base::TimeDelta::FromSeconds(kNetworkScanIntervalSecs),
129       this, &NetworkDropdown::RequestNetworkScan);
130 }
131
132 NetworkDropdown::~NetworkDropdown() {
133   ash::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
134   if (NetworkHandler::IsInitialized()) {
135     NetworkHandler::Get()->network_state_handler()->RemoveObserver(
136         this, FROM_HERE);
137   }
138 }
139
140 void NetworkDropdown::OnItemChosen(int id) {
141   network_menu_->OnItemChosen(id);
142 }
143
144 gfx::NativeWindow NetworkDropdown::GetNativeWindow() const {
145   return LoginDisplayHostImpl::default_host()->GetNativeWindow();
146 }
147
148 void NetworkDropdown::OpenButtonOptions() {
149   LoginDisplayHostImpl::default_host()->OpenProxySettings();
150 }
151
152 bool NetworkDropdown::ShouldOpenButtonOptions() const {
153   return !oobe_;
154 }
155
156 void NetworkDropdown::OnConnectToNetworkRequested(
157     const std::string& service_path) {
158   actor_->OnConnectToNetworkRequested(service_path);
159 }
160
161 void NetworkDropdown::DefaultNetworkChanged(const NetworkState* network) {
162   Refresh();
163 }
164
165 void NetworkDropdown::NetworkConnectionStateChanged(
166     const NetworkState* network) {
167   Refresh();
168 }
169
170 void NetworkDropdown::NetworkListChanged() {
171   Refresh();
172 }
173
174 void NetworkDropdown::NetworkIconChanged() {
175   SetNetworkIconAndText();
176 }
177
178 void NetworkDropdown::Refresh() {
179   SetNetworkIconAndText();
180   network_menu_->UpdateMenu();
181 }
182
183 void NetworkDropdown::SetNetworkIconAndText() {
184   string16 text;
185   gfx::ImageSkia icon_image;
186   bool animating = false;
187   ash::network_icon::GetDefaultNetworkImageAndLabel(
188       ash::network_icon::ICON_TYPE_LIST, &icon_image, &text, &animating);
189   if (animating) {
190     ash::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
191   } else {
192     ash::network_icon::NetworkIconAnimation::GetInstance()->
193         RemoveObserver(this);
194   }
195   SkBitmap icon_bitmap = icon_image.GetRepresentation(
196       ui::GetImageScale(web_ui_->GetDeviceScaleFactor())).sk_bitmap();
197   std::string icon_str;
198   if (!icon_image.isNull())
199     icon_str = webui::GetBitmapDataUrl(icon_bitmap);
200   base::StringValue title(text);
201   base::StringValue icon(icon_str);
202   web_ui_->CallJavascriptFunction("cr.ui.DropDown.updateNetworkTitle",
203                                   title, icon);
204 }
205
206 void NetworkDropdown::RequestNetworkScan() {
207   NetworkHandler::Get()->network_state_handler()->RequestScan();
208   Refresh();
209 }
210
211 }  // namespace chromeos