OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/pdfium/pdfium_page.h" | 5 #include "pdf/pdfium/pdfium_page.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" |
14 #include "pdf/pdfium/pdfium_engine.h" | 15 #include "pdf/pdfium/pdfium_engine.h" |
15 | 16 |
16 // Used when doing hit detection. | 17 // Used when doing hit detection. |
17 #define kTolerance 20.0 | 18 #define kTolerance 20.0 |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // Dictionary Value key names for returning the accessible page content as JSON. | 22 // Dictionary Value key names for returning the accessible page content as JSON. |
22 const char kPageWidth[] = "width"; | 23 const char kPageWidth[] = "width"; |
23 const char kPageHeight[] = "height"; | 24 const char kPageHeight[] = "height"; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 base::ListValue* text_nodes = new base::ListValue(); | 191 base::ListValue* text_nodes = new base::ListValue(); |
191 | 192 |
192 if (area == DOCLINK_AREA) { | 193 if (area == DOCLINK_AREA) { |
193 std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page); | 194 std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page); |
194 text_nodes->Append(CreateURLNode(text_utf8, url)); | 195 text_nodes->Append(CreateURLNode(text_utf8, url)); |
195 } else if (area == WEBLINK_AREA && link) { | 196 } else if (area == WEBLINK_AREA && link) { |
196 text_nodes->Append(CreateURLNode(text_utf8, targets[0].url)); | 197 text_nodes->Append(CreateURLNode(text_utf8, targets[0].url)); |
197 } else if (area == WEBLINK_AREA && !link) { | 198 } else if (area == WEBLINK_AREA && !link) { |
198 size_t start = 0; | 199 size_t start = 0; |
199 for (size_t i = 0; i < targets.size(); ++i) { | 200 for (size_t i = 0; i < targets.size(); ++i) { |
200 // Remove the extra NULL character at end. | 201 // If there is an extra NULL character at end, find() will not return any |
201 // Otherwise, find() will not return any matches. | 202 // matches. There should not be any though. |
202 if (targets[i].url.size() > 0 && | 203 if (!targets[i].url.empty()) |
203 targets[i].url[targets[i].url.size() - 1] == '\0') { | 204 DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0'); |
204 targets[i].url.resize(targets[i].url.size() - 1); | |
205 } | |
206 // There should only ever be one NULL character | |
207 DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0'); | |
208 | 205 |
209 // PDFium may change the case of generated links. | 206 // PDFium may change the case of generated links. |
210 std::string lowerCaseURL = base::StringToLowerASCII(targets[i].url); | 207 std::string lowerCaseURL = base::StringToLowerASCII(targets[i].url); |
211 std::string lowerCaseText = base::StringToLowerASCII(text_utf8); | 208 std::string lowerCaseText = base::StringToLowerASCII(text_utf8); |
212 size_t pos = lowerCaseText.find(lowerCaseURL, start); | 209 size_t pos = lowerCaseText.find(lowerCaseURL, start); |
213 size_t length = targets[i].url.size(); | 210 size_t length = targets[i].url.size(); |
214 if (pos == std::string::npos) { | 211 if (pos == std::string::npos) { |
215 // Check if the link is a "mailto:" URL | 212 // Check if the link is a "mailto:" URL |
216 if (lowerCaseURL.compare(0, 7, "mailto:") == 0) { | 213 if (lowerCaseURL.compare(0, 7, "mailto:") == 0) { |
217 pos = lowerCaseText.find(lowerCaseURL.substr(7), start); | 214 pos = lowerCaseText.find(lowerCaseURL.substr(7), start); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 if (dest) | 313 if (dest) |
317 return GetDestinationTarget(dest, target); | 314 return GetDestinationTarget(dest, target); |
318 // TODO(gene): We don't fully support all types of the in-document | 315 // TODO(gene): We don't fully support all types of the in-document |
319 // links. Need to implement that. There is a bug to track that: | 316 // links. Need to implement that. There is a bug to track that: |
320 // http://code.google.com/p/chromium/issues/detail?id=55776 | 317 // http://code.google.com/p/chromium/issues/detail?id=55776 |
321 } break; | 318 } break; |
322 case PDFACTION_URI: { | 319 case PDFACTION_URI: { |
323 if (target) { | 320 if (target) { |
324 size_t buffer_size = | 321 size_t buffer_size = |
325 FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0); | 322 FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0); |
326 if (buffer_size > 1) { | 323 if (buffer_size > 0) { |
327 void* data = WriteInto(&target->url, buffer_size); | 324 PDFiumAPIStringBufferAdapter<std::string> api_string_adapter( |
328 FPDFAction_GetURIPath(engine_->doc(), action, data, buffer_size); | 325 &target->url, buffer_size, true); |
| 326 void* data = api_string_adapter.GetData(); |
| 327 size_t bytes_written = FPDFAction_GetURIPath( |
| 328 engine_->doc(), action, data, buffer_size); |
| 329 api_string_adapter.Close(bytes_written); |
329 } | 330 } |
330 } | 331 } |
331 return WEBLINK_AREA; | 332 return WEBLINK_AREA; |
332 } break; | 333 } break; |
333 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH | 334 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH |
334 // at the moment. | 335 // at the moment. |
335 } | 336 } |
336 } | 337 } |
337 | 338 |
338 return NONSELECTABLE_AREA; | 339 return NONSELECTABLE_AREA; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 if (calculated_links_) | 401 if (calculated_links_) |
401 return; | 402 return; |
402 | 403 |
403 calculated_links_ = true; | 404 calculated_links_ = true; |
404 FPDF_PAGELINK links = FPDFLink_LoadWebLinks(GetTextPage()); | 405 FPDF_PAGELINK links = FPDFLink_LoadWebLinks(GetTextPage()); |
405 int count = FPDFLink_CountWebLinks(links); | 406 int count = FPDFLink_CountWebLinks(links); |
406 for (int i = 0; i < count; ++i) { | 407 for (int i = 0; i < count; ++i) { |
407 base::string16 url; | 408 base::string16 url; |
408 int url_length = FPDFLink_GetURL(links, i, NULL, 0); | 409 int url_length = FPDFLink_GetURL(links, i, NULL, 0); |
409 if (url_length > 0) { | 410 if (url_length > 0) { |
| 411 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter( |
| 412 &url, url_length, true); |
410 unsigned short* data = | 413 unsigned short* data = |
411 reinterpret_cast<unsigned short*>(WriteInto(&url, url_length + 1)); | 414 reinterpret_cast<unsigned short*>(api_string_adapter.GetData()); |
412 FPDFLink_GetURL(links, i, data, url_length); | 415 int actual_length = FPDFLink_GetURL(links, i, data, url_length); |
| 416 api_string_adapter.Close(actual_length); |
413 } | 417 } |
414 Link link; | 418 Link link; |
415 link.url = base::UTF16ToUTF8(url); | 419 link.url = base::UTF16ToUTF8(url); |
416 | 420 |
417 // If the link cannot be converted to a pp::Var, then it is not possible to | 421 // If the link cannot be converted to a pp::Var, then it is not possible to |
418 // pass it to JS. In this case, ignore the link like other PDF viewers. | 422 // pass it to JS. In this case, ignore the link like other PDF viewers. |
419 // See http://crbug.com/312882 for an example. | 423 // See http://crbug.com/312882 for an example. |
420 pp::Var link_var(link.url); | 424 pp::Var link_var(link.url); |
421 if (!link_var.is_string()) | 425 if (!link_var.is_string()) |
422 continue; | 426 continue; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 page_->loading_count_--; | 500 page_->loading_count_--; |
497 } | 501 } |
498 | 502 |
499 PDFiumPage::Link::Link() { | 503 PDFiumPage::Link::Link() { |
500 } | 504 } |
501 | 505 |
502 PDFiumPage::Link::~Link() { | 506 PDFiumPage::Link::~Link() { |
503 } | 507 } |
504 | 508 |
505 } // namespace chrome_pdf | 509 } // namespace chrome_pdf |
OLD | NEW |