Browse Source

Working on WebView events

Josh Engebretson 10 years ago
parent
commit
46e64f39e3

+ 2 - 0
Source/AtomicWebView/UIWebView.h

@@ -46,6 +46,8 @@ public:
     UIWebView(Context* context, const String& initialURL = String::EMPTY);
     UIWebView(Context* context, const String& initialURL = String::EMPTY);
     virtual ~UIWebView();
     virtual ~UIWebView();
 
 
+    WebClient* GetWebClient() { return webClient_; }
+
     WebTexture2D* GetWebTexture2D() const;
     WebTexture2D* GetWebTexture2D() const;
 
 
 protected:
 protected:

+ 83 - 2
Source/AtomicWebView/WebClient.cpp

@@ -19,6 +19,8 @@
 #include "WebBrowserHost.h"
 #include "WebBrowserHost.h"
 #include "WebClient.h"
 #include "WebClient.h"
 #include "WebKeyboard.h"
 #include "WebKeyboard.h"
+#include "WebViewEvents.h"
+#include "WebString.h"
 
 
 namespace Atomic
 namespace Atomic
 {
 {
@@ -27,7 +29,7 @@ namespace Atomic
 void* GetNSWindowContentView(void* window);
 void* GetNSWindowContentView(void* window);
 #endif
 #endif
 
 
-class WebClientPrivate : public CefClient, public CefLifeSpanHandler
+class WebClientPrivate : public CefClient, public CefLifeSpanHandler, public CefLoadHandler
 {
 {
     friend class WebClient;
     friend class WebClient;
 
 
@@ -55,6 +57,74 @@ public:
         return this;
         return this;
     }
     }
 
 
+    virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
+        return this;
+    }
+
+    // CefLoadHandler
+
+    void OnLoadStart(CefRefPtr<CefBrowser> browser,
+                     CefRefPtr<CefFrame> frame) OVERRIDE
+    {
+        if (webClient_.Null())
+            return;
+
+        if (!frame->IsMain())
+            return;
+
+        VariantMap eventData;
+        eventData[WebViewLoadStart::P_CLIENT] = webClient_;
+
+        CefString cefURL = frame->GetURL();
+        String url;
+        ConvertCEFString(cefURL, url);
+        eventData[WebViewLoadStart::P_URL] = url;
+
+        webClient_->SendEvent(E_WEBVIEWLOADSTART, eventData);
+
+    }
+
+    void OnLoadEnd(CefRefPtr<CefBrowser> browser,
+                   CefRefPtr<CefFrame> frame,
+                   int httpStatusCode) OVERRIDE
+    {
+        if (webClient_.Null())
+            return;
+
+        if (!frame->IsMain())
+            return;
+
+        VariantMap eventData;
+        eventData[WebViewLoadEnd::P_CLIENT] = webClient_;
+
+        CefString cefURL = frame->GetURL();
+        String url;
+        ConvertCEFString(cefURL, url);
+        eventData[WebViewLoadEnd::P_URL] = url;
+
+        webClient_->SendEvent(E_WEBVIEWLOADEND, eventData);
+
+    }
+
+    void OnLoadError(CefRefPtr<CefBrowser> browser,
+                     CefRefPtr<CefFrame> frame,
+                     ErrorCode errorCode,
+                     const CefString& errorText,
+                     const CefString& failedUrl) OVERRIDE
+    {
+        if (webClient_.Null())
+            return;
+
+    }
+
+    void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
+                              bool isLoading,
+                              bool canGoBack,
+                              bool canGoForward) OVERRIDE
+    {
+
+    }
+
     bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
     bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
                                   CefProcessId source_process,
                                   CefProcessId source_process,
                                   CefRefPtr<CefProcessMessage> message) OVERRIDE
                                   CefRefPtr<CefProcessMessage> message) OVERRIDE
@@ -313,13 +383,24 @@ void WebClient::SendFocusEvent(bool focus)
     host->SendFocusEvent(focus);
     host->SendFocusEvent(focus);
 }
 }
 
 
+// Navigation
+
+void WebClient::LoadURL(const String& url)
+{
+    if (!d_->browser_.get())
+        return;
+
+    CefString _url(url.CString());
+    d_->browser_->GetMainFrame()->LoadURL(_url);
+
+}
+
 void WebClient::ShortcutCut()
 void WebClient::ShortcutCut()
 {
 {
     if (!d_->browser_.get())
     if (!d_->browser_.get())
         return;
         return;
 
 
     d_->browser_->GetFocusedFrame()->Cut();
     d_->browser_->GetFocusedFrame()->Cut();
-
 }
 }
 
 
 
 

+ 5 - 0
Source/AtomicWebView/WebClient.h

@@ -51,6 +51,11 @@ public:
     void ShortcutRedo();
     void ShortcutRedo();
     void ShortcutDelete();
     void ShortcutDelete();
 
 
+    // Navigation
+
+    /// Load the specified url into the main frame of the browser
+    void LoadURL(const String& url);
+
 private:
 private:
 
 
     void WasResized();
     void WasResized();

+ 28 - 0
Source/AtomicWebView/WebString.cpp

@@ -0,0 +1,28 @@
+
+#include <include/cef_client.h>
+
+#include "WebString.h"
+
+namespace Atomic
+{
+
+bool ConvertCEFString(const CefString& cefString, String& output)
+{
+    output = "Failed to Convert CefString";
+
+    cef_string_utf8_t utf8;
+    memset(&utf8, 0, sizeof(utf8));
+
+    if (!cef_string_utf16_to_utf8(cefString.c_str(), cefString.length(), &utf8))
+    {
+        cef_string_utf8_clear(&utf8);
+        return false;
+    }
+
+    output = utf8.str;
+    cef_string_utf8_clear(&utf8);
+
+    return true;
+}
+
+}

+ 13 - 0
Source/AtomicWebView/WebString.h

@@ -0,0 +1,13 @@
+
+#pragma once
+
+
+#include <Atomic/Container/Str.h>
+
+namespace Atomic
+{
+
+bool ConvertCEFString(const CefString& cefString, String& output);
+
+}
+

+ 23 - 0
Source/AtomicWebView/WebViewEvents.h

@@ -0,0 +1,23 @@
+
+#pragma once
+
+#include <Atomic/Core/Object.h>
+
+namespace Atomic
+{
+
+/// WebView load start
+EVENT(E_WEBVIEWLOADSTART, WebViewLoadStart)
+{
+    PARAM(P_CLIENT, Client);   // WebClient*
+    PARAM(P_URL, Url);   // String
+}
+
+/// WebView load end
+EVENT(E_WEBVIEWLOADEND, WebViewLoadEnd)
+{
+    PARAM(P_CLIENT, Client);   // WebClient*
+    PARAM(P_URL, Url);   // String
+}
+
+}