WebMessageHandler.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 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. 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. queryHandled_ = false;
  76. webMessageHandler_->SendEvent(E_WEBMESSAGE, eventData);
  77. currentCallback_ = nullptr;
  78. if (!queryHandled_)
  79. {
  80. LOGERROR("WebMessageHandlerPrivate::OnQuery - WebQuery was not handled");
  81. return false;
  82. }
  83. return true;
  84. }
  85. private:
  86. CefRefPtr<Callback> currentCallback_;
  87. bool queryHandled_;
  88. WeakPtr<WebMessageHandler> webMessageHandler_;
  89. };
  90. WebMessageHandler::WebMessageHandler(Context* context) : Object(context)
  91. {
  92. d_ = new WebMessageHandlerPrivate(this);
  93. }
  94. WebMessageHandler::~WebMessageHandler()
  95. {
  96. delete d_;
  97. d_ = nullptr;
  98. }
  99. void WebMessageHandler::Success(const String& response)
  100. {
  101. d_->Success(response);
  102. }
  103. void WebMessageHandler::Failure(int errorCode, const String& errorMessage)
  104. {
  105. d_->Failure(errorCode, errorMessage);
  106. }
  107. }