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

[pull] main from chromium:main #126

Merged
merged 6 commits into from
Dec 17, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ vars = {
# Three lines of non-changing comments so that
# the commit queue can handle CLs rolling catapult
# and whatever else without interference from each other.
'catapult_revision': '22e558b5843a77389ca3883d0950f0f34e6f690c',
'catapult_revision': 'a25500bfae56d9a4774abe8c1b4efa3c08d20965',
# Three lines of non-changing comments so that
# the commit queue can handle CLs rolling libFuzzer
# and whatever else without interference from each other.
Expand Down Expand Up @@ -1730,7 +1730,7 @@ deps = {
Var('chromium_git') + '/v8/v8.git' + '@' + Var('v8_revision'),

'src-internal': {
'url': 'https://chrome-internal.googlesource.com/chrome/src-internal.git@c2192b8adaaef9fc4e1e51809e9b10599e0cf78d',
'url': 'https://chrome-internal.googlesource.com/chrome/src-internal.git@5d9dcc48106be3936b37712bc1b9319f88db762b',
'condition': 'checkout_src_internal',
},

Expand Down
229 changes: 181 additions & 48 deletions chrome/browser/pdf/pdf_extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1591,67 +1591,77 @@ IN_PROC_BROWSER_TEST_P(PDFExtensionTest, MAYBE_PdfZoomWithoutBubble) {
#endif
}

using PDFExtensionKeyEventTest = PDFExtensionTest;
class PDFExtensionKeyEventTest : public PDFExtensionTest {
protected:
class ScrollEventWaiter {
public:
explicit ScrollEventWaiter(WebContents* guest_contents)
: message_queue_(guest_contents) {
content::ExecuteScriptAsync(
guest_contents,
R"(viewer.shadowRoot.querySelector('#scroller').onscroll = () => {
window.domAutomationController.send('dispatchedScrollEvent');
})");
}

namespace {
void Reset() { message_queue_.ClearQueue(); }

class ScrollEventWaiter {
public:
explicit ScrollEventWaiter(content::WebContents* guest_contents)
: message_queue_(guest_contents) {
content::ExecuteScriptAsync(
void Wait() {
std::string message;
ASSERT_TRUE(message_queue_.WaitForMessage(&message));
EXPECT_EQ("\"dispatchedScrollEvent\"", message);
}

private:
content::DOMMessageQueue message_queue_;
};

// Scroll increment in CSS pixels. Should match `SCROLL_INCREMENT` in
// //chrome/browser/resources/pdf/viewport.js.
static constexpr int kScrollIncrement = 40;

static int GetViewportHeight(WebContents* guest_contents) {
int viewport_height = 0;
EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
guest_contents,
R"(viewer.shadowRoot.querySelector('#scroller').onscroll = () => {
window.domAutomationController.send('dispatchedScrollEvent');
})");
"window.domAutomationController.send(viewer.viewport.size.height);",
&viewport_height));
return viewport_height;
}

void Reset() { message_queue_.ClearQueue(); }

void Wait() {
std::string message;
ASSERT_TRUE(message_queue_.WaitForMessage(&message));
EXPECT_EQ("\"dispatchedScrollEvent\"", message);
static int GetViewportScrollPositionX(WebContents* guest_contents) {
int position_x = 0;
EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
guest_contents,
"window.domAutomationController.send(viewer.viewport.position.x);",
&position_x));
return position_x;
}

private:
content::DOMMessageQueue message_queue_;
static int GetViewportScrollPositionY(WebContents* guest_contents) {
int position_y = 0;
EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
guest_contents,
"window.domAutomationController.send(viewer.viewport.position.y);",
&position_y));
return position_y;
}
};

int GetViewportHeight(content::WebContents* guest_contents) {
int viewport_height = 0;
EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
guest_contents,
"window.domAutomationController.send(viewer.viewport.size.height);",
&viewport_height));
return viewport_height;
}

int GetViewportScrollPositionY(content::WebContents* guest_contents) {
int position_y = 0;
EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
guest_contents,
"window.domAutomationController.send(viewer.viewport.position.y);",
&position_y));
return position_y;
}

} // namespace
// static
constexpr int PDFExtensionKeyEventTest::kScrollIncrement;

