summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/android/jni/WebViewCore.h
blob: 1cb51b27ec5d8dc18fa2ee5372665cffe7923420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 *
 * Copyright 2006, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

#ifndef ANDROID_WIDGET_HTMLWIDGET_H
#define ANDROID_WIDGET_HTMLWIDGET_H

#include "WebCoreViewBridge.h"
#include "CacheBuilder.h"
#include "CachedHistory.h"
#include "FrameView.h"
#include "SkColor.h"
#include "SkScalar.h"
#include "SkRegion.h"
#include <ui/Rect.h>
#include <jni.h>

namespace WebCore {
    class AtomicString;
    class Color;
    class GraphicsContext;
    class HTMLSelectElement;
    class RenderPart;
    class RenderText;
    class FrameAndroid;
    class Node;
    class RenderTextControl;
}

class SkPicture;

namespace android {
    
    class CachedRoot;
    
    class ListBoxReply;

    class WebViewCore : public WebCoreViewBridge 
    {
    public:
        /**
         * Initialize the ViewBridge with a JNI environment, a native HTMLWidget object
         * and an associated frame to perform actions on.
         */
        WebViewCore(JNIEnv* env, jobject javaView, WebCore::FrameView* view);
        virtual ~WebViewCore();

        /**
         * Set the scroll offset.
         * @param x The x position of the new scroll offset.
         * @param y The y position of the new scroll offset.
         */
//        void setScrollOffset(int x, int y);

        // Inherited from WebCoreViewBridge
        virtual void draw(WebCore::GraphicsContext* ctx, 
            const WebCore::IntRect& rect, bool invalCache);

        /**
         * Layout our Frame if needed and recursively layout all child frames.
         */
        virtual void layout();

        /**
         * Scroll to an absolute position.
         * @param x The x coordinate.
         * @param y The y coordinate.
         * @param animate If it is true, animate to the new scroll position
         *
         * This method calls Java to trigger a gradual scroll event.
         */
        virtual void scrollTo(int x, int y, bool animate = false);
                 
        /**
         * Scroll to the point x,y relative to the current position.
         * @param x The relative x position.
         * @param y The relative y position.
         */
        virtual void scrollBy(int x, int y);
        
        /**
         * Mark the display list as invalid, and post an event (once) to
         * rebuild the display list by calling webcore to draw the dom
         */
        virtual void contentInvalidate();
        virtual void contentInvalidate(const WebCore::IntRect &rect);
        
        // invalidate the view/display, NOT the content/DOM
        virtual void viewInvalidate() { sendViewInvalidate(); }
        
        /**
         * Called by webcore when the focus was set after returning to prior page
         * used to rebuild and display any changes in focus
         */
        virtual void notifyFocusSet();
        /**
         * Called by webcore when the progress indicator is done
         * used to rebuild and display any changes in focus
         */
        virtual void notifyProgressFinished();
        
        /**
         * On resize is called after a setSize event on WebCoreViewBridge. onResize
         * then tells the frame to relayout the contents due to the size change
         */
        virtual void onResize();

        /**
         * Notify the view that WebCore did its first layout.
         */
        virtual void didFirstLayout();

        /**
         * Notify the view to restore the screen width, which in turn restores 
         * the scale.
         */
        virtual void restoreScale(int);

        /* Set the view and frame */
        virtual void setView(WebCore::FrameView* view) {
            if (mView)
                mView->deref();
            mView = view;
            if (mView) {
                mView->ref();
                mFrame = (WebCore::FrameAndroid*)mView->frame();
                reset(false);
            } else
                mFrame = NULL;
        }

        // new methods for this subclass

        void reset(bool fromConstructor);

        WebCore::FrameAndroid* frame() const { return mFrame; }
        WebCore::String retrieveHref(WebCore::Frame* frame, WebCore::Node* node);

