Fix remoting GN Mac build.
[chromium-blink-merge.git] / pdf / out_of_process_instance.h
blob62d21c1c4286b0ac1572dca238a46f5977484ca9
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.
5 #ifndef PDF_OUT_OF_PROCESS_INSTANCE_H_
6 #define PDF_OUT_OF_PROCESS_INSTANCE_H_
8 #include <queue>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
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"
19 #include "ppapi/c/private/ppb_pdf.h"
20 #include "ppapi/c/private/ppp_pdf.h"
21 #include "ppapi/cpp/dev/printing_dev.h"
22 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
23 #include "ppapi/cpp/dev/selection_dev.h"
24 #include "ppapi/cpp/graphics_2d.h"
25 #include "ppapi/cpp/image_data.h"
26 #include "ppapi/cpp/input_event.h"
27 #include "ppapi/cpp/instance.h"
28 #include "ppapi/cpp/private/find_private.h"
29 #include "ppapi/cpp/private/uma_private.h"
30 #include "ppapi/cpp/url_loader.h"
31 #include "ppapi/utility/completion_callback_factory.h"
33 namespace pp {
34 class TextInput_Dev;
37 namespace chrome_pdf {
39 class OutOfProcessInstance : public pp::Instance,
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();
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;
58 // pp::Find_Private implementation.
59 virtual bool StartFind(const std::string& text, bool case_sensitive) override;
60 virtual void SelectFindResult(bool forward) override;
61 virtual void StopFind() override;
63 // pp::PaintManager::Client implementation.
64 virtual void OnPaint(const std::vector<pp::Rect>& paint_rects,
65 std::vector<PaintManager::ReadyRect>* ready,
66 std::vector<pp::Rect>* pending) override;
68 // pp::Printing_Dev implementation.
69 virtual uint32_t QuerySupportedPrintOutputFormats() override;
70 virtual int32_t PrintBegin(
71 const PP_PrintSettings_Dev& print_settings) override;
72 virtual pp::Resource PrintPages(
73 const PP_PrintPageNumberRange_Dev* page_ranges,
74 uint32_t page_range_count) override;
75 virtual void PrintEnd() override;
76 virtual bool IsPrintScalingDisabled() override;
78 // pp::Private implementation.
79 virtual pp::Var GetLinkAtPosition(const pp::Point& point);
80 virtual void GetPrintPresetOptionsFromDocument(
81 PP_PdfPrintPresetOptions_Dev* options);
83 // PPP_Selection_Dev implementation.
84 virtual pp::Var GetSelectedText(bool html) override;
86 void FlushCallback(int32_t result);
87 void DidOpen(int32_t result);
88 void DidOpenPreview(int32_t result);
90 // Called when the timer is fired.
91 void OnClientTimerFired(int32_t id);
93 // Called to print without re-entrancy issues.
94 void OnPrint(int32_t);
96 // PDFEngine::Client implementation.
97 void DocumentSizeUpdated(const pp::Size& size) override;
98 void Invalidate(const pp::Rect& rect) override;
99 void Scroll(const pp::Point& point) override;
100 void ScrollToX(int position) override;
101 void ScrollToY(int position) override;
102 void ScrollToPage(int page) override;
103 void NavigateTo(const std::string& url, bool open_in_new_tab) override;
104 void UpdateCursor(PP_CursorType_Dev cursor) override;
105 void UpdateTickMarks(const std::vector<pp::Rect>& tickmarks) override;
106 void NotifyNumberOfFindResultsChanged(int total, bool final_result) override;
107 void NotifySelectedFindResultChanged(int current_find_index) override;
108 void GetDocumentPassword(
109 pp::CompletionCallbackWithOutput<pp::Var> callback) override;
110 void Alert(const std::string& message) override;
111 bool Confirm(const std::string& message) override;
112 std::string Prompt(const std::string& question,
113 const std::string& default_answer) override;
114 std::string GetURL() override;
115 void Email(const std::string& to,
116 const std::string& cc,
117 const std::string& bcc,
118 const std::string& subject,
119 const std::string& body) override;
120 void Print() override;
121 void SubmitForm(const std::string& url,
122 const void* data,
123 int length) override;
124 std::string ShowFileSelectionDialog() override;
125 pp::URLLoader CreateURLLoader() override;
126 void ScheduleCallback(int id, int delay_in_ms) override;
127 void SearchString(const base::char16* string,
128 const base::char16* term,
129 bool case_sensitive,
130 std::vector<SearchStringResult>* results) override;
131 void DocumentPaintOccurred() override;
132 void DocumentLoadComplete(int page_count) override;
133 void DocumentLoadFailed() override;
134 pp::Instance* GetPluginInstance() override;
135 void DocumentHasUnsupportedFeature(const std::string& feature) override;
136 void DocumentLoadProgress(uint32 available, uint32 doc_size) override;
137 void FormTextFieldFocusChange(bool in_focus) override;
138 bool IsPrintPreview() override;
139 uint32 GetBackgroundColor() override;
140 void IsSelectingChanged(bool is_selecting) override;
142 // PreviewModeClient::Client implementation.
143 void PreviewDocumentLoadComplete() override;
144 void PreviewDocumentLoadFailed() override;
146 // Helper functions for implementing PPP_PDF.
147 void RotateClockwise();
148 void RotateCounterclockwise();
150 private:
151 void ResetRecentlySentFindUpdate(int32_t);
153 // Called whenever the plugin geometry changes to update the location of the
154 // background parts, and notifies the pdf engine.
155 void OnGeometryChanged(double old_zoom, float old_device_scale);
157 // Figures out the location of any background rectangles (i.e. those that
158 // aren't painted by the PDF engine).
159 void CalculateBackgroundParts();
161 // Computes document width/height in device pixels, based on current zoom and
162 // device scale
163 int GetDocumentPixelWidth() const;
164 int GetDocumentPixelHeight() const;
166 // Draws a rectangle with the specified dimensions and color in our buffer.
167 void FillRect(const pp::Rect& rect, uint32 color);
169 void LoadUrl(const std::string& url);
170 void LoadPreviewUrl(const std::string& url);
171 void LoadUrlInternal(const std::string& url, pp::URLLoader* loader,
172 void (OutOfProcessInstance::* method)(int32_t));
174 // Creates a URL loader and allows it to access all urls, i.e. not just the
175 // frame's origin.
176 pp::URLLoader CreateURLLoaderInternal();
178 // Figure out the initial page to display based on #page=N and #nameddest=foo
179 // in the |url_|.
180 // Returns -1 if there is no valid fragment. The returned value is 0-based,
181 // whereas page=N is 1-based.
182 int GetInitialPage(const std::string& url);
184 void FormDidOpen(int32_t result);
186 std::string GetLocalizedString(PP_ResourceString id);
188 void UserMetricsRecordAction(const std::string& action);
190 enum DocumentLoadState {
191 LOAD_STATE_LOADING,
192 LOAD_STATE_COMPLETE,
193 LOAD_STATE_FAILED,
196 // Set new zoom scale.
197 void SetZoom(double scale);
199 // Reduces the document to 1 page and appends |print_preview_page_count_|
200 // blank pages to the document for print preview.
201 void AppendBlankPrintPreviewPages();
203 // Process the preview page data information. |src_url| specifies the preview
204 // page data location. The |src_url| is in the format:
205 // chrome://print/id/page_number/print.pdf
206 // |dst_page_index| specifies the blank page index that needs to be replaced
207 // with the new page data.
208 void ProcessPreviewPageInfo(const std::string& src_url, int dst_page_index);
209 // Load the next available preview page into the blank page.
210 void LoadAvailablePreviewPage();
212 // Bound the given scroll offset to the document.
213 pp::FloatPoint BoundScrollOffsetToDocument(
214 const pp::FloatPoint& scroll_offset);
216 pp::ImageData image_data_;
217 // Used when the plugin is embedded in a page and we have to create the loader
218 // ourself.
219 pp::CompletionCallbackFactory<OutOfProcessInstance> loader_factory_;
220 pp::URLLoader embed_loader_;
221 pp::URLLoader embed_preview_loader_;
223 PP_CursorType_Dev cursor_; // The current cursor.
225 pp::CompletionCallbackFactory<OutOfProcessInstance> timer_factory_;
227 // Size, in pixels, of plugin rectangle.
228 pp::Size plugin_size_;
229 // Size, in DIPs, of plugin rectangle.
230 pp::Size plugin_dip_size_;
231 // Remaining area, in pixels, to render the pdf in after accounting for
232 // horizontal centering.
233 pp::Rect available_area_;
234 // Size of entire document in pixels (i.e. if each page is 800 pixels high and
235 // there are 10 pages, the height will be 8000).
236 pp::Size document_size_;
238 double zoom_; // Current zoom factor.
240 float device_scale_; // Current device scale factor.
241 // True if the plugin is full-page.
242 bool full_;
244 PaintManager paint_manager_;
246 struct BackgroundPart {
247 pp::Rect location;
248 uint32 color;
250 std::vector<BackgroundPart> background_parts_;
252 struct PrintSettings {
253 PrintSettings() {
254 Clear();
256 void Clear() {
257 is_printing = false;
258 print_pages_called_ = false;
259 memset(&pepper_print_settings, 0, sizeof(pepper_print_settings));
261 // This is set to true when PrintBegin is called and false when PrintEnd is
262 // called.
263 bool is_printing;
264 // To know whether this was an actual print operation, so we don't double
265 // count UMA logging.
266 bool print_pages_called_;
267 PP_PrintSettings_Dev pepper_print_settings;
270 PrintSettings print_settings_;
272 scoped_ptr<PDFEngine> engine_;
274 // This engine is used to render the individual preview page data. This is
275 // used only in print preview mode. This will use |PreviewModeClient|
276 // interface which has very limited access to the pp::Instance.
277 scoped_ptr<PDFEngine> preview_engine_;
279 std::string url_;
281 // Used for submitting forms.
282 pp::CompletionCallbackFactory<OutOfProcessInstance> form_factory_;
283 pp::URLLoader form_loader_;
285 // Used for printing without re-entrancy issues.
286 pp::CompletionCallbackFactory<OutOfProcessInstance> print_callback_factory_;
288 // The callback for receiving the password from the page.
289 scoped_ptr<pp::CompletionCallbackWithOutput<pp::Var> > password_callback_;
291 // True if we haven't painted the plugin viewport yet.
292 bool first_paint_;
294 DocumentLoadState document_load_state_;
295 DocumentLoadState preview_document_load_state_;
297 // A UMA resource for histogram reporting.
298 pp::UMAPrivate uma_;
300 // Used so that we only tell the browser once about an unsupported feature, to
301 // avoid the infobar going up more than once.
302 bool told_browser_about_unsupported_feature_;
304 // Keeps track of which unsupported features we reported, so we avoid spamming
305 // the stats if a feature shows up many times per document.
306 std::set<std::string> unsupported_features_reported_;
308 // Number of pages in print preview mode, 0 if not in print preview mode.
309 int print_preview_page_count_;
310 std::vector<int> print_preview_page_numbers_;
312 // Used to manage loaded print preview page information. A |PreviewPageInfo|
313 // consists of data source url string and the page index in the destination
314 // document.
315 typedef std::pair<std::string, int> PreviewPageInfo;
316 std::queue<PreviewPageInfo> preview_pages_info_;
318 // Used to signal the browser about focus changes to trigger the OSK.
319 // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
320 // http://crbug.com/132565
321 scoped_ptr<pp::TextInput_Dev> text_input_;
323 // The last document load progress value sent to the web page.
324 double last_progress_sent_;
326 // Whether an update to the number of find results found was sent less than
327 // |kFindResultCooldownMs| milliseconds ago.
328 bool recently_sent_find_update_;
330 // The tickmarks.
331 std::vector<pp::Rect> tickmarks_;
333 // Whether the plugin has received a viewport changed message. Nothing should
334 // be painted until this is received.
335 bool received_viewport_message_;
337 // If true, this means we told the RenderView that we're starting a network
338 // request so that it can start the throbber. We will tell it again once the
339 // document finishes loading.
340 bool did_call_start_loading_;
342 // If this is true, then don't scroll the plugin in response to DidChangeView
343 // messages. This will be true when the extension page is in the process of
344 // zooming the plugin so that flickering doesn't occur while zooming.
345 bool stop_scrolling_;
347 // The background color of the PDF viewer.
348 uint32 background_color_;
351 } // namespace chrome_pdf
353 #endif // PDF_OUT_OF_PROCESS_INSTANCE_H_