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_engine.h" | 14 #include "pdf/pdfium/pdfium_engine.h" |
15 | 15 |
16 // Used when doing hit detection. | 16 // Used when doing hit detection. |
17 #define kTolerance 20.0 | 17 #define kTolerance 20.0 |
18 | 18 |
| 19 namespace { |
| 20 |
19 // Dictionary Value key names for returning the accessible page content as JSON. | 21 // Dictionary Value key names for returning the accessible page content as JSON. |
20 const char kPageWidth[] = "width"; | 22 const char kPageWidth[] = "width"; |
21 const char kPageHeight[] = "height"; | 23 const char kPageHeight[] = "height"; |
22 const char kPageTextBox[] = "textBox"; | 24 const char kPageTextBox[] = "textBox"; |
23 const char kTextBoxLeft[] = "left"; | 25 const char kTextBoxLeft[] = "left"; |
24 const char kTextBoxTop[] = "top"; | 26 const char kTextBoxTop[] = "top"; |
25 const char kTextBoxWidth[] = "width"; | 27 const char kTextBoxWidth[] = "width"; |
26 const char kTextBoxHeight[] = "height"; | 28 const char kTextBoxHeight[] = "height"; |
27 const char kTextBoxFontSize[] = "fontSize"; | 29 const char kTextBoxFontSize[] = "fontSize"; |
28 const char kTextBoxNodes[] = "textNodes"; | 30 const char kTextBoxNodes[] = "textNodes"; |
29 const char kTextNodeType[] = "type"; | 31 const char kTextNodeType[] = "type"; |
30 const char kTextNodeText[] = "text"; | 32 const char kTextNodeText[] = "text"; |
31 const char kTextNodeURL[] = "url"; | 33 const char kTextNodeURL[] = "url"; |
32 const char kTextNodeTypeText[] = "text"; | 34 const char kTextNodeTypeText[] = "text"; |
33 const char kTextNodeTypeURL[] = "url"; | 35 const char kTextNodeTypeURL[] = "url"; |
34 const char kDocLinkURLPrefix[] = "#page"; | 36 const char kDocLinkURLPrefix[] = "#page"; |
35 | 37 |
| 38 } // namespace |
| 39 |
36 namespace chrome_pdf { | 40 namespace chrome_pdf { |
37 | 41 |
38 PDFiumPage::PDFiumPage(PDFiumEngine* engine, | 42 PDFiumPage::PDFiumPage(PDFiumEngine* engine, |
39 int i, | 43 int i, |
40 const pp::Rect& r, | 44 const pp::Rect& r, |
41 bool available) | 45 bool available) |
42 : engine_(engine), | 46 : engine_(engine), |
43 page_(NULL), | 47 page_(NULL), |
44 text_page_(NULL), | 48 text_page_(NULL), |
45 index_(i), | 49 index_(i), |
| 50 loading_count_(0), |
46 rect_(r), | 51 rect_(r), |
47 calculated_links_(false), | 52 calculated_links_(false), |
48 available_(available) { | 53 available_(available) { |
49 } | 54 } |
50 | 55 |
51 PDFiumPage::~PDFiumPage() { | 56 PDFiumPage::~PDFiumPage() { |
| 57 DCHECK_EQ(0, loading_count_); |
52 } | 58 } |
53 | 59 |
54 void PDFiumPage::Unload() { | 60 void PDFiumPage::Unload() { |
| 61 // Do not unload while in the middle of a load. |
| 62 if (loading_count_) |
| 63 return; |
| 64 |
55 if (text_page_) { | 65 if (text_page_) { |
56 FPDFText_ClosePage(text_page_); | 66 FPDFText_ClosePage(text_page_); |
57 text_page_ = NULL; | 67 text_page_ = NULL; |
58 } | 68 } |
59 | 69 |
60 if (page_) { | 70 if (page_) { |
61 if (engine_->form()) { | 71 if (engine_->form()) { |
62 FORM_OnBeforeClosePage(page_, engine_->form()); | 72 FORM_OnBeforeClosePage(page_, engine_->form()); |
63 } | 73 } |
64 FPDF_ClosePage(page_); | 74 FPDF_ClosePage(page_); |
65 page_ = NULL; | 75 page_ = NULL; |
66 } | 76 } |
67 } | 77 } |
68 | 78 |
69 FPDF_PAGE PDFiumPage::GetPage() { | 79 FPDF_PAGE PDFiumPage::GetPage() { |
70 ScopedUnsupportedFeature scoped_unsupported_feature(engine_); | 80 ScopedUnsupportedFeature scoped_unsupported_feature(engine_); |
71 if (!available_) | 81 if (!available_) |
72 return NULL; | 82 return NULL; |
73 if (!page_) { | 83 if (!page_) { |
| 84 ScopedLoadCounter scoped_load(this); |
74 page_ = FPDF_LoadPage(engine_->doc(), index_); | 85 page_ = FPDF_LoadPage(engine_->doc(), index_); |
75 if (page_ && engine_->form()) { | 86 if (page_ && engine_->form()) { |
76 FORM_OnAfterLoadPage(page_, engine_->form()); | 87 FORM_OnAfterLoadPage(page_, engine_->form()); |
77 } | 88 } |
78 } | 89 } |
79 return page_; | 90 return page_; |
80 } | 91 } |
81 | 92 |
82 FPDF_PAGE PDFiumPage::GetPrintPage() { | 93 FPDF_PAGE PDFiumPage::GetPrintPage() { |
83 ScopedUnsupportedFeature scoped_unsupported_feature(engine_); | 94 ScopedUnsupportedFeature scoped_unsupported_feature(engine_); |
84 if (!available_) | 95 if (!available_) |
85 return NULL; | 96 return NULL; |
86 if (!page_) | 97 if (!page_) { |
| 98 ScopedLoadCounter scoped_load(this); |
87 page_ = FPDF_LoadPage(engine_->doc(), index_); | 99 page_ = FPDF_LoadPage(engine_->doc(), index_); |
| 100 } |
88 return page_; | 101 return page_; |
89 } | 102 } |
90 | 103 |
91 void PDFiumPage::ClosePrintPage() { | 104 void PDFiumPage::ClosePrintPage() { |
| 105 // Do not close |page_| while in the middle of a load. |
| 106 if (loading_count_) |
| 107 return; |
| 108 |
92 if (page_) { | 109 if (page_) { |
93 FPDF_ClosePage(page_); | 110 FPDF_ClosePage(page_); |
94 page_ = NULL; | 111 page_ = NULL; |
95 } | 112 } |
96 } | 113 } |
97 | 114 |
98 FPDF_TEXTPAGE PDFiumPage::GetTextPage() { | 115 FPDF_TEXTPAGE PDFiumPage::GetTextPage() { |
99 if (!available_) | 116 if (!available_) |
100 return NULL; | 117 return NULL; |
101 if (!text_page_) | 118 if (!text_page_) { |
| 119 ScopedLoadCounter scoped_load(this); |
102 text_page_ = FPDFText_LoadPage(GetPage()); | 120 text_page_ = FPDFText_LoadPage(GetPage()); |
| 121 } |
103 return text_page_; | 122 return text_page_; |
104 } | 123 } |
105 | 124 |
106 base::Value* PDFiumPage::GetAccessibleContentAsValue(int rotation) { | 125 base::Value* PDFiumPage::GetAccessibleContentAsValue(int rotation) { |
107 base::DictionaryValue* node = new base::DictionaryValue(); | 126 base::DictionaryValue* node = new base::DictionaryValue(); |
108 | 127 |
109 if (!available_) | 128 if (!available_) |
110 return node; | 129 return node; |
111 | 130 |
112 double width = FPDF_GetPageWidth(GetPage()); | 131 double width = FPDF_GetPageWidth(GetPage()); |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 // http://www.netl.doe.gov/publications/proceedings/03/ubc/presentations/Goeck
ner-pres.pdf | 480 // http://www.netl.doe.gov/publications/proceedings/03/ubc/presentations/Goeck
ner-pres.pdf |
462 if (new_right < new_left) | 481 if (new_right < new_left) |
463 std::swap(new_right, new_left); | 482 std::swap(new_right, new_left); |
464 if (new_bottom < new_top) | 483 if (new_bottom < new_top) |
465 std::swap(new_bottom, new_top); | 484 std::swap(new_bottom, new_top); |
466 | 485 |
467 return pp::Rect( | 486 return pp::Rect( |
468 new_left, new_top, new_right - new_left + 1, new_bottom - new_top + 1); | 487 new_left, new_top, new_right - new_left + 1, new_bottom - new_top + 1); |
469 } | 488 } |
470 | 489 |
| 490 PDFiumPage::ScopedLoadCounter::ScopedLoadCounter(PDFiumPage* page) |
| 491 : page_(page) { |
| 492 page_->loading_count_++; |
| 493 } |
| 494 |
| 495 PDFiumPage::ScopedLoadCounter::~ScopedLoadCounter() { |
| 496 page_->loading_count_--; |
| 497 } |
| 498 |
471 PDFiumPage::Link::Link() { | 499 PDFiumPage::Link::Link() { |
472 } | 500 } |
473 | 501 |
474 PDFiumPage::Link::~Link() { | 502 PDFiumPage::Link::~Link() { |
475 } | 503 } |
476 | 504 |
477 } // namespace chrome_pdf | 505 } // namespace chrome_pdf |
OLD | NEW |