88b7d543191044b2e320ca95362c5c09294a8183
[WebKit-https.git] / Source / WebKit / WebProcess / InjectedBundle / DOM / InjectedBundleRangeHandle.cpp
1 /*
2  * Copyright (C) 2010, 2015-2016 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 #include "config.h"
27 #include "InjectedBundleRangeHandle.h"
28
29 #include "InjectedBundleNodeHandle.h"
30 #include "ShareableBitmap.h"
31 #include "WebImage.h"
32 #include <JavaScriptCore/APICast.h>
33 #include <JavaScriptCore/HeapInlines.h>
34 #include <WebCore/Document.h>
35 #include <WebCore/FloatRect.h>
36 #include <WebCore/Frame.h>
37 #include <WebCore/FrameSelection.h>
38 #include <WebCore/FrameView.h>
39 #include <WebCore/GraphicsContext.h>
40 #include <WebCore/IntRect.h>
41 #include <WebCore/JSRange.h>
42 #include <WebCore/Page.h>
43 #include <WebCore/Range.h>
44 #include <WebCore/VisibleSelection.h>
45 #include <wtf/HashMap.h>
46 #include <wtf/NeverDestroyed.h>
47
48 #if PLATFORM(MAC)
49 #include <WebCore/LocalDefaultSystemAppearance.h>
50 #endif
51
52 using namespace WebCore;
53
54 namespace WebKit {
55
56 typedef HashMap<Range*, InjectedBundleRangeHandle*> DOMHandleCache;
57
58 static DOMHandleCache& domHandleCache()
59 {
60     static NeverDestroyed<DOMHandleCache> cache;
61     return cache;
62 }
63
64 RefPtr<InjectedBundleRangeHandle> InjectedBundleRangeHandle::getOrCreate(JSContextRef context, JSObjectRef object)
65 {
66     Range* range = JSRange::toWrapped(toJS(context)->vm(), toJS(object));
67     return getOrCreate(range);
68 }
69
70 RefPtr<InjectedBundleRangeHandle> InjectedBundleRangeHandle::getOrCreate(Range* range)
71 {
72     if (!range)
73         return nullptr;
74
75     DOMHandleCache::AddResult result = domHandleCache().add(range, nullptr);
76     if (!result.isNewEntry)
77         return result.iterator->value;
78
79     auto rangeHandle = InjectedBundleRangeHandle::create(*range);
80     result.iterator->value = rangeHandle.ptr();
81     return WTFMove(rangeHandle);
82 }
83
84 Ref<InjectedBundleRangeHandle> InjectedBundleRangeHandle::create(Range& range)
85 {
86     return adoptRef(*new InjectedBundleRangeHandle(range));
87 }
88
89 InjectedBundleRangeHandle::InjectedBundleRangeHandle(Range& range)
90     : m_range(range)
91 {
92 }
93
94 InjectedBundleRangeHandle::~InjectedBundleRangeHandle()
95 {
96     domHandleCache().remove(m_range.ptr());
97 }
98
99 Range& InjectedBundleRangeHandle::coreRange() const
100 {
101     return m_range.get();
102 }
103
104 Ref<InjectedBundleNodeHandle> InjectedBundleRangeHandle::document()
105 {
106     return InjectedBundleNodeHandle::getOrCreate(m_range->ownerDocument());
107 }
108
109 WebCore::IntRect InjectedBundleRangeHandle::boundingRectInWindowCoordinates() const
110 {
111     FloatRect boundingRect = m_range->absoluteBoundingRect();
112     Frame* frame = m_range->ownerDocument().frame();
113     return frame->view()->contentsToWindow(enclosingIntRect(boundingRect));
114 }
115
116 RefPtr<WebImage> InjectedBundleRangeHandle::renderedImage(SnapshotOptions options)
117 {
118     Document& ownerDocument = m_range->ownerDocument();
119     Frame* frame = ownerDocument.frame();
120     if (!frame)
121         return nullptr;
122
123     FrameView* frameView = frame->view();
124     if (!frameView)
125         return nullptr;
126
127 #if PLATFORM(MAC)
128     LocalDefaultSystemAppearance localAppearance(frame->page()->useSystemAppearance(), frame->page()->useDarkAppearance());
129 #endif
130
131     Ref<Frame> protector(*frame);
132
133     VisibleSelection oldSelection = frame->selection().selection();
134     frame->selection().setSelection(VisibleSelection(m_range));
135
136     float scaleFactor = (options & SnapshotOptionsExcludeDeviceScaleFactor) ? 1 : frame->page()->deviceScaleFactor();
137     IntRect paintRect = enclosingIntRect(m_range->absoluteBoundingRect());
138     IntSize backingStoreSize = paintRect.size();
139     backingStoreSize.scale(scaleFactor);
140
141     RefPtr<ShareableBitmap> backingStore = ShareableBitmap::createShareable(backingStoreSize, { });
142     if (!backingStore)
143         return nullptr;
144
145     auto graphicsContext = backingStore->createGraphicsContext();
146     graphicsContext->scale(scaleFactor);
147
148     paintRect.move(frameView->frameRect().x(), frameView->frameRect().y());
149     paintRect.moveBy(-frameView->scrollPosition());
150
151     graphicsContext->translate(-paintRect.location());
152
153     PaintBehavior oldPaintBehavior = frameView->paintBehavior();
154     PaintBehavior paintBehavior = oldPaintBehavior | PaintBehaviorSelectionOnly | PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting;
155     if (options & SnapshotOptionsForceBlackText)
156         paintBehavior |= PaintBehaviorForceBlackText;
157     if (options & SnapshotOptionsForceWhiteText)
158         paintBehavior |= PaintBehaviorForceWhiteText;
159
160     frameView->setPaintBehavior(paintBehavior);
161     ownerDocument.updateLayout();
162
163     frameView->paint(*graphicsContext, paintRect);
164     frameView->setPaintBehavior(oldPaintBehavior);
165
166     frame->selection().setSelection(oldSelection);
167
168     return WebImage::create(backingStore.releaseNonNull());
169 }
170
171 String InjectedBundleRangeHandle::text() const
172 {
173     return m_range->text();
174 }
175
176 } // namespace WebKit