用户代理中的'mobile'关键字在 chromium 中从何而来?



我很困惑,在Android系统上的移动设备(phone(上,基于铬的浏览器(例如webview(的用户代理具有Mobile关键字,但在某些移动设备上(例如tablets(它不存在,那么在chrmoium代码中在哪里区分?

具有Mobile关键字的移动设备:

"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36"
也许你可以在 chromium 源中检查并引用 DeviceUtils.java 和 aw_content_client.cc,

它会在 DeviceUtils 中通过DeviceFormFactor.isTablet()来检查设备类型.java以决定是否需要添加 Mobile 关键字,并在 aw_content_client.cc 中设置 Mobile 关键字,希望这些信息对您有所帮助:

//DeviceUtils.java
package org.chromium.content.browser;
import android.content.Context;
import org.chromium.base.CommandLine;
import org.chromium.content.common.ContentSwitches;
import org.chromium.ui.base.DeviceFormFactor;
/**
 * A utility class that has helper methods for device configuration.
 */
public class DeviceUtils {
    /**
     * Appends the switch specifying which user agent should be used for this device.
     * @param context The context for the caller activity.
     */
    public static void addDeviceSpecificUserAgentSwitch(Context context) {
        if (DeviceFormFactor.isTablet(context)) {
            CommandLine.getInstance().appendSwitch(ContentSwitches.USE_MOBILE_UA);
        }
    }
}

   //aw_content_client.cc
   std::string GetUserAgent() {
  // "Version/4.0" had been hardcoded in the legacy WebView.
  std::string product = "Version/4.0 " + GetProduct();
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
        switches::kUseMobileUserAgent)) {
    product += " Mobile";
  }
  return content::BuildUserAgentFromProductAndExtraOSInfo(
          product,
          GetExtraOSUserAgentInfo());
}

最新更新