-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Key Event handler and factory for sending key events to be handle…
…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
Showing
14 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
35 changes: 35 additions & 0 deletions
35
chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler_factory.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
28 changes: 28 additions & 0 deletions
28
chrome/browser/chromeos/cros_apps/cros_apps_key_event_handler_factory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters