Skip to content

Commit

Permalink
Add Key Event handler and factory for sending key events to be handle…
Browse files Browse the repository at this point in the history
…d in background as part of Apps Platform Infra sample app demo

Bug: b/300341795
Change-Id: I33389edf9ad8c22218e5fab08fbca4ba001203d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4858098
Reviewed-by: Glen Robertson <glenrob@chromium.org>
Commit-Queue: Clement Yan <clamclamyan@google.com>
Reviewed-by: Stefan Kuhne <skuhne@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1212522}
  • Loading branch information
Clement Yan authored and Chromium LUCI CQ committed Oct 20, 2023
1 parent e1bfd70 commit 48912fa
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5811,6 +5811,7 @@ static_library("browser") {
"//ash/webui/projector_app/public/cpp",
"//ash/webui/settings/public/constants",
"//chrome/browser/chromeos",
"//chrome/browser/chromeos/cros_apps",
"//chrome/browser/chromeos/drivefs",
"//chrome/browser/chromeos/extensions/telemetry",
"//chrome/browser/policy:onc",
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4463,6 +4463,11 @@ const FeatureEntry kFeatureEntries[] = {
#endif // BUILDFLAG(IS_CHROMEOS_ASH)

#if BUILDFLAG(IS_CHROMEOS)
{"cros-apps-background-event-handling",
flag_descriptions::kCrosAppsBackgroundEventHandlingName,
flag_descriptions::kCrosAppsBackgroundEventHandlingDescription,
kOsCrOS | kOsLacros,
FEATURE_VALUE_TYPE(chromeos::features::kCrosAppsBackgroundEventHandling)},
{"cros-legacy-media-formats",
flag_descriptions::kCrOSLegacyMediaFormatsName,
flag_descriptions::kCrOSLegacyMediaFormatsDescription, kOsCrOS | kOsLacros,
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/chromeos/cros_apps/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ assert(is_chromeos)

source_set("cros_apps") {
sources = [
"cros_apps_key_event_handler.cc",
"cros_apps_key_event_handler.h",
"cros_apps_key_event_handler_factory.cc",
"cros_apps_key_event_handler_factory.h",
"cros_apps_tab_helper.cc",
"cros_apps_tab_helper.h",
]

deps = [
"//chrome/browser/profiles:profile",
"//chromeos/constants:constants",
"//components/keyed_service/content",
"//content/public/browser",
"//content/public/common",
]
Expand Down
45 changes: 45 additions & 0 deletions chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler.h"

#include "base/functional/callback_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "third_party/blink/public/common/messaging/string_message_codec.h"
#include "third_party/blink/public/common/messaging/transferable_message.h"
#include "third_party/blink/public/common/messaging/web_message_port.h"

CrosAppsKeyEventHandler::CrosAppsKeyEventHandler(Profile* profile)
: profile_(*profile) {
aura::Env::GetInstance()->AddPreTargetHandler(
this, ui::EventTarget::Priority::kAccessibility);
}

CrosAppsKeyEventHandler::~CrosAppsKeyEventHandler() {
aura::Env::GetInstance()->RemovePreTargetHandler(this);
}

void CrosAppsKeyEventHandler::OnKeyEvent(ui::KeyEvent* event) {
if (event->type() != ui::EventType::ET_KEY_PRESSED) {
return;
}

// Hard-coded url for demo app scope.
const GURL url = GURL(
"isolated-app://"
"uwsszrmaowqmxw4f262x5jozzhe5bc4tefqfa5lado674o462aoaaaic/");

blink::TransferableMessage msg =
blink::EncodeWebMessagePayload(base::UTF8ToUTF16(event->GetCodeString()));
msg.sender_agent_cluster_id =
blink::WebMessagePort::GetEmbedderAgentClusterID();
profile_->GetStoragePartitionForUrl(url)
->GetServiceWorkerContext()
->StartServiceWorkerAndDispatchMessage(
url, blink::StorageKey::CreateFirstParty(url::Origin::Create(url)),
std::move(msg), base::DoNothing());
}
30 changes: 30 additions & 0 deletions chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_CROS_APPS_CROS_APPS_KEY_EVENT_HANDLER_H_
#define CHROME_BROWSER_CHROMEOS_CROS_APPS_CROS_APPS_KEY_EVENT_HANDLER_H_

#include "base/memory/raw_ref.h"
#include "components/keyed_service/core/keyed_service.h"
#include "ui/aura/env.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"

class Profile;
class CrosAppsKeyEventHandler : public KeyedService, public ui::EventHandler {
public:
explicit CrosAppsKeyEventHandler(Profile* profile);
CrosAppsKeyEventHandler(const CrosAppsKeyEventHandler&) = delete;
CrosAppsKeyEventHandler& operator=(const CrosAppsKeyEventHandler&) = delete;
~CrosAppsKeyEventHandler() override;

// ui::EventHandler
void OnKeyEvent(ui::KeyEvent* event) override;

private:
// This class is a BrowserContextKeyedService, so it's owned by Profile.
const raw_ref<Profile> profile_;
};

#endif // CHROME_BROWSER_CHROMEOS_CROS_APPS_CROS_APPS_KEY_EVENT_HANDLER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler_factory.h"

#include "base/no_destructor.h"
#include "chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/constants/chromeos_features.h"

// static
CrosAppsKeyEventHandlerFactory& CrosAppsKeyEventHandlerFactory::GetInstance() {
static base::NoDestructor<CrosAppsKeyEventHandlerFactory> instance;
return *instance;
}

CrosAppsKeyEventHandlerFactory::CrosAppsKeyEventHandlerFactory()
: ProfileKeyedServiceFactory("CrosAppsKeyEventHandlerFactory") {}

CrosAppsKeyEventHandlerFactory::~CrosAppsKeyEventHandlerFactory() = default;

std::unique_ptr<KeyedService>
CrosAppsKeyEventHandlerFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
CHECK(base::FeatureList::IsEnabled(
chromeos::features::kCrosAppsBackgroundEventHandling));
return std::make_unique<CrosAppsKeyEventHandler>(
Profile::FromBrowserContext(context));
}

bool CrosAppsKeyEventHandlerFactory::ServiceIsCreatedWithBrowserContext()
const {
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_CROS_APPS_CROS_APPS_KEY_EVENT_HANDLER_FACTORY_H_
#define CHROME_BROWSER_CHROMEOS_CROS_APPS_CROS_APPS_KEY_EVENT_HANDLER_FACTORY_H_

#include "base/no_destructor.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"

// Class to retrieve the KeyEventHandler associated with a profile.
class CrosAppsKeyEventHandlerFactory : public ProfileKeyedServiceFactory {
public:
static CrosAppsKeyEventHandlerFactory& GetInstance();

private:
friend base::NoDestructor<CrosAppsKeyEventHandlerFactory>;

CrosAppsKeyEventHandlerFactory();
~CrosAppsKeyEventHandlerFactory() override;

// BrowserContextKeyedServiceFactory:
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override;
};

#endif // CHROME_BROWSER_CHROMEOS_CROS_APPS_CROS_APPS_KEY_EVENT_HANDLER_FACTORY_H_
9 changes: 9 additions & 0 deletions chrome/browser/flag-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,15 @@
"owners": [ "huitingyu@chromium.org", "hiramahmood@chromium.org", "bling-flags@google.com" ],
"expiry_milestone": 114
},
{
"name": "cros-apps-background-event-handling",
"owners": [
"clamclamyan",
"cros-apps-platform-core@google.com",
"//chrome/browser/chromeos/cros_apps/OWNERS"
],
"expiry_milestone": 132
},
{
"name": "cros-battery-saver",
"owners": [ "chromeos-bsm@google.com", "cwd@chromium.org" ],
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/flag_descriptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7185,6 +7185,11 @@ const char kAppToAppLinkCapturingWorkspaceAppsDescription[] =
"does not have link capturing enabled in settings. Only applies if the "
"target app is a Workspace app (Google Drive/Docs/Sheets/Slides).";

const char kCrosAppsBackgroundEventHandlingName[] =
"Experimental Background Events for CrOS Apps";
const char kCrosAppsBackgroundEventHandlingDescription[] =
"Enable key events for CrOS Apps running in background.";

const char kCrosWebAppInstallDialogName[] = "Web app install dialog";
const char kCrosWebAppInstallDialogDescription[] =
"Enables a more detailed, OS-level dialog for web app installs";
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/flag_descriptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4133,6 +4133,9 @@ extern const char kAppToAppLinkCapturingDescription[];
extern const char kAppToAppLinkCapturingWorkspaceAppsName[];
extern const char kAppToAppLinkCapturingWorkspaceAppsDescription[];

extern const char kCrosAppsBackgroundEventHandlingName[];
extern const char kCrosAppsBackgroundEventHandlingDescription[];

extern const char kCrosWebAppInstallDialogName[];
extern const char kCrosWebAppInstallDialogDescription[];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/certificate_provider/certificate_provider_service_factory.h"
#include "chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler_factory.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_download_observer_factory.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager_factory.h"
#include "chrome/browser/policy/messaging_layer/util/manual_test_heartbeat_event_factory.h"
Expand Down Expand Up @@ -686,6 +687,12 @@ void ChromeBrowserMainExtraPartsProfiles::
#if !BUILDFLAG(IS_ANDROID)
CouponServiceFactory::GetInstance();
#endif
#if BUILDFLAG(IS_CHROMEOS)
if (base::FeatureList::IsEnabled(
chromeos::features::kCrosAppsBackgroundEventHandling)) {
CrosAppsKeyEventHandlerFactory::GetInstance();
}
#endif
#if !BUILDFLAG(IS_ANDROID)
DevToolsAndroidBridge::Factory::GetInstance();
#endif
Expand Down
5 changes: 5 additions & 0 deletions chromeos/constants/chromeos_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ BASE_FEATURE(kBlinkExtensionDiagnostics,
"BlinkExtensionDiagnostics",
base::FEATURE_DISABLED_BY_DEFAULT);

// Enables handling of key press event in background.
BASE_FEATURE(kCrosAppsBackgroundEventHandling,
"CrosAppsBackgroundEventHandling",
base::FEATURE_DISABLED_BY_DEFAULT);

// Enables the use of cros-component UI elements. Contact:
// cros-jellybean-team@google.com.
BASE_FEATURE(kCrosComponents,
Expand Down
3 changes: 3 additions & 0 deletions chromeos/constants/chromeos_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "base/component_export.h"
#include "base/feature_list.h"
#include "build/buildflag.h"

// This file is only for the feature flags that are shared between ash-chrome
// and lacros-chrome that are not common. For ash features, please add them
Expand All @@ -31,6 +32,8 @@ COMPONENT_EXPORT(CHROMEOS_CONSTANTS) BASE_DECLARE_FEATURE(kCloudGamingDevice);
COMPONENT_EXPORT(CHROMEOS_CONSTANTS) BASE_DECLARE_FEATURE(kBlinkExtension);
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
BASE_DECLARE_FEATURE(kBlinkExtensionDiagnostics);
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
BASE_DECLARE_FEATURE(kCrosAppsBackgroundEventHandling);
COMPONENT_EXPORT(CHROMEOS_CONSTANTS) BASE_DECLARE_FEATURE(kCrosComponents);
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
BASE_DECLARE_FEATURE(kCrosWebAppInstallDialog);
Expand Down
2 changes: 2 additions & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60972,6 +60972,7 @@ from previous Chrome versions.
<int value="-2130982324" label="DesktopPWAsWebBundles:disabled"/>
<int value="-2129940395" label="WebAssemblySimd:disabled"/>
<int value="-2129781123" label="RequestDesktopSiteZoom:enabled"/>
<int value="-2129662187" label="CrosAppsBackgroundEventHandling:disabled"/>
<int value="-2129531046" label="LacrosProfileBackwardMigration:enabled"/>
<int value="-2128705444" label="AssistantAppSupport:enabled"/>
<int value="-2128535212" label="GuestOsFiles:enabled"/>
Expand Down Expand Up @@ -67020,6 +67021,7 @@ from previous Chrome versions.
<int value="780743733" label="QueryTilesLocalOrdering:disabled"/>
<int value="781085897" label="IncognitoReauthenticationForAndroid:disabled"/>
<int value="781152619" label="SupportToolScreenshot:disabled"/>
<int value="781298961" label="CrosAppsBackgroundEventHandling:enabled"/>
<int value="781573553" label="UnifiedPasswordManagerAndroid:enabled"/>
<int value="781785788" label="ClipboardSuggestionContentHidden:disabled"/>
<int value="782167080" label="enable-new-qp-input-view"/>
Expand Down

0 comments on commit 48912fa

Please sign in to comment.