Skip to content

Commit

Permalink
[AI Chat] WebManifest for PWA install
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed Dec 16, 2024
1 parent 7d3a262 commit 203dd46
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 1 deletion.
2 changes: 2 additions & 0 deletions browser/ui/webui/ai_chat/ai_chat_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ AIChatUI::AIChatUI(content::WebUI* web_ui)
webui::SetupWebUIDataSource(source, kAiChatUiGenerated, IDR_AI_CHAT_UI_HTML);

source->AddResourcePath("styles.css", IDR_AI_CHAT_UI_CSS);
source->AddResourcePath("manifest.webmanifest", IDR_AI_CHAT_UI_MANIFEST);
source->AddResourcePath("pwa_icon.svg", IDR_AI_CHAT_UI_PWA_ICON);

for (const auto& str : ai_chat::GetLocalizedStrings()) {
source->AddString(str.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

#include "src/chrome/browser/banners/app_banner_manager_desktop_browsertest.cc"

#include "brave/components/constants/webui_url_constants.h"
namespace webapps {

class AppBannerManagerDesktopBrowserTestForBraveAllowedPage
: public AppBannerManagerDesktopBrowserTest {
public:
void SetUp() override {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{}, GetDisabledFeatures());
TestAppBannerManagerDesktop::SetUp();
AppBannerManagerBrowserTestBase::SetUp();
}
};

IN_PROC_BROWSER_TEST_F(AppBannerManagerDesktopBrowserTestForBraveAllowedPage,
WebUiAIChatApp) {
TestAppBannerManagerDesktop* manager =
TestAppBannerManagerDesktop::FromWebContents(
browser()->tab_strip_model()->GetActiveWebContents());

// Simulate loading an AI Chat page
{
base::RunLoop run_loop;
manager->PrepareDone(run_loop.QuitClosure());

ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL(base::StrCat({"chrome://", kAIChatUIHost}))));
run_loop.Run();
}

EXPECT_EQ(InstallableWebAppCheckResult::kYes_Promotable,
manager->GetInstallableWebAppCheckResult());
}

} // namespace webapps
23 changes: 23 additions & 0 deletions chromium_src/chrome/browser/web_applications/web_app_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

#include "base/containers/contains.h"
#include "brave/components/constants/webui_url_constants.h"

// Make sure IsValidWebAppUrl also checks for allowed Brave WebUI hosts
#define IsValidWebAppUrl IsValidWebAppUrl_ChromiumImpl

#include "src/chrome/browser/web_applications/web_app_helpers.cc"
#undef IsValidWebAppUrl

namespace web_app {

bool IsValidWebAppUrl(const GURL& app_url) {
return IsValidWebAppUrl_ChromiumImpl(app_url) ||
(app_url.SchemeIs(content::kChromeUIScheme) &&
base::Contains(kInstallablePWAWebUIHosts, app_url.host()));
}

} // namespace web_app
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

#include "src/chrome/browser/web_applications/web_app_helpers_unittest.cc"

namespace web_app {

TEST(WebAppHelpers, Brave_IsValidWebAppUrl) {
EXPECT_TRUE(IsValidWebAppUrl(GURL("chrome://leo-ai")));
}

} // namespace web_app
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

#include "base/containers/contains.h"
#include "brave/components/constants/webui_url_constants.h"

// Include password_manager constants before override so we don't override the
// definition.
#include "components/password_manager/content/common/web_ui_constants.h"

// Add some extra items to WebUI hosts considered valid for PWAs
#define kChromeUIPasswordManagerHost \
kChromeUIPasswordManagerHost && \
!base::Contains(kInstallablePWAWebUIHosts, url.host())

#include "src/components/webapps/browser/banners/app_banner_manager.cc"
#undef kChromeUIPasswordManagerHost
2 changes: 2 additions & 0 deletions components/ai_chat/resources/ai_chat_ui_resources.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
<include name="IDR_AI_CHAT_UNTRUSTED_CONVERSATION_UI_HTML"
file="../ai_chat/resources/untrusted_conversation_frame/untrusted_conversation_frame.html"
type="BINDATA" />
<include name="IDR_AI_CHAT_UI_MANIFEST" file="../ai_chat/resources/page/manifest.webmanifest" type="BINDATA" />
<include name="IDR_AI_CHAT_UI_PWA_ICON" file="../ai_chat/resources/page/pwa_icon.svg" type="BINDATA" />
</grit-part>
1 change: 1 addition & 0 deletions components/ai_chat/resources/page/ai_chat_ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<script src="/strings.js"></script>
<script src="chat_ui.bundle.js" type="module"></script>
<link rel="icon" href="//resources/brave-icons/social-leo-favicon-fullheight-color.svg">
<link rel="manifest" href="manifest.webmanifest">
<style>
@media (prefers-color-scheme: dark) {
body {
Expand Down
15 changes: 15 additions & 0 deletions components/ai_chat/resources/page/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "$i18n{siteTitle}",
"name": "$i18n{siteTitle}",
"icons": [
{
"src": "/pwa_icon.svg",
"type": "image/svg+xml",
"sizes": "any"
}
],
"start_url": "/?source=pwa",
"id": "chrome://leo-ai/",
"display": "standalone",
"scope": "chrome://leo-ai/"
}
23 changes: 23 additions & 0 deletions components/ai_chat/resources/page/pwa_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions components/constants/webui_url_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#ifndef BRAVE_COMPONENTS_CONSTANTS_WEBUI_URL_CONSTANTS_H_
#define BRAVE_COMPONENTS_CONSTANTS_WEBUI_URL_CONSTANTS_H_

#include <string_view>

#include "base/containers/fixed_flat_set.h"
#include "build/build_config.h"

inline constexpr char kAdblockHost[] = "adblock";
Expand Down Expand Up @@ -92,4 +95,12 @@ inline constexpr char kRewriterUIHost[] = "rewriter";
inline constexpr char16_t kTransactionSimulationLearnMoreURL[] =
u"https://github.com/brave/brave-browser/wiki/Transaction-Simulation";

// Hosts that are allowed to be installed as PWAs, which is usually
// a blocked action for WebUIs. In Chromium, the "password-manager" host
// is already allowed.
inline constexpr auto kInstallablePWAWebUIHosts =
base::MakeFixedFlatSet<std::string_view>({
kAIChatUIHost,
});

#endif // BRAVE_COMPONENTS_CONSTANTS_WEBUI_URL_CONSTANTS_H_
2 changes: 1 addition & 1 deletion components/resources/ai_chat_ui_strings.grdp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<grit-part>
<message name="IDS_CHAT_UI_TITLE" translateable="false" desc="Title for the Brave AI product">
Leo
Leo AI
</message>
<message name="IDS_CHAT_UI_SUMMARIZE_FAILED_LABEL" desc="Label for when summarization is not possible">
The summarizer feature is currently available only for select articles and other long-form web pages.
Expand Down

0 comments on commit 203dd46

Please sign in to comment.