        /**
         *  Return the url of the image located at (x,y) in content coordinates, or
         *  null if there is no image at that point.
         *
         *  @param x    x content ordinate
         *  @param y    y content ordinate
         *  @return WebCore::String  url of the image located at (x,y), or null if there is
         *                  no image there.
         */
        WebCore::String retrieveImageRef(int x, int y);

        WebCore::String getSelection(SkRegion* );
        void recordPicture(SkPicture* picture, bool invalCache);
        void setFrameCacheOutOfDate();
        void setFinalFocus(WebCore::Frame* frame, WebCore::Node* node, 
            int x, int y, bool block);
        void setKitFocus(int moveGeneration, int buildGeneration,
            WebCore::Frame* frame, WebCore::Node* node, int x, int y,
            bool ignoreNullFocus);
        int getMaxXScroll() const { return mMaxXScroll; }
        int getMaxYScroll() const { return mMaxYScroll; }
        void setMaxXScroll(int maxx) { mMaxXScroll = maxx; }
        void setMaxYScroll(int maxy) { mMaxYScroll = maxy; }

        int contentWidth() const { return mView->contentsWidth(); }
        int contentHeight() const { return mView->contentsHeight(); }
    
        // the visible rect is in document coordinates, and describes the
        // intersection of the document with the "window" in the UI.
        void getVisibleRect(WebCore::IntRect* rect) const;
        void setVisibleRect(const WebCore::IntRect& rect);

        WebCore::FrameView* getFrameView()  { return mView; }
        void listBoxRequest(WebCoreReply* reply, const uint16_t** labels, size_t count, const int enabled[], size_t enabledCount, 
                bool multiple, const int selected[], size_t selectedCountOrSelection);

        /**
         * Handle keyDown events from Java.
         * @param keyCode The key pressed.
         * @return Whether keyCode was handled by this class.
         */
        bool keyUp(KeyCode keyCode, int keyValue);

        /**
         * Handle motionUp event from the UI thread (called touchUp in the
         * WebCore thread).
         */
        void touchUp(int touchGeneration, int buildGeneration, 
            WebCore::Frame* frame, WebCore::Node* node, int x, int y, 
            int size, bool isClick, bool retry);
        
        /**
         * Return a new WebCoreViewBridge to interface with the passed in view.
         */
        virtual WebCoreViewBridge* createBridgeForView(WebCore::FrameView* view);
        
        /**
         * Sets the index of the label from a popup
         */
        void popupReply(int index);
        void popupReply(SkTDArray<int>array);
        
        virtual void jsAlert(const WebCore::String& url, const WebCore::String& text);
        virtual bool jsConfirm(const WebCore::String& url, const WebCore::String& text);
        virtual bool jsPrompt(const WebCore::String& url, const WebCore::String& message, const WebCore::String& defaultValue, WebCore::String& result);
        virtual bool jsUnload(const WebCore::String& url, const WebCore::String& message);
        
        /**
         *  Delete text from start to end in the focused textfield. If there is no
         *  focus, or if start == end, silently fail, but set selection to that value.  
         *  If start and end are out of order, swap them.
         *  Use the frame, node, x, and y to ensure that the correct node is focused.
         *  Return a frame. Convenience so replaceTextfieldText can use this function.
         */
        WebCore::Frame* deleteSelection(WebCore::Frame* frame, WebCore::Node* node, int x,
            int y,int start, int end);

        /**
         *  Set the selection of the currently focused textfield to (start, end).
         *  If start and end are out of order, swap them.
         *  Use the frame, node, x, and y to ensure that the correct node is focused.
         *  Return a frame. Convenience so deleteSelection can use this function.
         */
        WebCore::Frame* setSelection(WebCore::Frame* frame, WebCore::Node* node, int x,
            int y,int start, int end);
        /**
         *  In the currenlty focused textfield, represented by frame, node, x, and y (which
         *  are used to ensure it has focus), replace the characters from oldStart to oldEnd
         *  (if oldStart == oldEnd, this will be an insert at that position) with replace,
         *  and set the selection to (start, end).
         */
        void replaceTextfieldText(WebCore::Frame* frame, WebCore::Node* node, int x, int y, 
                int oldStart, int oldEnd, jstring replace, int start, int end);
        void passToJs(WebCore::Frame* frame, WebCore::Node* node, int x, int y, int generation,
            jstring currentText, int jKeyCode, int keyVal, bool down, bool cap, bool fn, bool sym);

