Skip to content

Commit

Permalink
More PDFiumEngine cleanup.
Browse files Browse the repository at this point in the history
- Use base::TimeDelta for timers.
- Initialize more members in headers.
- Put some members near related members.
- Add FormFillTimerData which is more readable than a std::pair.

Change-Id: Ifc2ad1004abe3d83b66b68f678ef7ebb04098c3c
Reviewed-on: https://chromium-review.googlesource.com/727409
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Cr-Commit-Position: refs/heads/master@{#510227}
  • Loading branch information
leizleiz authored and Commit Bot committed Oct 19, 2017
1 parent cd7e160 commit 494f608
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 57 deletions.
19 changes: 11 additions & 8 deletions pdf/out_of_process_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1394,16 +1394,19 @@ pp::URLLoader OutOfProcessInstance::CreateURLLoader() {
return CreateURLLoaderInternal();
}

void OutOfProcessInstance::ScheduleTouchTimerCallback(int id, int delay_in_ms) {
pp::CompletionCallback callback = callback_factory_.NewCallback(
&OutOfProcessInstance::OnClientTouchTimerFired);
pp::Module::Get()->core()->CallOnMainThread(delay_in_ms, callback, id);
}

void OutOfProcessInstance::ScheduleCallback(int id, int delay_in_ms) {
void OutOfProcessInstance::ScheduleCallback(int id, base::TimeDelta delay) {
pp::CompletionCallback callback =
callback_factory_.NewCallback(&OutOfProcessInstance::OnClientTimerFired);
pp::Module::Get()->core()->CallOnMainThread(delay_in_ms, callback, id);
pp::Module::Get()->core()->CallOnMainThread(delay.InMilliseconds(), callback,
id);
}

void OutOfProcessInstance::ScheduleTouchTimerCallback(int id,
base::TimeDelta delay) {
pp::CompletionCallback callback = callback_factory_.NewCallback(
&OutOfProcessInstance::OnClientTouchTimerFired);
pp::Module::Get()->core()->CallOnMainThread(delay.InMilliseconds(), callback,
id);
}

void OutOfProcessInstance::SearchString(
Expand Down
4 changes: 2 additions & 2 deletions pdf/out_of_process_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class OutOfProcessInstance : public pp::Instance,
const void* data,
int length) override;
pp::URLLoader CreateURLLoader() override;
void ScheduleCallback(int id, int delay_in_ms) override;
void ScheduleTouchTimerCallback(int id, int delay_in_ms) override;
void ScheduleCallback(int id, base::TimeDelta delay) override;
void ScheduleTouchTimerCallback(int id, base::TimeDelta delay) override;
void SearchString(const base::char16* string,
const base::char16* term,
bool case_sensitive,
Expand Down
13 changes: 6 additions & 7 deletions pdf/pdf_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <vector>

#include "base/strings/string16.h"

#include "base/time/time.h"
#include "ppapi/c/dev/pp_cursor_type_dev.h"
#include "ppapi/c/dev/ppp_printing_dev.h"
#include "ppapi/c/ppb_input_event.h"
Expand Down Expand Up @@ -153,12 +153,11 @@ class PDFEngine {
// Creates and returns new URL loader for partial document requests.
virtual pp::URLLoader CreateURLLoader() = 0;

// Calls the client's OnCallback() function in |delay_in_ms| with the given
// |id|.
virtual void ScheduleCallback(int id, int delay_in_ms) = 0;
// Calls the client's OnTouchTimerCallback() function in |delay_in_ms| with
// the given |id|.
virtual void ScheduleTouchTimerCallback(int id, int delay_in_ms) = 0;
// Calls the client's OnCallback() function in |delay| with the given |id|.
virtual void ScheduleCallback(int id, base::TimeDelta delay) = 0;
// Calls the client's OnTouchTimerCallback() function in |delay| with the
// given |id|.
virtual void ScheduleTouchTimerCallback(int id, base::TimeDelta delay) = 0;

// Searches the given string for "term" and returns the results. Unicode-
// aware.
Expand Down
40 changes: 22 additions & 18 deletions pdf/pdfium/pdfium_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ const uint32_t kPendingPageColor = 0xFFEEEEEE;
const uint32_t kFormHighlightColor = 0xFFE4DD;
const int32_t kFormHighlightAlpha = 100;

const int32_t kMaxPasswordTries = 3;
constexpr int kMaxPasswordTries = 3;

const int32_t kTouchLongPressTimeoutMs = 300;
constexpr base::TimeDelta kTouchLongPressTimeout =
base::TimeDelta::FromMilliseconds(300);

// See Table 3.20 in
// http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf
Expand Down Expand Up @@ -694,7 +695,6 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
: client_(client),
current_zoom_(1.0),
current_rotation_(0),
password_tries_remaining_(0),
doc_(nullptr),
form_(nullptr),
defer_page_unload_(false),
Expand All @@ -704,20 +704,14 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
in_form_text_area_(false),
editable_form_text_area_(false),
mouse_left_button_down_(false),
next_page_to_search_(-1),
last_page_to_search_(-1),
last_character_index_to_search_(-1),
permissions_(0),
permissions_handler_revision_(-1),
fpdf_availability_(nullptr),
next_formfill_timer_id_(0),
next_touch_timer_id_(0),
last_page_mouse_down_(-1),
most_visible_page_(-1),
called_do_document_action_(false),
render_grayscale_(false),
render_annots_(true),
getting_password_(false) {
render_annots_(true) {
find_factory_.Initialize(this);
password_factory_.Initialize(this);

Expand Down Expand Up @@ -2720,12 +2714,14 @@ void PDFiumEngine::SetGrayscale(bool grayscale) {
}

void PDFiumEngine::OnCallback(int id) {
if (!formfill_timers_.count(id))
auto it = formfill_timers_.find(id);
if (it == formfill_timers_.end())
return;

formfill_timers_[id].second(id);
if (formfill_timers_.count(id)) // The callback might delete the timer.
client_->ScheduleCallback(id, formfill_timers_[id].first);
it->second.timer_callback(id);
it = formfill_timers_.find(id); // The callback might delete the timer.
if (it != formfill_timers_.end())
client_->ScheduleCallback(id, it->second.timer_period);
}

void PDFiumEngine::OnTouchTimerCallback(int id) {
Expand Down Expand Up @@ -3938,7 +3934,7 @@ bool PDFiumEngine::IsPointInEditableFormTextArea(FPDF_PAGE page,
void PDFiumEngine::ScheduleTouchTimer(const pp::TouchInputEvent& evt) {
touch_timers_[++next_touch_timer_id_] = evt;
client_->ScheduleTouchTimerCallback(next_touch_timer_id_,
kTouchLongPressTimeoutMs);
kTouchLongPressTimeout);
}

void PDFiumEngine::KillTouchTimer(int timer_id) {
Expand Down Expand Up @@ -3996,9 +3992,13 @@ int PDFiumEngine::Form_SetTimer(FPDF_FORMFILLINFO* param,
int elapse,
TimerCallback timer_func) {
PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
engine->formfill_timers_[++engine->next_formfill_timer_id_] =
std::pair<int, TimerCallback>(elapse, timer_func);
engine->client_->ScheduleCallback(engine->next_formfill_timer_id_, elapse);
base::TimeDelta elapse_time = base::TimeDelta::FromMilliseconds(elapse);
engine->formfill_timers_.emplace(
std::piecewise_construct,
std::forward_as_tuple(++engine->next_formfill_timer_id_),
std::forward_as_tuple(elapse_time, timer_func));
engine->client_->ScheduleCallback(engine->next_formfill_timer_id_,
elapse_time);
return engine->next_formfill_timer_id_;
}

Expand Down Expand Up @@ -4281,6 +4281,10 @@ void PDFiumEngine::SetSelectionBounds(const pp::Point& base,
: RangeSelectionDirection::Right;
}

PDFiumEngine::FormFillTimerData::FormFillTimerData(base::TimeDelta period,
TimerCallback callback)
: timer_period(period), timer_callback(callback) {}

ScopedUnsupportedFeature::ScopedUnsupportedFeature(PDFiumEngine* engine)
: old_engine_(g_engine_for_unsupported) {
g_engine_for_unsupported = engine;
Expand Down
41 changes: 23 additions & 18 deletions pdf/pdfium/pdfium_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,10 @@ class PDFiumEngine : public PDFEngine,
pp::CompletionCallbackFactory<PDFiumEngine> find_factory_;

pp::CompletionCallbackFactory<PDFiumEngine> password_factory_;
int32_t password_tries_remaining_;

// The current text used for searching.
std::string current_find_text_;
// Set to true if the user is being prompted for their password. Will be set
// to false after the user finishes getting their password.
bool getting_password_ = false;
int password_tries_remaining_ = 0;

// The PDFium wrapper object for the document.
FPDF_DOCUMENT doc_;
Expand Down Expand Up @@ -706,13 +706,15 @@ class PDFiumEngine : public PDFEngine,
// True if left mouse button is currently being held down.
bool mouse_left_button_down_;

// Used for searching.
// The current text used for searching.
std::string current_find_text_;
// The results found.
std::vector<PDFiumRange> find_results_;
// Which page to search next.
int next_page_to_search_;
int next_page_to_search_ = -1;
// Where to stop searching.
int last_page_to_search_;
int last_character_index_to_search_; // -1 if search until end of page.
int last_page_to_search_ = -1;
int last_character_index_to_search_ = -1; // -1 if search until end of page.
// Which result the user has currently selected.
FindTextIndex current_find_index_;
// Where to resume searching.
Expand All @@ -735,14 +737,20 @@ class PDFiumEngine : public PDFEngine,

pp::Size default_page_size_;

// Used to manage timers that form fill API needs. The pair holds the timer
// period, in ms, and the callback function.
std::map<int, std::pair<int, TimerCallback>> formfill_timers_;
int next_formfill_timer_id_;
// Used to manage timers that form fill API needs. The key is the timer id.
// The value holds the timer period and the callback function.
struct FormFillTimerData {
FormFillTimerData(base::TimeDelta period, TimerCallback callback);

base::TimeDelta timer_period;
TimerCallback timer_callback;
};
std::map<int, const FormFillTimerData> formfill_timers_;
int next_formfill_timer_id_ = 0;

// Used to manage timers for touch long press.
std::map<int, pp::TouchInputEvent> touch_timers_;
int next_touch_timer_id_;
int next_touch_timer_id_ = 0;

// Holds the zero-based page index of the last page that the mouse clicked on.
int last_page_mouse_down_;
Expand Down Expand Up @@ -793,17 +801,14 @@ class PDFiumEngine : public PDFEngine,
// Shadow matrix for generating the page shadow bitmap.
std::unique_ptr<ShadowMatrix> page_shadow_;

// Set to true if the user is being prompted for their password. Will be set
// to false after the user finishes getting their password.
bool getting_password_;

// While true, the document try to be opened and parsed after download each
// part. Else the document will be opened and parsed only on finish of
// downloading.
bool process_when_pending_request_complete_ = true;

enum class RangeSelectionDirection { Left, Right };
RangeSelectionDirection range_selection_direction_;
RangeSelectionDirection range_selection_direction_ =
RangeSelectionDirection::Right;

pp::Point range_selection_base_;

Expand Down
5 changes: 3 additions & 2 deletions pdf/preview_mode_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ pp::URLLoader PreviewModeClient::CreateURLLoader() {
return pp::URLLoader();
}

void PreviewModeClient::ScheduleCallback(int id, int delay_in_ms) {
void PreviewModeClient::ScheduleCallback(int id, base::TimeDelta delay) {
NOTREACHED();
}

void PreviewModeClient::ScheduleTouchTimerCallback(int id, int delay_in_ms) {
void PreviewModeClient::ScheduleTouchTimerCallback(int id,
base::TimeDelta delay) {
NOTREACHED();
}

Expand Down
4 changes: 2 additions & 2 deletions pdf/preview_mode_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class PreviewModeClient : public PDFEngine::Client {
const void* data,
int length) override;
pp::URLLoader CreateURLLoader() override;
void ScheduleCallback(int id, int delay_in_ms) override;
void ScheduleTouchTimerCallback(int id, int delay_in_ms) override;
void ScheduleCallback(int id, base::TimeDelta delay) override;
void ScheduleTouchTimerCallback(int id, base::TimeDelta delay) override;
void SearchString(const base::char16* string,
const base::char16* term,
bool case_sensitive,
Expand Down

0 comments on commit 494f608

Please sign in to comment.