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 IPFS updater version to brave://ipfs page #12159

Merged
merged 2 commits into from
Feb 7, 2022
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
1 change: 1 addition & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ source_set("ui") {
deps += [
"//brave/components/ipfs",
"//brave/components/ipfs_ui:generated_resources",
"//components/component_updater",
]
}

Expand Down
66 changes: 66 additions & 0 deletions browser/ui/webui/ipfs_dom_handler_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/webui/ipfs_ui.h"

#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"

class TestIPFSDomHandler : public IPFSDOMHandler {
public:
TestIPFSDomHandler() {
TestingProfile::Builder builder;
profile_ = builder.Build();
web_contents_ = content::WebContents::Create(
content::WebContents::CreateParams(profile_.get()));

test_web_ui_.set_web_contents(web_contents_.get());
set_web_ui(&test_web_ui_);
}

~TestIPFSDomHandler() override {
// The test handler unusually owns its own TestWebUI, so we make sure to
// unbind it from the base class before the derived class is destroyed.
set_web_ui(nullptr);
}
content::TestWebUI* web_ui() { return &test_web_ui_; }

private:
content::BrowserTaskEnvironment browser_task_environment;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<content::WebContents> web_contents_;
content::TestWebUI test_web_ui_;
};

TEST(TestIPFSDomHandler, AddComponentVersion) {
TestIPFSDomHandler handler;
ipfs::NodeInfo info;
info.id = "id1";
info.version = "version1";
std::string component = "1.0.11";
handler.SetIpfsClientUpdaterVersionForTesting(component);
handler.OnGetNodeInfo(true, info);
const auto& data = *handler.web_ui()->call_data()[0];
ASSERT_TRUE(data.arg1()->is_dict());
EXPECT_EQ(*data.arg1()->FindStringKey("id"), info.id);
EXPECT_EQ(*data.arg1()->FindStringKey("version"), info.version);
EXPECT_EQ(*data.arg1()->FindStringKey("component_version"), component);
}

TEST(TestIPFSDomHandler, ComponentNotRegistered) {
TestIPFSDomHandler handler;
ipfs::NodeInfo info;
info.id = "id1";
info.version = "version1";
handler.OnGetNodeInfo(true, info);
const auto& data = *handler.web_ui()->call_data()[0];
ASSERT_TRUE(data.arg1()->is_dict());
EXPECT_EQ(*data.arg1()->FindStringKey("id"), info.id);
EXPECT_EQ(*data.arg1()->FindStringKey("version"), info.version);
ASSERT_FALSE(data.arg1()->FindStringKey("component_version"));
}
31 changes: 28 additions & 3 deletions browser/ui/webui/ipfs_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/ipfs/addresses_config.h"
#include "brave/components/ipfs/brave_ipfs_client_updater.h"
#include "brave/components/ipfs/ipfs_service.h"
#include "brave/components/ipfs/repo_stats.h"
#include "brave/components/ipfs_ui/resources/grit/ipfs_generated_map.h"
#include "chrome/browser/browser_process_impl.h"
#include "components/component_updater/component_updater_service.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need a dependency added in browser/ui/BUILD.gn for components/component_updater

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#include "components/grit/brave_components_resources.h"
#include "components/grit/brave_components_strings.h"
#include "components/update_client/crx_update_item.h"
Expand All @@ -24,6 +26,20 @@

