Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / proxy_settings_dialog.cc
blob6fc9521dba9b6068ab4761a11c37b17d8ce75c95
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.
5 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h"
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"
23 namespace {
25 // Hints for size of proxy settings dialog.
26 const int kProxySettingsDialogReasonableWidth = 626;
27 const int kProxySettingsDialogReasonableHeight = 525;
28 const float kProxySettingsDialogReasonableWidthRatio = 0.4f;
29 const float kProxySettingsDialogReasonableHeightRatio = 0.4f;
31 const char* kProxySettingsURLParam = "?network=%s";
33 int 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);
39 GURL 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);
47 } // namespace
49 namespace chromeos {
51 // static
52 int ProxySettingsDialog::instance_count_ = 0;
54 ProxySettingsDialog::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_;
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));
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);
79 SetDialogTitle(l10n_util::GetStringFUTF16(IDS_PROXY_PAGE_TITLE_FORMAT,
80 base::ASCIIToUTF16(network_name)));
83 ProxySettingsDialog::~ProxySettingsDialog() {
84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
85 --instance_count_;
88 void 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());
96 bool ProxySettingsDialog::IsShown() {
97 return instance_count_ > 0;
100 } // namespace chromeos