Mac: Fix performance issues with remote CoreAnimation
[chromium-blink-merge.git] / content / renderer / pepper / pepper_plugin_instance_impl.h
blobefb0e4dd3b813bec9b8e5db93633ade6b48ecc7c
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 CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_INSTANCE_IMPL_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_INSTANCE_IMPL_H_
8 #include <list>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/strings/string16.h"
19 #include "cc/layers/content_layer_client.h"
20 #include "cc/layers/layer.h"
21 #include "cc/layers/texture_layer_client.h"
22 #include "content/common/content_export.h"
23 #include "content/public/renderer/pepper_plugin_instance.h"
24 #include "content/public/renderer/render_frame_observer.h"
25 #include "content/renderer/mouse_lock_dispatcher.h"
26 #include "gin/handle.h"
27 #include "ppapi/c/dev/pp_cursor_type_dev.h"
28 #include "ppapi/c/dev/ppp_printing_dev.h"
29 #include "ppapi/c/dev/ppp_selection_dev.h"
30 #include "ppapi/c/dev/ppp_text_input_dev.h"
31 #include "ppapi/c/dev/ppp_zoom_dev.h"
32 #include "ppapi/c/pp_completion_callback.h"
33 #include "ppapi/c/pp_instance.h"
34 #include "ppapi/c/pp_time.h"
35 #include "ppapi/c/pp_var.h"
36 #include "ppapi/c/ppb_audio_config.h"
37 #include "ppapi/c/ppb_gamepad.h"
38 #include "ppapi/c/ppb_input_event.h"
39 #include "ppapi/c/ppp_graphics_3d.h"
40 #include "ppapi/c/ppp_input_event.h"
41 #include "ppapi/c/ppp_mouse_lock.h"
42 #include "ppapi/c/private/ppb_content_decryptor_private.h"
43 #include "ppapi/c/private/ppp_find_private.h"
44 #include "ppapi/c/private/ppp_instance_private.h"
45 #include "ppapi/c/private/ppp_pdf.h"
46 #include "ppapi/shared_impl/ppb_instance_shared.h"
47 #include "ppapi/shared_impl/ppb_view_shared.h"
48 #include "ppapi/shared_impl/singleton_resource_id.h"
49 #include "ppapi/shared_impl/tracked_callback.h"
50 #include "ppapi/thunk/ppb_gamepad_api.h"
51 #include "ppapi/thunk/resource_creation_api.h"
52 #include "skia/ext/refptr.h"
53 #include "third_party/WebKit/public/platform/WebCanvas.h"
54 #include "third_party/WebKit/public/platform/WebString.h"
55 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
56 #include "third_party/WebKit/public/platform/WebURLResponse.h"
57 #include "third_party/WebKit/public/web/WebPlugin.h"
58 #include "third_party/WebKit/public/web/WebUserGestureToken.h"
59 #include "ui/base/ime/text_input_type.h"
60 #include "ui/events/latency_info.h"
61 #include "ui/gfx/rect.h"
62 #include "url/gurl.h"
63 #include "v8/include/v8.h"
65 struct PP_Point;
66 struct _NPP;
68 class SkBitmap;
69 class TransportDIB;
71 namespace blink {
72 class WebInputEvent;
73 class WebLayer;
74 class WebMouseEvent;
75 class WebPluginContainer;
76 class WebURLLoader;
77 class WebURLResponse;
78 struct WebCompositionUnderline;
79 struct WebCursorInfo;
80 struct WebURLError;
81 struct WebPrintParams;
84 namespace cc {
85 class TextureLayer;
88 namespace gfx {
89 class Range;
92 namespace ppapi {
93 class Resource;
94 struct InputEventData;
95 struct PPP_Instance_Combined;
96 class ScopedPPVar;
99 namespace content {
101 class ContentDecryptorDelegate;
102 class FullscreenContainer;
103 class MessageChannel;
104 class PepperCompositorHost;
105 class PepperGraphics2DHost;
106 class PepperPluginInstanceThrottler;
107 class PluginModule;
108 class PluginObject;
109 class PPB_Graphics3D_Impl;
110 class PPB_ImageData_Impl;
111 class PPB_URLLoader_Impl;
112 class RenderFrameImpl;
113 class RenderViewImpl;
115 // Represents one time a plugin appears on one web page.
117 // Note: to get from a PP_Instance to a PepperPluginInstance*, use the
118 // ResourceTracker.
119 class CONTENT_EXPORT PepperPluginInstanceImpl
120 : public base::RefCounted<PepperPluginInstanceImpl>,
121 public NON_EXPORTED_BASE(PepperPluginInstance),
122 public ppapi::PPB_Instance_Shared,
123 public NON_EXPORTED_BASE(cc::TextureLayerClient),
124 public RenderFrameObserver {
125 public:
126 // Create and return a PepperPluginInstanceImpl object which supports the most
127 // recent version of PPP_Instance possible by querying the given
128 // get_plugin_interface function. If the plugin does not support any valid
129 // PPP_Instance interface, returns NULL.
130 static PepperPluginInstanceImpl* Create(RenderFrameImpl* render_frame,
131 PluginModule* module,
132 blink::WebPluginContainer* container,
133 const GURL& plugin_url);
134 RenderFrameImpl* render_frame() const { return render_frame_; }
135 PluginModule* module() const { return module_.get(); }
137 blink::WebPluginContainer* container() const { return container_; }
139 PepperPluginInstanceThrottler* throttler() const { return throttler_.get(); }
141 // Returns the PP_Instance uniquely identifying this instance. Guaranteed
142 // nonzero.
143 PP_Instance pp_instance() const { return pp_instance_; }
145 ppapi::thunk::ResourceCreationAPI& resource_creation() {
146 return *resource_creation_.get();
149 MessageChannel* message_channel() { return message_channel_; }
150 v8::Local<v8::Object> GetMessageChannelObject();
151 // Called when |message_channel_| is destroyed as it may be destroyed prior to
152 // the plugin being destroyed.
153 void MessageChannelDestroyed();
155 // Return the v8 context for the frame that the plugin is contained in. Care
156 // should be taken to use the correct context for plugin<->JS interactions.
157 // In cases where JS calls into the plugin, the caller's context should
158 // typically be used. When calling from the plugin into JS, this context
159 // should typically used.
160 v8::Local<v8::Context> GetMainWorldContext();
162 // Does some pre-destructor cleanup on the instance. This is necessary
163 // because some cleanup depends on the plugin instance still existing (like
164 // calling the plugin's DidDestroy function). This function is called from
165 // the WebPlugin implementation when WebKit is about to remove the plugin.
166 void Delete();
168 // Returns true if Delete() has been called on this object.
169 bool is_deleted() const;
171 // Paints the current backing store to the web page.
172 void Paint(blink::WebCanvas* canvas,
173 const gfx::Rect& plugin_rect,
174 const gfx::Rect& paint_rect);
176 // Schedules a paint of the page for the given region. The coordinates are
177 // relative to the top-left of the plugin. This does nothing if the plugin
178 // has not yet been positioned. You can supply an empty gfx::Rect() to
179 // invalidate the entire plugin.
180 void InvalidateRect(const gfx::Rect& rect);
182 // Schedules a scroll of the plugin. This uses optimized scrolling only for
183 // full-frame plugins, as otherwise there could be other elements on top. The
184 // slow path can also be triggered if there is an overlapping frame.
185 void ScrollRect(int dx, int dy, const gfx::Rect& rect);
187 // Commit the backing texture to the screen once the side effects some
188 // rendering up to an offscreen SwapBuffers are visible.
189 void CommitBackingTexture();
191 // Called when the out-of-process plugin implementing this instance crashed.
192 void InstanceCrashed();
194 // PPB_Instance and PPB_Instance_Private implementation.
195 bool full_frame() const { return full_frame_; }
196 const ppapi::ViewData& view_data() const { return view_data_; }
198 // PPP_Instance and PPP_Instance_Private.
199 bool Initialize(const std::vector<std::string>& arg_names,
200 const std::vector<std::string>& arg_values,
201 bool full_frame);
202 bool HandleDocumentLoad(const blink::WebURLResponse& response);
203 bool HandleInputEvent(const blink::WebInputEvent& event,
204 blink::WebCursorInfo* cursor_info);
205 PP_Var GetInstanceObject(v8::Isolate* isolate);
206 void ViewChanged(const gfx::Rect& position,
207 const gfx::Rect& clip,
208 const std::vector<gfx::Rect>& cut_outs_rects);
210 // Handlers for composition events.
211 bool HandleCompositionStart(const base::string16& text);
212 bool HandleCompositionUpdate(
213 const base::string16& text,
214 const std::vector<blink::WebCompositionUnderline>& underlines,
215 int selection_start,
216 int selection_end);
217 bool HandleCompositionEnd(const base::string16& text);
218 bool HandleTextInput(const base::string16& text);
220 // Gets the current text input status.
221 ui::TextInputType text_input_type() const { return text_input_type_; }
222 gfx::Rect GetCaretBounds() const;
223 bool IsPluginAcceptingCompositionEvents() const;
224 void GetSurroundingText(base::string16* text, gfx::Range* range) const;
226 // Notifications about focus changes, see has_webkit_focus_ below.
227 void SetWebKitFocus(bool has_focus);
228 void SetContentAreaFocus(bool has_focus);
230 // Notification about page visibility. The default is "visible".
231 void PageVisibilityChanged(bool is_visible);
233 // Notifications that the view has started painting, and has flushed the
234 // painted content to the screen. These messages are used to send Flush
235 // callbacks to the plugin for DeviceContext2D/3D.
236 void ViewInitiatedPaint();
237 void ViewFlushedPaint();
239 // Tracks all live PluginObjects.
240 void AddPluginObject(PluginObject* plugin_object);
241 void RemovePluginObject(PluginObject* plugin_object);
243 base::string16 GetSelectedText(bool html);
244 base::string16 GetLinkAtPosition(const gfx::Point& point);
245 void RequestSurroundingText(size_t desired_number_of_characters);
246 void Zoom(double factor, bool text_only);
247 bool StartFind(const base::string16& search_text,
248 bool case_sensitive,
249 int identifier);
250 void SelectFindResult(bool forward);
251 void StopFind();
253 bool SupportsPrintInterface();
254 bool IsPrintScalingDisabled();
255 int PrintBegin(const blink::WebPrintParams& print_params);
256 bool PrintPage(int page_number, blink::WebCanvas* canvas);
257 void PrintEnd();
258 bool GetPrintPresetOptionsFromDocument(
259 blink::WebPrintPresetOptions* preset_options);
261 bool CanRotateView();
262 void RotateView(blink::WebPlugin::RotationType type);
264 // There are 2 implementations of the fullscreen interface
265 // PPB_FlashFullscreen is used by Pepper Flash.
266 // PPB_Fullscreen is intended for other applications including NaCl.
267 // The two interface are mutually exclusive.
269 // Implementation of PPB_FlashFullscreen.
271 // Because going to fullscreen is asynchronous (but going out is not), there
272 // are 3 states:
273 // - normal : fullscreen_container_ == NULL
274 // flash_fullscreen_ == false
275 // - fullscreen pending: fullscreen_container_ != NULL
276 // flash_fullscreen_ == false
277 // - fullscreen : fullscreen_container_ != NULL
278 // flash_fullscreen_ == true
280 // In normal state, events come from webkit and painting goes back to it.
281 // In fullscreen state, events come from the fullscreen container, and
282 // painting goes back to it.
283 // In pending state, events from webkit are ignored, and as soon as we
284 // receive events from the fullscreen container, we go to the fullscreen
285 // state.
286 bool FlashIsFullscreenOrPending();
288 // Updates |flash_fullscreen_| and sends focus change notification if
289 // necessary.
290 void UpdateFlashFullscreenState(bool flash_fullscreen);
292 FullscreenContainer* fullscreen_container() const {
293 return fullscreen_container_;
296 // Implementation of PPB_Fullscreen.
298 // Because going to/from fullscreen is asynchronous, there are 4 states:
299 // - normal : desired_fullscreen_state_ == false
300 // view_data_.is_fullscreen == false
301 // - fullscreen pending: desired_fullscreen_state_ == true
302 // view_data_.is_fullscreen == false
303 // - fullscreen : desired_fullscreen_state_ == true
304 // view_data_.is_fullscreen == true
305 // - normal pending : desired_fullscreen_state_ = false
306 // view_data_.is_fullscreen = true
307 bool IsFullscreenOrPending();
309 bool flash_fullscreen() const { return flash_fullscreen_; }
311 // Switches between fullscreen and normal mode. The transition is
312 // asynchronous. WebKit will trigger corresponding VewChanged calls.
313 // Returns true on success, false on failure (e.g. trying to enter fullscreen
314 // when not processing a user gesture or trying to set fullscreen when
315 // already in fullscreen mode).
316 bool SetFullscreen(bool fullscreen);
318 // Send the message on to the plugin.
319 void HandleMessage(ppapi::ScopedPPVar message);
321 // Send the message synchronously to the plugin, and get a result. Returns
322 // true if the plugin handled the message, false if it didn't. The plugin
323 // won't handle the message if it has not registered a PPP_MessageHandler.
324 bool HandleBlockingMessage(ppapi::ScopedPPVar message,
325 ppapi::ScopedPPVar* result);
327 // Returns true if the plugin is processing a user gesture.
328 bool IsProcessingUserGesture();
330 // Returns the user gesture token to use for creating a WebScopedUserGesture,
331 // if IsProcessingUserGesture returned true.
332 blink::WebUserGestureToken CurrentUserGestureToken();
334 // A mouse lock request was pending and this reports success or failure.
335 void OnLockMouseACK(bool succeeded);
336 // A mouse lock was in place, but has been lost.
337 void OnMouseLockLost();
338 // A mouse lock is enabled and mouse events are being delivered.
339 void HandleMouseLockedInputEvent(const blink::WebMouseEvent& event);
341 // Simulates an input event to the plugin by passing it down to WebKit,
342 // which sends it back up to the plugin as if it came from the user.
343 void SimulateInputEvent(const ppapi::InputEventData& input_event);
345 // Simulates an IME event at the level of RenderView which sends it back up to
346 // the plugin as if it came from the user.
347 bool SimulateIMEEvent(const ppapi::InputEventData& input_event);
348 void SimulateImeSetCompositionEvent(const ppapi::InputEventData& input_event);
350 // The document loader is valid when the plugin is "full-frame" and in this
351 // case is non-NULL as long as the corresponding loader resource is alive.
352 // This pointer is non-owning, so the loader must use set_document_loader to
353 // clear itself when it is destroyed.
354 blink::WebURLLoaderClient* document_loader() const {
355 return document_loader_;
357 void set_document_loader(blink::WebURLLoaderClient* loader) {
358 document_loader_ = loader;
361 ContentDecryptorDelegate* GetContentDecryptorDelegate();
363 // PluginInstance implementation
364 RenderView* GetRenderView() override;
365 blink::WebPluginContainer* GetContainer() override;
366 v8::Isolate* GetIsolate() const override;
367 ppapi::VarTracker* GetVarTracker() override;
368 const GURL& GetPluginURL() override;
369 base::FilePath GetModulePath() override;
370 PP_Resource CreateImage(gfx::ImageSkia* source_image, float scale) override;
371 PP_ExternalPluginResult SwitchToOutOfProcessProxy(
372 const base::FilePath& file_path,
373 ppapi::PpapiPermissions permissions,
374 const IPC::ChannelHandle& channel_handle,
375 base::ProcessId plugin_pid,
376 int plugin_child_id) override;
377 void SetAlwaysOnTop(bool on_top) override;
378 bool IsFullPagePlugin() override;
379 bool FlashSetFullscreen(bool fullscreen, bool delay_report) override;
380 bool IsRectTopmost(const gfx::Rect& rect) override;
381 int32_t Navigate(const ppapi::URLRequestInfoData& request,
382 const char* target,
383 bool from_user_action) override;
384 int MakePendingFileRefRendererHost(const base::FilePath& path) override;
385 void SetEmbedProperty(PP_Var key, PP_Var value) override;
386 void SetSelectedText(const base::string16& selected_text) override;
387 void SetLinkUnderCursor(const std::string& url) override;
388 void SetTextInputType(ui::TextInputType type) override;
389 void PostMessageToJavaScript(PP_Var message) override;
391 // PPB_Instance_API implementation.
392 PP_Bool BindGraphics(PP_Instance instance, PP_Resource device) override;
393 PP_Bool IsFullFrame(PP_Instance instance) override;
394 const ppapi::ViewData* GetViewData(PP_Instance instance) override;
395 PP_Bool FlashIsFullscreen(PP_Instance instance) override;
396 PP_Var GetWindowObject(PP_Instance instance) override;
397 PP_Var GetOwnerElementObject(PP_Instance instance) override;
398 PP_Var ExecuteScript(PP_Instance instance,
399 PP_Var script,
400 PP_Var* exception) override;
401 uint32_t GetAudioHardwareOutputSampleRate(PP_Instance instance) override;
402 uint32_t GetAudioHardwareOutputBufferSize(PP_Instance instance) override;
403 PP_Var GetDefaultCharSet(PP_Instance instance) override;
404 void SetPluginToHandleFindRequests(PP_Instance) override;
405 void NumberOfFindResultsChanged(PP_Instance instance,
406 int32_t total,
407 PP_Bool final_result) override;
408 void SelectedFindResultChanged(PP_Instance instance, int32_t index) override;
409 void SetTickmarks(PP_Instance instance,
410 const PP_Rect* tickmarks,
411 uint32_t count) override;
412 PP_Bool IsFullscreen(PP_Instance instance) override;
413 PP_Bool SetFullscreen(PP_Instance instance, PP_Bool fullscreen) override;
414 PP_Bool GetScreenSize(PP_Instance instance, PP_Size* size) override;
415 ppapi::Resource* GetSingletonResource(PP_Instance instance,
416 ppapi::SingletonResourceID id) override;
417 int32_t RequestInputEvents(PP_Instance instance,
418 uint32_t event_classes) override;
419 int32_t RequestFilteringInputEvents(PP_Instance instance,
420 uint32_t event_classes) override;
421 void ClearInputEventRequest(PP_Instance instance,
422 uint32_t event_classes) override;
423 void StartTrackingLatency(PP_Instance instance) override;
424 void ZoomChanged(PP_Instance instance, double factor) override;
425 void ZoomLimitsChanged(PP_Instance instance,
426 double minimum_factor,
427 double maximum_factor) override;
428 void PostMessage(PP_Instance instance, PP_Var message) override;
429 int32_t RegisterMessageHandler(PP_Instance instance,
430 void* user_data,
431 const PPP_MessageHandler_0_2* handler,
432 PP_Resource message_loop) override;
433 int32_t RegisterMessageHandler_1_1_Deprecated(
434 PP_Instance instance,
435 void* user_data,
436 const PPP_MessageHandler_0_1_Deprecated* handler,
437 PP_Resource message_loop) override;
438 void UnregisterMessageHandler(PP_Instance instance) override;
439 PP_Bool SetCursor(PP_Instance instance,
440 PP_MouseCursor_Type type,
441 PP_Resource image,
442 const PP_Point* hot_spot) override;
443 int32_t LockMouse(PP_Instance instance,
444 scoped_refptr<ppapi::TrackedCallback> callback) override;
445 void UnlockMouse(PP_Instance instance) override;
446 void SetTextInputType(PP_Instance instance, PP_TextInput_Type type) override;
447 void UpdateCaretPosition(PP_Instance instance,
448 const PP_Rect& caret,
449 const PP_Rect& bounding_box) override;
450 void CancelCompositionText(PP_Instance instance) override;
451 void SelectionChanged(PP_Instance instance) override;
452 void UpdateSurroundingText(PP_Instance instance,
453 const char* text,
454 uint32_t caret,
455 uint32_t anchor) override;
456 PP_Var ResolveRelativeToDocument(PP_Instance instance,
457 PP_Var relative,
458 PP_URLComponents_Dev* components) override;
459 PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) override;
460 PP_Bool DocumentCanAccessDocument(PP_Instance instance,
461 PP_Instance target) override;
462 PP_Var GetDocumentURL(PP_Instance instance,
463 PP_URLComponents_Dev* components) override;
464 PP_Var GetPluginInstanceURL(PP_Instance instance,
465 PP_URLComponents_Dev* components) override;
466 PP_Var GetPluginReferrerURL(PP_Instance instance,
467 PP_URLComponents_Dev* components) override;
469 // PPB_ContentDecryptor_Private implementation.
470 void PromiseResolved(PP_Instance instance, uint32 promise_id) override;
471 void PromiseResolvedWithSession(PP_Instance instance,
472 uint32 promise_id,
473 PP_Var web_session_id_var) override;
474 void PromiseResolvedWithKeyIds(PP_Instance instance,
475 uint32 promise_id,
476 PP_Var key_ids_var) override;
477 void PromiseRejected(PP_Instance instance,
478 uint32 promise_id,
479 PP_CdmExceptionCode exception_code,
480 uint32 system_code,
481 PP_Var error_description_var) override;
482 void SessionMessage(PP_Instance instance,
483 PP_Var web_session_id_var,
484 PP_Var message_var,
485 PP_Var destination_url_var) override;
486 void SessionKeysChange(PP_Instance instance,
487 PP_Var web_session_id_var,
488 PP_Bool has_additional_usable_key) override;
489 void SessionExpirationChange(PP_Instance instance,
490 PP_Var web_session_id_var,
491 PP_Time new_expiry_time) override;
492 void SessionReady(PP_Instance instance, PP_Var web_session_id_var) override;
493 void SessionClosed(PP_Instance instance, PP_Var web_session_id_var) override;
494 void SessionError(PP_Instance instance,
495 PP_Var web_session_id_var,
496 PP_CdmExceptionCode exception_code,
497 uint32 system_code,
498 PP_Var error_description_var) override;
499 void DeliverBlock(PP_Instance instance,
500 PP_Resource decrypted_block,
501 const PP_DecryptedBlockInfo* block_info) override;
502 void DecoderInitializeDone(PP_Instance instance,
503 PP_DecryptorStreamType decoder_type,
504 uint32_t request_id,
505 PP_Bool success) override;
506 void DecoderDeinitializeDone(PP_Instance instance,
507 PP_DecryptorStreamType decoder_type,
508 uint32_t request_id) override;
509 void DecoderResetDone(PP_Instance instance,
510 PP_DecryptorStreamType decoder_type,
511 uint32_t request_id) override;
512 void DeliverFrame(PP_Instance instance,
513 PP_Resource decrypted_frame,
514 const PP_DecryptedFrameInfo* frame_info) override;
515 void DeliverSamples(PP_Instance instance,
516 PP_Resource audio_frames,
517 const PP_DecryptedSampleInfo* sample_info) override;
519 // Reset this instance as proxied. Assigns the instance a new module, resets
520 // cached interfaces to point to the out-of-process proxy and re-sends
521 // DidCreate, DidChangeView, and HandleDocumentLoad (if necessary).
522 // This should be used only when switching an in-process instance to an
523 // external out-of-process instance.
524 PP_ExternalPluginResult ResetAsProxied(scoped_refptr<PluginModule> module);
526 // Checks whether this is a valid instance of the given module. After calling
527 // ResetAsProxied above, a NaCl plugin instance's module changes, so external
528 // hosts won't recognize it as a valid instance of the original module. This
529 // method fixes that be checking that either module_ or original_module_ match
530 // the given module.
531 bool IsValidInstanceOf(PluginModule* module);
533 // cc::TextureLayerClient implementation.
534 bool PrepareTextureMailbox(
535 cc::TextureMailbox* mailbox,
536 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
537 bool use_shared_memory) override;
539 // RenderFrameObserver
540 void OnDestruct() override;
542 void AddLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
544 private:
545 friend class base::RefCounted<PepperPluginInstanceImpl>;
546 friend class PpapiPluginInstanceTest;
547 friend class PpapiUnittest;
549 // Delete should be called by the WebPlugin before this destructor.
550 ~PepperPluginInstanceImpl() override;
552 // Class to record document load notifications and play them back once the
553 // real document loader becomes available. Used only by external instances.
554 class ExternalDocumentLoader : public blink::WebURLLoaderClient {
555 public:
556 ExternalDocumentLoader();
557 virtual ~ExternalDocumentLoader();
559 void ReplayReceivedData(WebURLLoaderClient* document_loader);
561 // blink::WebURLLoaderClient implementation.
562 virtual void didReceiveData(blink::WebURLLoader* loader,
563 const char* data,
564 int data_length,
565 int encoded_data_length);
566 virtual void didFinishLoading(blink::WebURLLoader* loader,
567 double finish_time,
568 int64_t total_encoded_data_length);
569 virtual void didFail(blink::WebURLLoader* loader,
570 const blink::WebURLError& error);
572 private:
573 std::list<std::string> data_;
574 bool finished_loading_;
575 scoped_ptr<blink::WebURLError> error_;
578 // Implements PPB_Gamepad_API. This is just to avoid having an excessive
579 // number of interfaces implemented by PepperPluginInstanceImpl.
580 class GamepadImpl : public ppapi::thunk::PPB_Gamepad_API,
581 public ppapi::Resource {
582 public:
583 GamepadImpl();
584 // Resource implementation.
585 ppapi::thunk::PPB_Gamepad_API* AsPPB_Gamepad_API() override;
586 void Sample(PP_Instance instance, PP_GamepadsSampleData* data) override;
588 private:
589 ~GamepadImpl() override;
592 // See the static Create functions above for creating PepperPluginInstanceImpl
593 // objects. This constructor is private so that we can hide the
594 // PPP_Instance_Combined details while still having 1 constructor to maintain
595 // for member initialization.
596 PepperPluginInstanceImpl(RenderFrameImpl* render_frame,
597 PluginModule* module,
598 ppapi::PPP_Instance_Combined* instance_interface,
599 blink::WebPluginContainer* container,
600 const GURL& plugin_url);
602 bool LoadFindInterface();
603 bool LoadInputEventInterface();
604 bool LoadMouseLockInterface();
605 bool LoadPdfInterface();
606 bool LoadPrintInterface();
607 bool LoadPrivateInterface();
608 bool LoadTextInputInterface();
609 bool LoadZoomInterface();
611 // Update any transforms that should be applied to the texture layer.
612 void UpdateLayerTransform();
614 // Determines if we think the plugin has focus, both content area and webkit
615 // (see has_webkit_focus_ below).
616 bool PluginHasFocus() const;
617 void SendFocusChangeNotification();
619 void UpdateTouchEventRequest();
621 // Returns true if the plugin has registered to accept wheel events.
622 bool IsAcceptingWheelEvents() const;
624 void ScheduleAsyncDidChangeView();
625 void SendAsyncDidChangeView();
626 void SendDidChangeView();
628 // Reports the current plugin geometry to the plugin by calling
629 // DidChangeView.
630 void ReportGeometry();
632 // Queries the plugin for supported print formats and sets |format| to the
633 // best format to use. Returns false if the plugin does not support any
634 // print format that we can handle (we can handle only PDF).
635 bool GetPreferredPrintOutputFormat(PP_PrintOutputFormat_Dev* format);
636 bool PrintPDFOutput(PP_Resource print_output, blink::WebCanvas* canvas);
638 // Updates the layer for compositing. This creates a layer and attaches to the
639 // container if:
640 // - we have a bound Graphics3D and the Graphics3D has a texture, OR
641 // we have a bound Graphics2D and are using software compositing
642 // - we are not in Flash full-screen mode (or transitioning to it)
643 // Otherwise it destroys the layer.
644 // It does either operation lazily.
645 // device_changed: true if the bound device has been changed, and
646 // UpdateLayer() will be forced to recreate the layer and attaches to the
647 // container.
648 void UpdateLayer(bool device_changed);
650 // Internal helper function for PrintPage().
651 bool PrintPageHelper(PP_PrintPageNumberRange_Dev* page_ranges,
652 int num_ranges,
653 blink::WebCanvas* canvas);
655 void DoSetCursor(blink::WebCursorInfo* cursor);
657 // Internal helper functions for HandleCompositionXXX().
658 bool SendCompositionEventToPlugin(PP_InputEvent_Type type,
659 const base::string16& text);
660 bool SendCompositionEventWithUnderlineInformationToPlugin(
661 PP_InputEvent_Type type,
662 const base::string16& text,
663 const std::vector<blink::WebCompositionUnderline>& underlines,
664 int selection_start,
665 int selection_end);
667 // Internal helper function for XXXInputEvents().
668 void RequestInputEventsHelper(uint32_t event_classes);
670 // Checks if the security origin of the document containing this instance can
671 // assess the security origin of the main frame document.
672 bool CanAccessMainFrame() const;
674 // Returns true if the WebView the plugin is in renders via the accelerated
675 // compositing path.
676 bool IsViewAccelerated();
678 // Track, set and reset size attributes to control the size of the plugin
679 // in and out of fullscreen mode.
680 void KeepSizeAttributesBeforeFullscreen();
681 void SetSizeAttributesForFullscreen();
682 void ResetSizeAttributesAfterFullscreen();
684 bool IsMouseLocked();
685 bool LockMouse();
686 MouseLockDispatcher* GetMouseLockDispatcher();
687 MouseLockDispatcher::LockTarget* GetOrCreateLockTargetAdapter();
688 void UnSetAndDeleteLockTargetAdapter();
690 void DidDataFromWebURLResponse(const blink::WebURLResponse& response,
691 int pending_host_id,
692 const ppapi::URLResponseInfoData& data);
694 RenderFrameImpl* render_frame_;
695 base::Closure instance_deleted_callback_;
696 scoped_refptr<PluginModule> module_;
697 scoped_ptr<ppapi::PPP_Instance_Combined> instance_interface_;
698 // If this is the NaCl plugin, we create a new module when we switch to the
699 // IPC-based PPAPI proxy. Store the original module and instance interface
700 // so we can shut down properly.
701 scoped_refptr<PluginModule> original_module_;
702 scoped_ptr<ppapi::PPP_Instance_Combined> original_instance_interface_;
704 PP_Instance pp_instance_;
706 // NULL until we have been initialized.
707 blink::WebPluginContainer* container_;
708 scoped_refptr<cc::Layer> compositor_layer_;
709 scoped_refptr<cc::TextureLayer> texture_layer_;
710 scoped_ptr<blink::WebLayer> web_layer_;
711 bool layer_bound_to_fullscreen_;
712 bool layer_is_hardware_;
714 // Plugin URL.
715 GURL plugin_url_;
717 // Responsible for turning on throttling if Power Saver is on.
718 scoped_ptr<PepperPluginInstanceThrottler> throttler_;
720 // Indicates whether this is a full frame instance, which means it represents
721 // an entire document rather than an embed tag.
722 bool full_frame_;
724 // Stores the current state of the plugin view.
725 ppapi::ViewData view_data_;
726 // The last state sent to the plugin. It is only valid after
727 // |sent_initial_did_change_view_| is set to true.
728 ppapi::ViewData last_sent_view_data_;
730 // Indicates if we've ever sent a didChangeView to the plugin. This ensures we
731 // always send an initial notification, even if the position and clip are the
732 // same as the default values.
733 bool sent_initial_did_change_view_;
735 // The current device context for painting in 2D, 3D or compositor.
736 scoped_refptr<PPB_Graphics3D_Impl> bound_graphics_3d_;
737 PepperGraphics2DHost* bound_graphics_2d_platform_;
738 PepperCompositorHost* bound_compositor_;
740 // We track two types of focus, one from WebKit, which is the focus among
741 // all elements of the page, one one from the browser, which is whether the
742 // tab/window has focus. We tell the plugin it has focus only when both of
743 // these values are set to true.
744 bool has_webkit_focus_;
745 bool has_content_area_focus_;
747 // The id of the current find operation, or -1 if none is in process.
748 int find_identifier_;
750 // Helper object that creates resources.
751 scoped_ptr<ppapi::thunk::ResourceCreationAPI> resource_creation_;
753 // The plugin-provided interfaces.
754 // When adding PPP interfaces, make sure to reset them in ResetAsProxied.
755 const PPP_Find_Private* plugin_find_interface_;
756 const PPP_InputEvent* plugin_input_event_interface_;
757 const PPP_MouseLock* plugin_mouse_lock_interface_;
758 const PPP_Pdf* plugin_pdf_interface_;
759 const PPP_Instance_Private* plugin_private_interface_;
760 const PPP_TextInput_Dev* plugin_textinput_interface_;
761 const PPP_Zoom_Dev* plugin_zoom_interface_;
763 // Flags indicating whether we have asked this plugin instance for the
764 // corresponding interfaces, so that we can ask only once.
765 // When adding flags, make sure to reset them in ResetAsProxied.
766 bool checked_for_plugin_input_event_interface_;
767 bool checked_for_plugin_pdf_interface_;
769 // This is only valid between a successful PrintBegin call and a PrintEnd
770 // call.
771 PP_PrintSettings_Dev current_print_settings_;
772 #if defined(OS_MACOSX)
773 // On the Mac, when we draw the bitmap to the PDFContext, it seems necessary
774 // to keep the pixels valid until CGContextEndPage is called. We use this
775 // variable to hold on to the pixels.
776 scoped_refptr<PPB_ImageData_Impl> last_printed_page_;
777 #endif // defined(OS_MACOSX)
778 // Always when printing to PDF on Linux and when printing for preview on Mac
779 // and Win, the entire document goes into one metafile. However, when users
780 // print only a subset of all the pages, it is impossible to know if a call
781 // to PrintPage() is the last call. Thus in PrintPage(), just store the page
782 // number in |ranges_|. The hack is in PrintEnd(), where a valid |canvas_|
783 // is preserved in PrintWebViewHelper::PrintPages. This makes it possible
784 // to generate the entire PDF given the variables below:
786 // The most recently used WebCanvas, guaranteed to be valid.
787 skia::RefPtr<blink::WebCanvas> canvas_;
788 // An array of page ranges.
789 std::vector<PP_PrintPageNumberRange_Dev> ranges_;
791 scoped_refptr<ppapi::Resource> gamepad_impl_;
792 scoped_refptr<ppapi::Resource> uma_private_impl_;
794 // The plugin print interface.
795 const PPP_Printing_Dev* plugin_print_interface_;
797 // The plugin 3D interface.
798 const PPP_Graphics3D* plugin_graphics_3d_interface_;
800 // Contains the cursor if it's set by the plugin.
801 scoped_ptr<blink::WebCursorInfo> cursor_;
803 // Set to true if this plugin thinks it will always be on top. This allows us
804 // to use a more optimized painting path in some cases.
805 bool always_on_top_;
806 // Even if |always_on_top_| is true, the plugin is not fully visible if there
807 // are some cut-out areas (occupied by iframes higher in the stacking order).
808 // This information is used in the optimized painting path.
809 std::vector<gfx::Rect> cut_outs_rects_;
811 // Implementation of PPB_FlashFullscreen.
813 // Plugin container for fullscreen mode. NULL if not in fullscreen mode. Note:
814 // there is a transition state where fullscreen_container_ is non-NULL but
815 // flash_fullscreen_ is false (see above).
816 FullscreenContainer* fullscreen_container_;
818 // True if we are in "flash" fullscreen mode. False if we are in normal mode
819 // or in transition to fullscreen. Normal fullscreen mode is indicated in
820 // the ViewData.
821 bool flash_fullscreen_;
823 // Implementation of PPB_Fullscreen.
825 // Since entering fullscreen mode is an asynchronous operation, we set this
826 // variable to the desired state at the time we issue the fullscreen change
827 // request. The plugin will receive a DidChangeView event when it goes
828 // fullscreen.
829 bool desired_fullscreen_state_;
831 // WebKit does not resize the plugin when going into fullscreen mode, so we do
832 // this here by modifying the various plugin attributes and then restoring
833 // them on exit.
834 blink::WebString width_before_fullscreen_;
835 blink::WebString height_before_fullscreen_;
836 blink::WebString border_before_fullscreen_;
837 blink::WebString style_before_fullscreen_;
838 gfx::Size screen_size_for_fullscreen_;
840 // The MessageChannel used to implement bidirectional postMessage for the
841 // instance.
842 v8::Persistent<v8::Object> message_channel_object_;
844 // A pointer to the MessageChannel underlying |message_channel_object_|. It is
845 // only valid as long as |message_channel_object_| is alive.
846 MessageChannel* message_channel_;
848 // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
849 SkBitmap* sad_plugin_;
851 typedef std::set<PluginObject*> PluginObjectSet;
852 PluginObjectSet live_plugin_objects_;
854 // Classes of events that the plugin has registered for, both for filtering
855 // and not. The bits are PP_INPUTEVENT_CLASS_*.
856 uint32_t input_event_mask_;
857 uint32_t filtered_input_event_mask_;
859 // Text composition status.
860 ui::TextInputType text_input_type_;
861 gfx::Rect text_input_caret_;
862 gfx::Rect text_input_caret_bounds_;
863 bool text_input_caret_set_;
865 // Text selection status.
866 std::string surrounding_text_;
867 size_t selection_caret_;
868 size_t selection_anchor_;
870 scoped_refptr<ppapi::TrackedCallback> lock_mouse_callback_;
872 // Track pending user gestures so out-of-process plugins can respond to
873 // a user gesture after it has been processed.
874 PP_TimeTicks pending_user_gesture_;
875 blink::WebUserGestureToken pending_user_gesture_token_;
877 // We store the arguments so we can re-send them if we are reset to talk to
878 // NaCl via the IPC NaCl proxy.
879 std::vector<std::string> argn_;
880 std::vector<std::string> argv_;
882 // Non-owning pointer to the document loader, if any.
883 blink::WebURLLoaderClient* document_loader_;
884 // State for deferring document loads. Used only by external instances.
885 blink::WebURLResponse external_document_response_;
886 scoped_ptr<ExternalDocumentLoader> external_document_loader_;
887 bool external_document_load_;
889 // The ContentDecryptorDelegate forwards PPP_ContentDecryptor_Private
890 // calls and handles PPB_ContentDecryptor_Private calls.
891 scoped_ptr<ContentDecryptorDelegate> content_decryptor_delegate_;
893 // The link currently under the cursor.
894 base::string16 link_under_cursor_;
896 // We store the isolate at construction so that we can be sure to use the
897 // Isolate in which this Instance was created when interacting with v8.
898 v8::Isolate* isolate_;
900 scoped_ptr<MouseLockDispatcher::LockTarget> lock_target_;
902 bool is_deleted_;
904 // The text that is currently selected in the plugin.
905 base::string16 selected_text_;
907 int64 last_input_number_;
909 bool is_tracking_latency_;
911 // We use a weak ptr factory for scheduling DidChangeView events so that we
912 // can tell whether updates are pending and consolidate them. When there's
913 // already a weak ptr pending (HasWeakPtrs is true), code should update the
914 // view_data_ but not send updates. This also allows us to cancel scheduled
915 // view change events.
916 base::WeakPtrFactory<PepperPluginInstanceImpl> view_change_weak_ptr_factory_;
917 base::WeakPtrFactory<PepperPluginInstanceImpl> weak_factory_;
919 DISALLOW_COPY_AND_ASSIGN(PepperPluginInstanceImpl);
922 } // namespace content
924 #endif // CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_INSTANCE_IMPL_H_