Skip to content

Commit

Permalink
Improvement to move OnScreenKeyboard to Cobalt extension. (#2317)
Browse files Browse the repository at this point in the history
OnScreenKeyboard API was moved from Starboard to Cobalt extensions

b/151173891

Change-Id: I4ec59afc18c7e4b2a8aa0a529c2cc7af229f5056
  • Loading branch information
iuriionishchenko authored Jan 31, 2024
1 parent 1ec1e66 commit 57d564c
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cobalt/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ static_library("browser") {
"client_hint_headers.h",
"device_authentication.cc",
"device_authentication.h",
"on_screen_keyboard_extension_bridge.cc",
"on_screen_keyboard_extension_bridge.h",
"on_screen_keyboard_starboard_bridge.cc",
"on_screen_keyboard_starboard_bridge.h",
"render_tree_combiner.cc",
Expand Down
29 changes: 23 additions & 6 deletions cobalt/browser/browser_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "cobalt/base/init_cobalt.h"
#include "cobalt/base/source_location.h"
#include "cobalt/base/tokens.h"
#include "cobalt/browser/on_screen_keyboard_extension_bridge.h"
#include "cobalt/browser/on_screen_keyboard_starboard_bridge.h"
#include "cobalt/browser/screen_shot_writer.h"
#include "cobalt/browser/switches.h"
Expand Down Expand Up @@ -237,12 +238,6 @@ BrowserModule::BrowserModule(const GURL& url,
updater_module_(updater_module),
#endif
splash_screen_cache_(new SplashScreenCache()),
on_screen_keyboard_bridge_(
OnScreenKeyboardStarboardBridge::IsSupported() &&
options.enable_on_screen_keyboard
? new OnScreenKeyboardStarboardBridge(base::Bind(
&BrowserModule::GetSbWindow, base::Unretained(this)))
: NULL),
web_module_loaded_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED),
web_module_created_callback_(options_.web_module_created_callback),
Expand Down Expand Up @@ -294,6 +289,28 @@ BrowserModule::BrowserModule(const GURL& url,
current_main_web_module_timeline_id_(-1) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::BrowserModule()");

if (options.enable_on_screen_keyboard) {
if (OnScreenKeyboardExtensionBridge::IsSupported()) {
const CobaltExtensionOnScreenKeyboardApi* on_screen_keyboard_extension =
static_cast<const CobaltExtensionOnScreenKeyboardApi*>(
SbSystemGetExtension(kCobaltExtensionOnScreenKeyboardName));
on_screen_keyboard_bridge_ =
std::make_unique<OnScreenKeyboardExtensionBridge>(
base::Bind(&BrowserModule::GetSbWindow, base::Unretained(this)),
on_screen_keyboard_extension);
} else {
if (OnScreenKeyboardStarboardBridge::IsSupported()) {
on_screen_keyboard_bridge_ =
std::make_unique<OnScreenKeyboardStarboardBridge>(base::Bind(
&BrowserModule::GetSbWindow, base::Unretained(this)));
} else {
on_screen_keyboard_bridge_ = NULL;
}
}
} else {
on_screen_keyboard_bridge_ = NULL;
}

// Apply platform memory setting adjustments and defaults.
ApplyAutoMemSettings();

Expand Down
118 changes: 118 additions & 0 deletions cobalt/browser/on_screen_keyboard_extension_bridge.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/browser/on_screen_keyboard_extension_bridge.h"

#include <memory>

#include "base/bind.h"
#include "base/callback.h"
#include "starboard/event.h"

namespace cobalt {
namespace browser {
// static
bool OnScreenKeyboardExtensionBridge::IsSupported() {
const CobaltExtensionOnScreenKeyboardApi* on_screen_keyboard_extension =
static_cast<const CobaltExtensionOnScreenKeyboardApi*>(
SbSystemGetExtension(kCobaltExtensionOnScreenKeyboardName));
return (on_screen_keyboard_extension &&
strcmp(on_screen_keyboard_extension->name,
kCobaltExtensionOnScreenKeyboardName) == 0 &&
on_screen_keyboard_extension->version >= 1);
}

void OnScreenKeyboardExtensionBridge::Show(const char* input_text, int ticket) {
// Delay providing the SbWindow until as late as possible.
on_screen_keyboard_extension_->Show(sb_window_provider_.Run(), input_text,
ticket);
}

void OnScreenKeyboardExtensionBridge::Hide(int ticket) {
// Delay providing the SbWindow until as late as possible.
on_screen_keyboard_extension_->Hide(sb_window_provider_.Run(), ticket);
}

void OnScreenKeyboardExtensionBridge::Focus(int ticket) {
// Delay providing the SbWindow until as late as possible.
on_screen_keyboard_extension_->Focus(sb_window_provider_.Run(), ticket);
}

void OnScreenKeyboardExtensionBridge::Blur(int ticket) {
// Delay providing the SbWindow until as late as possible.
on_screen_keyboard_extension_->Blur(sb_window_provider_.Run(), ticket);
}

void OnScreenKeyboardExtensionBridge::UpdateSuggestions(
const script::Sequence<std::string>& suggestions, int ticket) {
std::unique_ptr<const char*[]> suggestions_data(
new const char*[suggestions.size()]);
for (script::Sequence<std::string>::size_type i = 0; i < suggestions.size();
++i) {
suggestions_data[i] = suggestions.at(i).c_str();
}
// Delay providing the SbWindow until as late as possible.
on_screen_keyboard_extension_->UpdateSuggestions(
sb_window_provider_.Run(), suggestions_data.get(),
static_cast<int>(suggestions.size()), ticket);
}

bool OnScreenKeyboardExtensionBridge::IsShown() const {
// Delay providing the SbWindow until as late as possible.
return on_screen_keyboard_extension_->IsShown(sb_window_provider_.Run());
}

bool OnScreenKeyboardExtensionBridge::SuggestionsSupported() const {
// Delay providing the SbWindow until as late as possible.
return on_screen_keyboard_extension_->SuggestionsSupported(
sb_window_provider_.Run());
}

scoped_refptr<dom::DOMRect> OnScreenKeyboardExtensionBridge::BoundingRect()
const {
// Delay providing the SbWindow until as late as possible.
SbWindowRect sb_window_rect = SbWindowRect();
if (!on_screen_keyboard_extension_->GetBoundingRect(sb_window_provider_.Run(),
&sb_window_rect)) {
return nullptr;
}

scoped_refptr<dom::DOMRect> bounding_rect =
new dom::DOMRect(sb_window_rect.x, sb_window_rect.y, sb_window_rect.width,
sb_window_rect.height);
return bounding_rect;
}

bool OnScreenKeyboardExtensionBridge::IsValidTicket(int ticket) const {
return ticket != kSbEventOnScreenKeyboardInvalidTicket;
}

void OnScreenKeyboardExtensionBridge::SetKeepFocus(bool keep_focus) {
// Delay providing the SbWindow until as late as possible.
on_screen_keyboard_extension_->SetKeepFocus(sb_window_provider_.Run(),
keep_focus);
}

void OnScreenKeyboardExtensionBridge::SetBackgroundColor(uint8 r, uint8 g,
uint8 b) {
on_screen_keyboard_extension_->SetBackgroundColor(sb_window_provider_.Run(),
r, g, b);
}

void OnScreenKeyboardExtensionBridge::SetLightTheme(bool light_theme) {
on_screen_keyboard_extension_->SetLightTheme(sb_window_provider_.Run(),
light_theme);
}
} // namespace browser
} // namespace cobalt
75 changes: 75 additions & 0 deletions cobalt/browser/on_screen_keyboard_extension_bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_BROWSER_ON_SCREEN_KEYBOARD_EXTENSION_BRIDGE_H_
#define COBALT_BROWSER_ON_SCREEN_KEYBOARD_EXTENSION_BRIDGE_H_

#include <string>

#include "base/callback.h"
#include "cobalt/dom/on_screen_keyboard_bridge.h"
#include "starboard/extension/on_screen_keyboard.h"
#include "starboard/window.h"

namespace cobalt {
namespace browser {

// OnScreenKeyboardExtensionBridge implements the OnScreenKeyboardBridge,
// calling Extension methods to control the OnScreenKeyboard. The constructor
// takes in a callback provider for the SbWindow and an instance of extension.
class OnScreenKeyboardExtensionBridge : public dom::OnScreenKeyboardBridge {
public:
explicit OnScreenKeyboardExtensionBridge(
const base::Callback<SbWindow()>& sb_window_provider,
const CobaltExtensionOnScreenKeyboardApi* on_screen_keyboard_extension)
: sb_window_provider_(sb_window_provider),
on_screen_keyboard_extension_{on_screen_keyboard_extension} {
DCHECK(!sb_window_provider_.is_null());
}

static bool IsSupported();

void Show(const char* input_text, int ticket) override;

void Hide(int ticket) override;

void Focus(int ticket) override;

void Blur(int ticket) override;

void UpdateSuggestions(const script::Sequence<std::string>& suggestions,
int ticket) override;

bool IsShown() const override;

bool SuggestionsSupported() const override;

scoped_refptr<dom::DOMRect> BoundingRect() const override;

bool IsValidTicket(int ticket) const override;

void SetKeepFocus(bool keep_focus) override;

void SetBackgroundColor(uint8 r, uint8 g, uint8 b) override;

void SetLightTheme(bool light_theme) override;

private:
base::Callback<SbWindow()> sb_window_provider_;
const CobaltExtensionOnScreenKeyboardApi* const on_screen_keyboard_extension_;
};

} // namespace browser
} // namespace cobalt
#endif // COBALT_BROWSER_ON_SCREEN_KEYBOARD_EXTENSION_BRIDGE_H_
22 changes: 22 additions & 0 deletions starboard/extension/on_screen_keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ typedef struct CobaltExtensionOnScreenKeyboardApi {

// This function overrides the light theme of on-screen keyboard.
void (*SetLightTheme)(SbWindow window, bool light_theme);

void (*Show)(SbWindow window, const char* input_text, int ticket);

void (*Hide)(SbWindow window, int ticket);

void (*Focus)(SbWindow window, int ticket);

void (*Blur)(SbWindow window, int ticket);

void (*UpdateSuggestions)(SbWindow window,
const char* suggestions[],
int num_suggestions,
int ticket);

bool (*IsShown)(SbWindow window);

bool (*SuggestionsSupported)(SbWindow window);

bool (*GetBoundingRect)(SbWindow window, SbWindowRect* bounding_rect);

void (*SetKeepFocus)(SbWindow window, bool keep_focus);

} CobaltExtensionOnScreenKeyboardApi;

#ifdef __cplusplus
Expand Down

0 comments on commit 57d564c

Please sign in to comment.