Skip to content

[iOS] - Add Skus-Internals WebUI - 1.65.x #22917

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

Merged
merged 1 commit into from
Apr 5, 2024
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
38 changes: 24 additions & 14 deletions browser/ui/webui/skus_internals_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/skus/skus_service_factory.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/ai_chat/core/common/buildflags/buildflags.h"
#include "brave/components/brave_vpn/common/buildflags/buildflags.h"
#include "brave/components/skus/browser/pref_names.h"
#include "brave/components/skus/browser/resources/grit/skus_internals_generated_map.h"
Expand Down Expand Up @@ -94,24 +95,34 @@ void SkusInternalsUI::GetVpnState(GetVpnStateCallback callback) {
#if !BUILDFLAG(IS_ANDROID)
dict.Set("Last connection error", GetLastVPNConnectionError());
#endif
dict.Set("Order", GetVPNOrderInfo());
auto* profile = Profile::FromWebUI(web_ui());
if (!brave_vpn::IsBraveVPNEnabled(profile->GetPrefs())) {
dict.Set("Order", base::Value::Dict());
} else {
auto order_info = GetOrderInfo("vpn.");
order_info.Set(
"env", local_state_->GetString(brave_vpn::prefs::kBraveVPNEnvironment));
dict.Set("Order", std::move(order_info));
}
#endif
std::string result;
base::JSONWriter::Write(dict, &result);
std::move(callback).Run(result);
}

