Commit 86a6a36f authored by Hui Yingst's avatar Hui Yingst Committed by Chromium LUCI CQ

Add method ScheduleTaskOnMainThread() for scheduling tasks.

The Pepper plugin uses pp::Core::CallOnMainThread() to schedule tasks
on the main Pepper thread, while the non-Pepper plugin plans to use
base::SingleThreadTaskRunner() instead in the future.

This CL adds method ScheduleTaskOnMainThread() to PDFEngine::Client and
PaintManager::Client as a wrapper for pp::Core::CallOnMainThread() and
base::SingleThreadTaskRunner() calls, so that direct calls of these two
functions can be removed in the code shared by the Pepper and the
non-Pepper plugin.

Bug: 1108517
Change-Id: Ie1103adce2dbcf40ece40f318f7c3159f67e93ae
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2594508
Commit-Queue: Hui Yingst <nigi@chromium.org>
Reviewed-by: default avatarK. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838713}
parent 3464b822
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h" #include "base/metrics/histogram_functions.h"
...@@ -27,6 +28,7 @@ ...@@ -27,6 +28,7 @@
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h" #include "base/values.h"
#include "build/chromeos_buildflags.h" #include "build/chromeos_buildflags.h"
#include "net/base/escape.h" #include "net/base/escape.h"
...@@ -243,7 +245,8 @@ constexpr char kJSGetThumbnailHeight[] = "height"; ...@@ -243,7 +245,8 @@ constexpr char kJSGetThumbnailHeight[] = "height";
constexpr char kJSSetReadOnlyType[] = "setReadOnly"; constexpr char kJSSetReadOnlyType[] = "setReadOnly";
constexpr char kJSEnableReadOnly[] = "enableReadOnly"; constexpr char kJSEnableReadOnly[] = "enableReadOnly";
constexpr int kFindResultCooldownMs = 100; constexpr base::TimeDelta kFindResultCooldown =
base::TimeDelta::FromMilliseconds(100);
// Do not save files with over 100 MB. This cap should be kept in sync with and // Do not save files with over 100 MB. This cap should be kept in sync with and
// is also enforced in chrome/browser/resources/pdf/pdf_viewer.js. // is also enforced in chrome/browser/resources/pdf/pdf_viewer.js.
...@@ -256,7 +259,8 @@ constexpr int kInvalidPDFIndex = -2; ...@@ -256,7 +259,8 @@ constexpr int kInvalidPDFIndex = -2;
// A delay to wait between each accessibility page to keep the system // A delay to wait between each accessibility page to keep the system
// responsive. // responsive.
constexpr int kAccessibilityPageDelayMs = 100; constexpr base::TimeDelta kAccessibilityPageDelay =
base::TimeDelta::FromMilliseconds(100);
constexpr double kMinZoom = 0.01; constexpr double kMinZoom = 0.01;
...@@ -864,11 +868,10 @@ void OutOfProcessInstance::LoadAccessibility() { ...@@ -864,11 +868,10 @@ void OutOfProcessInstance::LoadAccessibility() {
SendAccessibilityViewportInfo(); SendAccessibilityViewportInfo();
// Schedule loading the first page. // Schedule loading the first page.
pp::Module::Get()->core()->CallOnMainThread( ScheduleTaskOnMainThread(
kAccessibilityPageDelayMs, kAccessibilityPageDelay,
PPCompletionCallbackFromResultCallback( base::BindOnce(&OutOfProcessInstance::SendNextAccessibilityPage,
base::BindOnce(&OutOfProcessInstance::SendNextAccessibilityPage, weak_factory_.GetWeakPtr()),
weak_factory_.GetWeakPtr())),
0); 0);
} }
...@@ -898,11 +901,10 @@ void OutOfProcessInstance::SendNextAccessibilityPage(int32_t page_index) { ...@@ -898,11 +901,10 @@ void OutOfProcessInstance::SendNextAccessibilityPage(int32_t page_index) {
pp_text_runs, pp_chars, page_objects); pp_text_runs, pp_chars, page_objects);
// Schedule loading the next page. // Schedule loading the next page.
pp::Module::Get()->core()->CallOnMainThread( ScheduleTaskOnMainThread(
kAccessibilityPageDelayMs, kAccessibilityPageDelay,
PPCompletionCallbackFromResultCallback( base::BindOnce(&OutOfProcessInstance::SendNextAccessibilityPage,
base::BindOnce(&OutOfProcessInstance::SendNextAccessibilityPage, weak_factory_.GetWeakPtr()),
weak_factory_.GetWeakPtr())),
page_index + 1); page_index + 1);
} }
...@@ -1324,11 +1326,10 @@ void OutOfProcessInstance::NotifyNumberOfFindResultsChanged(int total, ...@@ -1324,11 +1326,10 @@ void OutOfProcessInstance::NotifyNumberOfFindResultsChanged(int total,
NumberOfFindResultsChanged(total, final_result); NumberOfFindResultsChanged(total, final_result);
SetTickmarks(tickmarks_); SetTickmarks(tickmarks_);
recently_sent_find_update_ = true; recently_sent_find_update_ = true;
pp::Module::Get()->core()->CallOnMainThread( ScheduleTaskOnMainThread(
kFindResultCooldownMs, kFindResultCooldown,
PPCompletionCallbackFromResultCallback( base::BindOnce(&OutOfProcessInstance::ResetRecentlySentFindUpdate,
base::BindOnce(&OutOfProcessInstance::ResetRecentlySentFindUpdate, weak_factory_.GetWeakPtr()),
weak_factory_.GetWeakPtr())),
0); 0);
} }
...@@ -1465,9 +1466,10 @@ void OutOfProcessInstance::Print() { ...@@ -1465,9 +1466,10 @@ void OutOfProcessInstance::Print() {
return; return;
} }
pp::Module::Get()->core()->CallOnMainThread( ScheduleTaskOnMainThread(base::TimeDelta(),
0, PPCompletionCallbackFromResultCallback(base::BindOnce( base::BindOnce(&OutOfProcessInstance::OnPrint,
&OutOfProcessInstance::OnPrint, weak_factory_.GetWeakPtr()))); weak_factory_.GetWeakPtr()),
0);
} }
void OutOfProcessInstance::SubmitForm(const std::string& url, void OutOfProcessInstance::SubmitForm(const std::string& url,
...@@ -2284,13 +2286,26 @@ void OutOfProcessInstance::OnPaint(const std::vector<gfx::Rect>& paint_rects, ...@@ -2284,13 +2286,26 @@ void OutOfProcessInstance::OnPaint(const std::vector<gfx::Rect>& paint_rects,
engine()->PostPaint(); engine()->PostPaint();
if (!deferred_invalidates_.empty()) { if (!deferred_invalidates_.empty()) {
pp::Module::Get()->core()->CallOnMainThread( ScheduleTaskOnMainThread(
0, PPCompletionCallbackFromResultCallback( base::TimeDelta(),
base::BindOnce(&OutOfProcessInstance::InvalidateAfterPaintDone, base::BindOnce(&OutOfProcessInstance::InvalidateAfterPaintDone,
weak_factory_.GetWeakPtr()))); weak_factory_.GetWeakPtr()),
0);
} }
} }
void OutOfProcessInstance::ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here) {
int64_t delay_in_msec = delay.InMilliseconds();
DCHECK(delay_in_msec <= INT32_MAX);
pp::Module::Get()->core()->CallOnMainThread(
static_cast<int32_t>(delay_in_msec),
PPCompletionCallbackFromResultCallback(std::move(callback)), result);
}
void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url, void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url,
int dest_page_index) { int dest_page_index) {
DCHECK(IsPrintPreview()); DCHECK(IsPrintPreview());
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/queue.h" #include "base/containers/queue.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "pdf/pdf_view_plugin_base.h" #include "pdf/pdf_view_plugin_base.h"
#include "pdf/preview_mode_client.h" #include "pdf/preview_mode_client.h"
...@@ -162,6 +163,11 @@ class OutOfProcessInstance : public PdfViewPluginBase, ...@@ -162,6 +163,11 @@ class OutOfProcessInstance : public PdfViewPluginBase,
void OnPaint(const std::vector<gfx::Rect>& paint_rects, void OnPaint(const std::vector<gfx::Rect>& paint_rects,
std::vector<PaintReadyRect>* ready, std::vector<PaintReadyRect>* ready,
std::vector<gfx::Rect>* pending) override; std::vector<gfx::Rect>* pending) override;
void ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here = base::Location::Current()) override;
// PreviewModeClient::Client: // PreviewModeClient::Client:
void PreviewDocumentLoadComplete() override; void PreviewDocumentLoadComplete() override;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/check.h" #include "base/check.h"
#include "base/time/time.h"
#include "pdf/paint_ready_rect.h" #include "pdf/paint_ready_rect.h"
#include "pdf/ppapi_migration/callback.h" #include "pdf/ppapi_migration/callback.h"
#include "pdf/ppapi_migration/geometry_conversions.h" #include "pdf/ppapi_migration/geometry_conversions.h"
...@@ -161,10 +162,10 @@ void PaintManager::EnsureCallbackPending() { ...@@ -161,10 +162,10 @@ void PaintManager::EnsureCallbackPending() {
if (manual_callback_pending_) if (manual_callback_pending_)
return; return;
pp::Module::Get()->core()->CallOnMainThread( client_->ScheduleTaskOnMainThread(
0, base::TimeDelta(),
PPCompletionCallbackFromResultCallback(base::BindOnce( base::BindOnce(&PaintManager::OnManualCallbackComplete,
&PaintManager::OnManualCallbackComplete, weak_factory_.GetWeakPtr())), weak_factory_.GetWeakPtr()),
0); 0);
manual_callback_pending_ = true; manual_callback_pending_ = true;
} }
......
...@@ -10,10 +10,16 @@ ...@@ -10,10 +10,16 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "base/location.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "pdf/paint_aggregator.h" #include "pdf/paint_aggregator.h"
#include "pdf/ppapi_migration/callback.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
namespace base {
class TimeDelta;
} // namespace base
namespace gfx { namespace gfx {
class Point; class Point;
class Rect; class Rect;
...@@ -66,6 +72,18 @@ class PaintManager { ...@@ -66,6 +72,18 @@ class PaintManager {
std::vector<PaintReadyRect>* ready, std::vector<PaintReadyRect>* ready,
std::vector<gfx::Rect>* pending) = 0; std::vector<gfx::Rect>* pending) = 0;
// Schedules work to be executed on a main thread after a specific delay.
// The `result` parameter will be passed as the argument to the `callback`.
// `result` is needed sometimes to emulate calls of some callbacks, but it's
// not always needed. `delay` should be no longer than `INT32_MAX`
// milliseconds for the Pepper plugin implementation to prevent integer
// overflow.
virtual void ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here = base::Location::Current()) = 0;
protected: protected:
// You shouldn't be doing deleting through this interface. // You shouldn't be doing deleting through this interface.
~Client() = default; ~Client() = default;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/location.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -20,6 +21,7 @@ ...@@ -20,6 +21,7 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "build/chromeos_buildflags.h" #include "build/chromeos_buildflags.h"
#include "pdf/document_layout.h" #include "pdf/document_layout.h"
#include "pdf/ppapi_migration/callback.h"
#include "ppapi/c/dev/pp_cursor_type_dev.h" #include "ppapi/c/dev/pp_cursor_type_dev.h"
#include "ppapi/c/dev/ppp_printing_dev.h" #include "ppapi/c/dev/ppp_printing_dev.h"
#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/completion_callback.h"
...@@ -286,6 +288,18 @@ class PDFEngine { ...@@ -286,6 +288,18 @@ class PDFEngine {
// viewers. // viewers.
// See https://crbug.com/312882 for an example. // See https://crbug.com/312882 for an example.
virtual bool IsValidLink(const std::string& url) = 0; virtual bool IsValidLink(const std::string& url) = 0;
// Schedules work to be executed on a main thread after a specific delay.
// The `result` parameter will be passed as the argument to the `callback`.
// `result` is needed sometimes to emulate calls of some callbacks, but it's
// not always needed. `delay` should be no longer than `INT32_MAX`
// milliseconds for the Pepper plugin implementation to prevent integer
// overflow.
virtual void ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here = base::Location::Current()) = 0;
}; };
struct AccessibilityLinkInfo { struct AccessibilityLinkInfo {
......
...@@ -12,9 +12,11 @@ ...@@ -12,9 +12,11 @@
#include <vector> #include <vector>
#include "base/check_op.h" #include "base/check_op.h"
#include "base/location.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "base/thread_annotations.h" #include "base/thread_annotations.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "cc/paint/paint_canvas.h" #include "cc/paint/paint_canvas.h"
#include "net/cookies/site_for_cookies.h" #include "net/cookies/site_for_cookies.h"
#include "pdf/pdf_engine.h" #include "pdf/pdf_engine.h"
...@@ -329,6 +331,15 @@ void PdfViewWebPlugin::OnPaint(const std::vector<gfx::Rect>& paint_rects, ...@@ -329,6 +331,15 @@ void PdfViewWebPlugin::OnPaint(const std::vector<gfx::Rect>& paint_rects,
NOTIMPLEMENTED_LOG_ONCE(); NOTIMPLEMENTED_LOG_ONCE();
} }
void PdfViewWebPlugin::ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
from_here, base::BindOnce(std::move(callback), result), delay);
}
bool PdfViewWebPlugin::IsValid() const { bool PdfViewWebPlugin::IsValid() const {
return container_ && container_->GetDocument().GetFrame(); return container_ && container_->GetDocument().GetFrame();
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef PDF_PDF_VIEW_WEB_PLUGIN_H_ #ifndef PDF_PDF_VIEW_WEB_PLUGIN_H_
#define PDF_PDF_VIEW_WEB_PLUGIN_H_ #define PDF_PDF_VIEW_WEB_PLUGIN_H_
#include "base/location.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "pdf/pdf_view_plugin_base.h" #include "pdf/pdf_view_plugin_base.h"
#include "pdf/ppapi_migration/url_loader.h" #include "pdf/ppapi_migration/url_loader.h"
...@@ -108,6 +109,11 @@ class PdfViewWebPlugin final : public PdfViewPluginBase, ...@@ -108,6 +109,11 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
void OnPaint(const std::vector<gfx::Rect>& paint_rects, void OnPaint(const std::vector<gfx::Rect>& paint_rects,
std::vector<PaintReadyRect>* ready, std::vector<PaintReadyRect>* ready,
std::vector<gfx::Rect>* pending) override; std::vector<gfx::Rect>* pending) override;
void ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here = base::Location::Current()) override;
// BlinkUrlLoader::Client: // BlinkUrlLoader::Client:
bool IsValid() const override; bool IsValid() const override;
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "gin/array_buffer.h" #include "gin/array_buffer.h"
#include "gin/public/gin_embedders.h" #include "gin/public/gin_embedders.h"
...@@ -1745,10 +1746,10 @@ void PDFiumEngine::StartFind(const std::string& text, bool case_sensitive) { ...@@ -1745,10 +1746,10 @@ void PDFiumEngine::StartFind(const std::string& text, bool case_sensitive) {
if (doc_loader_set_for_testing_) { if (doc_loader_set_for_testing_) {
ContinueFind(case_sensitive ? 1 : 0); ContinueFind(case_sensitive ? 1 : 0);
} else { } else {
pp::Module::Get()->core()->CallOnMainThread( client_->ScheduleTaskOnMainThread(
0, base::TimeDelta(),
PPCompletionCallbackFromResultCallback(base::BindOnce( base::BindOnce(&PDFiumEngine::ContinueFind,
&PDFiumEngine::ContinueFind, find_weak_factory_.GetWeakPtr())), find_weak_factory_.GetWeakPtr()),
case_sensitive ? 1 : 0); case_sensitive ? 1 : 0);
} }
} }
......
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
#include <utility> #include <utility>
#include "base/callback.h" #include "base/callback.h"
#include "base/location.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "base/time/time.h"
#include "pdf/document_layout.h" #include "pdf/document_layout.h"
#include "pdf/ppapi_migration/url_loader.h" #include "pdf/ppapi_migration/url_loader.h"
...@@ -178,4 +180,12 @@ bool PreviewModeClient::IsValidLink(const std::string& url) { ...@@ -178,4 +180,12 @@ bool PreviewModeClient::IsValidLink(const std::string& url) {
return false; return false;
} }
void PreviewModeClient::ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here) {
NOTREACHED();
}
} // namespace chrome_pdf } // namespace chrome_pdf
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <vector> #include <vector>
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/location.h"
#include "pdf/pdf_engine.h" #include "pdf/pdf_engine.h"
namespace gfx { namespace gfx {
...@@ -78,6 +79,11 @@ class PreviewModeClient : public PDFEngine::Client { ...@@ -78,6 +79,11 @@ class PreviewModeClient : public PDFEngine::Client {
void SetSelectedText(const std::string& selected_text) override; void SetSelectedText(const std::string& selected_text) override;
void SetLinkUnderCursor(const std::string& link_under_cursor) override; void SetLinkUnderCursor(const std::string& link_under_cursor) override;
bool IsValidLink(const std::string& url) override; bool IsValidLink(const std::string& url) override;
void ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here = base::Location::Current()) override;
private: private:
Client* const client_; Client* const client_;
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <memory> #include <memory>
#include "base/location.h"
#include "base/time/time.h"
#include "pdf/document_layout.h" #include "pdf/document_layout.h"
#include "pdf/ppapi_migration/url_loader.h" #include "pdf/ppapi_migration/url_loader.h"
...@@ -71,4 +73,9 @@ bool TestClient::IsValidLink(const std::string& url) { ...@@ -71,4 +73,9 @@ bool TestClient::IsValidLink(const std::string& url) {
return !url.empty(); return !url.empty();
} }
void TestClient::ScheduleTaskOnMainThread(base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here) {}
} // namespace chrome_pdf } // namespace chrome_pdf
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/location.h"
#include "pdf/pdf_engine.h" #include "pdf/pdf_engine.h"
namespace chrome_pdf { namespace chrome_pdf {
...@@ -41,6 +42,11 @@ class TestClient : public PDFEngine::Client { ...@@ -41,6 +42,11 @@ class TestClient : public PDFEngine::Client {
void SetSelectedText(const std::string& selected_text) override; void SetSelectedText(const std::string& selected_text) override;
void SetLinkUnderCursor(const std::string& link_under_cursor) override; void SetLinkUnderCursor(const std::string& link_under_cursor) override;
bool IsValidLink(const std::string& url) override; bool IsValidLink(const std::string& url) override;
void ScheduleTaskOnMainThread(
base::TimeDelta delay,
ResultCallback callback,
int32_t result,
const base::Location& from_here = base::Location::Current()) override;
private: private:
// Not owned. Expected to dangle briefly, as the engine usually is destroyed // Not owned. Expected to dangle briefly, as the engine usually is destroyed
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment