Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry event for general settings #1269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 57 additions & 26 deletions src/runner/general_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <common/windows_colors.h>
#include <common/winstore.h>

#include "trace.h"

static std::wstring settings_theme = L"system";
static bool run_as_elevated = false;

Expand All @@ -18,6 +20,33 @@ namespace localized_strings
const std::wstring_view STARTUP_DISABLED_BY_USER = LR"(This setting has been disabled manually via <a href="https://ms_settings_startupapps" target="_blank">Startup Settings</a>.)";
}

json::JsonObject GeneralSettings::to_json()
{
json::JsonObject result;

result.SetNamedValue(L"packaged", json::value(isPackaged));
result.SetNamedValue(L"startup", json::value(isStartupEnabled));
if (!startupDisabledReason.empty())
{
result.SetNamedValue(L"startup_disabled_reason", json::value(startupDisabledReason));
}

json::JsonObject enabled;
for (const auto& [name, isEnabled] : isModulesEnabledMap)
{
enabled.SetNamedValue(name, json::value(isEnabled));
}
result.SetNamedValue(L"enabled", std::move(enabled));

result.SetNamedValue(L"is_elevated", json::value(isElevated));
result.SetNamedValue(L"run_elevated", json::value(isRunElevated));
result.SetNamedValue(L"theme", json::value(theme));
result.SetNamedValue(L"system_theme", json::value(systemTheme));
result.SetNamedValue(L"powertoys_version", json::value(powerToysVersion));

return result;
}

json::JsonObject load_general_settings()
{
auto loaded = PTSettingsHelper::load_general_settings();
Expand All @@ -30,56 +59,56 @@ json::JsonObject load_general_settings()
return loaded;
}

json::JsonObject get_general_settings()
GeneralSettings get_settings()
{
json::JsonObject result;
GeneralSettings settings{
.isPackaged = winstore::running_as_packaged(),
.isElevated = is_process_elevated(),
.isRunElevated = run_as_elevated,
.theme = settings_theme,
.systemTheme = WindowsColors::is_dark_mode() ? L"dark" : L"light",
.powerToysVersion = get_product_version(),
};

const bool packaged = winstore::running_as_packaged();
result.SetNamedValue(L"packaged", json::value(packaged));

bool startup{};
if (winstore::running_as_packaged())
{
using namespace localized_strings;
const auto task_state = winstore::get_startup_task_status_async().get();
switch (task_state)
{
case winstore::StartupTaskState::Disabled:
startup = false;
settings.isStartupEnabled = false;
break;
case winstore::StartupTaskState::Enabled:
startup = true;
settings.isStartupEnabled = true;
break;
case winstore::StartupTaskState::DisabledByPolicy:
result.SetNamedValue(L"startup_disabled_reason", json::value(STARTUP_DISABLED_BY_POLICY));
startup = false;
settings.startupDisabledReason = STARTUP_DISABLED_BY_POLICY;
settings.isStartupEnabled = false;
break;
case winstore::StartupTaskState::DisabledByUser:
result.SetNamedValue(L"startup_disabled_reason", json::value(STARTUP_DISABLED_BY_USER));
startup = false;
settings.startupDisabledReason = STARTUP_DISABLED_BY_USER;
settings.isStartupEnabled = false;
break;
}
}
else
{
startup = is_auto_start_task_active_for_this_user();
settings.isStartupEnabled = is_auto_start_task_active_for_this_user();
}
result.SetNamedValue(L"startup", json::value(startup));

json::JsonObject enabled;
for (auto& [name, powertoy] : modules())
{
enabled.SetNamedValue(name, json::value(powertoy.is_enabled()));
settings.isModulesEnabledMap[name] = powertoy.is_enabled();
}
result.SetNamedValue(L"enabled", std::move(enabled));

bool is_elevated = is_process_elevated();
result.SetNamedValue(L"is_elevated", json::value(is_elevated));
result.SetNamedValue(L"run_elevated", json::value(run_as_elevated));
result.SetNamedValue(L"theme", json::value(settings_theme));
result.SetNamedValue(L"system_theme", json::value(WindowsColors::is_dark_mode() ? L"dark" : L"light"));
result.SetNamedValue(L"powertoys_version", json::value(get_product_version()));
return result;
return settings;
}

