<commit_before>// Copyright 2013 The Chromium Authors. All rights reserved.
// Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebResourceResponse;
import android.widget.FrameLayout;

import java.io.IOException;
import java.io.InputStream;

import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.ThreadUtils;
import org.chromium.components.navigation_interception.InterceptNavigationDelegate;
import org.chromium.content.browser.ContentView;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.ContentViewRenderView;
import org.chromium.content.browser.ContentViewRenderView.CompositingSurfaceType;
import org.chromium.content.browser.ContentViewStatics;
import org.chromium.content.browser.LoadUrlParams;
import org.chromium.content.browser.NavigationHistory;
import org.chromium.media.MediaPlayerBridge;
import org.chromium.ui.base.ActivityWindowAndroid;

@JNINamespace("xwalk")
/**
 * This class is the implementation class for XWalkView by calling internal
 * various classes.
 */
class XWalkContent extends FrameLayout implements XWalkPreferences.KeyValueChangeListener {
    private static String TAG = "XWalkContent";
    private ContentViewCore mContentViewCore;
    private ContentView mContentView;
    private ContentViewRenderView mContentViewRenderView;
    private ActivityWindowAndroid mWindow;
    private XWalkDevToolsServer mDevToolsServer;
    private XWalkView mXWalkView;
    private XWalkContentsClientBridge mContentsClientBridge;
    private XWalkContentsIoThreadClient mIoThreadClient;
    private XWalkWebContentsDelegateAdapter mXWalkContentsDelegateAdapter;
    private XWalkSettings mSettings;
    private XWalkGeolocationPermissions mGeolocationPermissions;
    private XWalkLaunchScreenManager mLaunchScreenManager;

    long mXWalkContent;
    long mWebContents;
    boolean mReadyToLoad = false;
    String mPendingUrl = null;
    String mPendingData = null;

