53092f700f748634792550f46cadf05e615dabcc
[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                      LoginWebDialog::STYLE_BUBBLE) {
66   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
67   ++instance_count_;
68
69   gfx::Rect screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
70   SetDialogSize(CalculateSize(screen_bounds.width(),
71                               kProxySettingsDialogReasonableWidth,
72                               kProxySettingsDialogReasonableWidthRatio),
73                 CalculateSize(screen_bounds.height(),
74                               kProxySettingsDialogReasonableHeight,
75                               kProxySettingsDialogReasonableHeightRatio));
76
77   std::string network_name = network.name();
78   if (network_name.empty() && network.Matches(NetworkTypePattern::Ethernet())) {
79     network_name =
80         l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
81   }
82
83   SetDialogTitle(l10n_util::GetStringFUTF16(IDS_PROXY_PAGE_TITLE_FORMAT,
84                                             base::ASCIIToUTF16(network_name)));
85 }
86
87 ProxySettingsDialog::~ProxySettingsDialog() {
88   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
89   --instance_count_;
90 }
91
92 void ProxySettingsDialog::OnDialogClosed(const std::string& json_retval) {
93   LoginWebDialog::OnDialogClosed(json_retval);
94   content::NotificationService::current()->Notify(
95     chrome::NOTIFICATION_LOGIN_PROXY_CHANGED,
96     content::NotificationService::AllSources(),
97     content::NotificationService::NoDetails());
98 }
99
100 bool ProxySettingsDialog::IsShown() {
101   return instance_count_ > 0;
102 }
103
104 }  // namespace chromeos