namespace {

std::string GetIPFSUpdaterVersion() {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
if (!cus)
return std::string();

for (const auto& component : cus->GetComponents()) {
if (component.id != ipfs::kIpfsClientComponentId)
continue;
return component.version.GetString();
}
return std::string();
}

void CallOnGetDaemonStatus(content::WebUI* web_ui, const std::string& error) {
ipfs::IpfsService* service = ipfs::IpfsServiceFactory::GetForContext(
web_ui->GetWebContents()->GetBrowserContext());
Expand Down Expand Up @@ -93,8 +109,9 @@ void IPFSDOMHandler::RegisterMessages() {

IPFSUI::IPFSUI(content::WebUI* web_ui, const std::string& name)
: WebUIController(web_ui) {
CreateAndAddWebUIDataSource(web_ui, name, kIpfsGenerated, kIpfsGeneratedSize,
IDR_IPFS_HTML);
auto* source = CreateAndAddWebUIDataSource(web_ui, name, kIpfsGenerated,
kIpfsGeneratedSize, IDR_IPFS_HTML);
source->AddString("componentVersion", ipfs::kIpfsClientComponentName);
web_ui->AddMessageHandler(std::make_unique<IPFSDOMHandler>());
}

Expand Down Expand Up @@ -323,6 +340,11 @@ void IPFSDOMHandler::OnGarbageCollection(bool success,
web_ui()->CallJavascriptFunctionUnsafe("ipfs.onGarbageCollection",
std::move(result));
}
std::string IPFSDOMHandler::GetIpfsClientUpdaterVersion() const {
if (client_updater_version_for_testing_)
return client_updater_version_for_testing_.value();
return GetIPFSUpdaterVersion();
}

void IPFSDOMHandler::OnGetNodeInfo(bool success, const ipfs::NodeInfo& info) {
if (!web_ui()->CanCallJavascript())
Expand All @@ -331,7 +353,10 @@ void IPFSDOMHandler::OnGetNodeInfo(bool success, const ipfs::NodeInfo& info) {
base::Value node_value(base::Value::Type::DICTIONARY);
node_value.SetStringKey("id", info.id);
node_value.SetStringKey("version", info.version);

auto extension_version = GetIpfsClientUpdaterVersion();
if (!extension_version.empty()) {
node_value.SetStringKey("component_version", extension_version);
}
web_ui()->CallJavascriptFunctionUnsafe("ipfs.onGetNodeInfo",
std::move(node_value));
}
10 changes: 10 additions & 0 deletions browser/ui/webui/ipfs_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct RepoStats;
struct NodeInfo;
} // namespace ipfs

class TestIPFSDomHandler;

class IPFSDOMHandler : public content::WebUIMessageHandler,
public ipfs::IpfsServiceObserver {
public:
Expand All @@ -41,6 +43,8 @@ class IPFSDOMHandler : public content::WebUIMessageHandler,
void OnInstallationEvent(ipfs::ComponentUpdaterEvents event) override;

private:
FRIEND_TEST_ALL_PREFIXES(TestIPFSDomHandler, AddComponentVersion);
FRIEND_TEST_ALL_PREFIXES(TestIPFSDomHandler, ComponentNotRegistered);
void HandleGetConnectedPeers(base::Value::ConstListView args);
void HandleGetAddressesConfig(base::Value::ConstListView args);
void OnGetAddressesConfig(bool success,
Expand All @@ -60,6 +64,12 @@ class IPFSDOMHandler : public content::WebUIMessageHandler,
void HandleGarbageCollection(base::Value::ConstListView args);
void OnGarbageCollection(bool success, const std::string& error);

void SetIpfsClientUpdaterVersionForTesting(const std::string& version) {
client_updater_version_for_testing_ = version;
}
std::string GetIpfsClientUpdaterVersion() const;

absl::optional<std::string> client_updater_version_for_testing_;
base::ScopedObservation<ipfs::IpfsService, ipfs::IpfsServiceObserver>
service_observer_{this};
base::WeakPtrFactory<IPFSDOMHandler> weak_ptr_factory_;
Expand Down
1 change: 1 addition & 0 deletions components/definitions/ipfs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ declare namespace IPFS {
export interface NodeInfo {
id: string
version: string
component_version: string
}

export interface InstallationProgress {
Expand Down
4 changes: 4 additions & 0 deletions components/ipfs_ui/components/nodeInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class NodeInfo extends React.Component<Props, {}> {
<div>
{getLocale('version')}: {this.props.nodeInfo.version}
</div>
{this.props.nodeInfo.component_version &&
<div>
{getLocale('componentVersion')}: {this.props.nodeInfo.component_version}
</div>}
</Section>
)
}
Expand Down
3 changes: 2 additions & 1 deletion components/ipfs_ui/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export const defaultState: IPFS.State = {
},
nodeInfo: {
id: '',
version: ''
version: '',
component_version: ''
},
installationProgress: {
total_bytes: -1,
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ test("brave_unit_tests") {
"//brave/browser/ui/toolbar/brave_location_bar_model_delegate_unittest.cc",
"//brave/browser/ui/views/accelerator_table_unittest.cc",
"//brave/browser/ui/webui/brave_wallet/wallet_common_ui_unittest.cc",
"//brave/browser/ui/webui/ipfs_dom_handler_unittest.cc",
"//brave/browser/ui/webui/settings/brave_wallet_handler_unittest.cc",
"//brave/chromium_src/chrome/browser/devtools/url_constants_unittest.cc",
"//brave/chromium_src/chrome/browser/profiles/profile_avatar_icon_util_unittest.cc",
Expand Down