How to Set UaProfile on kitkat in the feature (4) of android browser

Source: Internet
Author: User

 

--- a/src/com/android/browser/BrowserSettings.java+++ b/src/com/android/browser/BrowserSettings.java@@ -272,6 +272,8 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener,         } else {             settings.setUserAgentString(USER_AGENTS[getUserAgent()]);         }+       String uaprofile = www.baidu.com;+       settings.setUserProfileString(uaprofile);     }
Generally, UaProfile is an xml webpage stored on the server of the carrier. We will replace www.baidu.com first.
The modified part is the framework. In the framework/base webkit, we have written two methods: get and set Profile.
diff --git a/core/java/android/webkit/WebSettings.java b/core/java/android/webkit/WebSettings.javaindex 98ef66e..4e273f9 100644--- a/core/java/android/webkit/WebSettings.java+++ b/core/java/android/webkit/WebSettings.java@@ -599,6 +599,9 @@ public abstract class WebSettings {         throw new MustOverrideException();     }+    /** {@hide} */+    public synchronized void setUserProfile(int ua) {+    }     /**      * Gets the user-agent as an integer code.      * 
  • @-1332,6 + 1335,16 @ public abstract class WebSettings {throw new MustOverrideException () ;}+/** {@ hide} */+ public synchronized void setUserProfileString (String ua) {+ throw new MustOverrideException (); ++/** {@ hide} */+ public synchronized String getUserProfileString () + {+ throw new MustOverrideException (); +}/*** Gets the WebView's user-agent string. *
In the webview of the framework, we also need to rewrite the methods in websettings.
--- a/chromium/java/com/android/webview/chromium/ContentSettingsAdapter.java+++ b/chromium/java/com/android/webview/chromium/ContentSettingsAdapter.java@@ -200,6 +200,16 @@ public class ContentSettingsAdapter extends android.webkit.WebSettings {     }     @Override+    public synchronized void setUserProfile(int ua)+    {+        if (ua == 0) {+            setUserProfileString(null);+        } else {+            Log.d(chao,setUserProfile not supported, ua= + ua);+        }+    }++    @Override     public synchronized int getUserAgent() {         // Minimal implementation for backwards compatibility: just identifies default vs custom.         return AwSettings.getDefaultUserAgent().equals(getUserAgentString()) ? 0 : -1;@@ -511,6 +521,16 @@ public class ContentSettingsAdapter extends android.webkit.WebSettings {     }     @Override+    public synchronized void setUserProfileString(String ua) {+        mAwSettings.setUserProfileString(ua);+    }++    @Override+    public synchronized String getUserProfileString() {+       return mAwSettings.getUserProfileString();+    }++    @Override     public synchronized String getUserAgentString() {         return mAwSettings.getUserAgentString();     }
Next, modify the part in extern/chromium_org.
First, the java part in android_webview can be called with the framework.
--- a/android_webview/java/src/org/chromium/android_webview/AwSettings.java+++ b/android_webview/java/src/org/chromium/android_webview/AwSettings.java@@ -14,7 +14,7 @@ import android.provider.Settings; import android.webkit.WebSettings.PluginState; import android.webkit.WebSettings; import android.webkit.WebView;-+import android.util.Log; import org.chromium.base.CalledByNative; import org.chromium.base.JNINamespace; import org.chromium.base.ThreadUtils;@@ -64,6 +64,7 @@ public class AwSettings {     // TODO(mnaganov): Should be obtained from Android. Problem: it is hidden.     private String mDefaultTextEncoding = Latin-1;     private String mUserAgent;+    private String mUserProfile;     private int mMinimumFontSize = 8;     private int mMinimumLogicalFontSize = 8;     private int mDefaultFontSize = 16;@@ -373,7 +374,6 @@ public class AwSettings {     private float getInitialPageScalePercentLocked() {         return mInitialPageScalePercent;     }-     /**      * See {@link android.webkit.WebSettings#setNeedInitialFocus}.      */@@ -469,6 +469,36 @@ public class AwSettings {     }     /**+     * Add by chao+     */+    public void setUserProfileString(String ua)+    {+        synchronized (mAwSettingsLock) {+            mUserProfile = ua;+            Log.e(chao,setUserProfileString ua =  + ua);+           mEventHandler.maybeRunOnUiThreadBlocking(new Runnable() {+                @Override+                public void run() {+                   if (mNativeAwSettings != 0) {+                        nativeUpdateUserProfileLocked(mNativeAwSettings);+                    }+                }+            });+        }+    }++    public String getUserProfileString() {+        synchronized (mAwSettingsLock) {+            return mUserProfile;+        }+    }++    @CalledByNative+    private String getUserProfileLocked() {+        return mUserProfile;+    }++    /**      * See {@link android.webkit.WebSettings#getUserAgentString}.      */     public String getUserAgentString() {@@ -1410,6 +1440,7 @@ public class AwSettings {     private native void nativeUpdateInitialPageScaleLocked(int nativeAwSettings);+    private native void nativeUpdateUserProfileLocked(int nativeAwSettings);     private native void nativeUpdateUserAgentLocked(int nativeAwSettings);     private native void nativeUpdateWebkitPreferencesLocked(int nativeAwSettings);
After processing part of the java end, you can modify the native part.
The part corresponding to AwSettings. java is aw_settings.cc.
diff --git a/android_webview/native/aw_settings.cc b/android_webview/native/aw_settings.ccindex e4aa588..a8863e2 100644--- a/android_webview/native/aw_settings.cc+++ b/android_webview/native/aw_settings.cc@@ -100,10 +100,29 @@ void AwSettings::UpdateEverythingLocked(JNIEnv* env, jobject obj) {   UpdateInitialPageScaleLocked(env, obj);   UpdateWebkitPreferencesLocked(env, obj);   UpdateUserAgentLocked(env, obj);+  UpdateUserProfileLocked(env, obj);   ResetScrollAndScaleState(env, obj);   UpdateFormDataPreferencesLocked(env, obj); }+void AwSettings::UpdateUserProfileLocked(JNIEnv* env, jobject obj) {+  if (!web_contents()) return;+  ScopedJavaLocalRef
 
   str =+      Java_AwSettings_getUserProfileLocked(env, obj);+  bool uaprofile_overidden = str.obj() != NULL;+  LOG(ERROR) << chao  uaprofile_overidden =  << uaprofile_overidden;+  if (uaprofile_overidden) {+    std::string override = base::android::ConvertJavaStringToUTF8(str);+    LOG(ERROR) << chao  override =  << override;+    web_contents()->SetUserProfileOverride(override);+  }+} void AwSettings::UpdateUserAgentLocked(JNIEnv* env, jobject obj) {   if (!web_contents()) return;
 
Because it is a c file, we need to declare the new file.
diff --git a/android_webview/native/aw_settings.h b/android_webview/native/aw_settings.hindex d1f81c3..e52adec 100644--- a/android_webview/native/aw_settings.h+++ b/android_webview/native/aw_settings.h@@ -32,6 +32,7 @@ class AwSettings : public content::WebContentsObserver {   void UpdateEverythingLocked(JNIEnv* env, jobject obj);   void UpdateInitialPageScaleLocked(JNIEnv* env, jobject obj);   void UpdateUserAgentLocked(JNIEnv* env, jobject obj);+  void UpdateUserProfileLocked(JNIEnv* env, jobject obj);   void UpdateWebkitPreferencesLocked(JNIEnv* env, jobject obj);   void UpdateFormDataPreferencesLocked(JNIEnv* env, jobject obj);
Because our method is to use web_contents ()-> SetUserProfileOverride (override) for corresponding implementation, we also need to modify the corresponding file.
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.ccindex 8f09208..21070ec 100644--- a/content/browser/web_contents/web_contents_impl.cc+++ b/content/browser/web_contents/web_contents_impl.cc@@ -863,6 +863,29 @@ WebUI* WebContentsImpl::GetCommittedWebUI() const {   return render_manager_.web_ui(); }+void WebContentsImpl::SetUserProfileOverride(const std::string& override) {+  LOG(ERROR) << chao override =  << override;+  WebPreferences prefs;+  prefs.user_profile_override = override;+  GURL url = controller_.GetActiveEntry()+      ? controller_.GetActiveEntry()->GetURL() : GURL::EmptyGURL();+  GetContentClient()->browser()->OverrideWebkitPrefs(host, url, &prefs);+}void WebContentsImpl::SetUserAgentOverride(const std::string& override) {   if (GetUserAgentOverride() == override)     return;
Similarly, declarations are essential.
diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.hindex a5eaf7d..f833e8c 100644--- a/content/browser/web_contents/web_contents_impl.h+++ b/content/browser/web_contents/web_contents_impl.h@@ -229,6 +229,8 @@ class CONTENT_EXPORT WebContentsImpl   virtual WebUI* GetCommittedWebUI() const OVERRIDE;   virtual void SetUserAgentOverride(const std::string& override) OVERRIDE;   virtual const std::string& GetUserAgentOverride() const OVERRIDE;+  virtual void SetUserProfileOverride(const std::string& override) OVERRIDE; #if defined(OS_WIN) && defined(USE_AURA)   virtual void SetParentNativeViewAccessible(       gfx::NativeViewAccessible accessible_parent) OVERRIDE;
Because it is an inherited relationship, it must be declared in its parent class.
diff --git a/content/public/browser/web_contents.h b/content/public/browser/web_contents.hindex 86b51a2..b09a8c8 100644--- a/content/public/browser/web_contents.h+++ b/content/public/browser/web_contents.h@@ -210,6 +210,8 @@ class WebContents : public PageNavigator,   // Allows overriding the user agent used for NavigationEntries it owns.   virtual void SetUserAgentOverride(const std::string& override) = 0;+  virtual void SetUserProfileOverride(const std::string& override) = 0;   virtual const std::string& GetUserAgentOverride() const = 0; #if defined(OS_WIN) && defined(USE_AURA)
We use webPrefrence as an intermediary, so we need to declare the corresponding content in webpreferences. Here, because only this object exists, we must use static variables for declaration.
diff --git a/webkit/common/webpreferences.cc b/webkit/common/webpreferences.ccindex 02a87df..409a549 100644--- a/webkit/common/webpreferences.cc+++ b/webkit/common/webpreferences.cc@@ -12,6 +12,7 @@ using WebKit::WebSettings;+std::string WebPreferences::user_profile_override = ; WebPreferences::WebPreferences()     : default_font_size(16),       default_fixed_font_size(13),@@ -154,6 +155,7 @@ WebPreferences::WebPreferences()       ASCIIToUTF16(Times New Roman); }+ WebPreferences::~WebPreferences() { }diff --git a/webkit/common/webpreferences.h b/webkit/common/webpreferences.hindex d94ec9c..e2d44cb 100644--- a/webkit/common/webpreferences.h+++ b/webkit/common/webpreferences.h@@ -18,7 +18,6 @@ #include base/strings/string16.h #include url/gurl.h #include webkit/common/webkit_common_export.h- namespace WebKit { class WebView; }@@ -79,6 +78,7 @@ struct WEBKIT_COMMON_EXPORT WebPreferences {   bool remote_fonts_enabled;   bool javascript_can_access_clipboard;   bool xss_auditor_enabled;+  static std::string user_profile_override;   // We don't use dns_prefetching_enabled to disable DNS prefetching.  Instead,   // we disable the feature at a lower layer so that we catch non-WebKit uses   // of DNS prefetch as well.
In thirdparty's webkit, we will start to use this always passed value for value assignment.
diff --git a/Source/core/loader/FrameLoader.cpp b/Source/core/loader/FrameLoader.cppindex f068e20..c7399dc 100644--- a/Source/core/loader/FrameLoader.cpp+++ b/Source/core/loader/FrameLoader.cpp@@ -34,7 +34,6 @@ #include config.h #include core/loader/FrameLoader.h- #include HTMLNames.h #include bindings/v8/DOMWrapperWorld.h #include bindings/v8/ScriptController.h@@ -90,7 +89,10 @@ #include wtf/TemporaryChange.h #include wtf/text/CString.h #include wtf/text/WTFString.h-+#include 
 
  +#include ../../../../../webkit/common/webpreferences.h+#include public/platform/WebString.h+#include 
  
    namespace WebCore { using namespace HTMLNames;@@ -1316,6 +1318,17 @@ int FrameLoader::numPendingOrLoadingRequests(bool recurse) const     return count; }+String FrameLoader::userProfile(const KURL& url) const+{+     WebPreferences prefs;++    std::string userProfile = prefs.user_profile_override;+    String tmp_Profile = WebKit::WebString::fromUTF8(userProfile);+    LOG_ERROR(chao userProfile);+    LOG_ERROR(chao userProfile = %s, userProfile);+    return tmp_Profile;+}+ String FrameLoader::userAgent(const KURL& url) const {     String userAgent = m_client->userAgent(url);@@ -1378,7 +1394,8 @@ void FrameLoader::addExtraFieldsToRequest(ResourceRequest& request)         return;     applyUserAgent(request);-+    LOG_ERROR(chao applyUserProfile  begin);+    applyUserProfile(request);     if (request.cachePolicy() == ReloadIgnoringCacheData) {         if (m_loadType == FrameLoadTypeReload)             request.setHTTPHeaderField(Cache-Control, max-age=0);@@ -1395,6 +1412,21 @@ void FrameLoader::addExtraFieldsToRequest(ResourceRequest& request)     addHTTPOriginIfNeeded(request, String()); }++void FrameLoader::applyUserProfile(ResourceRequest& request)+{+    String userProfile = this->userProfile(request.url());+    LOG_ERROR(chao FrameLoader::applyUserProfile userProfile = %s,userProfile);+    request.setHTTPUaProfile(userProfile);+}++ void FrameLoader::addHTTPOriginIfNeeded(ResourceRequest& request, const String& origin) {     if (!request.httpOrigin().isEmpty())
  
 
Declaration of related new functions:
diff --git a/Source/core/loader/FrameLoader.h b/Source/core/loader/FrameLoader.hindex c9ff319..c40f568 100644--- a/Source/core/loader/FrameLoader.h+++ b/Source/core/loader/FrameLoader.h@@ -182,7 +182,7 @@ public:     void receivedFirstData();     String userAgent(const KURL&) const;-+    String userProfile(const KURL&) const;     void dispatchDidClearWindowObjectInWorld(DOMWrapperWorld*);     void dispatchDidClearWindowObjectsInAllWorlds();     void dispatchDocumentElementAvailable();@@ -218,6 +218,8 @@ public:     Frame* findFrameForNavigation(const AtomicString& name, Document* activeDocument = 0);     void applyUserAgent(ResourceRequest&);+    void applyUserProfile(ResourceRequest& request);     bool shouldInterruptLoadForXFrameOptions(const String&, const KURL&, unsigned long requestIdentifier);
Then there is the implementation of this function:
diff --git a/Source/core/platform/network/ResourceRequest.h b/Source/core/platform/network/ResourceRequest.hindex d54305a..7062960 100644--- a/Source/core/platform/network/ResourceRequest.h+++ b/Source/core/platform/network/ResourceRequest.h@@ -145,7 +145,7 @@ namespace WebCore {         String httpUserAgent() const { return httpHeaderField(User-Agent); }         void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField(User-Agent, httpUserAgent); }         void clearHTTPUserAgent();-+        void setHTTPUaProfile(const String& httpUAProfile) { setHTTPHeaderField(x-wap-profile, httpUAProfile); }         String httpAccept() const { return httpHeaderField(Accept); }         void setHTTPAccept(const String& httpAccept) { setHTTPHeaderField(Accept, httpAccept); }         void clearHTTPAccept();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.