Home | History | Annotate | Download | only in login
      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/chromeos/login/proxy_settings_dialog.h"
      6 
      7 #include "base/strings/stringprintf.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/chromeos/login/helper.h"
     11 #include "chrome/common/url_constants.h"
     12 #include "chromeos/network/network_state.h"
     13 #include "content/public/browser/browser_thread.h"
     14 #include "content/public/browser/notification_service.h"
     15 #include "grit/generated_resources.h"
     16 #include "net/base/escape.h"
     17 #include "third_party/cros_system_api/dbus/service_constants.h"
     18 #include "ui/base/l10n/l10n_util.h"
     19 #include "ui/gfx/rect.h"
     20 #include "ui/gfx/size.h"
     21 
     22 namespace {
     23 
     24 // Hints for size of proxy settings dialog.
     25 const int kProxySettingsDialogReasonableWidth = 626;
     26 const int kProxySettingsDialogReasonableHeight = 525;
     27 const float kProxySettingsDialogReasonableWidthRatio = 0.4f;
     28 const float kProxySettingsDialogReasonableHeightRatio = 0.4f;
     29 
     30 const char* kProxySettingsURLParam = "?network=%s";
     31 
     32 int CalculateSize(int screen_size, int min_comfortable, float desired_ratio) {
     33   int desired_size = static_cast<int>(desired_ratio * screen_size);
     34   desired_size = std::max(min_comfortable, desired_size);
     35   return std::min(screen_size, desired_size);
     36 }
     37 
     38 GURL GetURLForProxySettings(const std::string& service_path) {
     39   std::string url(chrome::kChromeUIProxySettingsURL);
     40   url += base::StringPrintf(
     41       kProxySettingsURLParam,
     42       net::EscapeUrlEncodedData(service_path, true).c_str());
     43   return GURL(url);
     44 }
     45 
     46 }  // namespace
     47 
     48 namespace chromeos {
     49 
     50 // static
     51 int ProxySettingsDialog::instance_count_ = 0;
     52 
     53 ProxySettingsDialog::ProxySettingsDialog(const NetworkState& network,
     54                                          LoginWebDialog::Delegate* delegate,
     55                                          gfx::NativeWindow window)
     56     : LoginWebDialog(delegate,
     57                      window,
     58                      string16(),
     59                      GetURLForProxySettings(network.path()),
     60                      LoginWebDialog::STYLE_BUBBLE) {
     61   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     62   ++instance_count_;
     63 
     64   gfx::Rect screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
     65   SetDialogSize(CalculateSize(screen_bounds.width(),
     66                                kProxySettingsDialogReasonableWidth,
     67                                kProxySettingsDialogReasonableWidthRatio),
     68                 CalculateSize(screen_bounds.height(),
     69                                kProxySettingsDialogReasonableHeight,
     70                                kProxySettingsDialogReasonableHeightRatio));
     71 
     72   std::string network_name = network.name();
     73   if (network_name.empty() && network.type() == flimflam::kTypeEthernet) {
     74     network_name =
     75         l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
     76   }
     77 
     78   SetDialogTitle(l10n_util::GetStringFUTF16(IDS_PROXY_PAGE_TITLE_FORMAT,
     79                                             ASCIIToUTF16(network_name)));
     80 }
     81 
     82 ProxySettingsDialog::~ProxySettingsDialog() {
     83   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     84   --instance_count_;
     85 }
     86 
     87 void ProxySettingsDialog::OnDialogClosed(const std::string& json_retval) {
     88   LoginWebDialog::OnDialogClosed(json_retval);
     89   content::NotificationService::current()->Notify(
     90     chrome::NOTIFICATION_LOGIN_PROXY_CHANGED,
     91     content::NotificationService::AllSources(),
     92     content::NotificationService::NoDetails());
     93 }
     94 
     95 bool ProxySettingsDialog::IsShown() {
     96   return instance_count_ > 0;
     97 }
     98 
     99 }  // namespace chromeos
    100