WebMessageHandler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "include/wrapper/cef_message_router.h"
  23. #include <Atomic/IO/Log.h>
  24. #include "WebViewEvents.h"
  25. #include "WebString.h"
  26. #include "WebClient.h"
  27. #include "WebMessageHandler.h"
  28. namespace Atomic
  29. {
  30. class WebMessageHandlerPrivate : public CefMessageRouterBrowserSide::Handler
  31. {
  32. public:
  33. WebMessageHandlerPrivate(WebMessageHandler* webMessageHandler) : webMessageHandler_(webMessageHandler), queryHandled_(false)
  34. {
  35. }
  36. void Success(const String& response)
  37. {
  38. if (!currentCallback_)
  39. {
  40. ATOMIC_LOGERROR("WebMessageHandlerPrivate::Success - Called with no current callback");
  41. return;
  42. }
  43. queryHandled_ = true;
  44. currentCallback_->Success(CefString(response.CString()));
  45. }
  46. void Failure(int errorCode, const String& errorMessage)
  47. {
  48. if (!currentCallback_)
  49. {
  50. ATOMIC_LOGERROR("WebMessageHandlerPrivate::Failure - Called with no current callback");
  51. return;
  52. }
  53. queryHandled_ = true;
  54. currentCallback_->Failure(errorCode, CefString(errorMessage.CString()));
  55. }
  56. // Called due to atomicQuery execution.
  57. bool OnQuery(CefRefPtr<CefBrowser> browser,
  58. CefRefPtr<CefFrame> frame,
  59. int64 query_id,
  60. const CefString& request,
  61. bool persistent,
  62. CefRefPtr<Callback> callback) OVERRIDE
  63. {
  64. currentCallback_ = callback;
  65. VariantMap eventData;
  66. using namespace WebMessage;
  67. String request_;
  68. ConvertCEFString(request, request_);
  69. eventData[P_HANDLER] = webMessageHandler_;
  70. eventData[P_QUERYID] = (double) query_id;
  71. eventData[P_REQUEST] = request_;
  72. eventData[P_PERSISTENT] = persistent;
  73. eventData[P_CEFBROWSER] = (void*) browser;
  74. eventData[P_CEFFRAME] = (void*) frame;
  75. // defaults to not being deferred
  76. eventData[P_DEFERRED] = false;
  77. queryHandled_ = false;
  78. webMessageHandler_->SendEvent(E_WEBMESSAGE, eventData);
  79. bool deferred = eventData[P_DEFERRED].GetBool();
  80. if (!deferred)
  81. {
  82. if (!queryHandled_)
  83. {
  84. currentCallback_ = nullptr;
  85. ATOMIC_LOGERROR("WebMessageHandlerPrivate::OnQuery - WebQuery was not handled");
  86. return false;
  87. }
  88. }
  89. else
  90. {
  91. DeferredWebMessage* deferred = new DeferredWebMessage();
  92. deferred->browser_ = browser;
  93. deferred->frame_ = frame;
  94. deferred->queryID_ = query_id;
  95. deferred->request_ = request_;
  96. deferred->persistent_ = persistent;
  97. deferred->callback_ = currentCallback_;
  98. deferredWebMessages_.Push(deferred);
  99. }
  100. currentCallback_ = nullptr;
  101. return true;
  102. }
  103. void HandleDeferredResponse(double queryID, bool success, const String& response)
  104. {
  105. List<DeferredWebMessage*>::Iterator itr = deferredWebMessages_.Begin();
  106. while (itr != deferredWebMessages_.End())
  107. {
  108. if ((*itr)->queryID_ == queryID)
  109. break;
  110. itr++;
  111. }
  112. if (itr == deferredWebMessages_.End())
  113. {
  114. ATOMIC_LOGERRORF("WebMessageHandlerPrivate::HandleDeferredResponse - unable to find queryid: %d", queryID);
  115. return;
  116. }
  117. if (success)
  118. {
  119. (*itr)->callback_->Success(CefString(response.CString()));
  120. }
  121. else
  122. {
  123. (*itr)->callback_->Failure(0, CefString(response.CString()));
  124. }
  125. DeferredWebMessage* ptr = (*itr);
  126. deferredWebMessages_.Erase(itr);
  127. delete ptr;
  128. }
  129. private:
  130. CefRefPtr<Callback> currentCallback_;
  131. bool queryHandled_;
  132. struct DeferredWebMessage
  133. {
  134. CefRefPtr<CefBrowser> browser_;
  135. CefRefPtr<CefFrame> frame_;
  136. int64 queryID_;
  137. String request_;
  138. bool persistent_;
  139. CefRefPtr<Callback> callback_;
  140. };
  141. List<DeferredWebMessage*> deferredWebMessages_;
  142. WeakPtr<WebMessageHandler> webMessageHandler_;
  143. };
  144. WebMessageHandler::WebMessageHandler(Context* context) : Object(context)
  145. {
  146. d_ = new WebMessageHandlerPrivate(this);
  147. }
  148. WebMessageHandler::~WebMessageHandler()
  149. {
  150. delete d_;
  151. d_ = nullptr;
  152. }
  153. void WebMessageHandler::HandleDeferredResponse(double queryID, bool success, const String& response)
  154. {
  155. d_->HandleDeferredResponse(queryID, success, response);
  156. }
  157. void WebMessageHandler::Success(const String& response)
  158. {
  159. d_->Success(response);
  160. }
  161. void WebMessageHandler::Failure(int errorCode, const String& errorMessage)
  162. {
  163. d_->Failure(errorCode, errorMessage);
  164. }
  165. }