1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. 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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#ifndef UIScriptContext_h
29#define UIScriptContext_h
30
31#include <JavaScriptCore/JSRetainPtr.h>
32#include <wtf/HashMap.h>
33#include <wtf/RefPtr.h>
34#include <wtf/text/WTFString.h>
35
36namespace WebCore {
37class FloatRect;
38}
39
40namespace WTR {
41
42class UIScriptController;
43
44class UIScriptContextDelegate {
45public:
46 virtual void uiScriptDidComplete(const String& result, unsigned callbackID) = 0;
47};
48
49const unsigned firstNonPersistentCallbackID = 1000;
50
51typedef enum {
52 CallbackTypeInvalid = 0,
53 CallbackTypeWillBeginZooming,
54 CallbackTypeDidEndZooming,
55 CallbackTypeDidShowKeyboard,
56 CallbackTypeDidHideKeyboard,
57 CallbackTypeDidShowMenu,
58 CallbackTypeDidHideMenu,
59 CallbackTypeWillPresentPopover,
60 CallbackTypeDidDismissPopover,
61 CallbackTypeDidEndScrolling,
62 CallbackTypeDidStartFormControlInteraction,
63 CallbackTypeDidEndFormControlInteraction,
64 CallbackTypeDidShowForcePressPreview,
65 CallbackTypeDidDismissForcePressPreview,
66 CallbackTypeNonPersistent = firstNonPersistentCallbackID
67} CallbackType;
68
69class UIScriptContext {
70 WTF_MAKE_NONCOPYABLE(UIScriptContext);
71public:
72 UIScriptContext(UIScriptContextDelegate&);
73 ~UIScriptContext();
74
75 void runUIScript(const String& script, unsigned scriptCallbackID);
76 void requestUIScriptCompletion(JSStringRef);
77
78 // For one-shot tasks callbacks.
79 unsigned prepareForAsyncTask(JSValueRef taskCallback, CallbackType);
80 void asyncTaskComplete(unsigned taskCallbackID);
81
82 // For persistent callbacks.
83 unsigned registerCallback(JSValueRef taskCallback, CallbackType);
84 JSValueRef callbackWithID(unsigned callbackID);
85 void unregisterCallback(unsigned callbackID);
86 void fireCallback(unsigned callbackID);
87
88 unsigned nextTaskCallbackID(CallbackType);
89
90 JSObjectRef objectFromRect(const WebCore::FloatRect&) const;
91
92 JSGlobalContextRef jsContext() const { return m_context.get(); }
93
94private:
95 JSRetainPtr<JSGlobalContextRef> m_context;
96
97 bool hasOutstandingAsyncTasks() const { return !m_callbacks.isEmpty(); }
98 bool currentParentCallbackIsPendingCompletion() const { return m_uiScriptResultsPendingCompletion.contains(m_currentScriptCallbackID); }
99 bool currentParentCallbackHasOutstandingAsyncTasks() const;
100 void tryToCompleteUIScriptForCurrentParentCallback();
101
102 struct Task {
103 unsigned parentScriptCallbackID { 0 };
104 JSValueRef callback { nullptr };
105 };
106 HashMap<unsigned, Task> m_callbacks;
107 HashMap<unsigned, JSStringRef> m_uiScriptResultsPendingCompletion;
108
109 UIScriptContextDelegate& m_delegate;
110 RefPtr<UIScriptController> m_controller;
111
112 unsigned m_currentScriptCallbackID { 0 };
113 unsigned m_nextTaskCallbackID { 0 };
114};
115
116}
117
118#endif // UIScriptContext_h
119