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