json::JsonObject get_general_settings()
{
auto settings = get_settings();
return settings.to_json();
}

void apply_general_settings(const json::JsonObject& general_configs)
Expand Down Expand Up @@ -143,8 +172,10 @@ void apply_general_settings(const json::JsonObject& general_configs)
{
settings_theme = general_configs.GetNamedString(L"theme");
}
json::JsonObject save_settings = get_general_settings();
PTSettingsHelper::save_general_settings(save_settings);

GeneralSettings save_settings = get_settings();
PTSettingsHelper::save_general_settings(save_settings.to_json());
Trace::SettingsChanged(save_settings);
}

void start_initial_powertoys()
Expand Down
15 changes: 15 additions & 0 deletions src/runner/general_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

#include <common/json.h>

struct GeneralSettings
{
bool isPackaged;
bool isStartupEnabled;
std::wstring startupDisabledReason;
std::unordered_map<std::wstring, bool> isModulesEnabledMap;
bool isElevated;
bool isRunElevated;
std::wstring theme;
std::wstring systemTheme;
std::wstring powerToysVersion;

json::JsonObject to_json();
};

json::JsonObject load_general_settings();
json::JsonObject get_general_settings();
void apply_general_settings(const json::JsonObject& general_configs);
Expand Down
9 changes: 5 additions & 4 deletions src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void chdir_current_executable()
}
}

int runner()
int runner(bool isProcessElevated)
{
DPIAware::EnableDPIAwarenessForThisProcess();

Expand Down Expand Up @@ -79,7 +79,7 @@ int runner()
// Start initial powertoys
start_initial_powertoys();

Trace::EventLaunch(get_product_version());
Trace::EventLaunch(get_product_version(), isProcessElevated);

result = run_message_loop();
}
Expand Down Expand Up @@ -115,11 +115,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

auto general_settings = load_general_settings();
int rvalue = 0;
if (is_process_elevated() ||
bool isProcessElevated = is_process_elevated();
if (isProcessElevated ||
general_settings.GetNamedBoolean(L"run_elevated", false) == false ||
strcmp(lpCmdLine, "--dont-elevate") == 0)
{
result = runner();
result = runner(isProcessElevated);
}
else
{
Expand Down
34 changes: 33 additions & 1 deletion src/runner/trace.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pch.h"
#include "trace.h"

#include "general_settings.h"

TRACELOGGING_DEFINE_PROVIDER(
g_hProvider,
"Microsoft.PowerToys",
Expand All @@ -18,12 +20,42 @@ void Trace::UnregisterProvider()
TraceLoggingUnregister(g_hProvider);
}

void Trace::EventLaunch(const std::wstring& versionNumber)
void Trace::EventLaunch(const std::wstring& versionNumber, bool isProcessElevated)
{
TraceLoggingWrite(
g_hProvider,
"Runner_Launch",
TraceLoggingWideString(versionNumber.c_str(), "Version"),
TraceLoggingBoolean(isProcessElevated, "Elevated"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
}

void Trace::SettingsChanged(const GeneralSettings& settings)
{
std::wstring enabledModules;
for (const auto& [name, isEnabled] : settings.isModulesEnabledMap)
{
if (isEnabled)
{
if (!enabledModules.empty())
{
enabledModules += L", ";
}

enabledModules += name;
}
}

TraceLoggingWrite(
g_hProvider,
"GeneralSettingsChanged",
TraceLoggingBoolean(settings.isStartupEnabled, "RunAtStartup"),
TraceLoggingWideString(settings.startupDisabledReason.c_str(), "StartupDisabledReason"),
TraceLoggingWideString(enabledModules.c_str(), "ModulesEnabled"),
TraceLoggingBoolean(settings.isRunElevated, "AlwaysRunElevated"),
TraceLoggingWideString(settings.theme.c_str(), "Theme"),
ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance),
TraceLoggingBoolean(TRUE, "UTCReplace_AppSessionGuid"),
TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE));
Expand Down
5 changes: 4 additions & 1 deletion src/runner/trace.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

struct GeneralSettings;

class Trace
{
public:
static void RegisterProvider();
static void UnregisterProvider();
static void EventLaunch(const std::wstring& versionNumber);
static void EventLaunch(const std::wstring& versionNumber, bool isProcessElevated);
static void SettingsChanged(const GeneralSettings& settings);
};