소스 검색

Adding method to set WebView user agent, product version, and debug port from Engine.json configuration

JoshEngebretson 9 년 전
부모
커밋
0d87754836

+ 26 - 0
Source/Atomic/Engine/EngineConfig.cpp

@@ -219,6 +219,27 @@ bool EngineConfig::LoadInputConfig(const JSONValue& jinput)
 
 }
 
+bool EngineConfig::LoadWebViewConfig(const JSONValue& jwebview)
+{
+    if (!jwebview.IsObject())
+        return false;
+
+    for (JSONObject::ConstIterator i = jwebview.Begin(); i != jwebview.End(); ++i)
+    {
+        String key = i->first_.ToLower();
+        const JSONValue& jvalue = i->second_;
+
+        if (key == "useragent")
+            valueMap_["WebViewUserAgent"] = GetStringValue(jvalue, String::EMPTY);
+        else if (key == "productversion")
+            valueMap_["WebViewProductVersion"] = GetStringValue(jvalue, String::EMPTY);
+        else if (key == "debugPort")
+            valueMap_["WebViewDebugPort"] = GetIntValue(jvalue, 8080);
+    }
+
+    return true;
+
+}
 
 bool EngineConfig::LoadDesktopConfig(JSONValue root)
 {
@@ -247,6 +268,11 @@ bool EngineConfig::LoadDesktopConfig(JSONValue root)
     if (jinput.IsObject())
         LoadInputConfig(jinput);
 
+    const JSONValue& jwebview = jdesktop["webview"];
+    if (jwebview.IsObject())
+        LoadWebViewConfig(jwebview);
+
+
     return true;
 }
 

+ 2 - 0
Source/Atomic/Engine/EngineConfig.h

@@ -47,6 +47,8 @@ public:
 private:
 
     virtual bool LoadDesktopConfig(JSONValue root);
+
+    bool LoadWebViewConfig(const JSONValue& jwebview);
     bool LoadGraphicsConfig(const JSONValue& jgraphics);
     bool LoadWindowConfig(const JSONValue& jwindow);
     bool LoadSoundConfig(const JSONValue& jsound);

+ 1 - 0
Source/Atomic/Resource/Configuration.h

@@ -30,6 +30,7 @@ namespace Atomic
 
 class Context;
 
+/// Configuration base class for mapping JSON setting files to VariantMap configuration settings
 class Configuration
 {
 public:

+ 3 - 4
Source/AtomicEditor/Application/AEEditorCommon.cpp

@@ -54,7 +54,7 @@
 namespace Atomic
 {
     void jsapi_init_atomicnet(JSVM* vm);
-    void jsapi_init_webview(JSVM* vm);;
+    void jsapi_init_webview(JSVM* vm, const VariantMap& engineParameters);
 }
 
 using namespace ToolCore;
@@ -88,9 +88,8 @@ void AEEditorCommon::Start()
     jsapi_init_toolcore(vm_);
 
 #ifdef ATOMIC_WEBVIEW
-    // Initialize in Start so window already exists
-    context_->RegisterSubsystem(new WebBrowserHost(context_));
-    jsapi_init_webview(vm_);
+    // Initialize in Start so window already exists    
+    jsapi_init_webview(vm_, engineParameters_);
 #endif
 
 

+ 14 - 2
Source/AtomicWebView/WebBrowserHost.cpp

@@ -97,6 +97,9 @@ private:
 
 GlobalPropertyMap WebBrowserHost::globalProperties_;
 WeakPtr<WebBrowserHost> WebBrowserHost::instance_;
+String WebBrowserHost::userAgent_;
+String WebBrowserHost::productVersion_;
+int WebBrowserHost::debugPort_ = 3335;
 
 WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
 {
@@ -126,9 +129,18 @@ WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
 
     CefSettings settings;
     settings.windowless_rendering_enabled = true;
+
+    if (productVersion_.Length())
+    {
+        CefString(&settings.product_version).FromASCII(productVersion_.CString());
+    }
+
+    if (userAgent_.Length())
+    {
+        CefString(&settings.user_agent).FromASCII(userAgent_.CString());
+    }
     
-    // Enable remote debugging on port 3335
-    settings.remote_debugging_port = 3335;
+    settings.remote_debugging_port = debugPort_;
 
     d_ = new WebBrowserHostPrivate(this);
 

+ 24 - 0
Source/AtomicWebView/WebBrowserHost.h

@@ -50,6 +50,25 @@ public:
 
     static const GlobalPropertyMap& GetGlobalProperties() { return globalProperties_; }
 
+
+    // Configuration settings that must be set before creating WebBrowserHost subsystem
+
+    /// Set value that will be returned as the User-Agent HTTP header.
+    static void SetUserAgent(const String& userAgent) { userAgent_ = userAgent; }
+
+    /// Set value that will be inserted as the product portion of the default User-Agent string 
+    static void SetProductVersion(const String& productVersion) { productVersion_ = productVersion; }
+
+    /// Set to a value between 1024 and 65535 to enable remote debugging on the specified port
+    static void SetDebugPort(int debugPort) { debugPort_ = debugPort; }
+
+    /// Get User-Agent of the HTTP header. If empty the default User-Agent string will be used
+    static const String& GetUserAgent() { return userAgent_; }
+
+    /// Get value that will be inserted as the product portion of the default User-Agent string.  If empty the Chromium product version will be used      
+    static const String& GetProductVersion() { return productVersion_; }
+
+
 private:
 
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
@@ -62,6 +81,11 @@ private:
 
     static GlobalPropertyMap globalProperties_;
 
+    // configuration settings that must be set before WebBrowserHost subsystem is created
+    static String userAgent_;
+    static String productVersion_;
+    static int debugPort_;
+
 };
 
 }

+ 15 - 1
Source/AtomicWebView/WebViewJS.cpp

@@ -20,6 +20,7 @@
 // THE SOFTWARE.
 //
 
+#include <Atomic/Engine/Engine.h>
 #include <AtomicJS/Javascript/JSVM.h>
 
 #include "WebBrowserHost.h"
@@ -29,10 +30,23 @@ namespace Atomic
 
 extern void jsb_package_webview_init(JSVM* vm);
 
-void jsapi_init_webview(JSVM* vm)
+void jsapi_init_webview(JSVM* vm, const VariantMap& engineParameters)
 {
     duk_context* ctx = vm->GetJSContext();
 
+    const String& userAgent = Engine::GetParameter(engineParameters, "WebViewUserAgent", Variant("")).GetString();
+    const String& productVersion = Engine::GetParameter(engineParameters, "WebViewProductVersion", Variant("")).GetString();
+    int debugPort = Engine::GetParameter(engineParameters, "WebViewDebugPort", Variant(3335)).GetInt();
+
+    if (userAgent.Length())
+        WebBrowserHost::SetUserAgent(userAgent);
+    if (productVersion.Length())
+        WebBrowserHost::SetProductVersion(productVersion);
+
+    WebBrowserHost::SetDebugPort(debugPort);
+
+    vm->GetContext()->RegisterSubsystem(new WebBrowserHost(vm->GetContext()));
+
     jsb_package_webview_init(vm);
 
     duk_get_global_string(ctx, "WebView");