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

Side by Side Diff: pdf/out_of_process_instance.cc

Issue 2855423002: Use more StringPieces in the PDF plugin. (Closed)
Patch Set: Created 3 years, 7 months 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
« pdf/out_of_process_instance.h ('K') | « pdf/out_of_process_instance.h ('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 // 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
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 const char* argn[], 323 const char* argn[],
324 const char* argv[]) { 324 const char* argv[]) {
325 // Check if the PDF is being loaded in the PDF chrome extension. We only allow 325 // Check if the PDF is being loaded in the PDF chrome extension. We only allow
326 // the plugin to be loaded in the extension and print preview to avoid 326 // the plugin to be loaded in the extension and print preview to avoid
327 // exposing sensitive APIs directly to external websites. 327 // exposing sensitive APIs directly to external websites.
328 pp::Var document_url_var = pp::URLUtil_Dev::Get()->GetDocumentURL(this); 328 pp::Var document_url_var = pp::URLUtil_Dev::Get()->GetDocumentURL(this);
329 if (!document_url_var.is_string()) 329 if (!document_url_var.is_string())
330 return false; 330 return false;
331 std::string document_url = document_url_var.AsString(); 331 std::string document_url = document_url_var.AsString();
332 base::StringPiece document_url_piece(document_url); 332 base::StringPiece document_url_piece(document_url);
333 is_print_preview_ = document_url_piece.starts_with(kChromePrint); 333 is_print_preview_ = IsPrintPreviewUrl(document_url_piece);
334 if (!document_url_piece.starts_with(kChromeExtension) && !is_print_preview_) { 334 if (!document_url_piece.starts_with(kChromeExtension) && !is_print_preview_)
335 return false; 335 return false;
336 }
337 336
338 // Check if the plugin is full frame. This is passed in from JS. 337 // Check if the plugin is full frame. This is passed in from JS.
339 for (uint32_t i = 0; i < argc; ++i) { 338 for (uint32_t i = 0; i < argc; ++i) {
340 if (strcmp(argn[i], "full-frame") == 0) { 339 if (strcmp(argn[i], "full-frame") == 0) {
341 full_ = true; 340 full_ = true;
342 break; 341 break;
343 } 342 }
344 } 343 }
345 344
346 // Allow the plugin to handle find requests. 345 // Allow the plugin to handle find requests.
(...skipping 1229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 } 1575 }
1577 1576
1578 void OutOfProcessInstance::IsSelectingChanged(bool is_selecting) { 1577 void OutOfProcessInstance::IsSelectingChanged(bool is_selecting) {
1579 pp::VarDictionary message; 1578 pp::VarDictionary message;
1580 message.Set(kType, kJSSetIsSelectingType); 1579 message.Set(kType, kJSSetIsSelectingType);
1581 message.Set(kJSIsSelecting, pp::Var(is_selecting)); 1580 message.Set(kJSIsSelecting, pp::Var(is_selecting));
1582 PostMessage(message); 1581 PostMessage(message);
1583 } 1582 }
1584 1583
1585 void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url, 1584 void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url,
1586 int dst_page_index) { 1585 int dest_page_index) {
1587 DCHECK(IsPrintPreview()); 1586 DCHECK(IsPrintPreview());
1588 1587
1589 int src_page_index = ExtractPrintPreviewPageIndex(url); 1588 int src_page_index = ExtractPrintPreviewPageIndex(url);
1590 if (src_page_index < 1) 1589 if (src_page_index < 1)
1591 return; 1590 return;
1592 1591
1593 preview_pages_info_.push(std::make_pair(url, dst_page_index)); 1592 preview_pages_info_.push(std::make_pair(url, dest_page_index));
1594 LoadAvailablePreviewPage(); 1593 LoadAvailablePreviewPage();
1595 } 1594 }
1596 1595
1597 void OutOfProcessInstance::LoadAvailablePreviewPage() { 1596 void OutOfProcessInstance::LoadAvailablePreviewPage() {
1598 if (preview_pages_info_.empty() || 1597 if (preview_pages_info_.empty() ||
1599 document_load_state_ != LOAD_STATE_COMPLETE) { 1598 document_load_state_ != LOAD_STATE_COMPLETE) {
1600 return; 1599 return;
1601 } 1600 }
1602 1601
1603 std::string url = preview_pages_info_.front().first; 1602 std::string url = preview_pages_info_.front().first;
1604 int dst_page_index = preview_pages_info_.front().second; 1603 int dest_page_index = preview_pages_info_.front().second;
1605 int src_page_index = ExtractPrintPreviewPageIndex(url); 1604 int src_page_index = ExtractPrintPreviewPageIndex(url);
1606 if (src_page_index < 1 || dst_page_index >= print_preview_page_count_ || 1605 if (src_page_index < 1 || dest_page_index >= print_preview_page_count_ ||
1607 preview_document_load_state_ == LOAD_STATE_LOADING) { 1606 preview_document_load_state_ == LOAD_STATE_LOADING) {
1608 return; 1607 return;
1609 } 1608 }
1610 1609
1611 preview_document_load_state_ = LOAD_STATE_LOADING; 1610 preview_document_load_state_ = LOAD_STATE_LOADING;
1612 LoadPreviewUrl(url); 1611 LoadPreviewUrl(url);
1613 } 1612 }
1614 1613
1615 void OutOfProcessInstance::UserMetricsRecordAction(const std::string& action) { 1614 void OutOfProcessInstance::UserMetricsRecordAction(const std::string& action) {
1616 // TODO(raymes): Move this function to PPB_UMA_Private. 1615 // TODO(raymes): Move this function to PPB_UMA_Private.
1617 pp::PDF::UserMetricsRecordAction(this, pp::Var(action)); 1616 pp::PDF::UserMetricsRecordAction(this, pp::Var(action));
1618 } 1617 }
1619 1618
1620 pp::FloatPoint OutOfProcessInstance::BoundScrollOffsetToDocument( 1619 pp::FloatPoint OutOfProcessInstance::BoundScrollOffsetToDocument(
1621 const pp::FloatPoint& scroll_offset) { 1620 const pp::FloatPoint& scroll_offset) {
1622 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width(); 1621 float max_x = document_size_.width() * zoom_ - plugin_dip_size_.width();
1623 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f); 1622 float x = std::max(std::min(scroll_offset.x(), max_x), 0.0f);
1624 float min_y = -top_toolbar_height_; 1623 float min_y = -top_toolbar_height_;
1625 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height(); 1624 float max_y = document_size_.height() * zoom_ - plugin_dip_size_.height();
1626 float y = std::max(std::min(scroll_offset.y(), max_y), min_y); 1625 float y = std::max(std::min(scroll_offset.y(), max_y), min_y);
1627 return pp::FloatPoint(x, y); 1626 return pp::FloatPoint(x, y);
1628 } 1627 }
1629 1628
1630 } // namespace chrome_pdf 1629 } // namespace chrome_pdf
OLDNEW
« pdf/out_of_process_instance.h ('K') | « pdf/out_of_process_instance.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698