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