Commit 4f08d17c authored by jamescook's avatar jamescook Committed by Commit bot

chromeos: Convert "show other network" dialog to work with mash

Under mash code in chrome cannot directly access the ash aura window hierarchy.
Instead it must pass an ash window container id when a new window is being
opened.

Create a new NetworkConfigView::ShowForType to allow passing a container id.

Also clean up some unnecessary code around static initializers.

BUG=657021
TEST=open "join other" from system tray menu at login screen and after login

Review-Url: https://codereview.chromium.org/2445843002
Cr-Commit-Position: refs/heads/master@{#427160}
parent 919090e7
...@@ -49,6 +49,9 @@ namespace chromeos { ...@@ -49,6 +49,9 @@ namespace chromeos {
namespace { namespace {
// Used to check if a network config dialog is already showing.
NetworkConfigView* g_instance = nullptr;
gfx::NativeWindow GetParentForUnhostedDialog() { gfx::NativeWindow GetParentForUnhostedDialog() {
if (LoginDisplayHost::default_host()) { if (LoginDisplayHost::default_host()) {
return LoginDisplayHost::default_host()->GetNativeWindow(); return LoginDisplayHost::default_host()->GetNativeWindow();
...@@ -61,20 +64,6 @@ gfx::NativeWindow GetParentForUnhostedDialog() { ...@@ -61,20 +64,6 @@ gfx::NativeWindow GetParentForUnhostedDialog() {
return nullptr; return nullptr;
} }
// Avoid global static initializer.
NetworkConfigView** GetActiveDialogPointer() {
static NetworkConfigView* active_dialog = nullptr;
return &active_dialog;
}
NetworkConfigView* GetActiveDialog() {
return *(GetActiveDialogPointer());
}
void SetActiveDialog(NetworkConfigView* dialog) {
*(GetActiveDialogPointer()) = dialog;
}
} // namespace } // namespace
// static // static
...@@ -84,8 +73,8 @@ NetworkConfigView::NetworkConfigView() ...@@ -84,8 +73,8 @@ NetworkConfigView::NetworkConfigView()
: child_config_view_(nullptr), : child_config_view_(nullptr),
delegate_(nullptr), delegate_(nullptr),
advanced_button_(nullptr) { advanced_button_(nullptr) {
DCHECK(GetActiveDialog() == nullptr); DCHECK(!g_instance);
SetActiveDialog(this); g_instance = this;
} }
bool NetworkConfigView::InitWithNetworkState(const NetworkState* network) { bool NetworkConfigView::InitWithNetworkState(const NetworkState* network) {
...@@ -118,66 +107,84 @@ bool NetworkConfigView::InitWithType(const std::string& type) { ...@@ -118,66 +107,84 @@ bool NetworkConfigView::InitWithType(const std::string& type) {
} }
NetworkConfigView::~NetworkConfigView() { NetworkConfigView::~NetworkConfigView() {
DCHECK(GetActiveDialog() == this); DCHECK_EQ(g_instance, this);
SetActiveDialog(nullptr); g_instance = nullptr;
} }
// static // static
void NetworkConfigView::ShowInParent(const std::string& network_id, void NetworkConfigView::ShowInParent(const std::string& network_id,
gfx::NativeWindow parent) { gfx::NativeWindow parent) {
DCHECK(parent); DCHECK(parent);
ShowImpl(network_id, parent, ash::kShellWindowId_Invalid); NetworkConfigView* view = CreateForNetworkId(network_id);
if (view)
view->ShowDialog(parent);
} }
// static // static
void NetworkConfigView::ShowInContainer(const std::string& network_id, void NetworkConfigView::ShowInContainer(const std::string& network_id,
int container_id) { int container_id) {
DCHECK_NE(container_id, ash::kShellWindowId_Invalid); DCHECK_NE(container_id, ash::kShellWindowId_Invalid);
ShowImpl(network_id, nullptr, container_id); NetworkConfigView* view = CreateForNetworkId(network_id);
if (view)
view->ShowDialogInContainer(container_id);
} }
// static // static
void NetworkConfigView::ShowImpl(const std::string& network_id, NetworkConfigView* NetworkConfigView::CreateForNetworkId(
gfx::NativeWindow parent, const std::string& network_id) {
int container_id) { if (g_instance)
DCHECK(parent || container_id != ash::kShellWindowId_Invalid); return nullptr;
if (GetActiveDialog() != nullptr)
return;
const NetworkState* network = const NetworkState* network =
NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
network_id); network_id);
if (!network) { if (!network) {
LOG(ERROR) << "NetworkConfigView::Show called with invalid network"; LOG(ERROR)
return; << "NetworkConfigView::CreateForNetworkId called with invalid network";
return nullptr;
} }
NetworkConfigView* view = new NetworkConfigView(); NetworkConfigView* view = new NetworkConfigView();
if (!view->InitWithNetworkState(network)) { if (!view->InitWithNetworkState(network)) {
LOG(ERROR) << "NetworkConfigView::Show called with invalid network type: " LOG(ERROR) << "NetworkConfigView::CreateForNetworkId called with invalid "
"network type: "
<< network->type(); << network->type();
delete view; delete view;
return; return nullptr;
} }
NET_LOG(USER) << "NetworkConfigView::Show: " << network->path(); NET_LOG(USER) << "NetworkConfigView::CreateForNetworkId: " << network->path();
if (parent) return view;
view->ShowDialog(parent);
else
view->ShowDialogInContainer(container_id);
} }
// static // static
void NetworkConfigView::ShowForType(const std::string& type, void NetworkConfigView::ShowForType(const std::string& type,
gfx::NativeWindow parent) { gfx::NativeWindow parent) {
if (GetActiveDialog() != nullptr) // |parent| may be null.
return; NetworkConfigView* view = CreateForType(type);
if (view)
view->ShowDialog(parent);
}
// static
void NetworkConfigView::ShowForTypeInContainer(const std::string& type,
int container_id) {
DCHECK_NE(container_id, ash::kShellWindowId_Invalid);
NetworkConfigView* view = CreateForType(type);
if (view)
view->ShowDialogInContainer(container_id);
}
// static
NetworkConfigView* NetworkConfigView::CreateForType(const std::string& type) {
if (g_instance)
return nullptr;
NetworkConfigView* view = new NetworkConfigView(); NetworkConfigView* view = new NetworkConfigView();
if (!view->InitWithType(type)) { if (!view->InitWithType(type)) {
LOG(ERROR) << "NetworkConfigView::ShowForType called with invalid type: " LOG(ERROR) << "NetworkConfigView::CreateForType called with invalid type: "
<< type; << type;
delete view; delete view;
return; return nullptr;
} }
NET_LOG(USER) << "NetworkConfigView::ShowForType: " << type; NET_LOG(USER) << "NetworkConfigView::CreateForType: " << type;
view->ShowDialog(parent); return view;
} }
gfx::NativeWindow NetworkConfigView::GetNativeWindow() const { gfx::NativeWindow NetworkConfigView::GetNativeWindow() const {
......
...@@ -58,6 +58,10 @@ class NetworkConfigView : public views::DialogDelegateView, ...@@ -58,6 +58,10 @@ class NetworkConfigView : public views::DialogDelegateView,
// 'Type' property value. // 'Type' property value.
static void ShowForType(const std::string& type, gfx::NativeWindow parent); static void ShowForType(const std::string& type, gfx::NativeWindow parent);
// As above but places the dialog in the given ash window container on the
// primary display. Used as a fallback when no parent is available.
static void ShowForTypeInContainer(const std::string& type, int container_id);
// Returns corresponding native window. // Returns corresponding native window.
gfx::NativeWindow GetNativeWindow() const; gfx::NativeWindow GetNativeWindow() const;
...@@ -94,9 +98,11 @@ class NetworkConfigView : public views::DialogDelegateView, ...@@ -94,9 +98,11 @@ class NetworkConfigView : public views::DialogDelegateView,
NetworkConfigView(); NetworkConfigView();
~NetworkConfigView() override; ~NetworkConfigView() override;
static void ShowImpl(const std::string& network_id, // Returns null for invalid network or if dialog is already showing.
gfx::NativeWindow parent, static NetworkConfigView* CreateForNetworkId(const std::string& network_id);
int container_id);
// Returns null for invalid network type or if dialog is already showing.
static NetworkConfigView* CreateForType(const std::string& type);
// Login dialog for known networks. Returns true if successfully created. // Login dialog for known networks. Returns true if successfully created.
bool InitWithNetworkState(const NetworkState* network); bool InitWithNetworkState(const NetworkState* network);
......
...@@ -567,7 +567,8 @@ void SystemTrayDelegateChromeOS::ShowOtherNetworkDialog( ...@@ -567,7 +567,8 @@ void SystemTrayDelegateChromeOS::ShowOtherNetworkDialog(
ChooseMobileNetworkDialog::ShowDialog(GetNativeWindow()); ChooseMobileNetworkDialog::ShowDialog(GetNativeWindow());
return; return;
} }
NetworkConfigView::ShowForType(type, GetNativeWindow()); NetworkConfigView::ShowForTypeInContainer(
type, SystemTrayClient::GetDialogParentContainerId());
} }
bool SystemTrayDelegateChromeOS::GetBluetoothAvailable() { bool SystemTrayDelegateChromeOS::GetBluetoothAvailable() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment