1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef Internals_h
28#define Internals_h
29
30#include "bindings/core/v8/ExceptionStatePlaceholder.h"
31#include "bindings/core/v8/ScriptPromise.h"
32#include "bindings/core/v8/ScriptValue.h"
33#include "bindings/core/v8/ScriptWrappable.h"
34#include "core/css/CSSComputedStyleDeclaration.h"
35#include "core/dom/ContextLifecycleObserver.h"
36#include "core/page/scrolling/ScrollingCoordinator.h"
37#include "platform/heap/Handle.h"
38#include "wtf/ArrayBuffer.h"
39#include "wtf/PassRefPtr.h"
40#include "wtf/RefCounted.h"
41#include "wtf/text/WTFString.h"
42
43namespace blink {
44
45class CanvasRenderingContext2D;
46class ClientRect;
47class ClientRectList;
48class DOMPoint;
49class DOMStringList;
50class LocalDOMWindow;
51class DictionaryTest;
52class Document;
53class DocumentMarker;
54class Element;
55class ExceptionState;
56class ExecutionContext;
57class GCObservation;
58class HTMLElement;
59class HTMLMediaElement;
60class InternalProfilers;
61class InternalRuntimeFlags;
62class InternalSettings;
63class Iterator;
64class LayerRectList;
65class LocalFrame;
66class Node;
67class Page;
68class PagePopupController;
69class PrivateScriptTest;
70class Range;
71class SerializedScriptValue;
72class ShadowRoot;
73template <typename NodeType> class StaticNodeTypeList;
74typedef StaticNodeTypeList<Node> StaticNodeList;
75class TypeConversions;
76
77class Internals FINAL : public GarbageCollectedFinalized<Internals>, public ScriptWrappable, public ContextLifecycleObserver {
78    DEFINE_WRAPPERTYPEINFO();
79public:
80    static Internals* create(Document*);
81    virtual ~Internals();
82
83    static void resetToConsistentState(Page*);
84
85    String elementRenderTreeAsText(Element*, ExceptionState&);
86
87    String address(Node*);
88
89    GCObservation* observeGC(ScriptValue);
90
91    bool isPreloaded(const String& url);
92    bool isLoadingFromMemoryCache(const String& url);
93
94    bool isSharingStyle(Element*, Element*) const;
95
96    PassRefPtrWillBeRawPtr<CSSStyleDeclaration> computedStyleIncludingVisitedInfo(Node*) const;
97
98    ShadowRoot* shadowRoot(Element* host);
99    ShadowRoot* youngestShadowRoot(Element* host);
100    ShadowRoot* oldestShadowRoot(Element* host);
101    ShadowRoot* youngerShadowRoot(Node* shadow, ExceptionState&);
102    String shadowRootType(const Node*, ExceptionState&) const;
103    bool hasShadowInsertionPoint(const Node*, ExceptionState&) const;
104    bool hasContentElement(const Node*, ExceptionState&) const;
105    size_t countElementShadow(const Node*, ExceptionState&) const;
106    const AtomicString& shadowPseudoId(Element*);
107
108    // CSS Animation / Transition testing.
109    void pauseAnimations(double pauseTime, ExceptionState&);
110
111    bool isValidContentSelect(Element* insertionPoint, ExceptionState&);
112    Node* treeScopeRootNode(Node*);
113    Node* parentTreeScope(Node*);
114    bool hasSelectorForIdInShadow(Element* host, const AtomicString& idValue, ExceptionState&);
115    bool hasSelectorForClassInShadow(Element* host, const AtomicString& className, ExceptionState&);
116    bool hasSelectorForAttributeInShadow(Element* host, const AtomicString& attributeName, ExceptionState&);
117    bool hasSelectorForPseudoClassInShadow(Element* host, const String& pseudoClass, ExceptionState&);
118    unsigned short compareTreeScopePosition(const Node*, const Node*, ExceptionState&) const;
119
120    // FIXME: Rename these functions if walker is preferred.
121    Node* nextSiblingByWalker(Node*);
122    Node* firstChildByWalker(Node*);
123    Node* lastChildByWalker(Node*);
124    Node* nextNodeByWalker(Node*);
125    Node* previousNodeByWalker(Node*);
126
127    unsigned updateStyleAndReturnAffectedElementCount(ExceptionState&) const;
128    unsigned needsLayoutCount(ExceptionState&) const;
129    unsigned hitTestCount(Document*, ExceptionState&) const;
130
131    String visiblePlaceholder(Element*);
132    void selectColorInColorChooser(Element*, const String& colorValue);
133    void endColorChooser(Element*);
134    bool hasAutofocusRequest(Document*);
135    bool hasAutofocusRequest();
136    Vector<String> formControlStateOfHistoryItem(ExceptionState&);
137    void setFormControlStateOfHistoryItem(const Vector<String>&, ExceptionState&);
138    void setEnableMockPagePopup(bool, ExceptionState&);
139    PassRefPtrWillBeRawPtr<PagePopupController> pagePopupController();
140
141    PassRefPtrWillBeRawPtr<ClientRect> absoluteCaretBounds(ExceptionState&);
142
143    PassRefPtrWillBeRawPtr<ClientRect> boundingBox(Element*);
144
145    unsigned markerCountForNode(Node*, const String&, ExceptionState&);
146    unsigned activeMarkerCountForNode(Node*);
147    PassRefPtrWillBeRawPtr<Range> markerRangeForNode(Node*, const String& markerType, unsigned index, ExceptionState&);
148    String markerDescriptionForNode(Node*, const String& markerType, unsigned index, ExceptionState&);
149    void addTextMatchMarker(const Range*, bool isActive);
150    void setMarkersActive(Node*, unsigned startOffset, unsigned endOffset, bool);
151    void setMarkedTextMatchesAreHighlighted(Document*, bool);
152
153    void setScrollViewPosition(Document*, long x, long y, ExceptionState&);
154    String viewportAsText(Document*, float devicePixelRatio, int availableWidth, int availableHeight, ExceptionState&);
155
156    bool wasLastChangeUserEdit(Element* textField, ExceptionState&);
157    bool elementShouldAutoComplete(Element* inputElement, ExceptionState&);
158    String suggestedValue(Element*, ExceptionState&);
159    void setSuggestedValue(Element*, const String&, ExceptionState&);
160    void setEditingValue(Element* inputElement, const String&, ExceptionState&);
161    void setAutofilled(Element*, bool enabled, ExceptionState&);
162    void scrollElementToRect(Element*, long x, long y, long w, long h, ExceptionState&);
163
164    PassRefPtrWillBeRawPtr<Range> rangeFromLocationAndLength(Element* scope, int rangeLocation, int rangeLength);
165    unsigned locationFromRange(Element* scope, const Range*);
166    unsigned lengthFromRange(Element* scope, const Range*);
167    String rangeAsText(const Range*);
168
169    DOMPoint* touchPositionAdjustedToBestClickableNode(long x, long y, long width, long height, Document*, ExceptionState&);
170    Node* touchNodeAdjustedToBestClickableNode(long x, long y, long width, long height, Document*, ExceptionState&);
171    DOMPoint* touchPositionAdjustedToBestContextMenuNode(long x, long y, long width, long height, Document*, ExceptionState&);
172    Node* touchNodeAdjustedToBestContextMenuNode(long x, long y, long width, long height, Document*, ExceptionState&);
173    PassRefPtrWillBeRawPtr<ClientRect> bestZoomableAreaForTouchPoint(long x, long y, long width, long height, Document*, ExceptionState&);
174
175    int lastSpellCheckRequestSequence(Document*, ExceptionState&);
176    int lastSpellCheckProcessedSequence(Document*, ExceptionState&);
177
178    Vector<AtomicString> userPreferredLanguages() const;
179    void setUserPreferredLanguages(const Vector<String>&);
180
181    unsigned activeDOMObjectCount(Document*);
182    unsigned wheelEventHandlerCount(Document*);
183    unsigned scrollEventHandlerCount(Document*);
184    unsigned touchEventHandlerCount(Document*);
185    LayerRectList* touchEventTargetLayerRects(Document*, ExceptionState&);
186
187    // This is used to test rect based hit testing like what's done on touch screens.
188    PassRefPtrWillBeRawPtr<StaticNodeList> nodesFromRect(Document*, int x, int y, unsigned topPadding, unsigned rightPadding,
189        unsigned bottomPadding, unsigned leftPadding, bool ignoreClipping, bool allowChildFrameContent, ExceptionState&) const;
190
191    bool hasSpellingMarker(Document*, int from, int length);
192    bool hasGrammarMarker(Document*, int from, int length);
193    void setContinuousSpellCheckingEnabled(bool);
194
195    bool isOverwriteModeEnabled(Document*);
196    void toggleOverwriteModeEnabled(Document*);
197
198    unsigned numberOfScrollableAreas(Document*);
199
200    bool isPageBoxVisible(Document*, int pageNumber);
201
202    static const char* internalsId;
203
204    InternalSettings* settings() const;
205    InternalRuntimeFlags* runtimeFlags() const;
206    InternalProfilers* profilers();
207    unsigned workerThreadCount() const;
208
209    void setDeviceProximity(Document*, const String& eventType, double value, double min, double max, ExceptionState&);
210
211    String layerTreeAsText(Document*, unsigned flags, ExceptionState&) const;
212    String layerTreeAsText(Document*, ExceptionState&) const;
213    String elementLayerTreeAsText(Element*, unsigned flags, ExceptionState&) const;
214    String elementLayerTreeAsText(Element*, ExceptionState&) const;
215
216    bool scrollsWithRespectTo(Element*, Element*, ExceptionState&);
217    bool isUnclippedDescendant(Element*, ExceptionState&);
218
219    String scrollingStateTreeAsText(Document*) const;
220    String mainThreadScrollingReasons(Document*, ExceptionState&) const;
221    PassRefPtrWillBeRawPtr<ClientRectList> nonFastScrollableRects(Document*, ExceptionState&) const;
222
223    void garbageCollectDocumentResources(Document*) const;
224    void evictAllResources() const;
225
226    unsigned numberOfLiveNodes() const;
227    unsigned numberOfLiveDocuments() const;
228    String dumpRefCountedInstanceCounts() const;
229    Vector<String> consoleMessageArgumentCounts(Document*) const;
230    PassRefPtrWillBeRawPtr<LocalDOMWindow> openDummyInspectorFrontend(const String& url);
231    void closeDummyInspectorFrontend();
232    Vector<unsigned long> setMemoryCacheCapacities(unsigned long minDeadBytes, unsigned long maxDeadBytes, unsigned long totalBytes);
233    void setInspectorResourcesDataSizeLimits(int maximumResourcesContentSize, int maximumSingleResourceContentSize, ExceptionState&);
234
235    String counterValue(Element*);
236
237    int pageNumber(Element*, float pageWidth = 800, float pageHeight = 600);
238    Vector<String> shortcutIconURLs(Document*) const;
239    Vector<String> allIconURLs(Document*) const;
240
241    int numberOfPages(float pageWidthInPixels = 800, float pageHeightInPixels = 600);
242    String pageProperty(String, int, ExceptionState& = ASSERT_NO_EXCEPTION) const;
243    String pageSizeAndMarginsInPixels(int, int, int, int, int, int, int, ExceptionState& = ASSERT_NO_EXCEPTION) const;
244
245    void setDeviceScaleFactor(float scaleFactor, ExceptionState&);
246
247    void setIsCursorVisible(Document*, bool, ExceptionState&);
248
249    void mediaPlayerRequestFullscreen(HTMLMediaElement*);
250    double effectiveMediaVolume(HTMLMediaElement*);
251
252    void mediaPlayerRemoteRouteAvailabilityChanged(HTMLMediaElement*, bool);
253    void mediaPlayerPlayingRemotelyChanged(HTMLMediaElement*, bool);
254
255    void registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme);
256    void removeURLSchemeRegisteredAsBypassingContentSecurityPolicy(const String& scheme);
257
258    TypeConversions* typeConversions() const;
259    PrivateScriptTest* privateScriptTest() const;
260    DictionaryTest* dictionaryTest() const;
261
262    Vector<String> getReferencedFilePaths() const;
263
264    void startTrackingRepaints(Document*, ExceptionState&);
265    void stopTrackingRepaints(Document*, ExceptionState&);
266    void updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks(ExceptionState&);
267    void updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks(Node*, ExceptionState&);
268    void forceFullRepaint(Document*, ExceptionState&);
269
270    PassRefPtrWillBeRawPtr<ClientRectList> draggableRegions(Document*, ExceptionState&);
271    PassRefPtrWillBeRawPtr<ClientRectList> nonDraggableRegions(Document*, ExceptionState&);
272
273    PassRefPtr<ArrayBuffer> serializeObject(PassRefPtr<SerializedScriptValue>) const;
274    PassRefPtr<SerializedScriptValue> deserializeBuffer(PassRefPtr<ArrayBuffer>) const;
275
276    String getCurrentCursorInfo(Document*, ExceptionState&);
277
278    String markerTextForListItem(Element*);
279
280    void forceReload(bool endToEnd);
281
282    String getImageSourceURL(Element*);
283
284    bool isSelectPopupVisible(Node*);
285    bool selectPopupItemStyleIsRtl(Node*, int);
286    int selectPopupItemStyleFontHeight(Node*, int);
287
288    PassRefPtrWillBeRawPtr<ClientRect> selectionBounds(ExceptionState&);
289
290    bool loseSharedGraphicsContext3D();
291
292    void forceCompositingUpdate(Document*, ExceptionState&);
293
294    void setZoomFactor(float);
295
296    void setShouldRevealPassword(Element*, bool, ExceptionState&);
297
298    ScriptPromise createResolvedPromise(ScriptState*, ScriptValue);
299    ScriptPromise createRejectedPromise(ScriptState*, ScriptValue);
300    ScriptPromise addOneToPromise(ScriptState*, ScriptPromise);
301    ScriptPromise promiseCheck(ScriptState*, long, bool, const Dictionary&, const String&, const Vector<String>&, ExceptionState&);
302    ScriptPromise promiseCheckWithoutExceptionState(ScriptState*, const Dictionary&, const String&, const Vector<String>&);
303    ScriptPromise promiseCheckRange(ScriptState*, long);
304
305    void trace(Visitor*);
306
307    void setValueForUser(Element*, const String&);
308
309    String textSurroundingNode(Node*, int x, int y, unsigned long maxLength);
310
311    void setFocused(bool);
312
313    bool ignoreLayoutWithPendingStylesheets(Document*);
314
315    void setNetworkStateNotifierTestOnly(bool);
316    // Test must call setNetworkStateNotifierTestOnly(true) before calling setNetworkConnectionInfo.
317    void setNetworkConnectionInfo(const String&, ExceptionState&);
318    String serializeNavigationMarkup();
319    void hideAllTransitionElements();
320
321    unsigned countHitRegions(CanvasRenderingContext2D*);
322
323    void forcePluginPlaceholder(HTMLElement* plugin, const String& htmlSource, ExceptionState&);
324    void forcePluginPlaceholder(HTMLElement* plugin, const Dictionary& options, ExceptionState&);
325
326    Iterator* iterator(ScriptState*, ExceptionState&);
327
328private:
329    explicit Internals(Document*);
330    Document* contextDocument() const;
331    LocalFrame* frame() const;
332    Vector<String> iconURLs(Document*, int iconTypesMask) const;
333    PassRefPtrWillBeRawPtr<ClientRectList> annotatedRegions(Document*, bool draggable, ExceptionState&);
334
335    DocumentMarker* markerAt(Node*, const String& markerType, unsigned index, ExceptionState&);
336    Member<InternalRuntimeFlags> m_runtimeFlags;
337    Member<InternalProfilers> m_profilers;
338};
339
340} // namespace blink
341
342#endif // Internals_h
343