blob: 5b655770fc072aed044d0bc914cf7ad83133ce7f [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/chromeos/internet_config_dialog.h"
#include "ash/public/cpp/network_config_service.h"
#include "base/json/json_writer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/webui_util.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/internet_config_dialog_resources.h"
#include "chrome/grit/internet_config_dialog_resources_map.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_util.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h" // nogncheck
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
#include "ui/chromeos/strings/network_element_localized_strings_provider.h"
#include "ui/wm/core/shadow_types.h"
namespace chromeos {
namespace {
// Dialog height for configured networks that only require a passphrase.
// This height includes room for a 'connecting' or error message.
constexpr int kDialogHeightPasswordOnly = 365;
void AddInternetStrings(content::WebUIDataSource* html_source) {
// Add default strings first.
ui::network_element::AddLocalizedStrings(html_source);
ui::network_element::AddOncLocalizedStrings(html_source);
ui::network_element::AddConfigLocalizedStrings(html_source);
ui::network_element::AddErrorLocalizedStrings(html_source);
// Add additional strings and overrides needed by the dialog.
struct {
const char* name;
int id;
} localized_strings[] = {
{"internetJoinType", IDS_SETTINGS_INTERNET_JOIN_TYPE},
{"networkButtonConnect", IDS_SETTINGS_INTERNET_BUTTON_CONNECT},
{"cancel", IDS_CANCEL},
{"close", IDS_CANCEL},
{"save", IDS_SAVE},
};
for (const auto& entry : localized_strings)
html_source->AddLocalizedString(entry.name, entry.id);
}
std::string GetId(const std::string& network_type,
const std::string& network_id) {
std::string result = chrome::kChromeUIIntenetConfigDialogURL + network_type;
if (!network_id.empty()) {
result += ".";
result += network_id;
}
return result;
}
} // namespace
// static
void InternetConfigDialog::ShowDialogForNetworkId(const std::string& network_id,
gfx::NativeWindow parent) {
const NetworkState* network_state =
NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
network_id);
if (!network_state) {
LOG(ERROR) << "Network not found: " << network_id;
return;
}
std::string network_type =
chromeos::network_util::TranslateShillTypeToONC(network_state->type());
std::string id = GetId(network_type, network_id);
auto* instance = SystemWebDialogDelegate::FindInstance(id);
if (instance) {
instance->Focus();
return;
}
InternetConfigDialog* dialog =
new InternetConfigDialog(id, network_type, network_id);
dialog->ShowSystemDialog(parent);
}
// static
void InternetConfigDialog::ShowDialogForNetworkType(
const std::string& network_type,
gfx::NativeWindow parent) {
std::string id = GetId(network_type, "");
auto* instance = SystemWebDialogDelegate::FindInstance(id);
if (instance) {
instance->Focus();
return;
}
InternetConfigDialog* dialog = new InternetConfigDialog(id, network_type, "");
dialog->ShowSystemDialog(parent);
}
InternetConfigDialog::InternetConfigDialog(const std::string& dialog_id,
const std::string& network_type,
const std::string& network_id)
: SystemWebDialogDelegate(GURL(chrome::kChromeUIIntenetConfigDialogURL),
std::u16string() /* title */),
dialog_id_(dialog_id),
network_type_(network_type),
network_id_(network_id) {}
InternetConfigDialog::~InternetConfigDialog() = default;
const std::string& InternetConfigDialog::Id() {
return dialog_id_;
}
void InternetConfigDialog::AdjustWidgetInitParams(
views::Widget::InitParams* params) {
params->type = views::Widget::InitParams::Type::TYPE_WINDOW_FRAMELESS;
params->shadow_type = views::Widget::InitParams::ShadowType::kDrop;
params->shadow_elevation = wm::kShadowElevationActiveWindow;
}
void InternetConfigDialog::GetDialogSize(gfx::Size* size) const {
const NetworkState* network =
network_id_.empty() ? nullptr
: NetworkHandler::Get()
->network_state_handler()
->GetNetworkStateFromGuid(network_id_);
int height = network && network->SecurityRequiresPassphraseOnly()
? kDialogHeightPasswordOnly
: InternetConfigDialog::kDialogHeight;
size->SetSize(InternetConfigDialog::kDialogWidth, height);
}
std::string InternetConfigDialog::GetDialogArgs() const {
base::DictionaryValue args;
args.SetKey("type", base::Value(network_type_));
args.SetKey("guid", base::Value(network_id_));
std::string json;
base::JSONWriter::Write(args, &json);
return json;
}
// InternetConfigDialogUI
InternetConfigDialogUI::InternetConfigDialogUI(content::WebUI* web_ui)
: ui::MojoWebDialogUI(web_ui) {
content::WebUIDataSource* source = content::WebUIDataSource::Create(
chrome::kChromeUIInternetConfigDialogHost);
source->DisableTrustedTypesCSP();
AddInternetStrings(source);
source->AddLocalizedString("title", IDS_SETTINGS_INTERNET_CONFIG);
source->UseStringsJs();
webui::SetupWebUIDataSource(
source,
base::make_span(kInternetConfigDialogResources,
kInternetConfigDialogResourcesSize),
IDR_INTERNET_CONFIG_DIALOG_INTERNET_CONFIG_DIALOG_CONTAINER_HTML);
content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
}
InternetConfigDialogUI::~InternetConfigDialogUI() {}
void InternetConfigDialogUI::BindInterface(
mojo::PendingReceiver<chromeos::network_config::mojom::CrosNetworkConfig>
receiver) {
ash::GetNetworkConfigService(std::move(receiver));
}
WEB_UI_CONTROLLER_TYPE_IMPL(InternetConfigDialogUI)
} // namespace chromeos