1 /* 2 * Copyright 2008, The Android Open Source Project 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef PluginWidgetAndroid_h 27 #define PluginWidgetAndroid_h 28 29 #include "android_npapi.h" 30 #include "ANPSystem_npapi.h" 31 #include "IntPoint.h" 32 #include "IntRect.h" 33 #include "MediaLayer.h" 34 #include "SkRect.h" 35 #include <jni.h> 36 37 namespace WebCore { 38 class PluginView; 39 } 40 41 namespace android { 42 class PluginSurface; 43 class WebViewCore; 44 } 45 46 class SkCanvas; 47 class SkFlipPixelRef; 48 49 /* 50 This is our extended state in a PluginView. This object is created and 51 kept insync with the PluginView, but is also available to WebViewCore 52 to allow its draw() method to be called from outside of the PluginView. 53 */ 54 struct PluginWidgetAndroid { 55 // initialize with our host pluginview. This will delete us when it is 56 // destroyed. 57 PluginWidgetAndroid(WebCore::PluginView* view); 58 ~PluginWidgetAndroid(); 59 60 WebCore::PluginView* pluginView() const { return m_pluginView; } 61 62 // Needed by PluginSurface to manage the java SurfaceView. 63 android::WebViewCore* webViewCore() const { return m_core; } 64 65 /* Can't determine our core at construction time, so PluginView calls this 66 as soon as it has a parent. 67 */ 68 void init(android::WebViewCore*); 69 /* Called each time the PluginView gets a new size or position. 70 */ 71 void setWindow(NPWindow* window, bool isTransparent); 72 73 /* Called whenever the plugin itself requests a new drawing model. If the 74 hardware does not support the requested model then false is returned, 75 otherwise true is returned. 76 */ 77 bool setDrawingModel(ANPDrawingModel); 78 79 /* Called to check if the plugin is running in "windowed" mode (i.e. surface 80 view). 81 */ 82 bool isSurfaceDrawingModel() const { return kSurface_ANPDrawingModel == m_drawingModel; } 83 84 bool isOpenGLDrawingModel() const { return kOpenGL_ANPDrawingModel == m_drawingModel; } 85 86 void checkSurfaceReady(); 87 88 /* Returns true (and optionally updates rect with the dirty bounds in the 89 page coordinate) if the plugin has invalidate us. 90 */ 91 bool isDirty(SkIRect* dirtyBounds = NULL) const; 92 /* Called by PluginView to invalidate a portion of the plugin area (in 93 local plugin coordinates). If signalRedraw is true, this also triggers 94 a subsequent call to draw(NULL). 95 */ 96 void inval(const WebCore::IntRect&, bool signalRedraw); 97 98 /* Called to draw into the plugin's bitmap. If canvas is non-null, the 99 bitmap itself is then drawn into the canvas. 100 */ 101 void draw(SkCanvas* canvas = NULL); 102 103 /* Send this event to the plugin instance. A non-zero value will be 104 returned if the plugin handled the event. 105 */ 106 int16_t sendEvent(const ANPEvent&); 107 108 /* Update the plugins event flags. If a flag is set to true then the plugin 109 wants to be notified of events of this type. 110 */ 111 void updateEventFlags(ANPEventFlags); 112 113 /* Called to check if a plugin wants to accept a given event type. It 114 returns true if the plugin wants the events and false otherwise. 115 */ 116 bool isAcceptingEvent(ANPEventFlag); 117 118 /* Notify the plugin of the currently visible screen coordinates (document 119 space) and the current zoom level. 120 */ 121 void setVisibleScreen(const ANPRectI& visibleScreenRect, float zoom); 122 123 /** Returns a rectangle representing the visible area of the plugin on 124 screen. The coordinates are relative to the size of the plugin in the 125 document and will not be negative or exceed the plugin's size. 126 */ 127 ANPRectI visibleRect(); 128 129 /** Registers a set of rectangles that the plugin would like to keep on 130 screen. The rectangles are listed in order of priority with the highest 131 priority rectangle in location rects[0]. The browser will attempt to keep 132 as many of the rectangles on screen as possible and will scroll them into 133 view in response to the invocation of this method and other various events. 134 The count specifies how many rectangles are in the array. If the count is 135 zero it signals the plugin that any existing rectangles should be cleared 136 and no rectangles will be tracked. 137 */ 138 void setVisibleRects(const ANPRectI rects[], int32_t count); 139 140 /** Called when a plugin wishes to enter into full screen mode. It invokes 141 the plugin's Java class (defined in the plugin's apk manifest), which is 142 called asynchronously and provides a View to be displayed full screen. 143 */ 144 void requestFullScreen(); 145 146 /** Called when a plugin wishes to exit from full screen mode. As a result, 147 the plugin's full-screen view is discarded by the view system. It is also 148 called in order to notify the native code that the browser has discarded 149 the view. 150 */ 151 void exitFullScreen(bool pluginInitiated); 152 153 bool inFullScreen() { return m_isFullScreen; } 154 155 void setFullScreenOrientation(ANPScreenOrientation orientation); 156 157 /** Called to check if a plugin currently has document focus, which is 158 required for certain operations (e.g. show/hide keyboard). It returns 159 true if the plugin currently has focus and false otherwise. 160 */ 161 bool hasFocus() const { return m_hasFocus; } 162 163 /** Called to ensure the surface is being correctly displayed within the 164 view hierarchy. For instance, if the visibility of the plugin has 165 changed then we need to ensure the surface is added or removed from the 166 view system. 167 */ 168 void layoutSurface(bool pluginBoundsChanged = false); 169 170 /** send the surface the currently visible portion of the plugin. This is not 171 the portion of the plugin visible on the screen but rather the portion of 172 the plugin that is not obscured by other HTML content. 173 */ 174 void setSurfaceClip(const SkIRect& clip); 175 176 /** Called when a plugin wishes to be zoomed and centered in the current view. 177 */ 178 void requestCenterFitZoom(); 179 180 WebCore::MediaLayer* getLayer() const { return m_layer; } 181 182 void setPowerState(ANPPowerState powerState); 183 184 void viewInvalidate(); 185 186 private: 187 void computeVisiblePluginRect(); 188 void scrollToVisiblePluginRect(); 189 void sendSizeAndVisibilityEvents(const bool updateDimensions); 190 191 WebCore::MediaLayer* m_layer; 192 193 WebCore::PluginView* m_pluginView; 194 android::WebViewCore* m_core; 195 SkFlipPixelRef* m_flipPixelRef; 196 ANPDrawingModel m_drawingModel; 197 ANPEventFlags m_eventFlags; 198 NPWindow* m_pluginWindow; 199 SkIRect m_pluginBounds; // relative to the page 200 SkIRect m_visibleDocRect; // relative to the page 201 SkIRect m_requestedVisibleRect; // relative to the page 202 bool m_hasFocus; 203 bool m_isFullScreen; 204 bool m_visible; 205 float m_cachedZoomLevel; // used for comparison only 206 jobject m_embeddedView; 207 bool m_embeddedViewAttached; 208 bool m_acceptEvents; 209 bool m_isSurfaceClippedOut; 210 ANPPowerState m_powerState; 211 int m_fullScreenOrientation; 212 bool m_drawEventDelayed; 213 214 /* We limit the number of rectangles to minimize storage and ensure adequate 215 speed. 216 */ 217 enum { 218 MAX_REQUESTED_RECTS = 5, 219 }; 220 221 ANPRectI m_requestedVisibleRects[MAX_REQUESTED_RECTS]; 222 int32_t m_requestedVisibleRectCount; 223 }; 224 225 #endif 226