Skip to content

Commit

Permalink
Simulate generic down/up key events to make AllowSetForegroundWindow …
Browse files Browse the repository at this point in the history
…call succeed

Despite the fact that the Windows notification center grants the helper
permission to set the foreground window, the helper fails to pass the baton
to Chrome at an alarming rate; see https://crbug.com/837796.

Sending generic down/up key events seems to fix it.

Bug: 837796, 734095
Change-Id: I9b590d5149df35cfcb8219b0a386c5a9cfdb8eb0
Reviewed-on: https://chromium-review.googlesource.com/1192416
Commit-Queue: Xi Cheng <chengx@chromium.org>
Reviewed-by: Greg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586968}
  • Loading branch information
Xi Cheng authored and Commit Bot committed Aug 29, 2018
1 parent 366fa91 commit a3c45c4
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions chrome/notification_helper/notification_activator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "chrome/notification_helper/notification_activator.h"

#include <windows.h>

#include <shellapi.h>

#include "base/command_line.h"
Expand Down Expand Up @@ -143,6 +145,21 @@ HRESULT NotificationActivator::Activate(
if (info.hProcess != nullptr) {
base::Process process(info.hProcess);
DWORD pid = ::GetProcessId(process.Handle());

// Despite the fact that the Windows notification center grants the helper
// permission to set the foreground window, the helper fails to pass the
// baton to Chrome at an alarming rate; see https://crbug.com/837796.
// Sending generic down/up key events seems to fix it.
INPUT keyboard_inputs[2] = {};

keyboard_inputs[0].type = INPUT_KEYBOARD;
keyboard_inputs[0].ki.dwFlags = 0; // Key press.

keyboard_inputs[1] = keyboard_inputs[0];
keyboard_inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP; // key release.

::SendInput(2, keyboard_inputs, sizeof(keyboard_inputs[0]));

if (!::AllowSetForegroundWindow(pid)) {
#if !defined(NDEBUG)
DWORD error_code = ::GetLastError();
Expand Down

0 comments on commit a3c45c4

Please sign in to comment.