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

feat: Add Secure Keyboard Entry APIs in macOS #20678

Merged
merged 6 commits into from
May 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,25 @@ app.moveToApplicationsFolder({

Would mean that if an app already exists in the user directory, if the user chooses to 'Continue Move' then the function would continue with its default behavior and the existing app will be trashed and the active app moved into its place.

### `app.isSecureKeyboardEntryEnabled()` _macOS_

Returns `Boolean` - whether `Secure Keyboard Entry` is enabled.

By default this API will return `false`.

### `app.setSecureKeyboardEntryEnabled(enabled)` _macOS_

* `enabled` Boolean - Enable or disable `Secure Keyboard Entry`

Set the `Secure Keyboard Entry` is enabled in your application.

By using this API, important information such as password and other sensitive information can be prevented from being intercepted by other processes.

See [Apple's documentation](https://developer.apple.com/library/archive/technotes/tn2150/_index.html) for more
details.

**Note:** Enable `Secure Keyboard Entry` only when it is needed and disable it when it is no longer needed.

## Properties

### `app.accessibilitySupportEnabled` _macOS_ _Windows_
Expand Down
8 changes: 8 additions & 0 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,14 @@ void App::BuildPrototype(v8::Isolate* isolate,
base::BindRepeating(&Browser::SetAboutPanelOptions, browser))
.SetMethod("showAboutPanel",
base::BindRepeating(&Browser::ShowAboutPanel, browser))
#if defined(OS_MACOSX)
.SetMethod(
"isSecureKeyboardEntryEnabled",
base::BindRepeating(&Browser::IsSecureKeyboardEntryEnabled, browser))
.SetMethod(
"setSecureKeyboardEntryEnabled",
base::BindRepeating(&Browser::SetSecureKeyboardEntryEnabled, browser))
#endif
#if defined(OS_MACOSX) || defined(OS_WIN)
.SetMethod("showEmojiPanel",
base::BindRepeating(&Browser::ShowEmojiPanel, browser))
Expand Down
14 changes: 14 additions & 0 deletions shell/browser/browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "base/files/file_path.h"
#endif

#if defined(OS_MACOSX)
#include "ui/base/cocoa/secure_password_input.h"
#endif

namespace base {
class FilePath;
}
Expand Down Expand Up @@ -261,6 +265,12 @@ class Browser : public WindowListObserver {

void RemoveObserver(BrowserObserver* obs) { observers_.RemoveObserver(obs); }

#if defined(OS_MACOSX)
// Returns whether secure input is enabled
bool IsSecureKeyboardEntryEnabled();
void SetSecureKeyboardEntryEnabled(bool enabled);
#endif

bool is_shutting_down() const { return is_shutdown_; }
bool is_quiting() const { return is_quiting_; }
bool is_ready() const { return is_ready_; }
Expand Down Expand Up @@ -305,6 +315,10 @@ class Browser : public WindowListObserver {

std::unique_ptr<gin_helper::Promise<void>> ready_promise_;

#if defined(OS_MACOSX)
std::unique_ptr<ui::ScopedPasswordInputEnabler> password_input_enabler_;
#endif

#if defined(OS_LINUX) || defined(OS_WIN)
base::Value about_panel_options_;
#elif defined(OS_MACOSX)
Expand Down
14 changes: 14 additions & 0 deletions shell/browser/browser_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "shell/browser/browser.h"

#include <memory>
#include <string>
#include <utility>

Expand Down Expand Up @@ -430,4 +431,17 @@ void RemoveFromLoginItems() {
return true;
}

bool Browser::IsSecureKeyboardEntryEnabled() {
return password_input_enabler_.get() != nullptr;
}

void Browser::SetSecureKeyboardEntryEnabled(bool enabled) {
if (enabled) {
Copy link
Contributor

Choose a reason for hiding this comment

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

calling setSecureInputEnabled() with the same value twice should have no effect IMO.

password_input_enabler_ =
std::make_unique<ui::ScopedPasswordInputEnabler>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

} else {
password_input_enabler_.reset();
}
}

} // namespace electron
9 changes: 9 additions & 0 deletions spec-main/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,15 @@ describe('app module', () => {
expect(getSwitchValue).to.equal('');
});
});

ifdescribe(process.platform === 'darwin')('app.setSecureKeyboardEntryEnabled', () => {
it('changes Secure Keyboard Entry is enabled', () => {
app.setSecureKeyboardEntryEnabled(true);
expect(app.isSecureKeyboardEntryEnabled()).to.equal(true);
app.setSecureKeyboardEntryEnabled(false);
expect(app.isSecureKeyboardEntryEnabled()).to.equal(false);
});
});
});

describe('default behavior', () => {
Expand Down