base::Value::Dict SkusInternalsUI::GetVPNOrderInfo() const {
void SkusInternalsUI::GetLeoState(GetLeoStateCallback callback) {
base::Value::Dict dict;
#if BUILDFLAG(ENABLE_BRAVE_VPN)
auto* profile = Profile::FromWebUI(web_ui());
if (!brave_vpn::IsBraveVPNEnabled(profile->GetPrefs())) {
return dict;
}

dict.Set("env",
local_state_->GetString(brave_vpn::prefs::kBraveVPNEnvironment));
#if BUILDFLAG(ENABLE_AI_CHAT)
dict.Set("Order", GetOrderInfo("leo."));
#endif
std::string result;
base::JSONWriter::Write(dict, &result);
std::move(callback).Run(result);
}

base::Value::Dict SkusInternalsUI::GetOrderInfo(
const std::string& location) const {
base::Value::Dict dict;
const auto& skus_state = local_state_->GetDict(skus::prefs::kSkusState);
for (const auto kv : skus_state) {
if (!base::StartsWith(kv.first, "skus:")) {
Expand Down Expand Up @@ -140,11 +151,11 @@ base::Value::Dict SkusInternalsUI::GetVPNOrderInfo() const {
continue;
}

if (auto* location = order_dict->FindString("location")) {
if (!base::StartsWith(*location, "vpn.")) {
if (auto* order_location = order_dict->FindString("location")) {
if (!base::StartsWith(*order_location, location)) {
continue;
}
order_dict_output.Set("location", *location);
order_dict_output.Set("location", *order_location);
}

if (auto* id = order_dict->FindString("id")) {
Expand All @@ -157,7 +168,6 @@ base::Value::Dict SkusInternalsUI::GetVPNOrderInfo() const {
// Set output with env like {skus:production: {...}}.
dict.Set(kv.first, std::move(order_dict_output));
}
#endif
return dict;
}

Expand Down
3 changes: 2 additions & 1 deletion browser/ui/webui/skus_internals_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SkusInternalsUI : public content::WebUIController,
void GetEventLog(GetEventLogCallback callback) override;
void GetSkusState(GetSkusStateCallback callback) override;
void GetVpnState(GetVpnStateCallback callback) override;
void GetLeoState(GetLeoStateCallback callback) override;
void ResetSkusState() override;
void CopySkusStateToClipboard() override;
void DownloadSkusState() override;
Expand All @@ -51,7 +52,7 @@ class SkusInternalsUI : public content::WebUIController,
void FileSelectionCanceled(void* params) override;

std::string GetLastVPNConnectionError() const;
base::Value::Dict GetVPNOrderInfo() const;
base::Value::Dict GetOrderInfo(const std::string& location) const;
std::string GetSkusStateAsString() const;

void EnsureMojoConnected();
Expand Down
12 changes: 8 additions & 4 deletions components/resources/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,26 @@ brave_grit("static_resources") {
}

repack("resources") {
deps = [ ":static_resources" ]
sources = [ "$root_gen_dir/components/brave_components_static.pak" ]
deps = [
":static_resources",
"//brave/components/skus/browser/resources:generated_resources",
]
sources = [
"$root_gen_dir/brave/components/skus/browser/resources/skus_internals_generated.pak",
"$root_gen_dir/components/brave_components_static.pak",
]

if (!is_ios) {
deps += [
"//brave/components/brave_adblock_ui:generated_resources",
"//brave/components/brave_adblock_ui/adblock_internals:generated_resources",
"//brave/components/cosmetic_filters/resources/data:generated_resources",
"//brave/components/skus/browser/resources:generated_resources",
]

sources += [
"$root_gen_dir/brave/components/brave_adblock/adblock_internals/resources/brave_adblock_internals_generated.pak",
"$root_gen_dir/brave/components/brave_adblock/resources/brave_adblock_generated.pak",
"$root_gen_dir/brave/components/cosmetic_filters/resources/cosmetic_filters_generated.pak",
"$root_gen_dir/brave/components/skus/browser/resources/skus_internals_generated.pak",
]
}

Expand Down
5 changes: 5 additions & 0 deletions components/skus/browser/resources/skus_internals.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<meta name="viewport" content="width=device-width">
<title>SKUs internals</title>
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<if expr="is_ios">
<!-- TODO(crbug.com/487000): Remove this once injected by web. -->
<script src="chrome://resources/js/ios/web_ui.js"></script>
<script src="chrome://resources/js/ios/mojo_api.js"></script>
</if>
<script src="/skus_internals.bundle.js"></script>
<style>
body {
Expand Down
14 changes: 14 additions & 0 deletions components/skus/browser/resources/skus_internals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ const defaultState: CreateOrderFromReceiptInputFields = {
function App() {
const [skusState, setSkusState] = React.useState({})
const [vpnState, setVpnState] = React.useState({})
const [leoState, setLeoState] = React.useState({})
const [createOrderFormData, setCreateOrderFormData] = React.useState<CreateOrderFromReceiptInputFields>(defaultState)

const resetSkusState = () => {
API.resetSkusState()
setSkusState({})
setVpnState({})
setLeoState({})
}

const getSkusState = () => {
Expand All @@ -64,6 +66,12 @@ function App() {
})
}

const getLeoState = () => {
API.getLeoState().then((r: any) => {
setLeoState(JSON.parse(r.response))
})
}

const submitCreateOrderFromReceipt = () => {
API.createOrderFromReceipt(
createOrderFormData.domain, createOrderFormData.receipt).then((r: any) =>
Expand Down Expand Up @@ -101,6 +109,7 @@ function App() {
Reset SKUs state
</button>
<button onClick={getVpnState}>Fetch VPN state</button>
<button onClick={getLeoState}>Fetch Leo state</button>
<button onClick={getSkusState}>Fetch SKUs state</button>
</ButtonContainer>

Expand All @@ -109,6 +118,11 @@ function App() {
<JsonView data={vpnState} shouldInitiallyExpand={(level) => true} />
</StateContainer>

<StateContainer>
<b>Leo State:</b>
<JsonView data={leoState} shouldInitiallyExpand={(level) => true} />
</StateContainer>

<StateContainer>
<b>SKUs State:</b>
<button onClick={() => API.copySkusStateToClipboard()}>Copy</button>
Expand Down
1 change: 1 addition & 0 deletions components/skus/common/skus_internals.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface SkusInternals {
GetEventLog() => (string response);
GetSkusState() => (string response);
GetVpnState() => (string response);
GetLeoState() => (string response);
CopySkusStateToClipboard();
DownloadSkusState();
ResetSkusState();
Expand Down
Loading