Commit 494b2d3b authored by Alexander Alekseev's avatar Alexander Alekseev Committed by Chromium LUCI CQ

Ash HUD UiDevTools: Add "Create Ui Dev Tools" button to HUD.

This CL enables dynamic UiDevTools creation and adds "Create Ui
Dev Tools" button to Ash HUD.

Bug: 1138734

Change-Id: I4458f355241ce6c4c43494f9ee14956f0253b30f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2535010Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarWei Li <weili@chromium.org>
Commit-Queue: Alexander Alekseev <alemate@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845059}
parent 9646484a
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "ash/hud_display/hud_display.h" #include "ash/hud_display/hud_display.h"
#include "ash/hud_display/hud_properties.h" #include "ash/hud_display/hud_properties.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
...@@ -19,7 +20,9 @@ ...@@ -19,7 +20,9 @@
#include "ui/compositor/compositor.h" #include "ui/compositor/compositor.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h" #include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/checkbox.h" #include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
#include "ui/views/controls/slider.h" #include "ui/views/controls/slider.h"
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
...@@ -335,6 +338,20 @@ void AnimationSpeedControl::Layout() { ...@@ -335,6 +338,20 @@ void AnimationSpeedControl::Layout() {
views::View::Layout(); views::View::Layout();
} }
std::unique_ptr<views::LabelButton> CreateActionButton(
views::Button::PressedCallback::Callback callback,
const base::string16& text) {
auto button = std::make_unique<views::LabelButton>(callback, text);
button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
button->SetEnabledTextColors(kHUDBackground);
button->SetProperty(kHUDClickHandler, HTCLIENT);
constexpr float kActionButtonCournerRadius = 2;
button->SetBackground(views::CreateRoundedRectBackground(
kHUDDefaultColor, kActionButtonCournerRadius));
button->SetFocusBehavior(views::View::FocusBehavior::ACCESSIBLE_ONLY);
return button;
}
} // anonymous namespace } // anonymous namespace
BEGIN_METADATA(HUDSettingsView, views::View) BEGIN_METADATA(HUDSettingsView, views::View)
...@@ -430,6 +447,44 @@ HUDSettingsView::HUDSettingsView(HUDDisplayView* hud_display) { ...@@ -430,6 +447,44 @@ HUDSettingsView::HUDSettingsView(HUDDisplayView* hud_display) {
}, },
base::Unretained(hud_display)))); base::Unretained(hud_display))));
AddChildView(std::make_unique<AnimationSpeedControl>()); AddChildView(std::make_unique<AnimationSpeedControl>());
// Ui Dev Tools controls.
constexpr int kUiDevToolsControlButtonMargin = 6;
views::View* ui_devtools_controls =
AddChildView(std::make_unique<views::View>());
ui_devtools_controls
->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical))
->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kStretch);
ui_devtools_controls->SetBorder(
views::CreateEmptyBorder(gfx::Insets(kUiDevToolsControlButtonMargin)));
ui_dev_tools_control_button_ =
ui_devtools_controls->AddChildView(CreateActionButton(
base::BindRepeating(&HUDSettingsView::OnEnableUiDevToolsButtonPressed,
base::Unretained(this)),
base::string16()));
UpdateDevToolsControlButtonLabel();
}
void HUDSettingsView::OnEnableUiDevToolsButtonPressed(const ui::Event& event) {
if (Shell::Get()->shell_delegate()->IsUiDevToolsStarted()) {
Shell::Get()->shell_delegate()->StopUiDevTools();
} else {
Shell::Get()->shell_delegate()->StartUiDevTools();
}
UpdateDevToolsControlButtonLabel();
}
void HUDSettingsView::UpdateDevToolsControlButtonLabel() {
if (!Shell::Get()->shell_delegate()->IsUiDevToolsStarted()) {
ui_dev_tools_control_button_->SetText(
base::ASCIIToUTF16("Create Ui Dev Tools"));
} else {
const int port = Shell::Get()->shell_delegate()->GetUiDevToolsPort();
ui_dev_tools_control_button_->SetText(base::ASCIIToUTF16(
base::StringPrintf("Ui Dev Tools: ON, port %d", port).c_str()));
}
} }
HUDSettingsView::~HUDSettingsView() = default; HUDSettingsView::~HUDSettingsView() = default;
......
...@@ -11,6 +11,14 @@ ...@@ -11,6 +11,14 @@
#include "ash/hud_display/hud_constants.h" #include "ash/hud_display/hud_constants.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace ui {
class Event;
}
namespace views {
class LabelButton;
}
namespace ash { namespace ash {
namespace hud_display { namespace hud_display {
...@@ -30,8 +38,17 @@ class HUDSettingsView : public views::View { ...@@ -30,8 +38,17 @@ class HUDSettingsView : public views::View {
// Shows/hides the view. // Shows/hides the view.
void ToggleVisibility(); void ToggleVisibility();
// Creates Ui Dev Tools.
void OnEnableUiDevToolsButtonPressed(const ui::Event& event);
private: private:
// Replace "Create Ui Dev Tools" button label with "DevTools running".
void UpdateDevToolsControlButtonLabel();
std::vector<std::unique_ptr<HUDCheckboxHandler>> checkbox_handlers_; std::vector<std::unique_ptr<HUDCheckboxHandler>> checkbox_handlers_;
// Container for "Create Ui Dev Tools" button or "DevTools running" label.
views::LabelButton* ui_dev_tools_control_button_ = nullptr;
}; };
} // namespace hud_display } // namespace hud_display
......
...@@ -32,4 +32,12 @@ media_session::MediaSessionService* ShellDelegate::GetMediaSessionService() { ...@@ -32,4 +32,12 @@ media_session::MediaSessionService* ShellDelegate::GetMediaSessionService() {
return nullptr; return nullptr;
} }
bool ShellDelegate::IsUiDevToolsStarted() const {
return false;
}
int ShellDelegate::GetUiDevToolsPort() const {
return -1;
}
} // namespace ash } // namespace ash
...@@ -114,6 +114,12 @@ class ASH_EXPORT ShellDelegate { ...@@ -114,6 +114,12 @@ class ASH_EXPORT ShellDelegate {
// Returns if window browser sessions are restoring. // Returns if window browser sessions are restoring.
virtual bool IsSessionRestoreInProgress() const = 0; virtual bool IsSessionRestoreInProgress() const = 0;
// Ui Dev Tools control.
virtual bool IsUiDevToolsStarted() const;
virtual void StartUiDevTools() {}
virtual void StopUiDevTools() {}
virtual int GetUiDevToolsPort() const;
}; };
} // namespace ash } // namespace ash
......
...@@ -33,9 +33,11 @@ ...@@ -33,9 +33,11 @@
#include "chrome/browser/ui/browser_tabstrip.h" #include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/views/chrome_browser_main_extra_parts_views.h"
#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_util.h" #include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_util.h"
#include "chromeos/services/multidevice_setup/multidevice_setup_service.h" #include "chromeos/services/multidevice_setup/multidevice_setup_service.h"
#include "components/ui_devtools/devtools_server.h"
#include "content/public/browser/device_service.h" #include "content/public/browser/device_service.h"
#include "content/public/browser/media_session_service.h" #include "content/public/browser/media_session_service.h"
#include "content/public/browser/render_widget_host.h" #include "content/public/browser/render_widget_host.h"
...@@ -218,3 +220,21 @@ bool ChromeShellDelegate::IsSessionRestoreInProgress() const { ...@@ -218,3 +220,21 @@ bool ChromeShellDelegate::IsSessionRestoreInProgress() const {
Profile* profile = ProfileManager::GetActiveUserProfile(); Profile* profile = ProfileManager::GetActiveUserProfile();
return SessionRestore::IsRestoring(profile); return SessionRestore::IsRestoring(profile);
} }
bool ChromeShellDelegate::IsUiDevToolsStarted() const {
return ChromeBrowserMainExtraPartsViews::Get()->GetUiDevToolsServerInstance();
}
void ChromeShellDelegate::StartUiDevTools() {
ChromeBrowserMainExtraPartsViews::Get()->CreateUiDevTools();
}
void ChromeShellDelegate::StopUiDevTools() {
ChromeBrowserMainExtraPartsViews::Get()->DestroyUiDevTools();
}
int ChromeShellDelegate::GetUiDevToolsPort() const {
return ChromeBrowserMainExtraPartsViews::Get()
->GetUiDevToolsServerInstance()
->port();
}
...@@ -47,6 +47,10 @@ class ChromeShellDelegate : public ash::ShellDelegate { ...@@ -47,6 +47,10 @@ class ChromeShellDelegate : public ash::ShellDelegate {
std::unique_ptr<ash::NearbyShareDelegate> CreateNearbyShareDelegate( std::unique_ptr<ash::NearbyShareDelegate> CreateNearbyShareDelegate(
ash::NearbyShareController* controller) const override; ash::NearbyShareController* controller) const override;
bool IsSessionRestoreInProgress() const override; bool IsSessionRestoreInProgress() const override;
bool IsUiDevToolsStarted() const override;
void StartUiDevTools() override;
void StopUiDevTools() override;
int GetUiDevToolsPort() const override;
private: private:
DISALLOW_COPY_AND_ASSIGN(ChromeShellDelegate); DISALLOW_COPY_AND_ASSIGN(ChromeShellDelegate);
......
...@@ -50,6 +50,13 @@ ...@@ -50,6 +50,13 @@
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#endif // defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS) #endif // defined(OS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
namespace {
// Owned by ChromeBrowserMainParts.
ChromeBrowserMainExtraPartsViews* g_main_parts_views = nullptr;
} // namespace
// This connector is used in ui_devtools's TracingAgent to hook up with the // This connector is used in ui_devtools's TracingAgent to hook up with the
// tracing service. // tracing service.
class UiDevtoolsConnector : public ui_devtools::ConnectorDelegate { class UiDevtoolsConnector : public ui_devtools::ConnectorDelegate {
...@@ -63,12 +70,22 @@ class UiDevtoolsConnector : public ui_devtools::ConnectorDelegate { ...@@ -63,12 +70,22 @@ class UiDevtoolsConnector : public ui_devtools::ConnectorDelegate {
} }
}; };
ChromeBrowserMainExtraPartsViews::ChromeBrowserMainExtraPartsViews() {} ChromeBrowserMainExtraPartsViews::ChromeBrowserMainExtraPartsViews() {
DCHECK(!g_main_parts_views);
g_main_parts_views = this;
}
ChromeBrowserMainExtraPartsViews::~ChromeBrowserMainExtraPartsViews() { ChromeBrowserMainExtraPartsViews::~ChromeBrowserMainExtraPartsViews() {
DCHECK_EQ(g_main_parts_views, this);
g_main_parts_views = nullptr;
constrained_window::SetConstrainedWindowViewsClient(nullptr); constrained_window::SetConstrainedWindowViewsClient(nullptr);
} }
// static
ChromeBrowserMainExtraPartsViews* ChromeBrowserMainExtraPartsViews::Get() {
return g_main_parts_views;
}
void ChromeBrowserMainExtraPartsViews::ToolkitInitialized() { void ChromeBrowserMainExtraPartsViews::ToolkitInitialized() {
// The delegate needs to be set before any UI is created so that windows // The delegate needs to be set before any UI is created so that windows
// display the correct icon. // display the correct icon.
...@@ -96,13 +113,7 @@ void ChromeBrowserMainExtraPartsViews::PreCreateThreads() { ...@@ -96,13 +113,7 @@ void ChromeBrowserMainExtraPartsViews::PreCreateThreads() {
void ChromeBrowserMainExtraPartsViews::PreProfileInit() { void ChromeBrowserMainExtraPartsViews::PreProfileInit() {
if (ui_devtools::UiDevToolsServer::IsUiDevToolsEnabled( if (ui_devtools::UiDevToolsServer::IsUiDevToolsEnabled(
ui_devtools::switches::kEnableUiDevTools)) { ui_devtools::switches::kEnableUiDevTools)) {
// Starts the UI Devtools server for browser UI (and Ash UI on Chrome OS). CreateUiDevTools();
auto connector = std::make_unique<UiDevtoolsConnector>();
devtools_server_ = ui_devtools::CreateUiDevToolsServerForViews(
g_browser_process->system_network_context_manager()->GetContext(),
std::move(connector));
devtools_process_observer_ = std::make_unique<DevtoolsProcessObserver>(
devtools_server_->tracing_agent());
} }
media_router::MediaRouterDialogController::SetGetOrCreate( media_router::MediaRouterDialogController::SetGetOrCreate(
...@@ -168,3 +179,26 @@ void ChromeBrowserMainExtraPartsViews::PostMainMessageLoopRun() { ...@@ -168,3 +179,26 @@ void ChromeBrowserMainExtraPartsViews::PostMainMessageLoopRun() {
// shutdown. // shutdown.
relaunch_notification_controller_.reset(); relaunch_notification_controller_.reset();
} }
void ChromeBrowserMainExtraPartsViews::CreateUiDevTools() {
DCHECK(!devtools_server_);
DCHECK(!devtools_process_observer_);
// Starts the UI Devtools server for browser UI (and Ash UI on Chrome OS).
auto connector = std::make_unique<UiDevtoolsConnector>();
devtools_server_ = ui_devtools::CreateUiDevToolsServerForViews(
g_browser_process->system_network_context_manager()->GetContext(),
std::move(connector));
devtools_process_observer_ = std::make_unique<DevtoolsProcessObserver>(
devtools_server_->tracing_agent());
}
const ui_devtools::UiDevToolsServer*
ChromeBrowserMainExtraPartsViews::GetUiDevToolsServerInstance() {
return devtools_server_.get();
}
void ChromeBrowserMainExtraPartsViews::DestroyUiDevTools() {
devtools_process_observer_.reset();
devtools_server_.reset();
}
...@@ -33,6 +33,9 @@ class ChromeBrowserMainExtraPartsViews : public ChromeBrowserMainExtraParts { ...@@ -33,6 +33,9 @@ class ChromeBrowserMainExtraPartsViews : public ChromeBrowserMainExtraParts {
ChromeBrowserMainExtraPartsViews(); ChromeBrowserMainExtraPartsViews();
~ChromeBrowserMainExtraPartsViews() override; ~ChromeBrowserMainExtraPartsViews() override;
// Returns global singleton.
static ChromeBrowserMainExtraPartsViews* Get();
// Overridden from ChromeBrowserMainExtraParts: // Overridden from ChromeBrowserMainExtraParts:
void ToolkitInitialized() override; void ToolkitInitialized() override;
void PreCreateThreads() override; void PreCreateThreads() override;
...@@ -40,6 +43,11 @@ class ChromeBrowserMainExtraPartsViews : public ChromeBrowserMainExtraParts { ...@@ -40,6 +43,11 @@ class ChromeBrowserMainExtraPartsViews : public ChromeBrowserMainExtraParts {
void PostBrowserStart() override; void PostBrowserStart() override;
void PostMainMessageLoopRun() override; void PostMainMessageLoopRun() override;
// Manipulate UiDevTools.
void CreateUiDevTools();
const ui_devtools::UiDevToolsServer* GetUiDevToolsServerInstance();
void DestroyUiDevTools();
private: private:
std::unique_ptr<views::ViewsDelegate> views_delegate_; std::unique_ptr<views::ViewsDelegate> views_delegate_;
std::unique_ptr<views::LayoutProvider> layout_provider_; std::unique_ptr<views::LayoutProvider> layout_provider_;
......
...@@ -155,7 +155,11 @@ bool UiDevToolsServer::IsUiDevToolsEnabled(const char* enable_devtools_flag) { ...@@ -155,7 +155,11 @@ bool UiDevToolsServer::IsUiDevToolsEnabled(const char* enable_devtools_flag) {
// static // static
int UiDevToolsServer::GetUiDevToolsPort(const char* enable_devtools_flag, int UiDevToolsServer::GetUiDevToolsPort(const char* enable_devtools_flag,
int default_port) { int default_port) {
DCHECK(IsUiDevToolsEnabled(enable_devtools_flag)); // `enable_devtools_flag` is specified only when UiDevTools were started with
// browser start. If not specified at run time, we should use default port.
if (!IsUiDevToolsEnabled(enable_devtools_flag))
return default_port;
std::string switch_value = std::string switch_value =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
enable_devtools_flag); enable_devtools_flag);
......
...@@ -24,6 +24,8 @@ DOMAgentAura::DOMAgentAura() { ...@@ -24,6 +24,8 @@ DOMAgentAura::DOMAgentAura() {
DCHECK(!dom_agent_aura_); DCHECK(!dom_agent_aura_);
dom_agent_aura_ = this; dom_agent_aura_ = this;
aura::Env::GetInstance()->AddObserver(this); aura::Env::GetInstance()->AddObserver(this);
for (auto* window_tree_host : aura::Env::GetInstance()->window_tree_hosts())
OnHostInitialized(window_tree_host);
} }
DOMAgentAura::~DOMAgentAura() { DOMAgentAura::~DOMAgentAura() {
......
...@@ -244,10 +244,17 @@ void Env::NotifyWindowInitialized(Window* window) { ...@@ -244,10 +244,17 @@ void Env::NotifyWindowInitialized(Window* window) {
} }
void Env::NotifyHostInitialized(WindowTreeHost* host) { void Env::NotifyHostInitialized(WindowTreeHost* host) {
window_tree_hosts_.push_back(host);
for (EnvObserver& observer : observers_) for (EnvObserver& observer : observers_)
observer.OnHostInitialized(host); observer.OnHostInitialized(host);
} }
void Env::NotifyHostDestroyed(WindowTreeHost* host) {
base::Erase(window_tree_hosts_, host);
for (EnvObserver& observer : observers_)
observer.OnHostDestroyed(host);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Env, ui::EventTarget implementation: // Env, ui::EventTarget implementation:
......
...@@ -133,6 +133,10 @@ class AURA_EXPORT Env : public ui::EventTarget, ...@@ -133,6 +133,10 @@ class AURA_EXPORT Env : public ui::EventTarget,
void RemoveEventObserver(ui::EventObserver* observer); void RemoveEventObserver(ui::EventObserver* observer);
void NotifyEventObservers(const ui::Event& event); void NotifyEventObservers(const ui::Event& event);
const std::vector<aura::WindowTreeHost*>& window_tree_hosts() const {
return window_tree_hosts_;
}
private: private:
friend class test::EnvTestHelper; friend class test::EnvTestHelper;
friend class EventInjector; friend class EventInjector;
...@@ -149,6 +153,9 @@ class AURA_EXPORT Env : public ui::EventTarget, ...@@ -149,6 +153,9 @@ class AURA_EXPORT Env : public ui::EventTarget,
// Called by the WindowTreeHost when it is initialized. Notifies observers. // Called by the WindowTreeHost when it is initialized. Notifies observers.
void NotifyHostInitialized(WindowTreeHost* host); void NotifyHostInitialized(WindowTreeHost* host);
// Called by the WindowTreeHost before it is destroyed. Notifies observers.
void NotifyHostDestroyed(WindowTreeHost* host);
// Overridden from ui::EventTarget: // Overridden from ui::EventTarget:
bool CanAcceptEvent(const ui::Event& event) override; bool CanAcceptEvent(const ui::Event& event) override;
ui::EventTarget* GetParentTarget() override; ui::EventTarget* GetParentTarget() override;
...@@ -189,6 +196,8 @@ class AURA_EXPORT Env : public ui::EventTarget, ...@@ -189,6 +196,8 @@ class AURA_EXPORT Env : public ui::EventTarget,
std::unique_ptr<WindowOcclusionTracker> window_occlusion_tracker_; std::unique_ptr<WindowOcclusionTracker> window_occlusion_tracker_;
std::vector<aura::WindowTreeHost*> window_tree_hosts_;
DISALLOW_COPY_AND_ASSIGN(Env); DISALLOW_COPY_AND_ASSIGN(Env);
}; };
......
...@@ -20,6 +20,9 @@ class AURA_EXPORT EnvObserver { ...@@ -20,6 +20,9 @@ class AURA_EXPORT EnvObserver {
// Called when a WindowTreeHost is initialized. // Called when a WindowTreeHost is initialized.
virtual void OnHostInitialized(WindowTreeHost* host) {} virtual void OnHostInitialized(WindowTreeHost* host) {}
// Called before WindowTreeHost is destroyed.
virtual void OnHostDestroyed(WindowTreeHost* host) {}
// Called right before Env is destroyed. // Called right before Env is destroyed.
virtual void OnWillDestroyEnv() {} virtual void OnWillDestroyEnv() {}
......
...@@ -383,6 +383,7 @@ void WindowTreeHost::DestroyCompositor() { ...@@ -383,6 +383,7 @@ void WindowTreeHost::DestroyCompositor() {
} }
void WindowTreeHost::DestroyDispatcher() { void WindowTreeHost::DestroyDispatcher() {
Env::GetInstance()->NotifyHostDestroyed(this);
delete window_; delete window_;
window_ = nullptr; window_ = nullptr;
dispatcher_.reset(); dispatcher_.reset();
......
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