Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: third_party/WebKit/Source/web/tests/WebFrameTest.cpp

Issue 2454433002: [Extensions + Blink] Account for user gesture in v8 function calls (Closed)
Patch Set: nits Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/web/SuspendableScriptExecutor.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 EXPECT_EQ(webViewHelper.webView()->mainFrame()->firstChild(), 351 EXPECT_EQ(webViewHelper.webView()->mainFrame()->firstChild(),
352 WebLocalFrame::frameForContext(webViewHelper.webView() 352 WebLocalFrame::frameForContext(webViewHelper.webView()
353 ->mainFrame() 353 ->mainFrame()
354 ->firstChild() 354 ->firstChild()
355 ->mainWorldScriptContext())); 355 ->mainWorldScriptContext()));
356 } 356 }
357 357
358 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback { 358 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback {
359 public: 359 public:
360 explicit ScriptExecutionCallbackHelper(v8::Local<v8::Context> context) 360 explicit ScriptExecutionCallbackHelper(v8::Local<v8::Context> context)
361 : m_didComplete(false), m_context(context) {} 361 : m_didComplete(false), m_boolValue(false), m_context(context) {}
362 ~ScriptExecutionCallbackHelper() {} 362 ~ScriptExecutionCallbackHelper() {}
363 363
364 bool didComplete() const { return m_didComplete; } 364 bool didComplete() const { return m_didComplete; }
365 const String& stringValue() const { return m_stringValue; } 365 const String& stringValue() const { return m_stringValue; }
366 bool boolValue() { return m_boolValue; }
366 367
367 private: 368 private:
368 void completed(const WebVector<v8::Local<v8::Value>>& values) override { 369 void completed(const WebVector<v8::Local<v8::Value>>& values) override {
369 m_didComplete = true; 370 m_didComplete = true;
370 if (!values.isEmpty() && values[0]->IsString()) { 371 if (!values.isEmpty()) {
371 m_stringValue = 372 if (values[0]->IsString()) {
372 toCoreString(values[0]->ToString(m_context).ToLocalChecked()); 373 m_stringValue =
374 toCoreString(values[0]->ToString(m_context).ToLocalChecked());
375 } else if (values[0]->IsBoolean()) {
376 m_boolValue = values[0].As<v8::Boolean>()->Value();
377 }
373 } 378 }
374 } 379 }
375 380
376 bool m_didComplete; 381 bool m_didComplete;
377 String m_stringValue; 382 String m_stringValue;
383 bool m_boolValue;
378 v8::Local<v8::Context> m_context; 384 v8::Local<v8::Context> m_context;
379 }; 385 };
380 386
381 TEST_P(ParameterizedWebFrameTest, RequestExecuteScript) { 387 TEST_P(ParameterizedWebFrameTest, RequestExecuteScript) {
382 registerMockedHttpURLLoad("foo.html"); 388 registerMockedHttpURLLoad("foo.html");
383 389
384 FrameTestHelpers::WebViewHelper webViewHelper; 390 FrameTestHelpers::WebViewHelper webViewHelper;
385 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); 391 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true);
386 392
387 v8::HandleScope scope(v8::Isolate::GetCurrent()); 393 v8::HandleScope scope(v8::Isolate::GetCurrent());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 EXPECT_FALSE(callbackHelper.didComplete()); 426 EXPECT_FALSE(callbackHelper.didComplete());
421 427
422 // If the frame navigates, pending scripts should be removed, but the callback 428 // If the frame navigates, pending scripts should be removed, but the callback
423 // should always be ran. 429 // should always be ran.
424 FrameTestHelpers::loadFrame(webViewHelper.webView()->mainFrame(), 430 FrameTestHelpers::loadFrame(webViewHelper.webView()->mainFrame(),
425 m_baseURL + "bar.html"); 431 m_baseURL + "bar.html");
426 EXPECT_TRUE(callbackHelper.didComplete()); 432 EXPECT_TRUE(callbackHelper.didComplete());
427 EXPECT_EQ(String(), callbackHelper.stringValue()); 433 EXPECT_EQ(String(), callbackHelper.stringValue());
428 } 434 }
429 435
436 TEST_P(ParameterizedWebFrameTest, RequestExecuteV8Function) {
437 registerMockedHttpURLLoad("foo.html");
438
439 FrameTestHelpers::WebViewHelper webViewHelper;
440 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true);
441
442 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) {
443 info.GetReturnValue().Set(v8String(info.GetIsolate(), "hello"));
444 };
445
446 v8::HandleScope scope(v8::Isolate::GetCurrent());
447 v8::Local<v8::Context> context =
448 webViewHelper.webView()->mainFrame()->mainWorldScriptContext();
449 ScriptExecutionCallbackHelper callbackHelper(context);
450 v8::Local<v8::Function> function =
451 v8::Function::New(context, callback).ToLocalChecked();
452 webViewHelper.webView()
453 ->mainFrame()
454 ->toWebLocalFrame()
455 ->requestExecuteV8Function(context, function,
456 v8::Undefined(context->GetIsolate()), 0,
457 nullptr, &callbackHelper);
458 runPendingTasks();
459 EXPECT_TRUE(callbackHelper.didComplete());
460 EXPECT_EQ("hello", callbackHelper.stringValue());
461 }
462
463 TEST_P(ParameterizedWebFrameTest, RequestExecuteV8FunctionWhileSuspended) {
464 registerMockedHttpURLLoad("foo.html");
465
466 FrameTestHelpers::WebViewHelper webViewHelper;
467 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true);
468
469 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) {
470 info.GetReturnValue().Set(v8String(info.GetIsolate(), "hello"));
471 };
472
473 v8::HandleScope scope(v8::Isolate::GetCurrent());
474 v8::Local<v8::Context> context =
475 webViewHelper.webView()->mainFrame()->mainWorldScriptContext();
476
477 // Suspend scheduled tasks so the script doesn't run.
478 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl();
479 mainFrame->frame()->document()->suspendScheduledTasks();
480
481 ScriptExecutionCallbackHelper callbackHelper(context);
482 v8::Local<v8::Function> function =
483 v8::Function::New(context, callback).ToLocalChecked();
484 mainFrame->requestExecuteV8Function(context, function,
485 v8::Undefined(context->GetIsolate()), 0,
486 nullptr, &callbackHelper);
487 runPendingTasks();
488 EXPECT_FALSE(callbackHelper.didComplete());
489
490 mainFrame->frame()->document()->resumeScheduledTasks();
491 runPendingTasks();
492 EXPECT_TRUE(callbackHelper.didComplete());
493 EXPECT_EQ("hello", callbackHelper.stringValue());
494 }
495
496 TEST_P(ParameterizedWebFrameTest,
497 RequestExecuteV8FunctionWhileSuspendedWithUserGesture) {
498 registerMockedHttpURLLoad("foo.html");
499
500 FrameTestHelpers::WebViewHelper webViewHelper;
501 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true);
502
503 auto callback = [](const v8::FunctionCallbackInfo<v8::Value>& info) {
504 info.GetReturnValue().Set(v8::Boolean::New(
505 info.GetIsolate(), UserGestureIndicator::processingUserGesture()));
506 };
507
508 // Suspend scheduled tasks so the script doesn't run.
509 WebLocalFrameImpl* mainFrame = webViewHelper.webView()->mainFrameImpl();
510 Document* document = mainFrame->frame()->document();
511 document->suspendScheduledTasks();
512
513 v8::HandleScope scope(v8::Isolate::GetCurrent());
514 v8::Local<v8::Context> context =
515 webViewHelper.webView()->mainFrame()->mainWorldScriptContext();
516
517 std::unique_ptr<UserGestureIndicator> indicator =
518 wrapUnique(new UserGestureIndicator(DocumentUserGestureToken::create(
519 document, UserGestureToken::NewGesture)));
520 ScriptExecutionCallbackHelper callbackHelper(context);
521 v8::Local<v8::Function> function =
522 v8::Function::New(context, callback).ToLocalChecked();
523 mainFrame->requestExecuteV8Function(
524 mainFrame->mainWorldScriptContext(), function,
525 v8::Undefined(context->GetIsolate()), 0, nullptr, &callbackHelper);
526
527 runPendingTasks();
528 EXPECT_FALSE(callbackHelper.didComplete());
529
530 mainFrame->frame()->document()->resumeScheduledTasks();
531 runPendingTasks();
532 EXPECT_TRUE(callbackHelper.didComplete());
533 EXPECT_EQ(true, callbackHelper.boolValue());
534 }
535
430 TEST_P(ParameterizedWebFrameTest, IframeScriptRemovesSelf) { 536 TEST_P(ParameterizedWebFrameTest, IframeScriptRemovesSelf) {
431 registerMockedHttpURLLoad("single_iframe.html"); 537 registerMockedHttpURLLoad("single_iframe.html");
432 registerMockedHttpURLLoad("visible_iframe.html"); 538 registerMockedHttpURLLoad("visible_iframe.html");
433 539
434 FrameTestHelpers::WebViewHelper webViewHelper; 540 FrameTestHelpers::WebViewHelper webViewHelper;
435 webViewHelper.initializeAndLoad(m_baseURL + "single_iframe.html", true); 541 webViewHelper.initializeAndLoad(m_baseURL + "single_iframe.html", true);
436 542
437 v8::HandleScope scope(v8::Isolate::GetCurrent()); 543 v8::HandleScope scope(v8::Isolate::GetCurrent());
438 ScriptExecutionCallbackHelper callbackHelper( 544 ScriptExecutionCallbackHelper callbackHelper(
439 webViewHelper.webView()->mainFrame()->mainWorldScriptContext()); 545 webViewHelper.webView()->mainFrame()->mainWorldScriptContext());
(...skipping 9913 matching lines...) Expand 10 before | Expand all | Expand 10 after
10353 Frame* mainFrame = webViewHelper.webView()->mainFrameImpl()->frame(); 10459 Frame* mainFrame = webViewHelper.webView()->mainFrameImpl()->frame();
10354 HashSet<AtomicString> names; 10460 HashSet<AtomicString> names;
10355 for (Frame* frame = mainFrame->tree().firstChild(); frame; 10461 for (Frame* frame = mainFrame->tree().firstChild(); frame;
10356 frame = frame->tree().traverseNext()) { 10462 frame = frame->tree().traverseNext()) {
10357 EXPECT_TRUE(names.add(frame->tree().uniqueName()).isNewEntry); 10463 EXPECT_TRUE(names.add(frame->tree().uniqueName()).isNewEntry);
10358 } 10464 }
10359 EXPECT_EQ(10u, names.size()); 10465 EXPECT_EQ(10u, names.size());
10360 } 10466 }
10361 10467
10362 } // namespace blink 10468 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/SuspendableScriptExecutor.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698