Home | History | Annotate | Download | only in chromeos
      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/ui/webui/options/chromeos/internet_options_handler.h"
      6 
      7 #include <ctype.h>
      8 
      9 #include <map>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/base64.h"
     14 #include "base/basictypes.h"
     15 #include "base/callback.h"
     16 #include "base/i18n/time_formatting.h"
     17 #include "base/string16.h"
     18 #include "base/string_number_conversions.h"
     19 #include "base/time.h"
     20 #include "base/utf_string_conversions.h"
     21 #include "base/values.h"
     22 #include "chrome/browser/browser_process.h"
     23 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h"
     24 #include "chrome/browser/chromeos/cros/cros_library.h"
     25 #include "chrome/browser/chromeos/customization_document.h"
     26 #include "chrome/browser/chromeos/login/user_manager.h"
     27 #include "chrome/browser/chromeos/sim_dialog_delegate.h"
     28 #include "chrome/browser/chromeos/status/network_menu.h"
     29 #include "chrome/browser/chromeos/user_cros_settings_provider.h"
     30 #include "chrome/browser/prefs/pref_service.h"
     31 #include "chrome/browser/ui/browser.h"
     32 #include "chrome/browser/ui/browser_list.h"
     33 #include "chrome/browser/ui/browser_window.h"
     34 #include "chrome/browser/ui/views/window.h"
     35 #include "chrome/browser/ui/webui/web_ui_util.h"
     36 #include "chrome/common/pref_names.h"
     37 #include "chrome/common/time_format.h"
     38 #include "content/common/notification_service.h"
     39 #include "content/common/notification_type.h"
     40 #include "grit/chromium_strings.h"
     41 #include "grit/generated_resources.h"
     42 #include "grit/locale_settings.h"
     43 #include "grit/theme_resources.h"
     44 #include "third_party/skia/include/core/SkBitmap.h"
     45 #include "ui/base/l10n/l10n_util.h"
     46 #include "ui/base/resource/resource_bundle.h"
     47 #include "views/window/window.h"
     48 
     49 static const char kOtherNetworksFakePath[] = "?";
     50 
     51 InternetOptionsHandler::InternetOptionsHandler()
     52     : chromeos::CrosOptionsPageUIHandler(
     53           new chromeos::UserCrosSettingsProvider),
     54       use_settings_ui_(false) {
     55   registrar_.Add(this, NotificationType::REQUIRE_PIN_SETTING_CHANGE_ENDED,
     56       NotificationService::AllSources());
     57   registrar_.Add(this, NotificationType::ENTER_PIN_ENDED,
     58       NotificationService::AllSources());
     59   chromeos::NetworkLibrary* netlib =
     60       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
     61   netlib->AddNetworkManagerObserver(this);
     62   netlib->AddCellularDataPlanObserver(this);
     63   MonitorNetworks(netlib);
     64 }
     65 
     66 InternetOptionsHandler::~InternetOptionsHandler() {
     67   chromeos::NetworkLibrary *netlib =
     68       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
     69   netlib->RemoveNetworkManagerObserver(this);
     70   netlib->RemoveCellularDataPlanObserver(this);
     71   netlib->RemoveObserverForAllNetworks(this);
     72 }
     73 
     74 void InternetOptionsHandler::GetLocalizedValues(
     75     DictionaryValue* localized_strings) {
     76   DCHECK(localized_strings);
     77 
     78   RegisterTitle(localized_strings, "internetPage",
     79                 IDS_OPTIONS_INTERNET_TAB_LABEL);
     80 
     81   localized_strings->SetString("wired_title",
     82       l10n_util::GetStringUTF16(
     83           IDS_OPTIONS_SETTINGS_SECTION_TITLE_WIRED_NETWORK));
     84   localized_strings->SetString("wireless_title",
     85       l10n_util::GetStringUTF16(
     86           IDS_OPTIONS_SETTINGS_SECTION_TITLE_WIRELESS_NETWORK));
     87   localized_strings->SetString("remembered_title",
     88       l10n_util::GetStringUTF16(
     89           IDS_OPTIONS_SETTINGS_SECTION_TITLE_REMEMBERED_NETWORK));
     90 
     91   localized_strings->SetString("connect_button",
     92       l10n_util::GetStringUTF16(
     93           IDS_OPTIONS_SETTINGS_CONNECT));
     94   localized_strings->SetString("disconnect_button",
     95       l10n_util::GetStringUTF16(
     96           IDS_OPTIONS_SETTINGS_DISCONNECT));
     97   localized_strings->SetString("options_button",
     98       l10n_util::GetStringUTF16(
     99           IDS_OPTIONS_SETTINGS_OPTIONS));
    100   localized_strings->SetString("forget_button",
    101       l10n_util::GetStringUTF16(
    102           IDS_OPTIONS_SETTINGS_FORGET));
    103   localized_strings->SetString("activate_button",
    104       l10n_util::GetStringUTF16(
    105           IDS_OPTIONS_SETTINGS_ACTIVATE));
    106   localized_strings->SetString("buyplan_button",
    107       l10n_util::GetStringUTF16(
    108           IDS_OPTIONS_SETTINGS_BUY_PLAN));
    109 
    110   localized_strings->SetString("wifiNetworkTabLabel",
    111       l10n_util::GetStringUTF16(
    112           IDS_OPTIONS_SETTINGS_INTERNET_TAB_WIFI));
    113   localized_strings->SetString("cellularPlanTabLabel",
    114       l10n_util::GetStringUTF16(
    115           IDS_OPTIONS_SETTINGS_INTERNET_TAB_PLAN));
    116   localized_strings->SetString("cellularConnTabLabel",
    117       l10n_util::GetStringUTF16(
    118           IDS_OPTIONS_SETTINGS_INTERNET_TAB_CONNECTION));
    119   localized_strings->SetString("cellularDeviceTabLabel",
    120       l10n_util::GetStringUTF16(
    121           IDS_OPTIONS_SETTINGS_INTERNET_TAB_DEVICE));
    122   localized_strings->SetString("networkTabLabel",
    123       l10n_util::GetStringUTF16(
    124           IDS_OPTIONS_SETTINGS_INTERNET_TAB_NETWORK));
    125   localized_strings->SetString("securityTabLabel",
    126         l10n_util::GetStringUTF16(
    127             IDS_OPTIONS_SETTINGS_INTERNET_TAB_SECURITY));
    128 
    129   localized_strings->SetString("connectionState",
    130       l10n_util::GetStringUTF16(
    131           IDS_OPTIONS_SETTINGS_INTERNET_CONNECTION_STATE));
    132   localized_strings->SetString("inetAddress",
    133       l10n_util::GetStringUTF16(
    134           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_ADDRESS));
    135   localized_strings->SetString("inetSubnetAddress",
    136       l10n_util::GetStringUTF16(
    137           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SUBNETMASK));
    138   localized_strings->SetString("inetGateway",
    139       l10n_util::GetStringUTF16(
    140           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_GATEWAY));
    141   localized_strings->SetString("inetDns",
    142       l10n_util::GetStringUTF16(
    143           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_DNSSERVER));
    144   localized_strings->SetString("hardwareAddress",
    145       l10n_util::GetStringUTF16(
    146           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_HARDWARE_ADDRESS));
    147 
    148   localized_strings->SetString("accessLockedMsg",
    149       l10n_util::GetStringUTF16(
    150           IDS_STATUSBAR_NETWORK_LOCKED));
    151   localized_strings->SetString("inetSsid",
    152       l10n_util::GetStringUTF16(
    153           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_NETWORK_ID));
    154   localized_strings->SetString("inetPassProtected",
    155       l10n_util::GetStringUTF16(
    156           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_NET_PROTECTED));
    157   localized_strings->SetString("inetAutoConnectNetwork",
    158       l10n_util::GetStringUTF16(
    159           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_AUTO_CONNECT));
    160   localized_strings->SetString("inetLogin",
    161       l10n_util::GetStringUTF16(
    162           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_LOGIN));
    163   localized_strings->SetString("inetShowPass",
    164       l10n_util::GetStringUTF16(
    165           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHOWPASSWORD));
    166   localized_strings->SetString("inetSecurityNone",
    167       l10n_util::GetStringFUTF16(
    168           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_SELECT,
    169           l10n_util::GetStringUTF16(
    170               IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_NONE)));
    171   localized_strings->SetString("inetSecurityWEP",
    172       l10n_util::GetStringFUTF16(
    173           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_SELECT,
    174           l10n_util::GetStringUTF16(
    175               IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_WEP)));
    176   localized_strings->SetString("inetSecurityWPA",
    177       l10n_util::GetStringFUTF16(
    178           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_SELECT,
    179           l10n_util::GetStringUTF16(
    180               IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_WPA)));
    181   localized_strings->SetString("inetSecurityRSN",
    182       l10n_util::GetStringFUTF16(
    183           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_SELECT,
    184           l10n_util::GetStringUTF16(
    185               IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SECURITY_RSN)));
    186   localized_strings->SetString("inetPassPrompt",
    187       l10n_util::GetStringUTF16(
    188           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSWORD));
    189   localized_strings->SetString("inetSsidPrompt",
    190       l10n_util::GetStringUTF16(
    191           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SSID));
    192   localized_strings->SetString("inetStatus",
    193       l10n_util::GetStringUTF16(
    194           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_STATUS_TITLE));
    195   localized_strings->SetString("inetConnect",
    196       l10n_util::GetStringUTF16(
    197           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CONNECT_TITLE));
    198 
    199   localized_strings->SetString("serviceName",
    200       l10n_util::GetStringUTF16(
    201           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_SERVICE_NAME));
    202   localized_strings->SetString("networkTechnology",
    203       l10n_util::GetStringUTF16(
    204           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_NETWORK_TECHNOLOGY));
    205   localized_strings->SetString("operatorName",
    206       l10n_util::GetStringUTF16(
    207           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_OPERATOR));
    208   localized_strings->SetString("operatorCode",
    209       l10n_util::GetStringUTF16(
    210           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_OPERATOR_CODE));
    211   localized_strings->SetString("activationState",
    212       l10n_util::GetStringUTF16(
    213           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_ACTIVATION_STATE));
    214   localized_strings->SetString("roamingState",
    215       l10n_util::GetStringUTF16(
    216           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_ROAMING_STATE));
    217   localized_strings->SetString("restrictedPool",
    218       l10n_util::GetStringUTF16(
    219           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_RESTRICTED_POOL));
    220   localized_strings->SetString("errorState",
    221       l10n_util::GetStringUTF16(
    222           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_ERROR_STATE));
    223   localized_strings->SetString("manufacturer",
    224       l10n_util::GetStringUTF16(
    225           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_MANUFACTURER));
    226   localized_strings->SetString("modelId",
    227       l10n_util::GetStringUTF16(
    228           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_MODEL_ID));
    229   localized_strings->SetString("firmwareRevision",
    230       l10n_util::GetStringUTF16(
    231           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_FIRMWARE_REVISION));
    232   localized_strings->SetString("hardwareRevision",
    233       l10n_util::GetStringUTF16(
    234           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_HARDWARE_REVISION));
    235   localized_strings->SetString("prlVersion",
    236       l10n_util::GetStringUTF16(
    237           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_PRL_VERSION));
    238   localized_strings->SetString("cellularApnLabel",
    239       l10n_util::GetStringUTF16(
    240           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_APN));
    241   localized_strings->SetString("cellularApnUsername",
    242       l10n_util::GetStringUTF16(
    243           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_APN_USERNAME));
    244   localized_strings->SetString("cellularApnPassword",
    245       l10n_util::GetStringUTF16(
    246           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_APN_PASSWORD));
    247   localized_strings->SetString("cellularApnClear",
    248       l10n_util::GetStringUTF16(
    249           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_APN_CLEAR));
    250   localized_strings->SetString("cellularApnSet",
    251       l10n_util::GetStringUTF16(
    252           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_APN_SET));
    253 
    254   localized_strings->SetString("accessSecurityTabLink",
    255       l10n_util::GetStringUTF16(
    256           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_ACCESS_SECURITY_TAB));
    257   localized_strings->SetString("lockSimCard",
    258       l10n_util::GetStringUTF16(
    259           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_LOCK_SIM_CARD));
    260   localized_strings->SetString("changePinButton",
    261       l10n_util::GetStringUTF16(
    262           IDS_OPTIONS_SETTINGS_INTERNET_CELLULAR_CHANGE_PIN_BUTTON));
    263 
    264   localized_strings->SetString("planName",
    265       l10n_util::GetStringUTF16(
    266           IDS_OPTIONS_SETTINGS_INTERNET_CELL_PLAN_NAME));
    267   localized_strings->SetString("planLoading",
    268       l10n_util::GetStringUTF16(
    269           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_LOADING_PLAN));
    270   localized_strings->SetString("noPlansFound",
    271       l10n_util::GetStringUTF16(
    272           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_NO_PLANS_FOUND));
    273   localized_strings->SetString("purchaseMore",
    274       l10n_util::GetStringUTF16(
    275           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PURCHASE_MORE));
    276   localized_strings->SetString("dataRemaining",
    277       l10n_util::GetStringUTF16(
    278           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_DATA_REMAINING));
    279   localized_strings->SetString("planExpires",
    280       l10n_util::GetStringUTF16(
    281           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_EXPIRES));
    282   localized_strings->SetString("showPlanNotifications",
    283       l10n_util::GetStringUTF16(
    284           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHOW_MOBILE_NOTIFICATION));
    285   localized_strings->SetString("autoconnectCellular",
    286       l10n_util::GetStringUTF16(
    287           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_AUTO_CONNECT));
    288   localized_strings->SetString("customerSupport",
    289       l10n_util::GetStringUTF16(
    290           IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CUSTOMER_SUPPORT));
    291 
    292   localized_strings->SetString("enableWifi",
    293       l10n_util::GetStringFUTF16(
    294           IDS_STATUSBAR_NETWORK_DEVICE_ENABLE,
    295           l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_WIFI)));
    296   localized_strings->SetString("disableWifi",
    297       l10n_util::GetStringFUTF16(
    298           IDS_STATUSBAR_NETWORK_DEVICE_DISABLE,
    299           l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_WIFI)));
    300   localized_strings->SetString("enableCellular",
    301       l10n_util::GetStringFUTF16(
    302           IDS_STATUSBAR_NETWORK_DEVICE_ENABLE,
    303           l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_CELLULAR)));
    304   localized_strings->SetString("disableCellular",
    305       l10n_util::GetStringFUTF16(
    306           IDS_STATUSBAR_NETWORK_DEVICE_DISABLE,
    307           l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_CELLULAR)));
    308   localized_strings->SetString("enableDataRoaming",
    309       l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_ENABLE_DATA_ROAMING));
    310   localized_strings->SetString("generalNetworkingTitle",
    311       l10n_util::GetStringUTF16(
    312           IDS_OPTIONS_SETTINGS_INTERNET_CONTROL_TITLE));
    313   localized_strings->SetString("detailsInternetDismiss",
    314       l10n_util::GetStringUTF16(IDS_CLOSE));
    315   localized_strings->SetString("ownerOnly", l10n_util::GetStringUTF16(
    316       IDS_OPTIONS_ACCOUNTS_OWNER_ONLY));
    317   localized_strings->SetString("ownerUserId", UTF8ToUTF16(
    318       chromeos::UserCrosSettingsProvider::cached_owner()));
    319 
    320   chromeos::NetworkLibrary* cros =
    321       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    322   FillNetworkInfo(localized_strings, cros);
    323 
    324   localized_strings->SetBoolean("networkUseSettingsUI", use_settings_ui_);
    325 }
    326 
    327 void InternetOptionsHandler::Initialize() {
    328   chromeos::NetworkLibrary* cros =
    329       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    330   cros->RequestNetworkScan();
    331 }
    332 
    333 void InternetOptionsHandler::RegisterMessages() {
    334   // Setup handlers specific to this panel.
    335   DCHECK(web_ui_);
    336   web_ui_->RegisterMessageCallback("buttonClickCallback",
    337       NewCallback(this, &InternetOptionsHandler::ButtonClickCallback));
    338   web_ui_->RegisterMessageCallback("refreshCellularPlan",
    339       NewCallback(this, &InternetOptionsHandler::RefreshCellularPlanCallback));
    340   web_ui_->RegisterMessageCallback("loginToNetwork",
    341       NewCallback(this, &InternetOptionsHandler::LoginCallback));
    342   web_ui_->RegisterMessageCallback("loginToCertNetwork",
    343       NewCallback(this, &InternetOptionsHandler::LoginCertCallback));
    344   web_ui_->RegisterMessageCallback("loginToOtherNetwork",
    345       NewCallback(this, &InternetOptionsHandler::LoginToOtherCallback));
    346   web_ui_->RegisterMessageCallback("setDetails",
    347       NewCallback(this, &InternetOptionsHandler::SetDetailsCallback));
    348   web_ui_->RegisterMessageCallback("enableWifi",
    349       NewCallback(this, &InternetOptionsHandler::EnableWifiCallback));
    350   web_ui_->RegisterMessageCallback("disableWifi",
    351       NewCallback(this, &InternetOptionsHandler::DisableWifiCallback));
    352   web_ui_->RegisterMessageCallback("enableCellular",
    353       NewCallback(this, &InternetOptionsHandler::EnableCellularCallback));
    354   web_ui_->RegisterMessageCallback("disableCellular",
    355       NewCallback(this, &InternetOptionsHandler::DisableCellularCallback));
    356   web_ui_->RegisterMessageCallback("buyDataPlan",
    357       NewCallback(this, &InternetOptionsHandler::BuyDataPlanCallback));
    358   web_ui_->RegisterMessageCallback("showMorePlanInfo",
    359       NewCallback(this, &InternetOptionsHandler::BuyDataPlanCallback));
    360   web_ui_->RegisterMessageCallback("setApn",
    361         NewCallback(this, &InternetOptionsHandler::SetApnCallback));
    362   web_ui_->RegisterMessageCallback("setSimCardLock",
    363         NewCallback(this, &InternetOptionsHandler::SetSimCardLockCallback));
    364   web_ui_->RegisterMessageCallback("changePin",
    365         NewCallback(this, &InternetOptionsHandler::ChangePinCallback));
    366 }
    367 
    368 void InternetOptionsHandler::EnableWifiCallback(const ListValue* args) {
    369   chromeos::NetworkLibrary* cros =
    370       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    371   cros->EnableWifiNetworkDevice(true);
    372 }
    373 
    374 void InternetOptionsHandler::DisableWifiCallback(const ListValue* args) {
    375   chromeos::NetworkLibrary* cros =
    376       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    377   cros->EnableWifiNetworkDevice(false);
    378 }
    379 
    380 void InternetOptionsHandler::EnableCellularCallback(const ListValue* args) {
    381   chromeos::NetworkLibrary* cros =
    382       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    383   const chromeos::NetworkDevice* cellular = cros->FindCellularDevice();
    384   if (!cellular) {
    385     LOG(ERROR) << "Didn't find cellular device, it should have been available.";
    386     cros->EnableCellularNetworkDevice(true);
    387   } else if (cellular->sim_lock_state() == chromeos::SIM_UNLOCKED ||
    388              cellular->sim_lock_state() == chromeos::SIM_UNKNOWN) {
    389       cros->EnableCellularNetworkDevice(true);
    390   } else {
    391     chromeos::SimDialogDelegate::ShowDialog(GetNativeWindow(),
    392         chromeos::SimDialogDelegate::SIM_DIALOG_UNLOCK);
    393   }
    394 }
    395 
    396 void InternetOptionsHandler::DisableCellularCallback(const ListValue* args) {
    397   chromeos::NetworkLibrary* cros =
    398       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    399   cros->EnableCellularNetworkDevice(false);
    400 }
    401 
    402 void InternetOptionsHandler::BuyDataPlanCallback(const ListValue* args) {
    403   if (!web_ui_)
    404     return;
    405   Browser* browser = BrowserList::FindBrowserWithFeature(
    406       web_ui_->GetProfile(), Browser::FEATURE_TABSTRIP);
    407   if (browser)
    408     browser->OpenMobilePlanTabAndActivate();
    409 }
    410 
    411 void InternetOptionsHandler::SetApnCallback(const ListValue* args) {
    412   std::string service_path;
    413   std::string apn;
    414   std::string username;
    415   std::string password;
    416   if (args->GetSize() != 4 ||
    417       !args->GetString(0, &service_path) ||
    418       !args->GetString(1, &apn) ||
    419       !args->GetString(2, &username) ||
    420       !args->GetString(3, &password)) {
    421     NOTREACHED();
    422     return;
    423   }
    424 
    425   chromeos::NetworkLibrary* cros =
    426       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    427   chromeos::CellularNetwork* network =
    428         cros->FindCellularNetworkByPath(service_path);
    429   if (network) {
    430     network->SetApn(chromeos::CellularNetwork::Apn(
    431         apn, network->apn().network_id, username, password));
    432   }
    433 }
    434 
    435 void InternetOptionsHandler::SetSimCardLockCallback(const ListValue* args) {
    436   bool require_pin_new_value;
    437   if (!args->GetBoolean(0, &require_pin_new_value)) {
    438     NOTREACHED();
    439     return;
    440   }
    441   // 1. Bring up SIM unlock dialog, pass new RequirePin setting in URL.
    442   // 2. Dialog will ask for current PIN in any case.
    443   // 3. If card is locked it will first call PIN unlock operation
    444   // 4. Then it will call Set RequirePin, passing the same PIN.
    445   // 5. We'll get notified by REQUIRE_PIN_SETTING_CHANGE_ENDED notification.
    446   chromeos::SimDialogDelegate::SimDialogMode mode;
    447   if (require_pin_new_value)
    448     mode = chromeos::SimDialogDelegate::SIM_DIALOG_SET_LOCK_ON;
    449   else
    450     mode = chromeos::SimDialogDelegate::SIM_DIALOG_SET_LOCK_OFF;
    451   chromeos::SimDialogDelegate::ShowDialog(GetNativeWindow(), mode);
    452 }
    453 
    454 void InternetOptionsHandler::ChangePinCallback(const ListValue* args) {
    455   chromeos::SimDialogDelegate::ShowDialog(GetNativeWindow(),
    456       chromeos::SimDialogDelegate::SIM_DIALOG_CHANGE_PIN);
    457 }
    458 
    459 void InternetOptionsHandler::RefreshNetworkData(
    460     chromeos::NetworkLibrary* cros) {
    461   DictionaryValue dictionary;
    462   FillNetworkInfo(&dictionary, cros);
    463   web_ui_->CallJavascriptFunction(
    464       "options.InternetOptions.refreshNetworkData", dictionary);
    465 }
    466 
    467 void InternetOptionsHandler::OnNetworkManagerChanged(
    468     chromeos::NetworkLibrary* cros) {
    469   if (!web_ui_)
    470     return;
    471   MonitorNetworks(cros);
    472   RefreshNetworkData(cros);
    473 }
    474 
    475 void InternetOptionsHandler::OnNetworkChanged(
    476     chromeos::NetworkLibrary* cros,
    477     const chromeos::Network* network) {
    478   if (web_ui_)
    479     RefreshNetworkData(cros);
    480 }
    481 
    482 // Monitor wireless networks for changes. It is only necessary
    483 // to set up individual observers for the cellular networks
    484 // (if any) and for the connected Wi-Fi network (if any). The
    485 // only change we are interested in for Wi-Fi networks is signal
    486 // strength. For non-connected Wi-Fi networks, all information is
    487 // reported via scan results, which trigger network manager
    488 // updates. Only the connected Wi-Fi network has changes reported
    489 // via service property updates.
    490 void InternetOptionsHandler::MonitorNetworks(chromeos::NetworkLibrary* cros) {
    491   cros->RemoveObserverForAllNetworks(this);
    492   const chromeos::WifiNetwork* wifi_network = cros->wifi_network();
    493   if (wifi_network != NULL)
    494     cros->AddNetworkObserver(wifi_network->service_path(), this);
    495   // Always monitor the cellular networks, if any, so that changes
    496   // in network technology, roaming status, and signal strength
    497   // will be shown.
    498   const chromeos::CellularNetworkVector& cell_networks =
    499       cros->cellular_networks();
    500   for (size_t i = 0; i < cell_networks.size(); ++i) {
    501     chromeos::CellularNetwork* cell_network = cell_networks[i];
    502     cros->AddNetworkObserver(cell_network->service_path(), this);
    503   }
    504 }
    505 
    506 void InternetOptionsHandler::OnCellularDataPlanChanged(
    507     chromeos::NetworkLibrary* cros) {
    508   if (!web_ui_)
    509     return;
    510   const chromeos::CellularNetwork* cellular = cros->cellular_network();
    511   if (!cellular)
    512     return;
    513   const chromeos::CellularDataPlanVector* plans =
    514       cros->GetDataPlans(cellular->service_path());
    515   DictionaryValue connection_plans;
    516   ListValue* plan_list = new ListValue();
    517   if (plans) {
    518     for (chromeos::CellularDataPlanVector::const_iterator iter = plans->begin();
    519          iter != plans->end(); ++iter) {
    520       plan_list->Append(CellularDataPlanToDictionary(*iter));
    521     }
    522   }
    523   connection_plans.SetString("servicePath", cellular->service_path());
    524   connection_plans.SetBoolean("needsPlan", cellular->needs_new_plan());
    525   connection_plans.SetBoolean("activated",
    526       cellular->activation_state() == chromeos::ACTIVATION_STATE_ACTIVATED);
    527   connection_plans.Set("plans", plan_list);
    528   SetActivationButtonVisibility(cellular, &connection_plans);
    529   web_ui_->CallJavascriptFunction(
    530       "options.InternetOptions.updateCellularPlans", connection_plans);
    531 }
    532 
    533 
    534 void InternetOptionsHandler::Observe(NotificationType type,
    535                                      const NotificationSource& source,
    536                                      const NotificationDetails& details) {
    537   chromeos::CrosOptionsPageUIHandler::Observe(type, source, details);
    538   if (type == NotificationType::REQUIRE_PIN_SETTING_CHANGE_ENDED) {
    539     bool require_pin = *Details<bool>(details).ptr();
    540     DictionaryValue dictionary;
    541     dictionary.SetBoolean("requirePin", require_pin);
    542     web_ui_->CallJavascriptFunction(
    543         "options.InternetOptions.updateSecurityTab", dictionary);
    544   } else if (type == NotificationType::ENTER_PIN_ENDED) {
    545     // We make an assumption (which is valid for now) that the SIM
    546     // unlock dialog is put up only when the user is trying to enable
    547     // mobile data.
    548     bool cancelled = *Details<bool>(details).ptr();
    549     if (cancelled) {
    550       chromeos::NetworkLibrary* cros =
    551           chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    552       DictionaryValue dictionary;
    553       FillNetworkInfo(&dictionary, cros);
    554       web_ui_->CallJavascriptFunction(
    555           "options.InternetOptions.setupAttributes", dictionary);
    556     }
    557     // The case in which the correct PIN was entered and the SIM is
    558     // now unlocked is handled in NetworkMenuButton.
    559   }
    560 }
    561 
    562 DictionaryValue* InternetOptionsHandler::CellularDataPlanToDictionary(
    563     const chromeos::CellularDataPlan* plan) {
    564   DictionaryValue* plan_dict = new DictionaryValue();
    565   plan_dict->SetInteger("planType", plan->plan_type);
    566   plan_dict->SetString("name", plan->plan_name);
    567   plan_dict->SetString("planSummary", plan->GetPlanDesciption());
    568   plan_dict->SetString("dataRemaining", plan->GetDataRemainingDesciption());
    569   plan_dict->SetString("planExpires", plan->GetPlanExpiration());
    570   plan_dict->SetString("warning", plan->GetRemainingWarning());
    571   return plan_dict;
    572 }
    573 
    574 void InternetOptionsHandler::SetDetailsCallback(const ListValue* args) {
    575   std::string service_path;
    576   std::string auto_connect_str;
    577 
    578   if (args->GetSize() < 2 ||
    579       !args->GetString(0, &service_path) ||
    580       !args->GetString(1, &auto_connect_str)) {
    581     NOTREACHED();
    582     return;
    583   }
    584 
    585   if (!chromeos::UserManager::Get()->current_user_is_owner()) {
    586     LOG(WARNING) << "Non-owner tried to change a network.";
    587     return;
    588   }
    589 
    590   chromeos::NetworkLibrary* cros =
    591       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    592   chromeos::WifiNetwork* network = cros->FindWifiNetworkByPath(service_path);
    593   if (!network)
    594     return;
    595 
    596   bool auto_connect = auto_connect_str == "true";
    597   if (auto_connect != network->auto_connect())
    598     network->SetAutoConnect(auto_connect);
    599 }
    600 
    601 void InternetOptionsHandler::PopulateDictionaryDetails(
    602     const chromeos::Network* net, chromeos::NetworkLibrary* cros) {
    603   DCHECK(net);
    604   DictionaryValue dictionary;
    605   std::string hardware_address;
    606   chromeos::NetworkIPConfigVector ipconfigs = cros->GetIPConfigs(
    607       net->device_path(), &hardware_address,
    608       chromeos::NetworkLibrary::FORMAT_COLON_SEPARATED_HEX);
    609   if (!hardware_address.empty())
    610     dictionary.SetString("hardwareAddress", hardware_address);
    611   scoped_ptr<ListValue> ipconfig_list(new ListValue());
    612   for (chromeos::NetworkIPConfigVector::const_iterator it = ipconfigs.begin();
    613        it != ipconfigs.end(); ++it) {
    614     scoped_ptr<DictionaryValue> ipconfig_dict(new DictionaryValue());
    615     const chromeos::NetworkIPConfig& ipconfig = *it;
    616     ipconfig_dict->SetString("address", ipconfig.address);
    617     ipconfig_dict->SetString("subnetAddress", ipconfig.netmask);
    618     ipconfig_dict->SetString("gateway", ipconfig.gateway);
    619     ipconfig_dict->SetString("dns", ipconfig.name_servers);
    620     ipconfig_list->Append(ipconfig_dict.release());
    621   }
    622   dictionary.Set("ipconfigs", ipconfig_list.release());
    623 
    624   chromeos::ConnectionType type = net->type();
    625   dictionary.SetInteger("type", type);
    626   dictionary.SetString("servicePath", net->service_path());
    627   dictionary.SetBoolean("connecting", net->connecting());
    628   dictionary.SetBoolean("connected", net->connected());
    629   dictionary.SetString("connectionState", net->GetStateString());
    630 
    631   if (type == chromeos::TYPE_WIFI) {
    632     const chromeos::WifiNetwork* wifi =
    633         cros->FindWifiNetworkByPath(net->service_path());
    634     if (!wifi) {
    635       LOG(WARNING) << "Cannot find network " << net->service_path();
    636     } else {
    637       PopulateWifiDetails(wifi, &dictionary);
    638     }
    639   } else if (type == chromeos::TYPE_CELLULAR) {
    640     const chromeos::CellularNetwork* cellular =
    641         cros->FindCellularNetworkByPath(net->service_path());
    642     if (!cellular) {
    643       LOG(WARNING) << "Cannot find network " << net->service_path();
    644     } else {
    645       PopulateCellularDetails(cros, cellular, &dictionary);
    646     }
    647   } else if (type == chromeos::TYPE_VPN) {
    648     const chromeos::VirtualNetwork* vpn =
    649         cros->FindVirtualNetworkByPath(net->service_path());
    650     if (!vpn) {
    651       LOG(WARNING) << "Cannot find network " << net->service_path();
    652     } else {
    653       PopulateVPNDetails(vpn, &dictionary);
    654     }
    655   }
    656 
    657   web_ui_->CallJavascriptFunction(
    658       "options.InternetOptions.showDetailedInfo", dictionary);
    659 }
    660 
    661 void InternetOptionsHandler::PopulateWifiDetails(
    662     const chromeos::WifiNetwork* wifi,
    663     DictionaryValue* dictionary) {
    664   dictionary->SetString("ssid", wifi->name());
    665   dictionary->SetBoolean("autoConnect", wifi->auto_connect());
    666   dictionary->SetBoolean("encrypted", wifi->encrypted());
    667 }
    668 
    669 void InternetOptionsHandler::PopulateCellularDetails(
    670     chromeos::NetworkLibrary* cros,
    671     const chromeos::CellularNetwork* cellular,
    672     DictionaryValue* dictionary) {
    673   // Cellular network / connection settings.
    674   dictionary->SetString("serviceName", cellular->name());
    675   dictionary->SetString("networkTechnology",
    676                         cellular->GetNetworkTechnologyString());
    677   dictionary->SetString("operatorName", cellular->operator_name());
    678   dictionary->SetString("operatorCode", cellular->operator_code());
    679   dictionary->SetString("activationState",
    680                         cellular->GetActivationStateString());
    681   dictionary->SetString("roamingState",
    682                         cellular->GetRoamingStateString());
    683   dictionary->SetString("restrictedPool",
    684                         cellular->restricted_pool() ?
    685                         l10n_util::GetStringUTF8(
    686                             IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL) :
    687                         l10n_util::GetStringUTF8(
    688                             IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL));
    689   dictionary->SetString("errorState", cellular->GetErrorString());
    690   dictionary->SetString("supportUrl", cellular->payment_url());
    691   dictionary->SetBoolean("needsPlan", cellular->needs_new_plan());
    692   dictionary->SetBoolean("gsm", cellular->is_gsm());
    693   const chromeos::CellularNetwork::Apn& apn = cellular->apn();
    694   dictionary->SetString("apn", apn.apn);
    695   dictionary->SetString("apn_network_id", apn.network_id);
    696   dictionary->SetString("apn_username", apn.username);
    697   dictionary->SetString("apn_password", apn.password);
    698   const chromeos::CellularNetwork::Apn& last_good_apn =
    699       cellular->last_good_apn();
    700   dictionary->SetString("last_good_apn", last_good_apn.apn);
    701   dictionary->SetString("last_good_apn_network_id", last_good_apn.network_id);
    702   dictionary->SetString("last_good_apn_username", last_good_apn.username);
    703   dictionary->SetString("last_good_apn_password", last_good_apn.password);
    704 
    705   // Device settings.
    706   const chromeos::NetworkDevice* device =
    707       cros->FindNetworkDeviceByPath(cellular->device_path());
    708   if (device) {
    709     dictionary->SetString("manufacturer", device->manufacturer());
    710     dictionary->SetString("modelId", device->model_id());
    711     dictionary->SetString("firmwareRevision", device->firmware_revision());
    712     dictionary->SetString("hardwareRevision", device->hardware_revision());
    713     dictionary->SetString("prlVersion",
    714                           StringPrintf("%u", device->prl_version()));
    715     dictionary->SetString("meid", device->meid());
    716     dictionary->SetString("imei", device->imei());
    717     dictionary->SetString("mdn", device->mdn());
    718     dictionary->SetString("imsi", device->imsi());
    719     dictionary->SetString("esn", device->esn());
    720     dictionary->SetString("min", device->min());
    721     dictionary->SetBoolean("simCardLockEnabled",
    722         device->sim_pin_required() == chromeos::SIM_PIN_REQUIRED);
    723 
    724     chromeos::ServicesCustomizationDocument* customization =
    725         chromeos::ServicesCustomizationDocument::GetInstance();
    726     if (customization->IsReady()) {
    727       std::string carrier_id = cros->GetCellularHomeCarrierId();
    728       const chromeos::ServicesCustomizationDocument::CarrierDeal* deal =
    729           customization->GetCarrierDeal(carrier_id, false);
    730       if (deal && !deal->top_up_url.empty())
    731         dictionary->SetString("carrierUrl", deal->top_up_url);
    732     }
    733   }
    734 
    735   SetActivationButtonVisibility(cellular, dictionary);
    736 }
    737 
    738 void InternetOptionsHandler::PopulateVPNDetails(
    739     const chromeos::VirtualNetwork* vpn,
    740     DictionaryValue* dictionary) {
    741   // TODO(altimofeev): implement this.
    742 }
    743 
    744 void InternetOptionsHandler::SetActivationButtonVisibility(
    745     const chromeos::CellularNetwork* cellular,
    746     DictionaryValue* dictionary) {
    747   if (cellular->needs_new_plan()) {
    748     dictionary->SetBoolean("showBuyButton", true);
    749   } else if (cellular->activation_state() !=
    750                  chromeos::ACTIVATION_STATE_ACTIVATING &&
    751              cellular->activation_state() !=
    752                  chromeos::ACTIVATION_STATE_ACTIVATED) {
    753     dictionary->SetBoolean("showActivateButton", true);
    754   }
    755 }
    756 
    757 void InternetOptionsHandler::LoginCallback(const ListValue* args) {
    758   std::string service_path;
    759   std::string password;
    760 
    761   if (args->GetSize() != 2 ||
    762       !args->GetString(0, &service_path) ||
    763       !args->GetString(1, &password)) {
    764     NOTREACHED();
    765     return;
    766   }
    767 
    768   chromeos::NetworkLibrary* cros =
    769       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    770   cros->ConnectToWifiNetwork(service_path);
    771 }
    772 
    773 void InternetOptionsHandler::LoginCertCallback(const ListValue* args) {
    774   std::string service_path;
    775   std::string identity;
    776   std::string certpath;
    777   if (args->GetSize() < 3 ||
    778       !args->GetString(0, &service_path) ||
    779       !args->GetString(1, &certpath) ||
    780       !args->GetString(2, &identity)) {
    781     return;
    782   }
    783   chromeos::NetworkLibrary* cros =
    784       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    785   chromeos::WifiNetwork* network = cros->FindWifiNetworkByPath(service_path);
    786   if (network) {
    787     std::string passphrase;
    788     if (args->GetSize() == 4 && args->GetString(3, &passphrase))
    789       network->SetPassphrase(passphrase);
    790     cros->ConnectToWifiNetwork(network);
    791   }
    792 }
    793 
    794 void InternetOptionsHandler::LoginToOtherCallback(const ListValue* args) {
    795   std::string security;
    796   std::string ssid;
    797   std::string password;
    798 
    799   if (args->GetSize() != 3 ||
    800       !args->GetString(0, &security) ||
    801       !args->GetString(1, &ssid) ||
    802       !args->GetString(2, &password)) {
    803     NOTREACHED();
    804     return;
    805   }
    806 
    807   chromeos::ConnectionSecurity sec = chromeos::SECURITY_UNKNOWN;
    808   if (security == "none") {
    809     sec = chromeos::SECURITY_NONE;
    810   } else if (security == "wep") {
    811     sec = chromeos::SECURITY_WEP;
    812   } else if (security == "wpa") {
    813     sec = chromeos::SECURITY_WPA;
    814   } else if (security == "rsn") {
    815     sec = chromeos::SECURITY_RSN;
    816   }
    817 
    818   chromeos::NetworkLibrary* cros =
    819       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    820 
    821   cros->ConnectToWifiNetwork(sec, ssid, password, std::string(), std::string());
    822 }
    823 
    824 void InternetOptionsHandler::CreateModalPopup(views::WindowDelegate* view) {
    825   DCHECK(!use_settings_ui_);
    826   views::Window* window = browser::CreateViewsWindow(GetNativeWindow(),
    827                                                      gfx::Rect(),
    828                                                      view);
    829   window->SetIsAlwaysOnTop(true);
    830   window->Show();
    831 }
    832 
    833 gfx::NativeWindow InternetOptionsHandler::GetNativeWindow() const {
    834   // TODO(beng): This is an improper direct dependency on Browser. Route this
    835   // through some sort of delegate.
    836   Browser* browser = BrowserList::FindBrowserWithProfile(web_ui_->GetProfile());
    837   return browser->window()->GetNativeHandle();
    838 }
    839 
    840 void InternetOptionsHandler::ButtonClickCallback(const ListValue* args) {
    841   std::string str_type;
    842   std::string service_path;
    843   std::string command;
    844   if (args->GetSize() != 3 ||
    845       !args->GetString(0, &str_type) ||
    846       !args->GetString(1, &service_path) ||
    847       !args->GetString(2, &command)) {
    848     NOTREACHED();
    849     return;
    850   }
    851 
    852   int type = atoi(str_type.c_str());
    853   if (type == chromeos::TYPE_ETHERNET) {
    854     chromeos::NetworkLibrary* cros =
    855         chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    856     const chromeos::EthernetNetwork* ether = cros->ethernet_network();
    857     PopulateDictionaryDetails(ether, cros);
    858   } else if (type == chromeos::TYPE_WIFI) {
    859     HandleWifiButtonClick(service_path, command);
    860   } else if (type == chromeos::TYPE_CELLULAR) {
    861     HandleCellularButtonClick(service_path, command);
    862   } else if (type == chromeos::TYPE_VPN) {
    863     HandleVPNButtonClick(service_path, command);
    864   } else {
    865     NOTREACHED();
    866   }
    867 }
    868 
    869 void InternetOptionsHandler::HandleWifiButtonClick(
    870     const std::string& service_path,
    871     const std::string& command) {
    872   chromeos::NetworkLibrary* cros =
    873       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    874   chromeos::WifiNetwork* wifi = NULL;
    875   if (command == "forget") {
    876     if (!chromeos::UserManager::Get()->current_user_is_owner()) {
    877       LOG(WARNING) << "Non-owner tried to forget a network.";
    878       return;
    879     }
    880     cros->ForgetWifiNetwork(service_path);
    881   } else if (!use_settings_ui_ && service_path == kOtherNetworksFakePath) {
    882     // Other wifi networks.
    883     CreateModalPopup(new chromeos::NetworkConfigView(chromeos::TYPE_WIFI));
    884   } else if ((wifi = cros->FindWifiNetworkByPath(service_path))) {
    885     if (command == "connect") {
    886       // Connect to wifi here. Open password page if appropriate.
    887       if (wifi->IsPassphraseRequired()) {
    888         if (use_settings_ui_) {
    889           if (wifi->encryption() == chromeos::SECURITY_8021X) {
    890             PopulateDictionaryDetails(wifi, cros);
    891           } else {
    892             DictionaryValue dictionary;
    893             dictionary.SetString("servicePath", wifi->service_path());
    894             web_ui_->CallJavascriptFunction(
    895                 "options.InternetOptions.showPasswordEntry", dictionary);
    896           }
    897         } else {
    898           CreateModalPopup(new chromeos::NetworkConfigView(wifi));
    899         }
    900       } else {
    901         cros->ConnectToWifiNetwork(wifi);
    902       }
    903     } else if (command == "disconnect") {
    904       cros->DisconnectFromNetwork(wifi);
    905     } else if (command == "options") {
    906       PopulateDictionaryDetails(wifi, cros);
    907     }
    908   }
    909 }
    910 
    911 void InternetOptionsHandler::HandleCellularButtonClick(
    912     const std::string& service_path,
    913     const std::string& command) {
    914   chromeos::NetworkLibrary* cros =
    915       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    916   chromeos::CellularNetwork* cellular = NULL;
    917   if (service_path == kOtherNetworksFakePath) {
    918     chromeos::ChooseMobileNetworkDialog::ShowDialog(GetNativeWindow());
    919   } else if ((cellular = cros->FindCellularNetworkByPath(service_path))) {
    920     if (command == "connect") {
    921       cros->ConnectToCellularNetwork(cellular);
    922     } else if (command == "disconnect") {
    923       cros->DisconnectFromNetwork(cellular);
    924     } else if (command == "activate") {
    925       Browser* browser = BrowserList::GetLastActive();
    926       if (browser)
    927         browser->OpenMobilePlanTabAndActivate();
    928     } else if (command == "options") {
    929       PopulateDictionaryDetails(cellular, cros);
    930     }
    931   }
    932 }
    933 
    934 void InternetOptionsHandler::HandleVPNButtonClick(
    935     const std::string& service_path,
    936     const std::string& command) {
    937   chromeos::NetworkLibrary* cros =
    938       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    939   chromeos::VirtualNetwork* network = NULL;
    940   // TODO(altimofeev): verify if service_path in condition is correct.
    941   if (!use_settings_ui_ && service_path == kOtherNetworksFakePath) {
    942     // Other VPN networks.
    943     CreateModalPopup(new chromeos::NetworkConfigView(chromeos::TYPE_VPN));
    944   } else if ((network = cros->FindVirtualNetworkByPath(service_path))) {
    945     if (command == "connect") {
    946       // Connect to VPN here. Open password page if appropriate.
    947       if (network->NeedMoreInfoToConnect()) {
    948         if (use_settings_ui_) {
    949           // TODO(altimofeev): implement this.
    950         } else {
    951           CreateModalPopup(new chromeos::NetworkConfigView(network));
    952         }
    953       } else {
    954         cros->ConnectToVirtualNetwork(network);
    955       }
    956     } else if (command == "disconnect") {
    957       cros->DisconnectFromNetwork(network);
    958     } else if (command == "options") {
    959       PopulateDictionaryDetails(network, cros);
    960     }
    961   }
    962 }
    963 void InternetOptionsHandler::RefreshCellularPlanCallback(
    964     const ListValue* args) {
    965   std::string service_path;
    966   if (args->GetSize() != 1 ||
    967       !args->GetString(0, &service_path)) {
    968     NOTREACHED();
    969     return;
    970   }
    971   chromeos::NetworkLibrary* cros =
    972       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
    973   const chromeos::CellularNetwork* cellular =
    974       cros->FindCellularNetworkByPath(service_path);
    975   if (cellular)
    976     cellular->RefreshDataPlansIfNeeded();
    977 }
    978 
    979 ListValue* InternetOptionsHandler::GetNetwork(
    980     const std::string& service_path,
    981     const SkBitmap& icon,
    982     const std::string& name,
    983     bool connecting,
    984     bool connected,
    985     bool connectable,
    986     chromeos::ConnectionType connection_type,
    987     bool remembered,
    988     chromeos::ActivationState activation_state,
    989     bool needs_new_plan) {
    990   ListValue* network = new ListValue();
    991 
    992   // 802.1X networks can be connected but not have saved credentials, and
    993   // hence be "not configured".  Give preference to the "connected" and
    994   // "connecting" states.  http://crosbug.com/14459
    995   int connection_state = IDS_STATUSBAR_NETWORK_DEVICE_DISCONNECTED;
    996   if (connected)
    997     connection_state = IDS_STATUSBAR_NETWORK_DEVICE_CONNECTED;
    998   else if (connecting)
    999     connection_state = IDS_STATUSBAR_NETWORK_DEVICE_CONNECTING;
   1000   else if (!connectable)
   1001     connection_state = IDS_STATUSBAR_NETWORK_DEVICE_NOT_CONFIGURED;
   1002   std::string status = l10n_util::GetStringUTF8(connection_state);
   1003   if (connection_type == chromeos::TYPE_CELLULAR) {
   1004     if (needs_new_plan) {
   1005       status = l10n_util::GetStringUTF8(IDS_OPTIONS_SETTINGS_NO_PLAN_LABEL);
   1006     } else if (activation_state != chromeos::ACTIVATION_STATE_ACTIVATED) {
   1007       status.append(" / ");
   1008       status.append(
   1009           chromeos::CellularNetwork::ActivationStateToString(activation_state));
   1010     }
   1011   }
   1012 
   1013   // To keep the consistency with JS implementation, do not change the order
   1014   // locally.
   1015   // TODO(kochi): Use dictionaly for future maintainability.
   1016   // 0) service path
   1017   network->Append(Value::CreateStringValue(service_path));
   1018   // 1) name
   1019   network->Append(Value::CreateStringValue(name));
   1020   // 2) status
   1021   network->Append(Value::CreateStringValue(status));
   1022   // 3) type
   1023   network->Append(Value::CreateIntegerValue(static_cast<int>(connection_type)));
   1024   // 4) connected
   1025   network->Append(Value::CreateBooleanValue(connected));
   1026   // 5) connecting
   1027   network->Append(Value::CreateBooleanValue(connecting));
   1028   // 6) icon data url
   1029   network->Append(Value::CreateStringValue(icon.isNull() ? "" :
   1030       web_ui_util::GetImageDataUrl(icon)));
   1031   // 7) remembered
   1032   network->Append(Value::CreateBooleanValue(remembered));
   1033   // 8) activation state
   1034   network->Append(Value::CreateIntegerValue(
   1035                     static_cast<int>(activation_state)));
   1036   // 9) needs new plan
   1037   network->Append(Value::CreateBooleanValue(needs_new_plan));
   1038   // 10) connectable
   1039   network->Append(Value::CreateBooleanValue(connectable));
   1040   return network;
   1041 }
   1042 
   1043 ListValue* InternetOptionsHandler::GetWiredList() {
   1044   chromeos::NetworkLibrary* cros =
   1045       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
   1046   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
   1047   ListValue* list = new ListValue();
   1048 
   1049   // If ethernet is not enabled, then don't add anything.
   1050   if (cros->ethernet_enabled()) {
   1051     const chromeos::EthernetNetwork* ethernet_network =
   1052         cros->ethernet_network();
   1053     const SkBitmap* icon = rb.GetBitmapNamed(IDR_STATUSBAR_WIRED_BLACK);
   1054     const SkBitmap* bottom_right_badge = !ethernet_network ||
   1055         (!ethernet_network->connecting() && !ethernet_network->connected()) ?
   1056             rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_DISCONNECTED) : NULL;
   1057     const SkBitmap* bottom_left_badge =
   1058         chromeos::NetworkMenu::BadgeForPrivateNetworkStatus(ethernet_network);
   1059     if (ethernet_network) {
   1060       list->Append(GetNetwork(
   1061           ethernet_network->service_path(),
   1062           chromeos::NetworkMenu::IconForDisplay(icon, bottom_right_badge, NULL,
   1063                                                 bottom_left_badge),
   1064           l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET),
   1065           ethernet_network->connecting(),
   1066           ethernet_network->connected(),
   1067           ethernet_network->connectable(),
   1068           chromeos::TYPE_ETHERNET,
   1069           false,
   1070           chromeos::ACTIVATION_STATE_UNKNOWN,
   1071           false));
   1072     }
   1073   }
   1074   return list;
   1075 }
   1076 
   1077 ListValue* InternetOptionsHandler::GetWirelessList() {
   1078   chromeos::NetworkLibrary* cros =
   1079       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
   1080   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
   1081   ListValue* list = new ListValue();
   1082 
   1083   const chromeos::Network* active_network = cros->active_network();
   1084   bool has_vpn = active_network && cros->virtual_network();
   1085   bool vpn_on_wireless = has_vpn &&
   1086       active_network->type() == chromeos::TYPE_WIFI;
   1087   const chromeos::WifiNetworkVector& wifi_networks = cros->wifi_networks();
   1088   for (chromeos::WifiNetworkVector::const_iterator it =
   1089       wifi_networks.begin(); it != wifi_networks.end(); ++it) {
   1090     const SkBitmap* icon =
   1091         chromeos::NetworkMenu::IconForNetworkStrength(*it, true);
   1092     const SkBitmap* bottom_right_badge = (*it)->encrypted() ?
   1093         rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_SECURE) : NULL;
   1094     const SkBitmap* bottom_left_badge =
   1095         vpn_on_wireless && active_network == (*it) ?
   1096         chromeos::NetworkMenu::BadgeForPrivateNetworkStatus(NULL) : NULL;
   1097     list->Append(GetNetwork(
   1098         (*it)->service_path(),
   1099         chromeos::NetworkMenu::IconForDisplay(icon, bottom_right_badge, NULL,
   1100                                               bottom_left_badge),
   1101         (*it)->name(),
   1102         (*it)->connecting(),
   1103         (*it)->connected(),
   1104         (*it)->connectable(),
   1105         chromeos::TYPE_WIFI,
   1106         false,
   1107         chromeos::ACTIVATION_STATE_UNKNOWN,
   1108         false));
   1109   }
   1110 
   1111   // Add "Other WiFi network..." if wifi is enabled.
   1112   if (cros->wifi_enabled()) {
   1113     list->Append(GetNetwork(
   1114         kOtherNetworksFakePath,
   1115         *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0_BLACK),
   1116         l10n_util::GetStringUTF8(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS),
   1117         false,
   1118         false,
   1119         true,
   1120         chromeos::TYPE_WIFI,
   1121         false,
   1122         chromeos::ACTIVATION_STATE_UNKNOWN,
   1123         false));
   1124   }
   1125 
   1126   bool vpn_on_cellular = has_vpn &&
   1127                          active_network->type() == chromeos::TYPE_CELLULAR;
   1128   const chromeos::CellularNetworkVector cellular_networks =
   1129       cros->cellular_networks();
   1130   for (chromeos::CellularNetworkVector::const_iterator it =
   1131       cellular_networks.begin(); it != cellular_networks.end(); ++it) {
   1132     const SkBitmap* icon =
   1133         chromeos::NetworkMenu::IconForNetworkStrength(*it, true);
   1134     const SkBitmap* bottom_right_badge =
   1135         chromeos::NetworkMenu::BadgeForNetworkTechnology(*it);
   1136     const SkBitmap* roaming_badge =
   1137         chromeos::NetworkMenu::BadgeForRoamingStatus(*it);
   1138     const SkBitmap* bottom_left_badge =
   1139         vpn_on_cellular && active_network == (*it) ?
   1140         chromeos::NetworkMenu::BadgeForPrivateNetworkStatus(NULL) : NULL;
   1141     list->Append(GetNetwork(
   1142         (*it)->service_path(),
   1143         chromeos::NetworkMenu::IconForDisplay(icon, bottom_right_badge,
   1144                                               roaming_badge, bottom_left_badge),
   1145         (*it)->name(),
   1146         (*it)->connecting(),
   1147         (*it)->connected(),
   1148         (*it)->connectable(),
   1149         chromeos::TYPE_CELLULAR,
   1150         false,
   1151         (*it)->activation_state(),
   1152         (*it)->SupportsDataPlan() && (*it)->restricted_pool()));
   1153   }
   1154 
   1155   const chromeos::NetworkDevice* cellular_device = cros->FindCellularDevice();
   1156   if (cellular_device && cellular_device->support_network_scan() &&
   1157       cros->cellular_enabled()) {
   1158     list->Append(GetNetwork(
   1159         kOtherNetworksFakePath,
   1160         *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0_BLACK),
   1161         l10n_util::GetStringUTF8(IDS_OPTIONS_SETTINGS_OTHER_CELLULAR_NETWORKS),
   1162         false,
   1163         false,
   1164         true,
   1165         chromeos::TYPE_CELLULAR,
   1166         false,
   1167         chromeos::ACTIVATION_STATE_ACTIVATED,
   1168         false));
   1169   }
   1170 
   1171   return list;
   1172 }
   1173 
   1174 ListValue* InternetOptionsHandler::GetRememberedList() {
   1175   chromeos::NetworkLibrary* cros =
   1176       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
   1177   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
   1178   ListValue* list = new ListValue();
   1179 
   1180   const chromeos::WifiNetworkVector& remembered_wifi_networks =
   1181       cros->remembered_wifi_networks();
   1182   const chromeos::Network* active_network = cros->active_network();
   1183   bool vpn_on_wireless = active_network && cros->virtual_network() &&
   1184                          active_network->type() == chromeos::TYPE_WIFI;
   1185 
   1186   for (chromeos::WifiNetworkVector::const_iterator rit =
   1187            remembered_wifi_networks.begin();
   1188        rit != remembered_wifi_networks.end(); ++rit) {
   1189     chromeos::WifiNetwork* remembered = *rit;
   1190     chromeos::Network* network = cros->FindNetworkFromRemembered(remembered);
   1191 
   1192     const SkBitmap* icon = network ?
   1193         chromeos::NetworkMenu::IconForNetworkStrength(
   1194             static_cast<chromeos::WifiNetwork*>(network), true) :
   1195         rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0_BLACK);
   1196     // Place the secure badge on the icon if the remembered network is
   1197     // encrypted (the matching detected network, if any, will have the same
   1198     // encrypted property by definition).
   1199     const SkBitmap* bottom_right_badge = remembered->encrypted() ?
   1200         rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_SECURE) : NULL;
   1201     const SkBitmap* bottom_left_badge =
   1202         vpn_on_wireless && active_network == network ?
   1203         chromeos::NetworkMenu::BadgeForPrivateNetworkStatus(NULL) : NULL;
   1204     list->Append(GetNetwork(
   1205         remembered->service_path(),
   1206         chromeos::NetworkMenu::IconForDisplay(icon, bottom_right_badge, NULL,
   1207                                               bottom_left_badge),
   1208         remembered->name(),
   1209         network ? network->connecting() : false,
   1210         network ? network->connected() : false,
   1211         true,
   1212         chromeos::TYPE_WIFI,
   1213         true,
   1214         chromeos::ACTIVATION_STATE_UNKNOWN,
   1215         false));
   1216   }
   1217   return list;
   1218 }
   1219 
   1220 void InternetOptionsHandler::FillNetworkInfo(
   1221     DictionaryValue* dictionary, chromeos::NetworkLibrary* cros) {
   1222   dictionary->SetBoolean("accessLocked", cros->IsLocked());
   1223   dictionary->Set("wiredList", GetWiredList());
   1224   dictionary->Set("wirelessList", GetWirelessList());
   1225   dictionary->Set("rememberedList", GetRememberedList());
   1226   dictionary->SetBoolean("wifiAvailable", cros->wifi_available());
   1227   dictionary->SetBoolean("wifiEnabled", cros->wifi_enabled());
   1228   dictionary->SetBoolean("cellularAvailable", cros->cellular_available());
   1229   dictionary->SetBoolean("cellularEnabled", cros->cellular_enabled());
   1230 }
   1231