    public XWalkContent(Context context, AttributeSet attrs, XWalkView xwView) {
        super(context, attrs);

        // Initialize the WebContensDelegate.
        mXWalkView = xwView;
        mContentsClientBridge = new XWalkContentsClientBridge(mXWalkView);
        mXWalkContentsDelegateAdapter = new XWalkWebContentsDelegateAdapter(
            mContentsClientBridge);
        mIoThreadClient = new XWalkIoThreadClientImpl();

        // Initialize mWindow which is needed by content
        mWindow = new ActivityWindowAndroid(xwView.getActivity());

        // Initialize ContentViewRenderView
        boolean animated = XWalkPreferences.getValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW);
        CompositingSurfaceType surfaceType =
                animated ? CompositingSurfaceType.TEXTURE_VIEW : CompositingSurfaceType.SURFACE_VIEW;
        mContentViewRenderView = new ContentViewRenderView(context, mWindow, surfaceType) {
            protected void onReadyToRender() {
                if (mPendingUrl != null) {
                    doLoadUrl(mPendingUrl, mPendingData);
                    mPendingUrl = null;
                    mPendingData = null;
                }

                mReadyToLoad = true;
            }
        };
        mLaunchScreenManager = new XWalkLaunchScreenManager(context, mXWalkView);
        mContentViewRenderView.registerFirstRenderedFrameListener(mLaunchScreenManager);
        addView(mContentViewRenderView,
                new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));

        mXWalkContent = nativeInit(mXWalkContentsDelegateAdapter, mContentsClientBridge);
        mWebContents = nativeGetWebContents(mXWalkContent, mIoThreadClient,
                mContentsClientBridge.getInterceptNavigationDelegate());

        // Initialize ContentView.
        mContentView = ContentView.newInstance(getContext(), mWebContents, mWindow);
        addView(mContentView,
                new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));
        mContentView.getContentViewCore().setContentViewClient(mContentsClientBridge);

        mContentViewCore = mContentView.getContentViewCore();
        mContentViewRenderView.setCurrentContentViewCore(mContentViewCore);
        // For addJavascriptInterface
        mContentsClientBridge.installWebContentsObserver(mContentViewCore);

        mContentViewCore.setDownloadDelegate(mContentsClientBridge);

        // Set the third argument isAccessFromFileURLsGrantedByDefault to false, so that
        // the members mAllowUniversalAccessFromFileURLs and mAllowFileAccessFromFileURLs
        // won't be changed from false to true at the same time in the constructor of
        // XWalkSettings class.
        mSettings = new XWalkSettings(getContext(), mWebContents, false);
        // Enable AllowFileAccessFromFileURLs, so that files under file:// path could be
        // loaded by XMLHttpRequest.
        mSettings.setAllowFileAccessFromFileURLs(true);

        SharedPreferences sharedPreferences = new InMemorySharedPreferences();
        mGeolocationPermissions = new XWalkGeolocationPermissions(sharedPreferences);

        MediaPlayerBridge.setResourceLoadingFilter(
                new XWalkMediaPlayerResourceLoadingFilter());

        XWalkPreferences.load(this);
    }

    void doLoadUrl(String url, String content) {
        // Handle the same url loading by parameters.
        if (url != null && !url.isEmpty() &&
                TextUtils.equals(url, mContentViewCore.getUrl())) {
            mContentViewCore.reload(true);
        } else {
            LoadUrlParams params = null;
            if (content == null || content.isEmpty()) {
                params = new LoadUrlParams(url);
            } else {
                params = LoadUrlParams.createLoadDataParamsWithBaseUrl(
                        content, "text/html", false, url, null);
            }
            params.setOverrideUserAgent(LoadUrlParams.UA_OVERRIDE_TRUE);
            mContentViewCore.loadUrl(params);
        }

        mContentView.requestFocus();
    }

    public void loadUrl(String url, String data) {
        if ((url == null || url.isEmpty()) &&
                (data == null || data.isEmpty())) {
            return;
        }

        if (mReadyToLoad) {
            doLoadUrl(url, data);
        } else {
            mPendingUrl = url;
            mPendingData = data;
        }
    }

    public void reload(int mode) {
        if (mReadyToLoad) {
            switch (mode) {
                case XWalkView.RELOAD_IGNORE_CACHE:
                    mContentViewCore.reloadIgnoringCache(true);
                    break;
                case XWalkView.RELOAD_NORMAL:
                default:
                    mContentViewCore.reload(true);

            }
        }
    }

    public String getUrl() {
        String url = mContentViewCore.getUrl();
        if (url == null || url.trim().isEmpty()) return null;
        return url;
    }

    public String getTitle() {
        String title = mContentViewCore.getTitle().trim();
        if (title == null) title = "";<commit_msg>[Android] Decouple url loading and onReadyToRender callback<commit_after>// Copyright 2013 The Chromium Authors. All rights reserved.
// Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebResourceResponse;
import android.widget.FrameLayout;

import java.io.IOException;
import java.io.InputStream;

import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.ThreadUtils;
import org.chromium.components.navigation_interception.InterceptNavigationDelegate;
import org.chromium.content.browser.ContentView;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.ContentViewRenderView;
import org.chromium.content.browser.ContentViewRenderView.CompositingSurfaceType;
import org.chromium.content.browser.ContentViewStatics;
import org.chromium.content.browser.LoadUrlParams;
import org.chromium.content.browser.NavigationHistory;
import org.chromium.media.MediaPlayerBridge;
import org.chromium.ui.base.ActivityWindowAndroid;

@JNINamespace("xwalk")
/**
 * This class is the implementation class for XWalkView by calling internal
 * various classes.
 */
class XWalkContent extends FrameLayout implements XWalkPreferences.KeyValueChangeListener {
    private static String TAG = "XWalkContent";
    private ContentViewCore mContentViewCore;
    private ContentView mContentView;
    private ContentViewRenderView mContentViewRenderView;
    private ActivityWindowAndroid mWindow;
    private XWalkDevToolsServer mDevToolsServer;
    private XWalkView mXWalkView;
    private XWalkContentsClientBridge mContentsClientBridge;
    private XWalkContentsIoThreadClient mIoThreadClient;
    private XWalkWebContentsDelegateAdapter mXWalkContentsDelegateAdapter;
    private XWalkSettings mSettings;
    private XWalkGeolocationPermissions mGeolocationPermissions;
    private XWalkLaunchScreenManager mLaunchScreenManager;

    long mXWalkContent;
    long mWebContents;

    public XWalkContent(Context context, AttributeSet attrs, XWalkView xwView) {
        super(context, attrs);

        // Initialize the WebContensDelegate.
        mXWalkView = xwView;
        mContentsClientBridge = new XWalkContentsClientBridge(mXWalkView);
        mXWalkContentsDelegateAdapter = new XWalkWebContentsDelegateAdapter(
            mContentsClientBridge);
        mIoThreadClient = new XWalkIoThreadClientImpl();

        // Initialize mWindow which is needed by content
        mWindow = new ActivityWindowAndroid(xwView.getActivity());

        // Initialize ContentViewRenderView
        boolean animated = XWalkPreferences.getValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW);
        CompositingSurfaceType surfaceType =
                animated ? CompositingSurfaceType.TEXTURE_VIEW : CompositingSurfaceType.SURFACE_VIEW;
        mContentViewRenderView = new ContentViewRenderView(context, mWindow, surfaceType) {
            protected void onReadyToRender() {
                // Anything depending on the underlying Surface readiness should
                // be placed here.
            }
        };
        mLaunchScreenManager = new XWalkLaunchScreenManager(context, mXWalkView);
        mContentViewRenderView.registerFirstRenderedFrameListener(mLaunchScreenManager);
        addView(mContentViewRenderView,
                new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));

        mXWalkContent = nativeInit(mXWalkContentsDelegateAdapter, mContentsClientBridge);
        mWebContents = nativeGetWebContents(mXWalkContent, mIoThreadClient,
                mContentsClientBridge.getInterceptNavigationDelegate());

        // Initialize ContentView.
        mContentView = ContentView.newInstance(getContext(), mWebContents, mWindow);
        addView(mContentView,
                new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));
        mContentView.getContentViewCore().setContentViewClient(mContentsClientBridge);

        mContentViewCore = mContentView.getContentViewCore();
        mContentViewRenderView.setCurrentContentViewCore(mContentViewCore);
        // For addJavascriptInterface
        mContentsClientBridge.installWebContentsObserver(mContentViewCore);

        mContentViewCore.setDownloadDelegate(mContentsClientBridge);

        // Set the third argument isAccessFromFileURLsGrantedByDefault to false, so that
        // the members mAllowUniversalAccessFromFileURLs and mAllowFileAccessFromFileURLs
        // won't be changed from false to true at the same time in the constructor of
        // XWalkSettings class.
        mSettings = new XWalkSettings(getContext(), mWebContents, false);
        // Enable AllowFileAccessFromFileURLs, so that files under file:// path could be
        // loaded by XMLHttpRequest.
        mSettings.setAllowFileAccessFromFileURLs(true);

        SharedPreferences sharedPreferences = new InMemorySharedPreferences();
        mGeolocationPermissions = new XWalkGeolocationPermissions(sharedPreferences);

        MediaPlayerBridge.setResourceLoadingFilter(
                new XWalkMediaPlayerResourceLoadingFilter());

        XWalkPreferences.load(this);
    }

    void doLoadUrl(String url, String content) {
        // Handle the same url loading by parameters.
        if (url != null && !url.isEmpty() &&
                TextUtils.equals(url, mContentViewCore.getUrl())) {
            mContentViewCore.reload(true);
        } else {
            LoadUrlParams params = null;
            if (content == null || content.isEmpty()) {
                params = new LoadUrlParams(url);
            } else {
                params = LoadUrlParams.createLoadDataParamsWithBaseUrl(
                        content, "text/html", false, url, null);
            }
            params.setOverrideUserAgent(LoadUrlParams.UA_OVERRIDE_TRUE);
            mContentViewCore.loadUrl(params);
        }

        mContentView.requestFocus();
    }

    public void loadUrl(String url, String data) {
        if ((url == null || url.isEmpty()) &&
                (data == null || data.isEmpty())) {
            return;
        }

        doLoadUrl(url, data);
    }

    public void reload(int mode) {
        switch (mode) {
            case XWalkView.RELOAD_IGNORE_CACHE:
                mContentViewCore.reloadIgnoringCache(true);
                break;
            case XWalkView.RELOAD_NORMAL:
            default:
                mContentViewCore.reload(true);
        }
    }

    public String getUrl() {
        String url = mContentViewCore.getUrl();
        if (url == null || url.trim().isEmpty()) return null;
        return url;
    }

    public String getTitle() {
        String title = mContentViewCore.getTitle().trim();
        if (title == null) title = "";
{'fraction_non_alphanumeric': 0.0734820077626472, 'fraction_numerical': 0.0015788434971383462, 'mean_word_length': 3.0549479861296347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5709732', 'n_tokens_mistral': 3973, 'n_tokens_neox': 3797, 'n_words': 852}