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 "GraphicsContext.h"
32#include "IntPoint.h"
33#include "IntRect.h"
34#include "MediaLayer.h"
35#include "SkRect.h"
36#include <jni.h>
37
38namespace WebCore {
39    class PluginView;
40}
41
42namespace android {
43    class PluginSurface;
44    class WebViewCore;
45}
46
47class SkCanvas;
48class SkFlipPixelRef;
49
50/*
51    This is our extended state in a PluginView. This object is created and
52    kept insync with the PluginView, but is also available to WebViewCore
53    to allow its draw() method to be called from outside of the PluginView.
54 */
55struct PluginWidgetAndroid {
56    // initialize with our host pluginview. This will delete us when it is
57    // destroyed.
58    PluginWidgetAndroid(WebCore::PluginView* view);
59    ~PluginWidgetAndroid();
60
61    WebCore::PluginView* pluginView() const { return m_pluginView; }
62
63    // Needed by PluginSurface to manage the java SurfaceView.
64    android::WebViewCore* webViewCore() const { return m_core; }
65
66    /*  Can't determine our core at construction time, so PluginView calls this
67        as soon as it has a parent.
68     */
69    void init(android::WebViewCore*);
70    /*  Called each time the PluginView gets a new size or position.
71     */
72    void setWindow(NPWindow* window, bool isTransparent);
73
74    /*  Called whenever the plugin itself requests a new drawing model. If the
75        hardware does not support the requested model then false is returned,
76        otherwise true is returned.
77     */
78    bool setDrawingModel(ANPDrawingModel);
79
80    /*  Called to check if the plugin is running in "windowed" mode (i.e. surface
81        view).
82     */
83    bool isSurfaceDrawingModel() const { return kSurface_ANPDrawingModel == m_drawingModel; }
84
85    bool isOpenGLDrawingModel() const { return kOpenGL_ANPDrawingModel == m_drawingModel; }
86
87    void checkSurfaceReady();
88
89    /*  Returns true (and optionally updates rect with the dirty bounds in the
90        page coordinate) if the plugin has invalidate us.
91     */
92    bool isDirty(SkIRect* dirtyBounds = NULL) const;
93    /*  Called by PluginView to invalidate a portion of the plugin area (in
94        local plugin coordinates). If signalRedraw is true, this also triggers
95        a subsequent call to draw(NULL).
96     */
97    void inval(const WebCore::IntRect&, bool signalRedraw);
98
99    /*  Called to draw into the plugin's bitmap. If canvas is non-null, the
100        bitmap itself is then drawn into the canvas.
101     */
102    void draw(PlatformGraphicsContext* context = NULL);
103
104    /*  Send this event to the plugin instance. A non-zero value will be
105        returned if the plugin handled the event.
106     */
107    int16_t sendEvent(const ANPEvent&);
108
109    /*  Update the plugins event flags. If a flag is set to true then the plugin
110        wants to be notified of events of this type.
111     */
112    void updateEventFlags(ANPEventFlags);
113
114    /*  Called to check if a plugin wants to accept a given event type. It
115        returns true if the plugin wants the events and false otherwise.
116     */
117    bool isAcceptingEvent(ANPEventFlag);
118
119    /*  Notify the plugin of the currently visible screen coordinates (document
120        space) and the current zoom level.
121     */
122    void setVisibleScreen(const ANPRectI& visibleScreenRect, float zoom);
123
124    /** Returns a rectangle representing the visible area of the plugin on
125        screen. The coordinates are relative to the size of the plugin in the
126        document and will not be negative or exceed the plugin's size.
127     */
128    ANPRectI visibleRect();
129
130    /** Registers a set of rectangles that the plugin would like to keep on
131        screen. The rectangles are listed in order of priority with the highest
132        priority rectangle in location rects[0].  The browser will attempt to keep
133        as many of the rectangles on screen as possible and will scroll them into
134        view in response to the invocation of this method and other various events.
135        The count specifies how many rectangles are in the array. If the count is
136        zero it signals the plugin that any existing rectangles should be cleared
137        and no rectangles will be tracked.
138     */
139    void setVisibleRects(const ANPRectI rects[], int32_t count);
140
141    /** Called when a plugin wishes to enter into full screen mode. It invokes
142        the plugin's Java class (defined in the plugin's apk manifest), which is
143        called asynchronously and provides a View to be displayed full screen.
144     */
145    void requestFullScreen();
146
147    /** Called when a plugin wishes to exit from full screen mode. As a result,
148        the plugin's full-screen view is discarded by the view system. It is also
149        called in order to notify the native code that the browser has discarded
150        the view.
151     */
152    void exitFullScreen(bool pluginInitiated);
153
154    bool inFullScreen() { return m_isFullScreen; }
155
156    void setFullScreenOrientation(ANPScreenOrientation orientation);
157
158    /** Called to check if a plugin currently has document focus, which is
159        required for certain operations (e.g. show/hide keyboard). It returns
160        true if the plugin currently has focus and false otherwise.
161     */
162    bool hasFocus() const { return m_hasFocus; }
163
164    /** Called to ensure the surface is being correctly displayed within the
165        view hierarchy. For instance, if the visibility of the plugin has
166        changed then we need to ensure the surface is added or removed from the
167        view system.
168     */
169    void layoutSurface(bool pluginBoundsChanged = false);
170
171    /** send the surface the currently visible portion of the plugin. This is not
172        the portion of the plugin visible on the screen but rather the portion of
173        the plugin that is not obscured by other HTML content.
174     */
175    void setSurfaceClip(const SkIRect& clip);
176
177    /** Called when a plugin wishes to be zoomed and centered in the current view.
178     */
179    void requestCenterFitZoom();
180
181    WebCore::MediaLayer* getLayer() const { return m_layer; }
182
183    void setPowerState(ANPPowerState powerState);
184
185    void viewInvalidate();
186
187private:
188    void computeVisiblePluginRect();
189    void scrollToVisiblePluginRect();
190    void sendSizeAndVisibilityEvents(const bool updateDimensions);
191
192    WebCore::MediaLayer*   m_layer;
193
194    WebCore::PluginView*    m_pluginView;
195    android::WebViewCore*   m_core;
196    SkFlipPixelRef*         m_flipPixelRef;
197    ANPDrawingModel         m_drawingModel;
198    ANPEventFlags           m_eventFlags;
199    NPWindow*               m_pluginWindow;
200    SkIRect                 m_pluginBounds; // relative to the page
201    SkIRect                 m_visibleDocRect; // relative to the page
202    SkIRect                 m_requestedVisibleRect; // relative to the page
203    bool                    m_hasFocus;
204    bool                    m_isFullScreen;
205    bool                    m_visible;
206    float                   m_cachedZoomLevel; // used for comparison only
207    jobject                 m_embeddedView;
208    bool                    m_embeddedViewAttached;
209    bool                    m_acceptEvents;
210    bool                    m_isSurfaceClippedOut;
211    ANPPowerState           m_powerState;
212    int                     m_fullScreenOrientation;
213    bool                    m_drawEventDelayed;
214
215    /* We limit the number of rectangles to minimize storage and ensure adequate
216       speed.
217    */
218    enum {
219        MAX_REQUESTED_RECTS = 5,
220    };
221
222    ANPRectI                m_requestedVisibleRects[MAX_REQUESTED_RECTS];
223    int32_t                 m_requestedVisibleRectCount;
224};
225
226#endif
227