首页 > 代码库 > WebView setBlockNetworkImage来龙去脉

WebView setBlockNetworkImage来龙去脉

本身含义阻止图片网络数据
webSettings.setBlockNetworkImage(true);
解除数据阻止
webSettings.setBlockNetworkImage(false);


实际上简单的一句话为什么会造成这么强大的功力
我们来看下具体的实现
WebSettingsClassic.java
    /**
     * @see android.webkit.WebSettings#setBlockNetworkImage(boolean)
     */
    @Override
    public synchronized void setBlockNetworkImage(boolean flag) {
        if (mBlockNetworkImage != flag) {
            mBlockNetworkImage = flag;
            postSync();
        }
    }
关键代码看上去只有
mBlockNetworkImage = flag;
但是不要忽略了
postSync();
这个就是通知c++层去读取mBlockNetworkImage数值
我们看下具体的postSync函数实现
    /* Post a SYNC message to handle syncing the native settings. */
    private synchronized void postSync() {
        // Only post if a sync is not pending
        if (!mSyncPending) {
            mSyncPending = mEventHandler.sendMessage(
                    Message.obtain(null, EventHandler.SYNC));
        }
    }


通知给
            // create a new handler
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case SYNC:
                            synchronized (WebSettingsClassic.this) {
                                if (mBrowserFrame.mNativeFrame != 0) {
                                    nativeSync(mBrowserFrame.mNativeFrame);
                                }
                                mSyncPending = false;
                            }
                            break;


    // Synchronize the native and java settings.
    private native void nativeSync(int nativeFrame);
最后调用的函数


c++底层函数实现
WebKit/android/jni/WebSettings.cpp
//-------------------------------------------------------------
// JNI registration
//-------------------------------------------------------------
static JNINativeMethod gWebSettingsMethods[] = {
    { "nativeSync", "(I)V",
        (void*) WebSettings::Sync }
};
    static void Sync(JNIEnv* env, jobject obj, jint frame)
    {
        WebCore::Frame* pFrame = (WebCore::Frame*)frame;
        ALOG_ASSERT(pFrame, "%s must take a valid frame pointer!", __FUNCTION__);
        WebCore::Settings* s = pFrame->settings();
        if (!s)
            return;
        WebCore::CachedResourceLoader* cachedResourceLoader = pFrame->document()->cachedResourceLoader();


        flag = env->GetBooleanField(obj, gFieldIds->mBlockNetworkImage);
        s->setBlockNetworkImage(flag);
        if(!flag)
            cachedResourceLoader->setBlockNetworkImage(false);
   }




具体实现
CachedResourceLoader.cpp里面
void CachedResourceLoader::setBlockNetworkImage(bool block)
{
    if (block == m_blockNetworkImage)
        return;


    m_blockNetworkImage = block;


    if (!m_autoLoadImages || m_blockNetworkImage)
        return;


    DocumentResourceMap::iterator end = m_documentResources.end();
    for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != end; ++it) {
        CachedResource* resource = it->second.get();
        if (resource->type() == CachedResource::ImageResource) {
            CachedImage* image = const_cast<CachedImage*>(static_cast<const CachedImage*>(resource));
            image->setAutoLoadWasPreventedBySettings(false);
            if (image->stillNeedsLoad()) {
                image->setLoading(true);
                load(image, true);
            }
        }
    }
}


终于找到这个家伙了,原来搞了一个循环在呼呼的执行,发送一个个的网络请求
整体执行流程现在来看基本清晰了。

上面代码是针对android4.1.1版本代码的分析

联系方式

lihui40319@sina.com

QQ:390012381

转载请注明出处:http://blog.csdn.net/lihui130135











WebView setBlockNetworkImage来龙去脉