IN_PROC_BROWSER_TEST_P(PDFExtensionKeyEventTest, ScrollWithSpaceShortcut) {
content::WebContents* guest_contents = LoadPdfGetGuestContents(
IN_PROC_BROWSER_TEST_P(PDFExtensionKeyEventTest, ScrollWithSpace) {
WebContents* guest_contents = LoadPdfGetGuestContents(
embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
SetInputFocusOnPlugin(guest_contents);
ASSERT_EQ(0, GetViewportScrollPositionY(guest_contents));

// Get the viewport size first since the scroll distance is based on the
// viewport height.
int viewport_height = GetViewportHeight(guest_contents);
// Get the viewport height, since the scroll distance is based on it.
const int viewport_height = GetViewportHeight(guest_contents);
ASSERT_GT(viewport_height, 0);

// The vertical scroll position is at 0 before scrolling.
EXPECT_EQ(0, GetViewportScrollPositionY(guest_contents));

// Press space key to scroll down.
// Press Space to scroll down.
ScrollEventWaiter scroll_waiter(guest_contents);
content::SimulateKeyPress(guest_contents, ui::DomKey::FromCharacter(' '),
ui::DomCode::SPACE, ui::VKEY_SPACE,
Expand All @@ -1660,14 +1670,137 @@ IN_PROC_BROWSER_TEST_P(PDFExtensionKeyEventTest, ScrollWithSpaceShortcut) {
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(viewport_height, GetViewportScrollPositionY(guest_contents));

// Press shift + space key to scroll back up to top.
// Press Space to scroll down again.
scroll_waiter.Reset();
content::SimulateKeyPress(guest_contents, ui::DomKey::FromCharacter(' '),
ui::DomCode::SPACE, ui::VKEY_SPACE,
/*control=*/false, /*shift=*/false, /*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(viewport_height * 2, GetViewportScrollPositionY(guest_contents));

// Press Shift+Space to scroll up.
scroll_waiter.Reset();
content::SimulateKeyPress(guest_contents, ui::DomKey::FromCharacter(' '),
ui::DomCode::SPACE, ui::VKEY_SPACE,
/*control=*/false, /*shift=*/true, /*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(0, GetViewportScrollPositionY(guest_contents));
EXPECT_EQ(viewport_height, GetViewportScrollPositionY(guest_contents));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionKeyEventTest, ScrollWithPageDownUp) {
WebContents* guest_contents = LoadPdfGetGuestContents(
embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
SetInputFocusOnPlugin(guest_contents);
ASSERT_EQ(0, GetViewportScrollPositionY(guest_contents));

// Get the viewport height, since the scroll distance is based on it.
const int viewport_height = GetViewportHeight(guest_contents);
ASSERT_GT(viewport_height, 0);

// Press PageDown to scroll down.
ScrollEventWaiter scroll_waiter(guest_contents);
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::PAGE_DOWN,
ui::DomCode::PAGE_DOWN, ui::VKEY_NEXT,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(viewport_height, GetViewportScrollPositionY(guest_contents));

// Press PageDown to scroll down again.
scroll_waiter.Reset();
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::PAGE_DOWN,
ui::DomCode::PAGE_DOWN, ui::VKEY_NEXT,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(viewport_height * 2, GetViewportScrollPositionY(guest_contents));

// Press PageUp to scroll up.
scroll_waiter.Reset();
content::SimulateKeyPressWithoutChar(
guest_contents, ui::DomKey::PAGE_UP, ui::DomCode::PAGE_UP, ui::VKEY_PRIOR,
/*control=*/false, /*shift=*/false, /*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(viewport_height, GetViewportScrollPositionY(guest_contents));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionKeyEventTest, ScrollWithArrowLeftRight) {
WebContents* guest_contents = LoadPdfGetGuestContents(
embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf#zoom=200"));
SetInputFocusOnPlugin(guest_contents);
ASSERT_EQ(0, GetViewportScrollPositionY(guest_contents));

// Press ArrowRight to scroll right.
ScrollEventWaiter scroll_waiter(guest_contents);
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::ARROW_RIGHT,
ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionX(guest_contents));

// Press ArrowRight to scroll right again.
scroll_waiter.Reset();
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::ARROW_RIGHT,
ui::DomCode::ARROW_RIGHT, ui::VKEY_RIGHT,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(kScrollIncrement * 2, GetViewportScrollPositionX(guest_contents));

// Press ArrowLeft to scroll left.
scroll_waiter.Reset();
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::ARROW_LEFT,
ui::DomCode::ARROW_LEFT, ui::VKEY_LEFT,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionX(guest_contents));
}

IN_PROC_BROWSER_TEST_P(PDFExtensionKeyEventTest, ScrollWithArrowDownUp) {
WebContents* guest_contents = LoadPdfGetGuestContents(
embedded_test_server()->GetURL("/pdf/test-bookmarks.pdf"));
SetInputFocusOnPlugin(guest_contents);
ASSERT_EQ(0, GetViewportScrollPositionY(guest_contents));

// Press ArrowDown to scroll down.
ScrollEventWaiter scroll_waiter(guest_contents);
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::ARROW_DOWN,
ui::DomCode::ARROW_DOWN, ui::VKEY_DOWN,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionY(guest_contents));

// Press ArrowDown to scroll down again.
scroll_waiter.Reset();
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::ARROW_DOWN,
ui::DomCode::ARROW_DOWN, ui::VKEY_DOWN,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(kScrollIncrement * 2, GetViewportScrollPositionY(guest_contents));

// Press ArrowUp to scroll up.
scroll_waiter.Reset();
content::SimulateKeyPressWithoutChar(guest_contents, ui::DomKey::ARROW_UP,
ui::DomCode::ARROW_UP, ui::VKEY_UP,
/*control=*/false, /*shift=*/false,
/*alt=*/false,
/*command=*/false);
ASSERT_NO_FATAL_FAILURE(scroll_waiter.Wait());
EXPECT_EQ(kScrollIncrement, GetViewportScrollPositionY(guest_contents));
}

INSTANTIATE_TEST_SUITE_P(All, PDFExtensionKeyEventTest, testing::Values(true));
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/resources/pdf/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ js_library("local_storage_proxy") {

js_library("controller") {
deps = [
":gesture_detector",
":internal_plugin",
":viewport",
"//ui/webui/resources/js:assert.m",
Expand Down Expand Up @@ -343,7 +344,6 @@ js_library("pdf_viewer") {
":browser_api",
":constants",
":controller",
":gesture_detector",
":ink_controller",
":local_storage_proxy",
":metrics",
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/resources/pdf/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {PromiseResolver} from 'chrome://resources/js/promise_resolver.m.js';

import {NamedDestinationMessageData, Point, SaveRequestType} from './constants.js';
import {Gesture} from './gesture_detector.js';
import {UnseasonedPdfPluginElement} from './internal_plugin.js';
import {PartialPoint, PinchPhase, Viewport} from './viewport.js';

Expand Down Expand Up @@ -567,6 +568,10 @@ export class PluginController {
}

switch (messageData.type) {
case 'gesture':
this.viewport_.dispatchGesture(
/** @type {{ gesture: !Gesture }} */ (messageData).gesture);
break;
case 'goToPage':
this.viewport_.goToPage(
/** @type {{type: string, page: number}} */ (messageData).page);
Expand Down
25 changes: 23 additions & 2 deletions chrome/browser/resources/pdf/pdf_internal_plugin_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,27 @@ document.addEventListener('keydown', e => {
case ' ':
// Preventing Space happens in the "keypress" event handler.
break;
case 'PageDown':
case 'PageUp':
// Always prevent PageDown/PageUp.
e.preventDefault();
break;

case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
// Don't prevent ArrowLeft/ArrowRight in form fields.
if (!isFormFieldFocused) {
case 'ArrowUp':
// Don't prevent arrow navigation in form fields, or if modified.
if (!isFormFieldFocused && !hasKeyModifiers(e)) {
e.preventDefault();
}
break;

case 'Escape':
case 'Tab':
// Print Preview is interested in Escape and Tab.
break;

default:
if (e.ctrlKey || e.metaKey) {
// Take over Ctrl+A, but not other shortcuts, such as zoom or print.
Expand Down Expand Up @@ -168,3 +180,12 @@ document.addEventListener('keypress', e => {
break;
}
});

// TODO(crbug.com/1252096): Load from chrome://resources/js/util.m.js instead.
/**
* @param {!Event} e
* @return {boolean}
*/
function hasKeyModifiers(e) {
return !!(e.altKey || e.ctrlKey || e.metaKey || e.shiftKey);
}
1 change: 0 additions & 1 deletion chrome/browser/resources/pdf/pdf_scripting_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function DeserializeKeyEvent(dict) {
altKey: dict.altKey,
metaKey: dict.metaKey,
});
e.fromScriptingAPI = true;
return e;
}

Expand Down
11 changes: 4 additions & 7 deletions chrome/browser/resources/pdf/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {PluginController} from './controller.js';
import {ViewerErrorDialogElement} from './elements/viewer-error-dialog.js';
import {ViewerPdfSidenavElement} from './elements/viewer-pdf-sidenav.js';
import {ViewerToolbarElement} from './elements/viewer-toolbar.js';
import {Gesture} from './gesture_detector.js';
// <if expr="enable_ink">
import {InkController, InkControllerEventType} from './ink_controller.js';
//</if>
Expand Down Expand Up @@ -831,13 +830,11 @@ export class PDFViewerElement extends PDFViewerBaseElement {
this.documentHasFocus_ =
/** @type {{ hasFocus: boolean }} */ (data).hasFocus;
return;
case 'gesture':
this.viewport.dispatchGesture(
/** @type {{ gesture: !Gesture }} */ (data).gesture);
return;
case 'sendKeyEvent':
this.handleKeyEvent(/** @type {!KeyboardEvent} */ (DeserializeKeyEvent(
/** @type {{ keyEvent: Object }} */ (data).keyEvent)));
const keyEvent = DeserializeKeyEvent(
/** @type {{ keyEvent: Object }} */ (data).keyEvent);
keyEvent.fromPlugin = true;
this.handleKeyEvent(keyEvent);
return;
}
assertNotReached('Unknown message type received: ' + data.type);
Expand Down
Loading