Home | History | Annotate | Download | only in WebCoreSupport
      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 "Settings.h"
     39 #include "WebCookieJar.h"
     40 #include "WebRequestContext.h"
     41 #include "WebViewCore.h"
     42 #include "npruntime.h"
     43 
     44 #include <surfaceflinger/SurfaceComposerClient.h>
     45 #include <ui/DisplayInfo.h>
     46 #include <ui/PixelFormat.h>
     47 #include <wtf/android/AndroidThreading.h>
     48 #include <wtf/MainThread.h>
     49 
     50 using namespace android;
     51 
     52 namespace WebCore {
     53 
     54 WTF::Vector<String> PlatformBridge::getSupportedKeyStrengthList()
     55 {
     56     KeyGeneratorClient* client = JavaSharedClient::GetKeyGeneratorClient();
     57     if (!client)
     58         return WTF::Vector<String>();
     59 
     60     return client->getSupportedKeyStrengthList();
     61 }
     62 
     63 String PlatformBridge::getSignedPublicKeyAndChallengeString(unsigned index, const String& challenge, const KURL& url)
     64 {
     65     KeyGeneratorClient* client = JavaSharedClient::GetKeyGeneratorClient();
     66     if (!client)
     67         return String();
     68 
     69     return client->getSignedPublicKeyAndChallengeString(index, challenge, url);
     70 }
     71 
     72 void PlatformBridge::setCookies(const Document* document, const KURL& url, const String& value)
     73 {
     74 #if USE(CHROME_NETWORK_STACK)
     75     std::string cookieValue(value.utf8().data());
     76     GURL cookieGurl(url.string().utf8().data());
     77     bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled();
     78     WebCookieJar::get(isPrivateBrowsing)->cookieStore()->SetCookie(cookieGurl, cookieValue);
     79 #else
     80     CookieClient* client = JavaSharedClient::GetCookieClient();
     81     if (!client)
     82         return;
     83 
     84     client->setCookies(url, value);
     85 #endif
     86 }
     87 
     88 String PlatformBridge::cookies(const Document* document, const KURL& url)
     89 {
     90 #if USE(CHROME_NETWORK_STACK)
     91     GURL cookieGurl(url.string().utf8().data());
     92     bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled();
     93     std::string cookies = WebCookieJar::get(isPrivateBrowsing)->cookieStore()->GetCookies(cookieGurl);
     94     String cookieString(cookies.c_str());
     95     return cookieString;
     96 #else
     97     CookieClient* client = JavaSharedClient::GetCookieClient();
     98     if (!client)
     99         return String();
    100 
    101     return client->cookies(url);
    102 #endif
    103 }
    104 
    105 bool PlatformBridge::cookiesEnabled(const Document* document)
    106 {
    107 #if USE(CHROME_NETWORK_STACK)
    108     bool isPrivateBrowsing = document->settings() && document->settings()->privateBrowsingEnabled();
    109     return WebCookieJar::get(isPrivateBrowsing)->allowCookies();
    110 #else
    111     CookieClient* client = JavaSharedClient::GetCookieClient();
    112     if (!client)
    113         return false;
    114 
    115     return client->cookiesEnabled();
    116 #endif
    117 }
    118 
    119 NPObject* PlatformBridge::pluginScriptableObject(Widget* widget)
    120 {
    121 #if USE(V8)
    122     if (!widget->isPluginView())
    123         return 0;
    124 
    125     PluginView* pluginView = static_cast<PluginView*>(widget);
    126     return pluginView->getNPObject();
    127 #else
    128     return 0;
    129 #endif
    130 }
    131 
    132 bool PlatformBridge::isWebViewPaused(const WebCore::FrameView* frameView)
    133 {
    134     android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
    135     return webViewCore->isPaused();
    136 }
    137 
    138 bool PlatformBridge::popupsAllowed(NPP)
    139 {
    140     return false;
    141 }
    142 
    143 String PlatformBridge::resolveFilePathForContentUri(const String& contentUri)
    144 {
    145     FileSystemClient* client = JavaSharedClient::GetFileSystemClient();
    146     return client->resolveFilePathForContentUri(contentUri);
    147 }
    148 
    149 int PlatformBridge::PlatformBridge::screenDepth()
    150 {
    151     android::DisplayInfo info;
    152     android::SurfaceComposerClient::getDisplayInfo(android::DisplayID(0), &info);
    153     return info.pixelFormatInfo.bitsPerPixel;
    154 }
    155 
    156 FloatRect PlatformBridge::screenRect()
    157 {
    158     android::DisplayInfo info;
    159     android::SurfaceComposerClient::getDisplayInfo(android::DisplayID(0), &info);
    160     return FloatRect(0.0, 0.0, info.w, info.h);
    161 }
    162 
    163 // The visible size on screen in document coordinate
    164 int PlatformBridge::screenWidthInDocCoord(const WebCore::FrameView* frameView)
    165 {
    166     android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
    167     return webViewCore->screenWidth();
    168 }
    169 
    170 int PlatformBridge::screenHeightInDocCoord(const WebCore::FrameView* frameView)
    171 {
    172     android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
    173     return webViewCore->screenHeight();
    174 }
    175 
    176 String PlatformBridge::computeDefaultLanguage()
    177 {
    178 #if USE(CHROME_NETWORK_STACK)
    179     String acceptLanguages = WebRequestContext::acceptLanguage();
    180     size_t length = acceptLanguages.find(',');
    181     if (length == std::string::npos)
    182         length = acceptLanguages.length();
    183     return acceptLanguages.substring(0, length);
    184 #else
    185     return "en";
    186 #endif
    187 }
    188 
    189 void PlatformBridge::updateViewport(FrameView* frameView)
    190 {
    191     android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
    192     webViewCore->updateViewport();
    193 }
    194 
    195 void PlatformBridge::updateTextfield(FrameView* frameView, Node* nodePtr, bool changeToPassword, const WTF::String& text)
    196 {
    197     android::WebViewCore* webViewCore = android::WebViewCore::getWebViewCore(frameView);
    198     webViewCore->updateTextfield(nodePtr, changeToPassword, text);
    199 }
    200 
    201 void PlatformBridge::setScrollPosition(ScrollView* scrollView, int x, int y) {
    202     // Check to make sure the view is the main FrameView.
    203     android::WebViewCore *webViewCore = android::WebViewCore::getWebViewCore(scrollView);
    204     if (webViewCore->mainFrame()->view() == scrollView)
    205         webViewCore->scrollTo(x, y);
    206 }
    207 
    208 int PlatformBridge::lowMemoryUsageMB()
    209 {
    210     return MemoryUsage::lowMemoryUsageMb();
    211 }
    212 
    213 int PlatformBridge::highMemoryUsageMB()
    214 {
    215     return MemoryUsage::highMemoryUsageMb();
    216 }
    217 
    218 int PlatformBridge::highUsageDeltaMB()
    219 {
    220     return MemoryUsage::highUsageDeltaMb();
    221 }
    222 
    223 int PlatformBridge::memoryUsageMB()
    224 {
    225     return MemoryUsage::memoryUsageMb(false);
    226 }
    227 
    228 int PlatformBridge::actualMemoryUsageMB()
    229 {
    230     return MemoryUsage::memoryUsageMb(true);
    231 }
    232 
    233 bool PlatformBridge::canSatisfyMemoryAllocation(long bytes)
    234 {
    235     JNIEnv* env = JSC::Bindings::getJNIEnv();
    236     jclass bridgeClass = env->FindClass("android/webkit/JniUtil");
    237     jmethodID method = env->GetStaticMethodID(bridgeClass, "canSatisfyMemoryAllocation", "(J)Z");
    238     jboolean canAllocate = env->CallStaticBooleanMethod(bridgeClass, method, static_cast<jlong>(bytes));
    239     env->DeleteLocalRef(bridgeClass);
    240 
    241     return canAllocate == JNI_TRUE;
    242 }
    243 
    244 }  // namespace WebCore
    245 
    246 
    247 // This is the implementation of AndroidThreading, which is declared in
    248 // JavaScriptCore/wtf/android/AndroidThreading.h. It is provided here, rather
    249 // than in its own source file, to avoid linker problems.
    250 //
    251 // By default, when building a shared library, the linker strips from static
    252 // libraries any compilation units which do not contain any code referenced from
    253 // that static library. Since
    254 // AndroidThreading::scheduleDispatchFunctionsOnMainThread is not referenced
    255 // from libwebcore.a, implementing it in its own compilation unit results in it
    256 // being stripped. This stripping can be avoided by using the linker option
    257 // --whole-archive for libwebcore.a, but this adds considerably to the size of
    258 // libwebcore.so.
    259 
    260 namespace WTF {
    261 
    262 // Callback in the main thread.
    263 static void timeoutFired(void*)
    264 {
    265     dispatchFunctionsFromMainThread();
    266 }
    267 
    268 void AndroidThreading::scheduleDispatchFunctionsOnMainThread()
    269 {
    270     JavaSharedClient::EnqueueFunctionPtr(timeoutFired, 0);
    271 }
    272 
    273 }  // namespace WTF
    274