OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "pdf/out_of_process_instance.h" | 5 #include "pdf/out_of_process_instance.h" |
6 | 6 |
7 #include <algorithm> // for min/max() | 7 #include <algorithm> // for min/max() |
8 #define _USE_MATH_DEFINES // for M_PI | 8 #define _USE_MATH_DEFINES // for M_PI |
9 #include <cmath> // for log() and pow() | 9 #include <cmath> // for log() and pow() |
10 #include <math.h> | 10 #include <math.h> |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "ppapi/c/private/ppb_instance_private.h" | 27 #include "ppapi/c/private/ppb_instance_private.h" |
28 #include "ppapi/c/private/ppp_pdf.h" | 28 #include "ppapi/c/private/ppp_pdf.h" |
29 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 29 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
30 #include "ppapi/cpp/core.h" | 30 #include "ppapi/cpp/core.h" |
31 #include "ppapi/cpp/dev/memory_dev.h" | 31 #include "ppapi/cpp/dev/memory_dev.h" |
32 #include "ppapi/cpp/dev/text_input_dev.h" | 32 #include "ppapi/cpp/dev/text_input_dev.h" |
33 #include "ppapi/cpp/dev/url_util_dev.h" | 33 #include "ppapi/cpp/dev/url_util_dev.h" |
34 #include "ppapi/cpp/module.h" | 34 #include "ppapi/cpp/module.h" |
35 #include "ppapi/cpp/point.h" | 35 #include "ppapi/cpp/point.h" |
36 #include "ppapi/cpp/private/pdf.h" | 36 #include "ppapi/cpp/private/pdf.h" |
| 37 #include "ppapi/cpp/private/var_private.h" |
37 #include "ppapi/cpp/rect.h" | 38 #include "ppapi/cpp/rect.h" |
38 #include "ppapi/cpp/resource.h" | 39 #include "ppapi/cpp/resource.h" |
39 #include "ppapi/cpp/url_request_info.h" | 40 #include "ppapi/cpp/url_request_info.h" |
40 #include "ppapi/cpp/var_array.h" | 41 #include "ppapi/cpp/var_array.h" |
41 #include "ppapi/cpp/var_dictionary.h" | 42 #include "ppapi/cpp/var_dictionary.h" |
42 #include "ui/events/keycodes/keyboard_codes.h" | 43 #include "ui/events/keycodes/keyboard_codes.h" |
43 | 44 |
44 #if defined(OS_MACOSX) | 45 #if defined(OS_MACOSX) |
45 #include "base/mac/mac_util.h" | 46 #include "base/mac/mac_util.h" |
46 #endif | 47 #endif |
(...skipping 16 matching lines...) Expand all Loading... |
63 const char kAccessibleLoaded[] = "loaded"; | 64 const char kAccessibleLoaded[] = "loaded"; |
64 const char kAccessibleCopyable[] = "copyable"; | 65 const char kAccessibleCopyable[] = "copyable"; |
65 | 66 |
66 // Constants used in handling postMessage() messages. | 67 // Constants used in handling postMessage() messages. |
67 const char* kType = "type"; | 68 const char* kType = "type"; |
68 // Viewport message arguments. (Page -> Plugin). | 69 // Viewport message arguments. (Page -> Plugin). |
69 const char* kJSViewportType = "viewport"; | 70 const char* kJSViewportType = "viewport"; |
70 const char* kJSXOffset = "xOffset"; | 71 const char* kJSXOffset = "xOffset"; |
71 const char* kJSYOffset = "yOffset"; | 72 const char* kJSYOffset = "yOffset"; |
72 const char* kJSZoom = "zoom"; | 73 const char* kJSZoom = "zoom"; |
| 74 // Stop scrolling message (Page -> Plugin) |
| 75 const char* kJSStopScrollingType = "stopScrolling"; |
73 // Document dimension arguments (Plugin -> Page). | 76 // Document dimension arguments (Plugin -> Page). |
74 const char* kJSDocumentDimensionsType = "documentDimensions"; | 77 const char* kJSDocumentDimensionsType = "documentDimensions"; |
75 const char* kJSDocumentWidth = "width"; | 78 const char* kJSDocumentWidth = "width"; |
76 const char* kJSDocumentHeight = "height"; | 79 const char* kJSDocumentHeight = "height"; |
77 const char* kJSPageDimensions = "pageDimensions"; | 80 const char* kJSPageDimensions = "pageDimensions"; |
78 const char* kJSPageX = "x"; | 81 const char* kJSPageX = "x"; |
79 const char* kJSPageY = "y"; | 82 const char* kJSPageY = "y"; |
80 const char* kJSPageWidth = "width"; | 83 const char* kJSPageWidth = "width"; |
81 const char* kJSPageHeight = "height"; | 84 const char* kJSPageHeight = "height"; |
82 // Document load progress arguments (Plugin -> Page) | 85 // Document load progress arguments (Plugin -> Page) |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 paint_manager_(this, this, true), | 240 paint_manager_(this, this, true), |
238 first_paint_(true), | 241 first_paint_(true), |
239 document_load_state_(LOAD_STATE_LOADING), | 242 document_load_state_(LOAD_STATE_LOADING), |
240 preview_document_load_state_(LOAD_STATE_COMPLETE), | 243 preview_document_load_state_(LOAD_STATE_COMPLETE), |
241 uma_(this), | 244 uma_(this), |
242 told_browser_about_unsupported_feature_(false), | 245 told_browser_about_unsupported_feature_(false), |
243 print_preview_page_count_(0), | 246 print_preview_page_count_(0), |
244 last_progress_sent_(0), | 247 last_progress_sent_(0), |
245 recently_sent_find_update_(false), | 248 recently_sent_find_update_(false), |
246 received_viewport_message_(false), | 249 received_viewport_message_(false), |
247 did_call_start_loading_(false) { | 250 did_call_start_loading_(false), |
| 251 stop_scrolling_(false) { |
248 loader_factory_.Initialize(this); | 252 loader_factory_.Initialize(this); |
249 timer_factory_.Initialize(this); | 253 timer_factory_.Initialize(this); |
250 form_factory_.Initialize(this); | 254 form_factory_.Initialize(this); |
251 print_callback_factory_.Initialize(this); | 255 print_callback_factory_.Initialize(this); |
252 engine_.reset(PDFEngine::Create(this)); | 256 engine_.reset(PDFEngine::Create(this)); |
253 pp::Module::Get()->AddPluginInterface(kPPPPdfInterface, &ppp_private); | 257 pp::Module::Get()->AddPluginInterface(kPPPPdfInterface, &ppp_private); |
254 AddPerInstanceObject(kPPPPdfInterface, this); | 258 AddPerInstanceObject(kPPPPdfInterface, this); |
255 | 259 |
256 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_MOUSE); | 260 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_MOUSE); |
257 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); | 261 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 return; | 352 return; |
349 } | 353 } |
350 | 354 |
351 std::string type = dict.Get(kType).AsString(); | 355 std::string type = dict.Get(kType).AsString(); |
352 | 356 |
353 if (type == kJSViewportType && | 357 if (type == kJSViewportType && |
354 dict.Get(pp::Var(kJSXOffset)).is_int() && | 358 dict.Get(pp::Var(kJSXOffset)).is_int() && |
355 dict.Get(pp::Var(kJSYOffset)).is_int() && | 359 dict.Get(pp::Var(kJSYOffset)).is_int() && |
356 dict.Get(pp::Var(kJSZoom)).is_number()) { | 360 dict.Get(pp::Var(kJSZoom)).is_number()) { |
357 received_viewport_message_ = true; | 361 received_viewport_message_ = true; |
| 362 stop_scrolling_ = false; |
358 double zoom = dict.Get(pp::Var(kJSZoom)).AsDouble(); | 363 double zoom = dict.Get(pp::Var(kJSZoom)).AsDouble(); |
359 int x = dict.Get(pp::Var(kJSXOffset)).AsInt(); | 364 pp::Point scroll_offset(dict.Get(pp::Var(kJSXOffset)).AsInt(), |
360 int y = dict.Get(pp::Var(kJSYOffset)).AsInt(); | 365 dict.Get(pp::Var(kJSYOffset)).AsInt()); |
361 | 366 |
362 // Bound the input parameters. | 367 // Bound the input parameters. |
363 zoom = std::max(kMinZoom, zoom); | 368 zoom = std::max(kMinZoom, zoom); |
364 int max_x = document_size_.width() * zoom - plugin_dip_size_.width(); | |
365 x = std::max(std::min(x, max_x), 0); | |
366 int max_y = document_size_.height() * zoom - plugin_dip_size_.height(); | |
367 y = std::max(std::min(y, max_y), 0); | |
368 | |
369 SetZoom(zoom); | 369 SetZoom(zoom); |
370 engine_->ScrolledToXPosition(x * device_scale_); | 370 scroll_offset = BoundScrollOffsetToDocument(scroll_offset); |
371 engine_->ScrolledToYPosition(y * device_scale_); | 371 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_); |
| 372 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_); |
372 } else if (type == kJSGetPasswordCompleteType && | 373 } else if (type == kJSGetPasswordCompleteType && |
373 dict.Get(pp::Var(kJSPassword)).is_string()) { | 374 dict.Get(pp::Var(kJSPassword)).is_string()) { |
374 if (password_callback_) { | 375 if (password_callback_) { |
375 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_; | 376 pp::CompletionCallbackWithOutput<pp::Var> callback = *password_callback_; |
376 password_callback_.reset(); | 377 password_callback_.reset(); |
377 *callback.output() = dict.Get(pp::Var(kJSPassword)).pp_var(); | 378 *callback.output() = dict.Get(pp::Var(kJSPassword)).pp_var(); |
378 callback.Run(PP_OK); | 379 callback.Run(PP_OK); |
379 } else { | 380 } else { |
380 NOTREACHED(); | 381 NOTREACHED(); |
381 } | 382 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 document_load_state_ != LOAD_STATE_LOADING); | 419 document_load_state_ != LOAD_STATE_LOADING); |
419 bool has_permissions = | 420 bool has_permissions = |
420 engine_->HasPermission(PDFEngine::PERMISSION_COPY) || | 421 engine_->HasPermission(PDFEngine::PERMISSION_COPY) || |
421 engine_->HasPermission(PDFEngine::PERMISSION_COPY_ACCESSIBLE); | 422 engine_->HasPermission(PDFEngine::PERMISSION_COPY_ACCESSIBLE); |
422 node.SetBoolean(kAccessibleCopyable, has_permissions); | 423 node.SetBoolean(kAccessibleCopyable, has_permissions); |
423 std::string json; | 424 std::string json; |
424 base::JSONWriter::Write(&node, &json); | 425 base::JSONWriter::Write(&node, &json); |
425 reply.Set(pp::Var(kJSAccessibilityJSON), pp::Var(json)); | 426 reply.Set(pp::Var(kJSAccessibilityJSON), pp::Var(json)); |
426 } | 427 } |
427 PostMessage(reply); | 428 PostMessage(reply); |
| 429 } else if (type == kJSStopScrollingType) { |
| 430 stop_scrolling_ = true; |
428 } else { | 431 } else { |
429 NOTREACHED(); | 432 NOTREACHED(); |
430 } | 433 } |
431 } | 434 } |
432 | 435 |
433 bool OutOfProcessInstance::HandleInputEvent( | 436 bool OutOfProcessInstance::HandleInputEvent( |
434 const pp::InputEvent& event) { | 437 const pp::InputEvent& event) { |
435 // To simplify things, convert the event into device coordinates if it is | 438 // To simplify things, convert the event into device coordinates if it is |
436 // a mouse event. | 439 // a mouse event. |
437 pp::InputEvent event_device_res(event); | 440 pp::InputEvent event_device_res(event); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 return (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN); | 503 return (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN); |
501 } | 504 } |
502 | 505 |
503 void OutOfProcessInstance::DidChangeView(const pp::View& view) { | 506 void OutOfProcessInstance::DidChangeView(const pp::View& view) { |
504 pp::Rect view_rect(view.GetRect()); | 507 pp::Rect view_rect(view.GetRect()); |
505 float old_device_scale = device_scale_; | 508 float old_device_scale = device_scale_; |
506 float device_scale = view.GetDeviceScale(); | 509 float device_scale = view.GetDeviceScale(); |
507 pp::Size view_device_size(view_rect.width() * device_scale, | 510 pp::Size view_device_size(view_rect.width() * device_scale, |
508 view_rect.height() * device_scale); | 511 view_rect.height() * device_scale); |
509 | 512 |
510 if (view_device_size == plugin_size_ && device_scale == device_scale_) | 513 if (view_device_size != plugin_size_ || device_scale != device_scale_) { |
511 return; // We don't care about the position, only the size. | 514 device_scale_ = device_scale; |
| 515 plugin_dip_size_ = view_rect.size(); |
| 516 plugin_size_ = view_device_size; |
512 | 517 |
513 device_scale_ = device_scale; | 518 paint_manager_.SetSize(view_device_size, device_scale_); |
514 plugin_dip_size_ = view_rect.size(); | |
515 plugin_size_ = view_device_size; | |
516 | 519 |
517 paint_manager_.SetSize(view_device_size, device_scale_); | 520 pp::Size new_image_data_size = PaintManager::GetNewContextSize( |
| 521 image_data_.size(), |
| 522 plugin_size_); |
| 523 if (new_image_data_size != image_data_.size()) { |
| 524 image_data_ = pp::ImageData(this, |
| 525 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 526 new_image_data_size, |
| 527 false); |
| 528 first_paint_ = true; |
| 529 } |
518 | 530 |
519 pp::Size new_image_data_size = PaintManager::GetNewContextSize( | 531 if (image_data_.is_null()) { |
520 image_data_.size(), | 532 DCHECK(plugin_size_.IsEmpty()); |
521 plugin_size_); | 533 return; |
522 if (new_image_data_size != image_data_.size()) { | 534 } |
523 image_data_ = pp::ImageData(this, | 535 |
524 PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 536 OnGeometryChanged(zoom_, old_device_scale); |
525 new_image_data_size, | |
526 false); | |
527 first_paint_ = true; | |
528 } | 537 } |
529 | 538 |
530 if (image_data_.is_null()) { | 539 if (!stop_scrolling_) { |
531 DCHECK(plugin_size_.IsEmpty()); | 540 pp::Point scroll_offset( |
532 return; | 541 BoundScrollOffsetToDocument(view.GetScrollOffset())); |
| 542 engine_->ScrolledToXPosition(scroll_offset.x() * device_scale_); |
| 543 engine_->ScrolledToYPosition(scroll_offset.y() * device_scale_); |
533 } | 544 } |
534 | |
535 OnGeometryChanged(zoom_, old_device_scale); | |
536 } | 545 } |
537 | 546 |
538 pp::Var OutOfProcessInstance::GetLinkAtPosition( | 547 pp::Var OutOfProcessInstance::GetLinkAtPosition( |
539 const pp::Point& point) { | 548 const pp::Point& point) { |
540 pp::Point offset_point(point); | 549 pp::Point offset_point(point); |
541 ScalePoint(device_scale_, &offset_point); | 550 ScalePoint(device_scale_, &offset_point); |
542 offset_point.set_x(offset_point.x() - available_area_.x()); | 551 offset_point.set_x(offset_point.x() - available_area_.x()); |
543 return engine_->GetLinkAtPosition(offset_point); | 552 return engine_->GetLinkAtPosition(offset_point); |
544 } | 553 } |
545 | 554 |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 OnGeometryChanged(zoom_, device_scale_); | 788 OnGeometryChanged(zoom_, device_scale_); |
780 } | 789 } |
781 | 790 |
782 void OutOfProcessInstance::Invalidate(const pp::Rect& rect) { | 791 void OutOfProcessInstance::Invalidate(const pp::Rect& rect) { |
783 pp::Rect offset_rect(rect); | 792 pp::Rect offset_rect(rect); |
784 offset_rect.Offset(available_area_.point()); | 793 offset_rect.Offset(available_area_.point()); |
785 paint_manager_.InvalidateRect(offset_rect); | 794 paint_manager_.InvalidateRect(offset_rect); |
786 } | 795 } |
787 | 796 |
788 void OutOfProcessInstance::Scroll(const pp::Point& point) { | 797 void OutOfProcessInstance::Scroll(const pp::Point& point) { |
789 paint_manager_.ScrollRect(available_area_, point); | 798 if (!image_data_.is_null()) |
| 799 paint_manager_.ScrollRect(available_area_, point); |
790 } | 800 } |
791 | 801 |
792 void OutOfProcessInstance::ScrollToX(int x) { | 802 void OutOfProcessInstance::ScrollToX(int x) { |
793 pp::VarDictionary position; | 803 pp::VarDictionary position; |
794 position.Set(kType, kJSSetScrollPositionType); | 804 position.Set(kType, kJSSetScrollPositionType); |
795 position.Set(kJSPositionX, pp::Var(x / device_scale_)); | 805 position.Set(kJSPositionX, pp::Var(x / device_scale_)); |
796 PostMessage(position); | 806 PostMessage(position); |
797 } | 807 } |
798 | 808 |
799 void OutOfProcessInstance::ScrollToY(int y) { | 809 void OutOfProcessInstance::ScrollToY(int y) { |
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1397 preview_document_load_state_ = LOAD_STATE_LOADING; | 1407 preview_document_load_state_ = LOAD_STATE_LOADING; |
1398 LoadPreviewUrl(url); | 1408 LoadPreviewUrl(url); |
1399 } | 1409 } |
1400 | 1410 |
1401 void OutOfProcessInstance::UserMetricsRecordAction( | 1411 void OutOfProcessInstance::UserMetricsRecordAction( |
1402 const std::string& action) { | 1412 const std::string& action) { |
1403 // TODO(raymes): Move this function to PPB_UMA_Private. | 1413 // TODO(raymes): Move this function to PPB_UMA_Private. |
1404 pp::PDF::UserMetricsRecordAction(this, pp::Var(action)); | 1414 pp::PDF::UserMetricsRecordAction(this, pp::Var(action)); |
1405 } | 1415 } |
1406 | 1416 |
| 1417 pp::Point OutOfProcessInstance::BoundScrollOffsetToDocument( |
| 1418 const pp::Point& scroll_offset) { |
| 1419 int max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); |
| 1420 int x = std::max(std::min(scroll_offset.x(), max_x), 0); |
| 1421 int max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); |
| 1422 int y = std::max(std::min(scroll_offset.y(), max_y), 0); |
| 1423 return pp::Point(x, y); |
| 1424 } |
| 1425 |
1407 } // namespace chrome_pdf | 1426 } // namespace chrome_pdf |
OLD | NEW |