Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, macos-latest, windows-2022]
node-version: [22.x]
steps:
- name: Checkout
Expand Down
6 changes: 4 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"src/macos/PolicyWatcher.cc",
"src/macos/StringPolicy.cc",
"src/macos/NumberPolicy.cc",
"src/macos/BooleanPolicy.cc"
"src/macos/BooleanPolicy.cc",
"src/macos/UnionPolicy.cc"
],
"defines": [
"MACOS",
Expand Down Expand Up @@ -48,7 +49,8 @@
"src/windows/PolicyWatcher.cc",
"src/windows/StringPolicy.cc",
"src/windows/NumberPolicy.cc",
"src/windows/BooleanPolicy.cc"
"src/windows/BooleanPolicy.cc",
"src/windows/UnionPolicy.cc"
],
"defines": [
"WINDOWS"
Expand Down
20 changes: 12 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ interface Watcher {
type StringPolicy = { type: "string" };
type NumberPolicy = { type: "number" };
type BooleanPolicy = { type: "boolean" };
type PolicyType = "string" | "number" | "boolean";
type UnionPolicy = { type: readonly [PolicyType, ...PolicyType[]] };
Comment thread
rzhao271 marked this conversation as resolved.

export interface Policies {
[policyName: string]: StringPolicy | NumberPolicy | BooleanPolicy;
[policyName: string]: StringPolicy | NumberPolicy | BooleanPolicy | UnionPolicy;
}

export interface WatcherOptions {
Expand All @@ -23,15 +25,17 @@ export interface WatcherOptions {
export type PolicyUpdate<T extends Policies> = {
[K in keyof T]:
| undefined
| (T[K] extends StringPolicy
? string
: (T[K] extends BooleanPolicy
? boolean
: T[K] extends NumberPolicy
? number
: never));
| (T[K]["type"] extends readonly PolicyType[]
Comment thread
rzhao271 marked this conversation as resolved.
? PolicyTypeValue<T[K]["type"][number]>
: PolicyTypeValue<T[K]["type"]>);
};

type PolicyTypeValue<T extends PolicyType> =
T extends "string" ? string :
T extends "boolean" ? boolean :
T extends "number" ? number :
never;

export function createWatcher<T extends Policies>(
productName: string,
policies: T,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vscode/policy-watcher",
"version": "1.4.0",
"version": "1.5.0",
"description": "",
"main": "index.js",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions src/PolicyWatcher.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public:
void AddStringPolicy(const std::string name);
void AddNumberPolicy(const std::string name);
void AddBooleanPolicy(const std::string name);
void AddUnionPolicy(const std::string name, const std::vector<std::string> &types);

void OnExecute(Napi::Env env);
void Execute(const ExecutionProgress &progress);
Expand Down
1 change: 1 addition & 0 deletions src/linux/PolicyWatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PolicyWatcher::~PolicyWatcher()
void PolicyWatcher::AddStringPolicy(const std::string name) {}
void PolicyWatcher::AddNumberPolicy(const std::string name) {}
void PolicyWatcher::AddBooleanPolicy(const std::string name) {}
void PolicyWatcher::AddUnionPolicy(const std::string name, const std::vector<std::string> &types) {}
void PolicyWatcher::OnExecute(Napi::Env env) {}
void PolicyWatcher::Execute(const ExecutionProgress &progress) {}
void PolicyWatcher::OnProgress(const Policy *const *policies, size_t count) {}
Expand Down
6 changes: 6 additions & 0 deletions src/macos/PolicyWatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "StringPolicy.hh"
#include "NumberPolicy.hh"
#include "BooleanPolicy.hh"
#include "UnionPolicy.hh"
#include <thread>

using namespace Napi;
Expand Down Expand Up @@ -57,6 +58,11 @@ void PolicyWatcher::AddBooleanPolicy(const std::string name)
policies.push_back(std::make_unique<BooleanPolicy>(name, productName));
}

void PolicyWatcher::AddUnionPolicy(const std::string name, const std::vector<std::string> &types)
{
policies.push_back(std::make_unique<UnionPolicy>(name, productName, types));
}

void PolicyWatcher::OnExecute(Napi::Env env)
{
AsyncProgressQueueWorker::OnExecute(env);
Expand Down
9 changes: 9 additions & 0 deletions src/macos/PreferencesPolicy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public:

PolicyRefreshResult refresh()
{
if (!CFPreferencesAppValueIsForced(key, appID))
{
if (!value.has_value())
return PolicyRefreshResult::NotSet;

value.reset();
return PolicyRefreshResult::Removed;
}

auto newValue = read();

// Check for no value or removal
Expand Down
73 changes: 73 additions & 0 deletions src/macos/UnionPolicy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

#include "UnionPolicy.hh"
#include <CoreFoundation/CoreFoundation.h>

UnionPolicy::UnionPolicy(const std::string name, const std::string &productName, const std::vector<std::string> &types)
: Policy(name),
appID(CFStringCreateWithCString(nullptr, productName.c_str(), kCFStringEncodingUTF8)),
key(CFStringCreateWithCString(nullptr, name.c_str(), kCFStringEncodingUTF8))
{
for (const auto &type : types) {
acceptsBoolean = acceptsBoolean || type == "boolean";
acceptsNumber = acceptsNumber || type == "number";
acceptsString = acceptsString || type == "string";
}
}

UnionPolicy::~UnionPolicy()
{
CFRelease(appID);
CFRelease(key);
}

PolicyRefreshResult UnionPolicy::refresh()
{
auto next = read();
if (value == next)
return value.has_value() ? PolicyRefreshResult::Unchanged : PolicyRefreshResult::NotSet;
auto removed = value.has_value() && !next.has_value();
value = next;
return removed ? PolicyRefreshResult::Removed : PolicyRefreshResult::Updated;
}

Napi::Value UnionPolicy::getValue(Napi::Env env) const
{
if (!value.has_value())
return env.Undefined();
if (std::holds_alternative<bool>(*value))
return Napi::Boolean::New(env, std::get<bool>(*value));
if (std::holds_alternative<double>(*value))
return Napi::Number::New(env, std::get<double>(*value));
return Napi::String::New(env, std::get<std::string>(*value));
}

std::optional<UnionPolicyValue> UnionPolicy::read() const
{
if (!CFPreferencesAppValueIsForced(key, appID))
return std::nullopt;
auto pref = CFPreferencesCopyAppValue(key, appID);
if (pref == nullptr)
return std::nullopt;

std::optional<UnionPolicyValue> result;
auto type = CFGetTypeID(pref);
if (type == CFBooleanGetTypeID() && acceptsBoolean) {
result = pref == kCFBooleanTrue;
} else if (type == CFNumberGetTypeID() && acceptsNumber) {
long long number;
if (CFNumberGetValue(static_cast<CFNumberRef>(pref), kCFNumberLongLongType, &number))
result = static_cast<double>(number);
} else if (type == CFStringGetTypeID() && acceptsString) {
CFIndex length = CFStringGetLength(static_cast<CFStringRef>(pref));
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
std::vector<char> buffer(maxSize);
if (CFStringGetCString(static_cast<CFStringRef>(pref), buffer.data(), maxSize, kCFStringEncodingUTF8))
result = std::string(buffer.data());
}
CFRelease(pref);
return result;
}
36 changes: 36 additions & 0 deletions src/macos/UnionPolicy.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

#ifndef UNION_POLICY_H
#define UNION_POLICY_H

#include <optional>
#include <string>
#include <variant>
#include <vector>
#include <CoreFoundation/CoreFoundation.h>
#include "../Policy.hh"

using UnionPolicyValue = std::variant<bool, double, std::string>;

class UnionPolicy : public Policy
{
public:
UnionPolicy(const std::string name, const std::string &productName, const std::vector<std::string> &types);
~UnionPolicy();
PolicyRefreshResult refresh();
Napi::Value getValue(Napi::Env env) const;

private:
std::optional<UnionPolicyValue> read() const;
CFStringRef appID;
CFStringRef key;
bool acceptsBoolean = false;
bool acceptsNumber = false;
bool acceptsString = false;
std::optional<UnionPolicyValue> value;
};

#endif
42 changes: 30 additions & 12 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

#include <napi.h>
#include <algorithm>
#include <vector>

#include "Policy.hh"
Expand Down Expand Up @@ -67,21 +68,38 @@ Value CreateWatcher(const CallbackInfo &info)

auto rawPolicy = rawPolicyValue.As<Object>();
auto rawPolicyType = rawPolicy.Get("type");
std::vector<std::string> policyTypes;
if (rawPolicyType.IsString()) {
policyTypes.push_back(std::string(rawPolicyType.As<String>()));
} else if (rawPolicyType.IsArray()) {
auto rawPolicyTypes = rawPolicyType.As<Array>();
if (rawPolicyTypes.Length() == 0)
throw TypeError::New(env, "Expected policy type array to be non-empty");
for (uint32_t i = 0; i < rawPolicyTypes.Length(); i++) {
auto rawType = rawPolicyTypes.Get(i);
if (!rawType.IsString())
throw TypeError::New(env, "Expected policy type array entries to be strings");
auto type = std::string(rawType.As<String>());
if (std::find(policyTypes.begin(), policyTypes.end(), type) == policyTypes.end())
policyTypes.push_back(type);
}
} else {
throw TypeError::New(env, "Expected policy type to be a string or non-empty string array");
}

if (!rawPolicyType.IsString())
throw TypeError::New(env, "Expected policy type to be string");

auto policyType = std::string(rawPolicyType.As<String>());

if (policyType == "string") {
watcher->AddStringPolicy(rawPolicyName.As<String>());
for (const auto &policyType : policyTypes) {
if (policyType != "string" && policyType != "number" && policyType != "boolean")
throw TypeError::New(env, "Unknown policy type '" + policyType + "'");
}
else if (policyType == "number") {
watcher->AddNumberPolicy(rawPolicyName.As<String>());
} else if (policyType == "boolean") {
watcher->AddBooleanPolicy(rawPolicyName.As<String>());

if (policyTypes.size() > 1) {
watcher->AddUnionPolicy(rawPolicyName.As<String>(), policyTypes);
} else if (policyTypes[0] == "string") {
watcher->AddStringPolicy(rawPolicyName.As<String>());
} else if (policyTypes[0] == "number") {
watcher->AddNumberPolicy(rawPolicyName.As<String>());
} else {
throw TypeError::New(env, "Unknown policy type '" + policyType + "'");
watcher->AddBooleanPolicy(rawPolicyName.As<String>());
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/windows/PolicyWatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "StringPolicy.hh"
#include "NumberPolicy.hh"
#include "BooleanPolicy.hh"
#include "UnionPolicy.hh"

using namespace Napi;

Expand Down Expand Up @@ -43,6 +44,11 @@ void PolicyWatcher::AddBooleanPolicy(const std::string name)
policies.push_back(std::make_unique<BooleanPolicy>(name, productName, registryPath));
}

void PolicyWatcher::AddUnionPolicy(const std::string name, const std::vector<std::string> &types)
{
policies.push_back(std::make_unique<UnionPolicy>(name, productName, registryPath, types));
}

void PolicyWatcher::OnExecute(Napi::Env env)
{
if ((handles[0] = CreateEvent(NULL, false, false, NULL)) == NULL)
Expand Down
Loading
Loading