Use a light scrollbar for transparent web views in dark mode.
[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/RenderView.h>
45 #include <WebCore/VisibleSelection.h>
46 #include <wtf/HashMap.h>
47 #include <wtf/NeverDestroyed.h>
48
49 #if PLATFORM(MAC)
50 #include <WebCore/LocalDefaultSystemAppearance.h>
51 #endif
52
53 namespace WebKit {
54 using namespace WebCore;
55
56 typedef HashMap<Range*, InjectedBundleRangeHandle*> DOMRangeHandleCache;
57
58 static DOMRangeHandleCache& domRangeHandleCache()
59 {
60     static NeverDestroyed<DOMRangeHandleCache> 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     DOMRangeHandleCache::AddResult result = domRangeHandleCache().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     domRangeHandleCache().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(frameView->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     OptionSet<PaintBehavior> oldPaintBehavior = frameView->paintBehavior();
154     OptionSet<PaintBehavior> paintBehavior = oldPaintBehavior;
155     paintBehavior.add({ PaintBehavior::SelectionOnly, PaintBehavior::FlattenCompositingLayers, PaintBehavior::Snapshotting });
156     if (options & SnapshotOptionsForceBlackText)
157         paintBehavior.add(PaintBehavior::ForceBlackText);
158     if (options & SnapshotOptionsForceWhiteText)
159         paintBehavior.add(PaintBehavior::ForceWhiteText);
160
161     frameView->setPaintBehavior(paintBehavior);
162     ownerDocument.updateLayout();
163
164     frameView->paint(*graphicsContext, paintRect);
165     frameView->setPaintBehavior(oldPaintBehavior);
166
167     frame->selection().setSelection(oldSelection);
168
169     return WebImage::create(backingStore.releaseNonNull());
170 }
171
172 String InjectedBundleRangeHandle::text() const
173 {
174     return m_range->text();
175 }
176
177 } // namespace WebKit