        void saveDocumentState(WebCore::Frame* frame, WebCore::Node* node, int x, int y);

        // TODO: I don't like this hack but I need to access the java object in
        // order to send it as a parameter to java
        jobject getJavaObject();

        // Return the parent WebView Java object associated with this
        // WebViewCore.
        jobject getWebViewJavaObject();
        
        bool pinXToDocument(int* yPtr);
        bool pinYToDocument(int* yPtr);

        WebCore::RenderLayer* getRenderLayer();
        
        /**
         * Tell the java side to update the focused textfield
         * @param pointer   Pointer to the node for the input field.
         * @param   changeToPassword  If true, we are changing the textfield to
         *          a password field, and ignore the String
         * @param text  If changeToPassword is false, this is the new text that 
         *              should go into the textfield.
         */
        virtual void updateTextfield(WebCore::Node* pointer, 
                bool changeToPassword, const WebCore::String& text);

        virtual void removeFrameGeneration(WebCore::Frame* );
        virtual void updateFrameGeneration(WebCore::Frame* );
        
        void setBackgroundColor(SkColor c);
        void setSnapAnchor(int x, int y);
        void snapToAnchor();
        void unblockFocus() { mBlockFocusChange = false; }
        void updateFrameCache();
        void dump();
        
        // jni methods
        static void Destroy(JNIEnv*, jobject);
        static void Dump(JNIEnv*, jobject);
        static void RefreshPlugins(JNIEnv*, jobject, jboolean);
        static void SetSize(JNIEnv*, jobject, jint, jint, jint, jfloat);
        static void SetVisibleRect(JNIEnv*, jobject, jint, jint, jint, jint);
        static jboolean KeyUp(JNIEnv*, jobject, jint, jint);
        static void DeleteSelection(JNIEnv*, jobject, jint, jint, jint, jint,
                jint, jint);
        static void SetSelection(JNIEnv*, jobject, jint, jint, jint, jint,
                jint, jint);
        static void ReplaceTextfieldText(JNIEnv*, jobject, jint, jint, jint,
                jint, jint, jint, jstring, jint, jint);
        static void PassToJs(JNIEnv*, jobject, jint, jint, jint, jint, jint,
                jstring, jint, jint, jboolean, jboolean, jboolean, jboolean);
        static void SaveDocumentState(JNIEnv*, jobject, jint, jint, jint, jint);
        static void Draw(JNIEnv*, jobject, jobject);
        static void SendListBoxChoices(JNIEnv*, jobject, jbooleanArray, jint);
        static void SendListBoxChoice(JNIEnv* env, jobject obj, jint choice);
        static void ClearMatches(JNIEnv*, jobject);
        static jboolean Find(JNIEnv*, jobject, jstring, jboolean, jboolean);
        static jstring FindAddress(JNIEnv*, jobject, jstring);
        static jint FindAll(JNIEnv*, jobject, jstring);
        static void TouchUp(JNIEnv*, jobject, jint, jint, jint, jint, jint,
                jint, jint, jboolean, jboolean);
        static jstring RetrieveHref(JNIEnv*, jobject, jint, jint);
        static jstring RetrieveImageRef(JNIEnv*, jobject, jint, jint);
        static void SetFinalFocus(JNIEnv*, jobject, jint, jint, jint, jint,
                jboolean);
        static void SetKitFocus(JNIEnv*, jobject, jint, jint, jint, jint, jint,
                jint, jboolean);
        static void UnblockFocus(JNIEnv*, jobject);
        static void UpdateFrameCache(JNIEnv*, jobject);
        static jint GetContentMinPrefWidth(JNIEnv*, jobject);
        static void SetViewportSettingsFromNative(JNIEnv*, jobject);
        static void SetBackgroundColor(JNIEnv *env, jobject obj, jint color);
        static void SetSnapAnchor(JNIEnv*, jobject, jint, jint);
        static void SnapToAnchor(JNIEnv*, jobject);
        static jstring GetSelection(JNIEnv*, jobject, jobject);
        // end jni methods

