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/ui/webui/chromeos/internet_detail_dialog.h"
6 
7 #include "ash/public/cpp/ash_features.h"
8 #include "ash/public/cpp/network_config_service.h"
9 #include "base/json/json_writer.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/webui/chromeos/network_element_localized_strings_provider.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/grit/browser_resources.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chromeos/network/network_handler.h"
17 #include "chromeos/network/network_state.h"
18 #include "chromeos/network/network_state_handler.h"
19 #include "chromeos/network/network_util.h"
20 #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"  // nogncheck
21 #include "components/strings/grit/components_strings.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h"
24 #include "ui/base/l10n/l10n_util.h"
25 
26 namespace chromeos {
27 
28 namespace {
29 
30 // Matches the width of the Settings content.
31 constexpr int kInternetDetailDialogWidth = 640;
32 
33 int s_internet_detail_dialog_count = 0;
34 
AddInternetStrings(content::WebUIDataSource * html_source)35 void AddInternetStrings(content::WebUIDataSource* html_source) {
36   // Add default strings first.
37   chromeos::network_element::AddLocalizedStrings(html_source);
38   chromeos::network_element::AddOncLocalizedStrings(html_source);
39   chromeos::network_element::AddDetailsLocalizedStrings(html_source);
40   // Add additional strings and overrides needed by the dialog.
41   struct {
42     const char* name;
43     int id;
44   } localized_strings[] = {
45       {"cancel", IDS_CANCEL},
46       {"close", IDS_CLOSE},
47       {"networkButtonConnect", IDS_SETTINGS_INTERNET_BUTTON_CONNECT},
48       {"networkButtonDisconnect", IDS_SETTINGS_INTERNET_BUTTON_DISCONNECT},
49       {"networkButtonForget", IDS_SETTINGS_INTERNET_BUTTON_FORGET},
50       {"networkIPAddress", IDS_SETTINGS_INTERNET_NETWORK_IP_ADDRESS},
51       {"networkSectionNetwork", IDS_SETTINGS_INTERNET_NETWORK_SECTION_NETWORK},
52       {"networkSectionProxy", IDS_SETTINGS_INTERNET_NETWORK_SECTION_PROXY},
53       {"networkIPConfigAuto", IDS_SETTINGS_INTERNET_NETWORK_IP_CONFIG_AUTO},
54       {"save", IDS_SAVE},
55       // Override for network_element::AddDetailsLocalizedStrings
56       {"networkProxyConnectionType",
57        IDS_SETTINGS_INTERNET_NETWORK_PROXY_CONNECTION_TYPE_DIALOG},
58   };
59   for (const auto& entry : localized_strings)
60     html_source->AddLocalizedString(entry.name, entry.id);
61 }
62 
GetNetworkName16(const NetworkState & network)63 base::string16 GetNetworkName16(const NetworkState& network) {
64   return network.Matches(NetworkTypePattern::Ethernet())
65              ? l10n_util::GetStringUTF16(IDS_NETWORK_TYPE_ETHERNET)
66              : base::UTF8ToUTF16(network.name());
67 }
68 
GetNetworkName8(const NetworkState & network)69 std::string GetNetworkName8(const NetworkState& network) {
70   return network.Matches(NetworkTypePattern::Ethernet())
71              ? l10n_util::GetStringUTF8(IDS_NETWORK_TYPE_ETHERNET)
72              : network.name();
73 }
74 
75 }  // namespace
76 
77 // static
IsShown()78 bool InternetDetailDialog::IsShown() {
79   return s_internet_detail_dialog_count > 0;
80 }
81 
82 // static
ShowDialog(const std::string & network_id)83 void InternetDetailDialog::ShowDialog(const std::string& network_id) {
84   auto* network_state_handler = NetworkHandler::Get()->network_state_handler();
85   const NetworkState* network;
86   if (!network_id.empty())
87     network = network_state_handler->GetNetworkStateFromGuid(network_id);
88   else
89     network = network_state_handler->DefaultNetwork();
90   if (!network) {
91     LOG(ERROR) << "Network not found: " << network_id;
92     return;
93   }
94   auto* instance = SystemWebDialogDelegate::FindInstance(network->guid());
95   if (instance) {
96     instance->Focus();
97     return;
98   }
99 
100   InternetDetailDialog* dialog = new InternetDetailDialog(*network);
101   dialog->ShowSystemDialog();
102 }
103 
InternetDetailDialog(const NetworkState & network)104 InternetDetailDialog::InternetDetailDialog(const NetworkState& network)
105     : SystemWebDialogDelegate(GURL(chrome::kChromeUIIntenetDetailDialogURL),
106                               GetNetworkName16(network)),
107       network_id_(network.guid()),
108       network_type_(network_util::TranslateShillTypeToONC(network.type())),
109       network_name_(GetNetworkName8(network)) {
110   ++s_internet_detail_dialog_count;
111 }
112 
~InternetDetailDialog()113 InternetDetailDialog::~InternetDetailDialog() {
114   --s_internet_detail_dialog_count;
115 }
116 
Id()117 const std::string& InternetDetailDialog::Id() {
118   return network_id_;
119 }
120 
GetDialogSize(gfx::Size * size) const121 void InternetDetailDialog::GetDialogSize(gfx::Size* size) const {
122   size->SetSize(kInternetDetailDialogWidth,
123                 SystemWebDialogDelegate::kDialogHeight);
124 }
125 
GetDialogArgs() const126 std::string InternetDetailDialog::GetDialogArgs() const {
127   base::DictionaryValue args;
128   args.SetKey("type", base::Value(network_type_));
129   args.SetKey("guid", base::Value(network_id_));
130   args.SetKey("name", base::Value(network_name_));
131   std::string json;
132   base::JSONWriter::Write(args, &json);
133   return json;
134 }
135 
136 // InternetDetailDialogUI
137 
InternetDetailDialogUI(content::WebUI * web_ui)138 InternetDetailDialogUI::InternetDetailDialogUI(content::WebUI* web_ui)
139     : ui::MojoWebDialogUI(web_ui) {
140   content::WebUIDataSource* source = content::WebUIDataSource::Create(
141       chrome::kChromeUIInternetDetailDialogHost);
142   source->AddBoolean("showTechnologyBadge",
143                      !ash::features::IsSeparateNetworkIconsEnabled());
144   AddInternetStrings(source);
145   source->AddLocalizedString("title", IDS_SETTINGS_INTERNET_DETAIL);
146   source->UseStringsJs();
147 #if BUILDFLAG(OPTIMIZE_WEBUI)
148   source->SetDefaultResource(IDR_INTERNET_DETAIL_DIALOG_VULCANIZED_HTML);
149   source->AddResourcePath("crisper.js", IDR_INTERNET_DETAIL_DIALOG_CRISPER_JS);
150 #else
151   source->SetDefaultResource(IDR_INTERNET_DETAIL_DIALOG_HTML);
152   source->AddResourcePath("internet_detail_dialog.js",
153                           IDR_INTERNET_DETAIL_DIALOG_JS);
154 #endif
155   content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
156 }
157 
~InternetDetailDialogUI()158 InternetDetailDialogUI::~InternetDetailDialogUI() {}
159 
BindInterface(mojo::PendingReceiver<chromeos::network_config::mojom::CrosNetworkConfig> receiver)160 void InternetDetailDialogUI::BindInterface(
161     mojo::PendingReceiver<chromeos::network_config::mojom::CrosNetworkConfig>
162         receiver) {
163   ash::GetNetworkConfigService(std::move(receiver));
164 }
165 
166 WEB_UI_CONTROLLER_TYPE_IMPL(InternetDetailDialogUI)
167 
168 }  // namespace chromeos
169