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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> // for min/max() | 10 #include <algorithm> // for min/max() |
11 #define _USE_MATH_DEFINES // for M_PI | 11 #define _USE_MATH_DEFINES // for M_PI |
12 #include <math.h> | 12 #include <math.h> |
13 #include <cmath> // for log() and pow() | 13 #include <cmath> // for log() and pow() |
14 #include <list> | 14 #include <list> |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/values.h" | 22 #include "base/values.h" |
23 #include "chrome/common/content_restriction.h" | 23 #include "chrome/common/content_restriction.h" |
| 24 #include "chrome/common/url_constants.h" |
24 #include "net/base/escape.h" | 25 #include "net/base/escape.h" |
25 #include "pdf/pdf.h" | 26 #include "pdf/pdf.h" |
26 #include "ppapi/c/dev/ppb_cursor_control_dev.h" | 27 #include "ppapi/c/dev/ppb_cursor_control_dev.h" |
27 #include "ppapi/c/pp_errors.h" | 28 #include "ppapi/c/pp_errors.h" |
28 #include "ppapi/c/pp_rect.h" | 29 #include "ppapi/c/pp_rect.h" |
29 #include "ppapi/c/private/ppb_instance_private.h" | 30 #include "ppapi/c/private/ppb_instance_private.h" |
30 #include "ppapi/c/private/ppp_pdf.h" | 31 #include "ppapi/c/private/ppp_pdf.h" |
31 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 32 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
32 #include "ppapi/cpp/core.h" | 33 #include "ppapi/cpp/core.h" |
33 #include "ppapi/cpp/dev/memory_dev.h" | 34 #include "ppapi/cpp/dev/memory_dev.h" |
34 #include "ppapi/cpp/dev/text_input_dev.h" | 35 #include "ppapi/cpp/dev/text_input_dev.h" |
35 #include "ppapi/cpp/dev/url_util_dev.h" | 36 #include "ppapi/cpp/dev/url_util_dev.h" |
36 #include "ppapi/cpp/module.h" | 37 #include "ppapi/cpp/module.h" |
37 #include "ppapi/cpp/point.h" | 38 #include "ppapi/cpp/point.h" |
38 #include "ppapi/cpp/private/pdf.h" | 39 #include "ppapi/cpp/private/pdf.h" |
39 #include "ppapi/cpp/private/var_private.h" | 40 #include "ppapi/cpp/private/var_private.h" |
40 #include "ppapi/cpp/rect.h" | 41 #include "ppapi/cpp/rect.h" |
41 #include "ppapi/cpp/resource.h" | 42 #include "ppapi/cpp/resource.h" |
42 #include "ppapi/cpp/url_request_info.h" | 43 #include "ppapi/cpp/url_request_info.h" |
43 #include "ppapi/cpp/var_array.h" | 44 #include "ppapi/cpp/var_array.h" |
44 #include "ppapi/cpp/var_dictionary.h" | 45 #include "ppapi/cpp/var_dictionary.h" |
45 #include "ui/events/keycodes/keyboard_codes.h" | 46 #include "ui/events/keycodes/keyboard_codes.h" |
46 | 47 |
47 namespace chrome_pdf { | 48 namespace chrome_pdf { |
48 | 49 |
49 namespace { | 50 namespace { |
50 | 51 |
51 const char kChromePrint[] = "chrome://print/"; | |
52 const char kChromeExtension[] = | 52 const char kChromeExtension[] = |
53 "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai"; | 53 "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai"; |
54 | 54 |
55 // Constants used in handling postMessage() messages. | 55 // Constants used in handling postMessage() messages. |
56 const char kType[] = "type"; | 56 const char kType[] = "type"; |
57 // Viewport message arguments. (Page -> Plugin). | 57 // Viewport message arguments. (Page -> Plugin). |
58 const char kJSViewportType[] = "viewport"; | 58 const char kJSViewportType[] = "viewport"; |
59 const char kJSXOffset[] = "xOffset"; | 59 const char kJSXOffset[] = "xOffset"; |
60 const char kJSYOffset[] = "yOffset"; | 60 const char kJSYOffset[] = "yOffset"; |
61 const char kJSZoom[] = "zoom"; | 61 const char kJSZoom[] = "zoom"; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 static_cast<OutOfProcessInstance*>(object); | 211 static_cast<OutOfProcessInstance*>(object); |
212 return obj_instance->EnableAccessibility(); | 212 return obj_instance->EnableAccessibility(); |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 const PPP_Pdf ppp_private = { | 216 const PPP_Pdf ppp_private = { |
217 &GetLinkAtPosition, &Transform, &GetPrintPresetOptionsFromDocument, | 217 &GetLinkAtPosition, &Transform, &GetPrintPresetOptionsFromDocument, |
218 &EnableAccessibility, | 218 &EnableAccessibility, |
219 }; | 219 }; |
220 | 220 |
221 int ExtractPrintPreviewPageIndex(const std::string& src_url) { | 221 int ExtractPrintPreviewPageIndex(base::StringPiece src_url) { |
222 // Sample |src_url| format: chrome://print/id/page_index/print.pdf | 222 // Sample |src_url| format: chrome://print/id/page_index/print.pdf |
223 std::vector<std::string> url_substr = | 223 std::vector<base::StringPiece> url_substr = |
224 base::SplitString(src_url.substr(strlen(kChromePrint)), "/", | 224 base::SplitStringPiece(src_url.substr(strlen(chrome::kChromeUIPrintURL)), |
225 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 225 "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
226 if (url_substr.size() != 3) | 226 if (url_substr.size() != 3) |
227 return -1; | 227 return -1; |
228 | 228 |
229 if (url_substr[2] != "print.pdf") | 229 if (url_substr[2] != "print.pdf") |
230 return -1; | 230 return -1; |
231 | 231 |
232 int page_index = 0; | 232 int page_index = 0; |
233 if (!base::StringToInt(url_substr[1], &page_index)) | 233 if (!base::StringToInt(url_substr[1], &page_index)) |
234 return -1; | 234 return -1; |
235 return page_index; | 235 return page_index; |
236 } | 236 } |
237 | 237 |
238 bool IsPrintPreviewUrl(const std::string& url) { | 238 bool IsPrintPreviewUrl(base::StringPiece url) { |
239 return url.substr(0, strlen(kChromePrint)) == kChromePrint; | 239 return url.starts_with(chrome::kChromeUIPrintURL); |
240 } | 240 } |
241 | 241 |
242 void ScalePoint(float scale, pp::Point* point) { | 242 void ScalePoint(float scale, pp::Point* point) { |
243 point->set_x(static_cast<int>(point->x() * scale)); | 243 point->set_x(static_cast<int>(point->x() * scale)); |
244 point->set_y(static_cast<int>(point->y() * scale)); | 244 point->set_y(static_cast<int>(point->y() * scale)); |
245 } | 245 } |
246 | 246 |
247 void ScaleRect(float scale, pp::Rect* rect) { | 247 void ScaleRect(float scale, pp::Rect* rect) { |
248 int left = static_cast<int>(floorf(rect->x() * scale)); | 248 int left = static_cast<int>(floorf(rect->x() * scale)); |
249 int top = static_cast<int>(floorf(rect->y() * scale)); | 249 int top = static_cast<int>(floorf(rect->y() * scale)); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 const char* argn[], | 320 const char* argn[], |
321 const char* argv[]) { | 321 const char* argv[]) { |
322 // Check if the PDF is being loaded in the PDF chrome extension. We only allow | 322 // Check if the PDF is being loaded in the PDF chrome extension. We only allow |
323 // the plugin to be loaded in the extension and print preview to avoid | 323 // the plugin to be loaded in the extension and print preview to avoid |
324 // exposing sensitive APIs directly to external websites. | 324 // exposing sensitive APIs directly to external websites. |
325 pp::Var document_url_var = pp::URLUtil_Dev::Get()->GetDocumentURL(this); | 325 pp::Var document_url_var = pp::URLUtil_Dev::Get()->GetDocumentURL(this); |
326 if (!document_url_var.is_string()) | 326 if (!document_url_var.is_string()) |
327 return false; | 327 return false; |
328 std::string document_url = document_url_var.AsString(); | 328 std::string document_url = document_url_var.AsString(); |
329 base::StringPiece document_url_piece(document_url); | 329 base::StringPiece document_url_piece(document_url); |
330 is_print_preview_ = document_url_piece.starts_with(kChromePrint); | 330 is_print_preview_ = IsPrintPreviewUrl(document_url_piece); |
331 if (!document_url_piece.starts_with(kChromeExtension) && !is_print_preview_) { | 331 if (!document_url_piece.starts_with(kChromeExtension) && !is_print_preview_) |
332 return false; | 332 return false; |
333 } | |
334 | 333 |
335 // Check if the plugin is full frame. This is passed in from JS. | 334 // Check if the plugin is full frame. This is passed in from JS. |
336 for (uint32_t i = 0; i < argc; ++i) { | 335 for (uint32_t i = 0; i < argc; ++i) { |
337 if (strcmp(argn[i], "full-frame") == 0) { | 336 if (strcmp(argn[i], "full-frame") == 0) { |
338 full_ = true; | 337 full_ = true; |
339 break; | 338 break; |
340 } | 339 } |
341 } | 340 } |
342 | 341 |
343 // Allow the plugin to handle find requests. | 342 // Allow the plugin to handle find requests. |
(...skipping 1222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1566 } | 1565 } |
1567 | 1566 |
1568 void OutOfProcessInstance::IsSelectingChanged(bool is_selecting) { | 1567 void OutOfProcessInstance::IsSelectingChanged(bool is_selecting) { |
1569 pp::VarDictionary message; | 1568 pp::VarDictionary message; |
1570 message.Set(kType, kJSSetIsSelectingType); | 1569 message.Set(kType, kJSSetIsSelectingType); |
1571 message.Set(kJSIsSelecting, pp::Var(is_selecting)); | 1570 message.Set(kJSIsSelecting, pp::Var(is_selecting)); |
1572 PostMessage(message); | 1571 PostMessage(message); |
1573 } | 1572 } |
1574 | 1573 |
1575 void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url, | 1574 void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url, |
1576 int dst_page_index) { | 1575 int dest_page_index) { |
1577 DCHECK(IsPrintPreview()); | 1576 DCHECK(IsPrintPreview()); |
1578 | 1577 |
1579 int src_page_index = ExtractPrintPreviewPageIndex(url); | 1578 int src_page_index = ExtractPrintPreviewPageIndex(url); |
1580 if (src_page_index < 1) | 1579 if (src_page_index < 1) |
1581 return; | 1580 return; |
1582 | 1581 |
1583 preview_pages_info_.push(std::make_pair(url, dst_page_index)); | 1582 preview_pages_info_.push(std::make_pair(url, dest_page_index)); |
1584 LoadAvailablePreviewPage(); | 1583 LoadAvailablePreviewPage(); |
1585 } | 1584 } |
1586 | 1585 |
1587 void OutOfProcessInstance::LoadAvailablePreviewPage() { | 1586 void OutOfProcessInstance::LoadAvailablePreviewPage() { |
1588 if (preview_pages_info_.empty() || | 1587 if (preview_pages_info_.empty() || |
1589 document_load_state_ != LOAD_STATE_COMPLETE) { | 1588 document_load_state_ != LOAD_STATE_COMPLETE) { |
1590 return; | 1589 return; |
1591 } | 1590 } |
1592 | 1591 |
1593 const std::string& url = preview_pages_info_.front().first; | 1592 const std::string& url = preview_pages_info_.front().first; |
1594 int dst_page_index = preview_pages_info_.front().second; | 1593 int dest_page_index = preview_pages_info_.front().second; |
1595 int src_page_index = ExtractPrintPreviewPageIndex(url); | 1594 int src_page_index = ExtractPrintPreviewPageIndex(url); |
1596 if (src_page_index < 1 || dst_page_index >= print_preview_page_count_ || | 1595 if (src_page_index < 1 || dest_page_index >= print_preview_page_count_ || |
1597 preview_document_load_state_ == LOAD_STATE_LOADING) { | 1596 preview_document_load_state_ == LOAD_STATE_LOADING) { |
1598 return; | 1597 return; |
1599 } | 1598 } |
1600 | 1599 |
1601 preview_document_load_state_ = LOAD_STATE_LOADING; | 1600 preview_document_load_state_ = LOAD_STATE_LOADING; |
1602 LoadUrl(url, /*is_print_preview=*/true); | 1601 LoadUrl(url, /*is_print_preview=*/true); |
1603 } | 1602 } |
1604 | 1603 |
1605 void OutOfProcessInstance::UserMetricsRecordAction(const std::string& action) { | 1604 void OutOfProcessInstance::UserMetricsRecordAction(const std::string& action) { |
1606 // TODO(raymes): Move this function to PPB_UMA_Private. | 1605 // TODO(raymes): Move this function to PPB_UMA_Private. |
1607 pp::PDF::UserMetricsRecordAction(this, pp::Var(action)); | 1606 pp::PDF::UserMetricsRecordAction(this, pp::Var(action)); |
1608 } | 1607 } |
1609 | 1608 |
1610 pp::FloatPoint OutOfProcessInstance::BoundScrollOffsetToDocument( | 1609 pp::FloatPoint OutOfProcessInstance::BoundScrollOffsetToDocument( |
1611 const pp::FloatPoint& scroll_offset) { | 1610 const pp::FloatPoint& scroll_offset) { |
1612 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); | 1611 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); |
1613 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f); | 1612 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f); |
1614 float min_y = -top_toolbar_height_; | 1613 float min_y = -top_toolbar_height_; |
1615 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); | 1614 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); |
1616 float y = std::max(std::min(scroll_offset.y(), max_y), min_y); | 1615 float y = std::max(std::min(scroll_offset.y(), max_y), min_y); |
1617 return pp::FloatPoint(x, y); | 1616 return pp::FloatPoint(x, y); |
1618 } | 1617 } |
1619 | 1618 |
1620 } // namespace chrome_pdf | 1619 } // namespace chrome_pdf |
OLD | NEW |