        // these members are shared with webview.cpp
        int retrieveFrameGeneration(WebCore::Frame* );
        static Mutex gFrameCacheMutex;
        CachedRoot* mFrameCacheKit; // nav data being built by webcore
        SkPicture* mNavPictureKit;
        int mGeneration; // copy of the number bumped by WebViewNative
        int mMoveGeneration; // copy of state in WebViewNative triggered by move
        int mTouchGeneration; // copy of state in WebViewNative triggered by touch
        int mLastGeneration; // last action using up to date cache
        bool mUpdatedFrameCache;
        bool mUseReplay;
        static Mutex gRecomputeFocusMutex;
        WTF::Vector<int> mRecomputeEvents;
        // These two fields go together: we use the mutex to protect access to 
        // mButtons, so that we, and webview.cpp can look/modify the mButtons 
        // field safely from our respective threads
        static Mutex gButtonMutex;
        SkTDArray<Container*>* mButtons;
        // end of shared members
    private:
        friend class ListBoxReply;
        struct FrameGen {
            const WebCore::Frame* mFrame;
            int mGeneration;
        };
        WTF::Vector<FrameGen> mFrameGenerations;
        static Mutex gFrameGenerationMutex;
        struct JavaGlue;
        struct JavaGlue*       mJavaGlue;
        WebCore::FrameView*    mView;
        WebCore::FrameAndroid* mFrame;
        WebCoreReply*          mPopupReply;
        WebCore::Node* mLastFocused;
        WebCore::IntRect mLastFocusedBounds;
        // Used in passToJS to avoid updating the UI text field until after the
        // key event has been processed.
        bool mBlockTextfieldUpdates;
        // Passed in with key events to know when they were generated.  Store it
        // with the cache so that we can ignore stale text changes.
        int mTextGeneration;
        CachedRoot* mTemp;
        SkPicture* mTempPict;
        int mBuildGeneration;
        int mMaxXScroll;
        int mMaxYScroll;
        WebCore::IntRect mVisibleRect;
        WebCore::IntPoint mMousePos;
        bool mFrameCacheOutOfDate;
        bool mBlockFocusChange;
        int mLastPassed;
        int mLastVelocity;
        CachedHistory mHistory;
        WebCore::Node* mSnapAnchorNode;
        WebCore::Frame* changedKitFocus(WebCore::Frame* frame,
            WebCore::Node* node, int x, int y);
        bool commonKitFocus(int generation, int buildGeneration, 
            WebCore::Frame* frame, WebCore::Node* node, int x, int y,
            bool ignoreNullFocus);
        bool finalKitFocus(WebCore::Frame* frame, WebCore::Node* node, int x, int y);
        void doMaxScroll(WebCore::CacheBuilder::Direction dir);
        void sendMarkNodeInvalid(WebCore::Node* );
        void sendNotifyFocusSet();
        void sendNotifyProgressFinished();
        void sendRecomputeFocus();
        void sendViewInvalidate();
        bool handleMouseClick(WebCore::Frame* framePtr, WebCore::Node* nodePtr);
        bool prepareFrameCache();
        void releaseFrameCache(bool newCache);
#if DEBUG_NAV_UI
        uint32_t mNow;
#endif
    };

}   // namespace android

#endif // ANDROID_WIDGET_HTMLWIDGET_H