Home | History | Annotate | Download | only in options
      1 // Copyright (c) 2013 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/options/network_connect.h"
      6 
      7 #include "ash/shell.h"
      8 #include "ash/shell_delegate.h"
      9 #include "ash/system/chromeos/network/network_connect.h"
     10 #include "ash/system/chromeos/network/network_observer.h"
     11 #include "ash/system/tray/system_tray_notifier.h"
     12 #include "base/command_line.h"
     13 #include "base/strings/stringprintf.h"
     14 #include "base/strings/utf_string_conversions.h"
     15 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
     16 #include "chrome/browser/chromeos/login/user_manager.h"
     17 #include "chrome/browser/chromeos/options/network_config_view.h"
     18 #include "chrome/browser/profiles/profile_manager.h"
     19 #include "chrome/browser/ui/browser_finder.h"
     20 #include "chrome/browser/ui/chrome_pages.h"
     21 #include "chrome/browser/ui/webui/chromeos/mobile_setup_dialog.h"
     22 #include "chrome/common/url_constants.h"
     23 #include "chromeos/chromeos_switches.h"
     24 #include "chromeos/login/login_state.h"
     25 #include "chromeos/network/certificate_pattern.h"
     26 #include "chromeos/network/managed_network_configuration_handler.h"
     27 #include "chromeos/network/network_event_log.h"
     28 #include "chromeos/network/network_handler.h"
     29 #include "chromeos/network/network_state.h"
     30 #include "chromeos/network/network_state_handler.h"
     31 #include "content/public/browser/user_metrics.h"
     32 #include "grit/generated_resources.h"
     33 #include "net/base/escape.h"
     34 #include "third_party/cros_system_api/dbus/service_constants.h"
     35 #include "ui/base/l10n/l10n_util.h"
     36 
     37 namespace chromeos {
     38 
     39 namespace {
     40 
     41 void EnrollmentComplete(const std::string& service_path) {
     42   NET_LOG_USER("Enrollment Complete", service_path);
     43 }
     44 
     45 }
     46 
     47 namespace network_connect {
     48 
     49 void ShowMobileSetup(const std::string& service_path) {
     50   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
     51   const NetworkState* cellular = handler->GetNetworkState(service_path);
     52   if (cellular && cellular->type() == flimflam::kTypeCellular &&
     53       cellular->activation_state() != flimflam::kActivationStateActivated &&
     54       cellular->activate_over_non_cellular_networks() &&
     55       !handler->DefaultNetwork()) {
     56     std::string technology = cellular->network_technology();
     57     ash::NetworkObserver::NetworkType network_type =
     58         (technology == flimflam::kNetworkTechnologyLte ||
     59          technology == flimflam::kNetworkTechnologyLteAdvanced)
     60         ? ash::NetworkObserver::NETWORK_CELLULAR_LTE
     61         : ash::NetworkObserver::NETWORK_CELLULAR;
     62     ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage(
     63         NULL,
     64         ash::NetworkObserver::ERROR_CONNECT_FAILED,
     65         network_type,
     66         l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
     67         l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
     68                                    UTF8ToUTF16((cellular->name()))),
     69         std::vector<string16>());
     70     return;
     71   }
     72   MobileSetupDialog::Show(service_path);
     73 }
     74 
     75 void ShowNetworkSettings(const std::string& service_path) {
     76   std::string page = chrome::kInternetOptionsSubPage;
     77   const NetworkState* network = service_path.empty() ? NULL :
     78       NetworkHandler::Get()->network_state_handler()->GetNetworkState(
     79           service_path);
     80   if (network) {
     81     std::string name(network->name());
     82     if (name.empty() && network->type() == flimflam::kTypeEthernet)
     83       name = l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
     84     page += base::StringPrintf(
     85         "?servicePath=%s&networkType=%s&networkName=%s",
     86         net::EscapeUrlEncodedData(service_path, true).c_str(),
     87         net::EscapeUrlEncodedData(network->type(), true).c_str(),
     88         net::EscapeUrlEncodedData(name, false).c_str());
     89   }
     90   content::RecordAction(
     91       content::UserMetricsAction("OpenInternetOptionsDialog"));
     92   Browser* browser = chrome::FindOrCreateTabbedBrowser(
     93       ProfileManager::GetDefaultProfileOrOffTheRecord(),
     94       chrome::HOST_DESKTOP_TYPE_ASH);
     95   chrome::ShowSettingsSubPage(browser, page);
     96 }
     97 
     98 void HandleUnconfiguredNetwork(const std::string& service_path,
     99                                gfx::NativeWindow parent_window) {
    100   const NetworkState* network = NetworkHandler::Get()->network_state_handler()->
    101       GetNetworkState(service_path);
    102   if (!network) {
    103     NET_LOG_ERROR("Configuring unknown network", service_path);
    104     return;
    105   }
    106 
    107   if (network->type() == flimflam::kTypeWifi) {
    108     // Only show the config view for secure networks, otherwise do nothing.
    109     if (network->security() != flimflam::kSecurityNone)
    110       NetworkConfigView::Show(service_path, parent_window);
    111     return;
    112   }
    113 
    114   if (network->type() == flimflam::kTypeWimax ||
    115       network->type() == flimflam::kTypeVPN) {
    116     NetworkConfigView::Show(service_path, parent_window);
    117     return;
    118   }
    119 
    120   if (network->type() == flimflam::kTypeCellular) {
    121     if (network->activation_state() != flimflam::kActivationStateActivated) {
    122       ash::network_connect::ActivateCellular(service_path);
    123       return;
    124     }
    125     if (network->cellular_out_of_credits()) {
    126       ShowMobileSetup(service_path);
    127       return;
    128     }
    129     // No special configure or setup for |network|, show the settings UI.
    130     if (LoginState::Get()->IsUserLoggedIn())
    131       ShowNetworkSettings(service_path);
    132   }
    133   NOTREACHED();
    134 }
    135 
    136 bool EnrollNetwork(const std::string& service_path,
    137                    gfx::NativeWindow parent_window) {
    138   const NetworkState* network = NetworkHandler::Get()->network_state_handler()->
    139       GetNetworkState(service_path);
    140   if (!network) {
    141     NET_LOG_ERROR("Enrolling Unknown network", service_path);
    142     return false;
    143   }
    144   // We skip certificate patterns for device policy ONC so that an unmanaged
    145   // user can't get to the place where a cert is presented for them
    146   // involuntarily.
    147   if (network->ui_data().onc_source() == onc::ONC_SOURCE_DEVICE_POLICY)
    148     return false;
    149 
    150   const CertificatePattern& certificate_pattern =
    151       network->ui_data().certificate_pattern();
    152   if (certificate_pattern.Empty())
    153     return false;
    154 
    155   NET_LOG_USER("Enrolling", service_path);
    156 
    157   EnrollmentDelegate* enrollment = CreateEnrollmentDelegate(
    158       parent_window, network->name(), ProfileManager::GetDefaultProfile());
    159   return enrollment->Enroll(certificate_pattern.enrollment_uri_list(),
    160                             base::Bind(&EnrollmentComplete, service_path));
    161 }
    162 
    163 const base::DictionaryValue* FindPolicyForActiveUser(
    164     const NetworkState* network,
    165     onc::ONCSource* onc_source) {
    166   const User* user = UserManager::Get()->GetActiveUser();
    167   std::string username_hash = user ? user->username_hash() : std::string();
    168   return NetworkHandler::Get()->managed_network_configuration_handler()->
    169       FindPolicyByGUID(username_hash, network->guid(), onc_source);
    170 }
    171 
    172 }  // namespace network_connect
    173 }  // namespace chromeos
    174