1 /*
2 * Copyright 2009, The Android Open Source Project
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 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * 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 THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include <PlatformBridge.h>
28
29 #include "CookieClient.h"
30 #include "Document.h"
31 #include "FileSystemClient.h"
32 #include "FrameView.h"
33 #include "JNIUtility.h"
34 #include "JavaSharedClient.h"
35 #include "KeyGeneratorClient.h"
36 #include "MemoryUsage.h"
37 #include "PluginView.h"
38 #include "RenderLayer.h"
39 #include "RenderView.h"
40 #include "Settings.h"
41 #include "WebCookieJar.h"
42 #include "WebRequestContext.h"
43 #include "WebViewCore.h"
44 #include "npruntime.h"
45
46 #include <gui/ISurfaceComposer.h>
47 #include <gui/SurfaceComposerClient.h>
48 #include <ui/DisplayInfo.h>
49 #include <ui/PixelFormat.h>
50 #include <wtf/android/AndroidThreading.h>
51 #include <wtf/MainThread.h>
52
53 #include <algorithm>
54
55 using namespace android;
56
57 namespace WebCore {
58
getSupportedKeyStrengthList()59 WTF::Vector<String> PlatformBridge::getSupportedKeyStrengthList()
60 {
61 KeyGeneratorClient* client = JavaSharedClient::GetKeyGeneratorClient();
62 if (!client)
63 return WTF::Vector<String>();
64
65 return client->getSupportedKeyStrengthList();
66 }
67
getSignedPublicKeyAndChallengeString(unsigned index,const String & challenge,const KURL & url)68 String PlatformBridge::getSignedPublicKeyAndChallengeString(unsigned index, const String& challenge, const KURL& url)
69 {
70 KeyGeneratorClient* client = JavaSharedClient::GetKeyGeneratorClient();
71 if (!client)
72 return String();
73
74 return client->getSignedPublicKeyAndChallengeString(index, challenge, url);
75 }
76
setCookies(const Document * document,const KURL & url,const String & value)77 void PlatformBridge::setCookies(const Document* document, const KURL& url, const String& value)
78 {
79 std::string cookieValue(value.utf8().data());
80 GURL cookieGurl(url.string().utf8().data());
81 bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled();
82 WebCookieJar* cookieJar = WebCookieJar::get(isPrivateBrowsing);
83 if (cookieJar->allowCookies())
84 cookieJar->cookieStore()->SetCookie(cookieGurl, cookieValue);
85 }
86
cookies(const Document * document,const KURL & url)87 String PlatformBridge::cookies(const Document* document, const KURL& url)
88 {
89 GURL cookieGurl(url.string().utf8().data());
90 bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled();
91 WebCookieJar* cookieJar = WebCookieJar::get(isPrivateBrowsing);
92 String cookieString;
93 if (cookieJar->allowCookies()) {
94 std::string cookies = cookieJar->cookieStore()->GetCookies(cookieGurl);
95 cookieString = cookies.c_str();
96 }
97 return cookieString;
98 }
99
cookiesEnabled(const Document * document)100 bool PlatformBridge::cookiesEnabled(const Document* document)
101 {
102 bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled();
103 return WebCookieJar::get(isPrivateBrowsing)->allowCookies();
104 }
105
pluginScriptableObject(Widget * widget)106 NPObject* PlatformBridge::pluginScriptableObject(Widget* widget)
107 {
108 if (!widget->isPluginView())
109 return 0;
110
111 PluginView* pluginView = static_cast<PluginView*>(widget);
112 return pluginView->getNPObject();
113 }
114
popupsAllowed(NPP)115 bool PlatformBridge::popupsAllowed(NPP)
116 {
117 return false;
118 }
119
resolveFilePathForContentUri(const String & contentUri)120 String PlatformBridge::resolveFilePathForContentUri(const String& contentUri)
121 {
122 FileSystemClient* client = JavaSharedClient::GetFileSystemClient();
123 return client->resolveFilePathForContentUri(contentUri);
124 }
125
screenDepth()126 int PlatformBridge::PlatformBridge::screenDepth()
127 {
128 android::sp<android::IBinder> display(
129 android::SurfaceComposerClient::getBuiltInDisplay(
130 android::ISurfaceComposer::eDisplayIdMain));
131 android::DisplayInfo info;
132 android::SurfaceComposerClient::getDisplayInfo(display, &info);
133 return info.pixelFormatInfo.bitsPerPixel;
134 }
135
screenRect()136 FloatRect PlatformBridge::screenRect()
137 {
138 android::sp<android::IBinder> display(
139 android::SurfaceComposerClient::getBuiltInDisplay(
140 android::ISurfaceComposer::eDisplayIdMain));
141 android::DisplayInfo info;
142 android::SurfaceComposerClient::getDisplayInfo(display, &info);
143 return FloatRect(0.0, 0.0, info.w, info.h);
144 }
145
146 // The visible size on screen in document coordinate
screenWidthInDocCoord(const WebCore::FrameView * frameView)147 int PlatformBridge::screenWidthInDocCoord(const WebCore::FrameView* frameView)
148 {
149 android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
150 return webViewCore->screenWidth();
151 }
152
screenHeightInDocCoord(const WebCore::FrameView * frameView)153 int PlatformBridge::screenHeightInDocCoord(const WebCore::FrameView* frameView)
154 {
155 android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
156 return webViewCore->screenHeight();
157 }
158
computeDefaultLanguage()159 String PlatformBridge::computeDefaultLanguage()
160 {
161 String acceptLanguages = WebRequestContext::acceptLanguage();
162 size_t length = acceptLanguages.find(',');
163 if (length == std::string::npos)
164 length = acceptLanguages.length();
165 return acceptLanguages.substring(0, length);
166 }
167
updateViewport(FrameView * frameView)168 void PlatformBridge::updateViewport(FrameView* frameView)
169 {
170 android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
171 webViewCore->updateViewport();
172 }
173
updateTextfield(FrameView * frameView,Node * nodePtr,const WTF::String & text)174 void PlatformBridge::updateTextfield(FrameView* frameView, Node* nodePtr, const WTF::String& text)
175 {
176 android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
177 webViewCore->updateTextfield(nodePtr, text);
178 }
179
setScrollPosition(ScrollView * scrollView,int x,int y)180 void PlatformBridge::setScrollPosition(ScrollView* scrollView, int x, int y) {
181 FrameView* frameView = scrollView->frameView();
182 if (!frameView) return;
183 // Check to make sure the view is the main FrameView.
184 android::WebViewCore *webViewCore = android::WebViewCore::getWebViewCore(scrollView);
185 if (webViewCore->mainFrame()->view() == scrollView) {
186 x = std::max(0, std::min(frameView->contentsWidth(), x));
187 y = std::max(0, std::min(frameView->contentsHeight(), y));
188 webViewCore->scrollTo(x, y);
189 }
190 }
191
lowMemoryUsageMB()192 int PlatformBridge::lowMemoryUsageMB()
193 {
194 return MemoryUsage::lowMemoryUsageMb();
195 }
196
highMemoryUsageMB()197 int PlatformBridge::highMemoryUsageMB()
198 {
199 return MemoryUsage::highMemoryUsageMb();
200 }
201
highUsageDeltaMB()202 int PlatformBridge::highUsageDeltaMB()
203 {
204 return MemoryUsage::highUsageDeltaMb();
205 }
206
memoryUsageMB()207 int PlatformBridge::memoryUsageMB()
208 {
209 return MemoryUsage::memoryUsageMb(false);
210 }
211
actualMemoryUsageMB()212 int PlatformBridge::actualMemoryUsageMB()
213 {
214 return MemoryUsage::memoryUsageMb(true);
215 }
216
canSatisfyMemoryAllocation(long bytes)217 bool PlatformBridge::canSatisfyMemoryAllocation(long bytes)
218 {
219 JNIEnv* env = JSC::Bindings::getJNIEnv();
220 jclass bridgeClass = env->FindClass("android/webkit/JniUtil");
221 jmethodID method = env->GetStaticMethodID(bridgeClass, "canSatisfyMemoryAllocation", "(J)Z");
222 jboolean canAllocate = env->CallStaticBooleanMethod(bridgeClass, method, static_cast<jlong>(bytes));
223 env->DeleteLocalRef(bridgeClass);
224
225 return canAllocate == JNI_TRUE;
226 }
227
228 } // namespace WebCore
229
230
231 // This is the implementation of AndroidThreading, which is declared in
232 // JavaScriptCore/wtf/android/AndroidThreading.h. It is provided here, rather
233 // than in its own source file, to avoid linker problems.
234 //
235 // By default, when building a shared library, the linker strips from static
236 // libraries any compilation units which do not contain any code referenced from
237 // that static library. Since
238 // AndroidThreading::scheduleDispatchFunctionsOnMainThread is not referenced
239 // from libwebcore.a, implementing it in its own compilation unit results in it
240 // being stripped. This stripping can be avoided by using the linker option
241 // --whole-archive for libwebcore.a, but this adds considerably to the size of
242 // libwebcore.so.
243
244 namespace WTF {
245
246 // Callback in the main thread.
timeoutFired(void *)247 static void timeoutFired(void*)
248 {
249 dispatchFunctionsFromMainThread();
250 }
251
scheduleDispatchFunctionsOnMainThread()252 void AndroidThreading::scheduleDispatchFunctionsOnMainThread()
253 {
254 JavaSharedClient::EnqueueFunctionPtr(timeoutFired, 0);
255 }
256
257 } // namespace WTF
258