OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef PDF_OUT_OF_PROCESS_INSTANCE_H_ |
| 6 #define PDF_OUT_OF_PROCESS_INSTANCE_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <utility> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "pdf/paint_manager.h" |
| 16 #include "pdf/pdf_engine.h" |
| 17 #include "pdf/preview_mode_client.h" |
| 18 |
| 19 #include "ppapi/c/private/ppb_pdf.h" |
| 20 #include "ppapi/cpp/dev/printing_dev.h" |
| 21 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" |
| 22 #include "ppapi/cpp/dev/selection_dev.h" |
| 23 #include "ppapi/cpp/graphics_2d.h" |
| 24 #include "ppapi/cpp/image_data.h" |
| 25 #include "ppapi/cpp/input_event.h" |
| 26 #include "ppapi/cpp/private/find_private.h" |
| 27 #include "ppapi/cpp/private/instance_private.h" |
| 28 #include "ppapi/cpp/private/uma_private.h" |
| 29 #include "ppapi/cpp/private/var_private.h" |
| 30 #include "ppapi/cpp/url_loader.h" |
| 31 #include "ppapi/utility/completion_callback_factory.h" |
| 32 |
| 33 namespace pp { |
| 34 class TextInput_Dev; |
| 35 } |
| 36 |
| 37 namespace chrome_pdf { |
| 38 |
| 39 class OutOfProcessInstance : public pp::InstancePrivate, |
| 40 public pp::Find_Private, |
| 41 public pp::Printing_Dev, |
| 42 public pp::Selection_Dev, |
| 43 public PaintManager::Client, |
| 44 public PDFEngine::Client, |
| 45 public PreviewModeClient::Client { |
| 46 public: |
| 47 explicit OutOfProcessInstance(PP_Instance instance); |
| 48 virtual ~OutOfProcessInstance(); |
| 49 |
| 50 // pp::Instance implementation. |
| 51 virtual bool Init(uint32_t argc, |
| 52 const char* argn[], |
| 53 const char* argv[]) OVERRIDE; |
| 54 virtual void HandleMessage(const pp::Var& message) OVERRIDE; |
| 55 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE; |
| 56 virtual void DidChangeView(const pp::View& view) OVERRIDE; |
| 57 virtual pp::Var GetInstanceObject() OVERRIDE; |
| 58 |
| 59 // pp::Find_Private implementation. |
| 60 virtual bool StartFind(const std::string& text, bool case_sensitive) OVERRIDE; |
| 61 virtual void SelectFindResult(bool forward) OVERRIDE; |
| 62 virtual void StopFind() OVERRIDE; |
| 63 |
| 64 // pp::PaintManager::Client implementation. |
| 65 virtual void OnPaint(const std::vector<pp::Rect>& paint_rects, |
| 66 std::vector<PaintManager::ReadyRect>* ready, |
| 67 std::vector<pp::Rect>* pending) OVERRIDE; |
| 68 |
| 69 // pp::Printing_Dev implementation. |
| 70 virtual uint32_t QuerySupportedPrintOutputFormats() OVERRIDE; |
| 71 virtual int32_t PrintBegin( |
| 72 const PP_PrintSettings_Dev& print_settings) OVERRIDE; |
| 73 virtual pp::Resource PrintPages( |
| 74 const PP_PrintPageNumberRange_Dev* page_ranges, |
| 75 uint32_t page_range_count) OVERRIDE; |
| 76 virtual void PrintEnd() OVERRIDE; |
| 77 virtual bool IsPrintScalingDisabled() OVERRIDE; |
| 78 |
| 79 // pp::Private implementation. |
| 80 virtual pp::Var GetLinkAtPosition(const pp::Point& point); |
| 81 |
| 82 // PPP_Selection_Dev implementation. |
| 83 virtual pp::Var GetSelectedText(bool html) OVERRIDE; |
| 84 |
| 85 void FlushCallback(int32_t result); |
| 86 void DidOpen(int32_t result); |
| 87 void DidOpenPreview(int32_t result); |
| 88 |
| 89 // Called when the timer is fired. |
| 90 void OnClientTimerFired(int32_t id); |
| 91 |
| 92 // Called to print without re-entrancy issues. |
| 93 void OnPrint(int32_t); |
| 94 |
| 95 // PDFEngine::Client implementation. |
| 96 virtual void DocumentSizeUpdated(const pp::Size& size); |
| 97 virtual void Invalidate(const pp::Rect& rect); |
| 98 virtual void Scroll(const pp::Point& point); |
| 99 virtual void ScrollToX(int position); |
| 100 virtual void ScrollToY(int position); |
| 101 virtual void ScrollToPage(int page); |
| 102 virtual void NavigateTo(const std::string& url, bool open_in_new_tab); |
| 103 virtual void UpdateCursor(PP_CursorType_Dev cursor); |
| 104 virtual void UpdateTickMarks(const std::vector<pp::Rect>& tickmarks); |
| 105 virtual void NotifyNumberOfFindResultsChanged(int total, bool final_result); |
| 106 virtual void NotifySelectedFindResultChanged(int current_find_index); |
| 107 virtual void GetDocumentPassword( |
| 108 pp::CompletionCallbackWithOutput<pp::Var> callback); |
| 109 virtual void Alert(const std::string& message); |
| 110 virtual bool Confirm(const std::string& message); |
| 111 virtual std::string Prompt(const std::string& question, |
| 112 const std::string& default_answer); |
| 113 virtual std::string GetURL(); |
| 114 virtual void Email(const std::string& to, |
| 115 const std::string& cc, |
| 116 const std::string& bcc, |
| 117 const std::string& subject, |
| 118 const std::string& body); |
| 119 virtual void Print(); |
| 120 virtual void SubmitForm(const std::string& url, |
| 121 const void* data, |
| 122 int length); |
| 123 virtual std::string ShowFileSelectionDialog(); |
| 124 virtual pp::URLLoader CreateURLLoader(); |
| 125 virtual void ScheduleCallback(int id, int delay_in_ms); |
| 126 virtual void SearchString(const base::char16* string, |
| 127 const base::char16* term, |
| 128 bool case_sensitive, |
| 129 std::vector<SearchStringResult>* results); |
| 130 virtual void DocumentPaintOccurred(); |
| 131 virtual void DocumentLoadComplete(int page_count); |
| 132 virtual void DocumentLoadFailed(); |
| 133 virtual pp::Instance* GetPluginInstance(); |
| 134 virtual void DocumentHasUnsupportedFeature(const std::string& feature); |
| 135 virtual void DocumentLoadProgress(uint32 available, uint32 doc_size); |
| 136 virtual void FormTextFieldFocusChange(bool in_focus); |
| 137 virtual bool IsPrintPreview(); |
| 138 |
| 139 // PreviewModeClient::Client implementation. |
| 140 virtual void PreviewDocumentLoadComplete() OVERRIDE; |
| 141 virtual void PreviewDocumentLoadFailed() OVERRIDE; |
| 142 |
| 143 // Helper functions for implementing PPP_PDF. |
| 144 void RotateClockwise(); |
| 145 void RotateCounterclockwise(); |
| 146 |
| 147 private: |
| 148 void ResetRecentlySentFindUpdate(int32_t); |
| 149 |
| 150 // Called whenever the plugin geometry changes to update the location of the |
| 151 // background parts, and notifies the pdf engine. |
| 152 void OnGeometryChanged(double old_zoom, float old_device_scale); |
| 153 |
| 154 // Figures out the location of any background rectangles (i.e. those that |
| 155 // aren't painted by the PDF engine). |
| 156 void CalculateBackgroundParts(); |
| 157 |
| 158 // Computes document width/height in device pixels, based on current zoom and |
| 159 // device scale |
| 160 int GetDocumentPixelWidth() const; |
| 161 int GetDocumentPixelHeight() const; |
| 162 |
| 163 // Draws a rectangle with the specified dimensions and color in our buffer. |
| 164 void FillRect(const pp::Rect& rect, unsigned int color); |
| 165 |
| 166 void LoadUrl(const std::string& url); |
| 167 void LoadPreviewUrl(const std::string& url); |
| 168 void LoadUrlInternal(const std::string& url, pp::URLLoader* loader, |
| 169 void (OutOfProcessInstance::* method)(int32_t)); |
| 170 |
| 171 // Creates a URL loader and allows it to access all urls, i.e. not just the |
| 172 // frame's origin. |
| 173 pp::URLLoader CreateURLLoaderInternal(); |
| 174 |
| 175 // Figure out the initial page to display based on #page=N and #nameddest=foo |
| 176 // in the |url_|. |
| 177 // Returns -1 if there is no valid fragment. The returned value is 0-based, |
| 178 // whereas page=N is 1-based. |
| 179 int GetInitialPage(const std::string& url); |
| 180 |
| 181 void FormDidOpen(int32_t result); |
| 182 |
| 183 std::string GetLocalizedString(PP_ResourceString id); |
| 184 |
| 185 void UserMetricsRecordAction(const std::string& action); |
| 186 |
| 187 enum DocumentLoadState { |
| 188 LOAD_STATE_LOADING, |
| 189 LOAD_STATE_COMPLETE, |
| 190 LOAD_STATE_FAILED, |
| 191 }; |
| 192 |
| 193 // Set new zoom scale. |
| 194 void SetZoom(double scale); |
| 195 |
| 196 // Reduces the document to 1 page and appends |print_preview_page_count_| |
| 197 // blank pages to the document for print preview. |
| 198 void AppendBlankPrintPreviewPages(); |
| 199 |
| 200 // Process the preview page data information. |src_url| specifies the preview |
| 201 // page data location. The |src_url| is in the format: |
| 202 // chrome://print/id/page_number/print.pdf |
| 203 // |dst_page_index| specifies the blank page index that needs to be replaced |
| 204 // with the new page data. |
| 205 void ProcessPreviewPageInfo(const std::string& src_url, int dst_page_index); |
| 206 // Load the next available preview page into the blank page. |
| 207 void LoadAvailablePreviewPage(); |
| 208 |
| 209 pp::ImageData image_data_; |
| 210 // Used when the plugin is embedded in a page and we have to create the loader |
| 211 // ourself. |
| 212 pp::CompletionCallbackFactory<OutOfProcessInstance> loader_factory_; |
| 213 pp::URLLoader embed_loader_; |
| 214 pp::URLLoader embed_preview_loader_; |
| 215 |
| 216 PP_CursorType_Dev cursor_; // The current cursor. |
| 217 |
| 218 pp::CompletionCallbackFactory<OutOfProcessInstance> timer_factory_; |
| 219 |
| 220 // Size, in pixels, of plugin rectangle. |
| 221 pp::Size plugin_size_; |
| 222 // Size, in DIPs, of plugin rectangle. |
| 223 pp::Size plugin_dip_size_; |
| 224 // Remaining area, in pixels, to render the pdf in after accounting for |
| 225 // horizontal centering. |
| 226 pp::Rect available_area_; |
| 227 // Size of entire document in pixels (i.e. if each page is 800 pixels high and |
| 228 // there are 10 pages, the height will be 8000). |
| 229 pp::Size document_size_; |
| 230 |
| 231 double zoom_; // Current zoom factor. |
| 232 |
| 233 float device_scale_; // Current device scale factor. |
| 234 bool printing_enabled_; |
| 235 // True if the plugin is full-page. |
| 236 bool full_; |
| 237 |
| 238 PaintManager paint_manager_; |
| 239 |
| 240 struct BackgroundPart { |
| 241 pp::Rect location; |
| 242 unsigned int color; |
| 243 }; |
| 244 std::vector<BackgroundPart> background_parts_; |
| 245 |
| 246 struct PrintSettings { |
| 247 PrintSettings() { |
| 248 Clear(); |
| 249 } |
| 250 void Clear() { |
| 251 is_printing = false; |
| 252 print_pages_called_ = false; |
| 253 memset(&pepper_print_settings, 0, sizeof(pepper_print_settings)); |
| 254 } |
| 255 // This is set to true when PrintBegin is called and false when PrintEnd is |
| 256 // called. |
| 257 bool is_printing; |
| 258 // To know whether this was an actual print operation, so we don't double |
| 259 // count UMA logging. |
| 260 bool print_pages_called_; |
| 261 PP_PrintSettings_Dev pepper_print_settings; |
| 262 }; |
| 263 |
| 264 PrintSettings print_settings_; |
| 265 |
| 266 scoped_ptr<PDFEngine> engine_; |
| 267 |
| 268 // This engine is used to render the individual preview page data. This is |
| 269 // used only in print preview mode. This will use |PreviewModeClient| |
| 270 // interface which has very limited access to the pp::Instance. |
| 271 scoped_ptr<PDFEngine> preview_engine_; |
| 272 |
| 273 std::string url_; |
| 274 |
| 275 // Used for submitting forms. |
| 276 pp::CompletionCallbackFactory<OutOfProcessInstance> form_factory_; |
| 277 pp::URLLoader form_loader_; |
| 278 |
| 279 // Used for printing without re-entrancy issues. |
| 280 pp::CompletionCallbackFactory<OutOfProcessInstance> print_callback_factory_; |
| 281 |
| 282 // True if we haven't painted the plugin viewport yet. |
| 283 bool first_paint_; |
| 284 |
| 285 DocumentLoadState document_load_state_; |
| 286 DocumentLoadState preview_document_load_state_; |
| 287 |
| 288 // JavaScript interface to control this instance. |
| 289 // This wraps a PDFScriptableObject in a pp::Var. |
| 290 pp::VarPrivate instance_object_; |
| 291 |
| 292 // A UMA resource for histogram reporting. |
| 293 pp::UMAPrivate uma_; |
| 294 |
| 295 // Used so that we only tell the browser once about an unsupported feature, to |
| 296 // avoid the infobar going up more than once. |
| 297 bool told_browser_about_unsupported_feature_; |
| 298 |
| 299 // Keeps track of which unsupported features we reported, so we avoid spamming |
| 300 // the stats if a feature shows up many times per document. |
| 301 std::set<std::string> unsupported_features_reported_; |
| 302 |
| 303 // Number of pages in print preview mode, 0 if not in print preview mode. |
| 304 int print_preview_page_count_; |
| 305 std::vector<int> print_preview_page_numbers_; |
| 306 |
| 307 // Used to manage loaded print preview page information. A |PreviewPageInfo| |
| 308 // consists of data source url string and the page index in the destination |
| 309 // document. |
| 310 typedef std::pair<std::string, int> PreviewPageInfo; |
| 311 std::queue<PreviewPageInfo> preview_pages_info_; |
| 312 |
| 313 // Used to signal the browser about focus changes to trigger the OSK. |
| 314 // TODO(abodenha@chromium.org) Implement full IME support in the plugin. |
| 315 // http://crbug.com/132565 |
| 316 scoped_ptr<pp::TextInput_Dev> text_input_; |
| 317 |
| 318 // The last document load progress value sent to the web page. |
| 319 double last_progress_sent_; |
| 320 |
| 321 // Whether an update to the number of find results found was sent less than |
| 322 // |kFindResultCooldownMs| milliseconds ago. |
| 323 bool recently_sent_find_update_; |
| 324 |
| 325 // The tickmarks. |
| 326 std::vector<pp::Rect> tickmarks_; |
| 327 |
| 328 // Whether the plugin has received a viewport changed message. Nothing should |
| 329 // be painted until this is received. |
| 330 bool received_viewport_message_; |
| 331 |
| 332 // The callback for receiving the password from the page. |
| 333 scoped_ptr<pp::CompletionCallbackWithOutput<pp::Var> > password_callback_; |
| 334 }; |
| 335 |
| 336 } // namespace chrome_pdf |
| 337 |
| 338 #endif // PDF_OUT_OF_PROCESS_INSTANCE_H_ |
OLD | NEW |