Advertisement
Guest User

cef_handler.cpp

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.36 KB | None | 0 0
  1. #include "web/cef_handler.h"
  2.  
  3. #include <sstream>
  4. #include <string>
  5.  
  6. #include "include/base/cef_bind.h"
  7. #include "include/cef_app.h"
  8. #include "include/views/cef_browser_view.h"
  9. #include "include/views/cef_window.h"
  10. #include "include/wrapper/cef_closure_task.h"
  11. #include "include/wrapper/cef_helpers.h"
  12.  
  13. #include "console/console.h"
  14.  
  15. GFX_ImplementTextureProfile(GFXWebTextureProfile,
  16.   GFXTextureProfile::DiffuseMap,
  17.   GFXTextureProfile::NoMipmap | GFXTextureProfile::Dynamic,
  18.   GFXTextureProfile::NONE);
  19.  
  20. namespace {
  21.  
  22. CefHandler* g_instance = NULL;
  23.  
  24. }  // namespace
  25.  
  26. CefHandler::CefHandler()
  27.     : is_closing_(false) {
  28.   DCHECK(!g_instance);
  29.   g_instance = this;
  30. }
  31.  
  32. CefHandler::~CefHandler() {
  33.   g_instance = NULL;
  34.   mTextureObject.free();
  35. }
  36.  
  37. // static
  38. CefHandler* CefHandler::GetInstance() {
  39.   return g_instance;
  40. }
  41.  
  42. void CefHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
  43.                                   const CefString& title) {
  44.   CEF_REQUIRE_UI_THREAD();
  45.  
  46.   // Set the title of the window using platform APIs.
  47.   PlatformTitleChange(browser, title);
  48. }
  49.  
  50. void CefHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
  51.   CEF_REQUIRE_UI_THREAD();
  52.  
  53.   // Create texture to write to
  54.   mTextureObject.set(mTextureSize, mTextureSize, GFXFormatR8G8B8X8, &GFXWebTextureProfile, avar("%s() -  (line %d)", __FUNCTION__, __LINE__), 0);
  55.  
  56.   // Add to the list of existing browsers.
  57.   browser_list_.push_back(browser);
  58. }
  59.  
  60. bool CefHandler::DoClose(CefRefPtr<CefBrowser> browser) {
  61.   CEF_REQUIRE_UI_THREAD();
  62.  
  63.   // Closing the main window requires special handling. See the DoClose()
  64.   // documentation in the CEF header for a detailed destription of this
  65.   // process.
  66.   if (browser_list_.size() == 1) {
  67.     // Set a flag to indicate that the window close should be allowed.
  68.     is_closing_ = true;
  69.   }
  70.  
  71.   // Allow the close. For windowed browsers this will result in the OS close
  72.   // event being sent.
  73.   return false;
  74. }
  75.  
  76. void CefHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
  77.   CEF_REQUIRE_UI_THREAD();
  78.  
  79.   // Remove from the list of existing browsers.
  80.   BrowserList::iterator bit = browser_list_.begin();
  81.   for (; bit != browser_list_.end(); ++bit) {
  82.     if ((*bit)->IsSame(browser)) {
  83.       browser_list_.erase(bit);
  84.       break;
  85.     }
  86.   }
  87.  
  88.   if (browser_list_.empty()) {
  89.     // All browser windows have closed. Quit the application message loop.
  90.     CefQuitMessageLoop();
  91.   }
  92. }
  93.  
  94. void CefHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
  95.                                 CefRefPtr<CefFrame> frame,
  96.                                 ErrorCode errorCode,
  97.                                 const CefString& errorText,
  98.                                 const CefString& failedUrl) {
  99.   CEF_REQUIRE_UI_THREAD();
  100.  
  101.   // Don't display an error for downloaded files.
  102.   if (errorCode == ERR_ABORTED)
  103.     return;
  104.  
  105.   // Display a load error message.
  106.   std::stringstream ss;
  107.   ss << "<html><body bgcolor=\"white\">"
  108.         "<h2>Failed to load URL "
  109.      << std::string(failedUrl) << " with error " << std::string(errorText)
  110.      << " (" << errorCode << ").</h2></body></html>";
  111.   frame->LoadString(ss.str(), failedUrl);
  112. }
  113.  
  114. void CefHandler::CloseAllBrowsers(bool force_close) {
  115.   if (!CefCurrentlyOn(TID_UI)) {
  116.     // Execute on the UI thread.
  117.     CefPostTask(TID_UI, base::Bind(&CefHandler::CloseAllBrowsers, this,
  118.                                    force_close));
  119.     return;
  120.   }
  121.  
  122.   if (browser_list_.empty())
  123.     return;
  124.  
  125.   BrowserList::const_iterator it = browser_list_.begin();
  126.   for (; it != browser_list_.end(); ++it)
  127.     (*it)->GetHost()->CloseBrowser(force_close);
  128. }
  129.  
  130. //---------------------------------------------------------------------------------------------------------------
  131. // CefRenderHandler methods, see http://magpcss.org/ceforum/apidocs3/projects/(default)/CefRenderHandler.html
  132.  
  133. // Called to retrieve the root window rectangle in screen coordinates. Return true if the rectangle was provided.
  134. bool CefHandler::GetRootScreenRect(CefRefPtr<CefBrowser> browser, CefRect& rect) {
  135.   Con::printf("!!! GetRootScreenRect");
  136.   rect = CefRect(0, 0, mTextureSize, mTextureSize);
  137.   return true;
  138. }
  139.  
  140. // Called to retrieve the view rectangle which is relative to screen coordinates. Return true if the rectangle was provided.
  141. bool CefHandler::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect) {
  142.   Con::printf("!!! GetViewRect");
  143.   rect = CefRect(0, 0, mTextureSize, mTextureSize);
  144.   return true;
  145. }
  146.  
  147. // Called to retrieve the translation from view coordinates to actual screen coordinates. Return true if the screen coordinates were provided.
  148. bool CefHandler::GetScreenPoint(CefRefPtr<CefBrowser> browser, int viewX, int viewY, int& screenX, int& screenY) {
  149.   Con::printf("!!! GetScreenPoint");
  150.   return false;
  151. }
  152.  
  153. // Called to allow the client to fill in the CefScreenInfo object with appropriate values. Return true if the |screen_info| structure has been modified.
  154. // If the screen info rectangle is left empty the rectangle from GetViewRect will be used. If the rectangle is still empty or invalid popups may not be drawn correctly.
  155. bool CefHandler::GetScreenInfo(CefRefPtr<CefBrowser> browser, CefScreenInfo& screen_info) {
  156.   Con::printf("!!! GetScreenInfo");
  157.   return false;
  158. }
  159.  
  160.  
  161. // Called when the browser wants to show or hide the popup widget. The popup should be shown if |show| is true and hidden if |show| is false.
  162. void CefHandler::OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) {
  163.   Con::printf("!!! OnPopupShow");
  164. }
  165.  
  166. // Called when the browser wants to move or resize the popup widget. |rect| contains the new location and size in view coordinates.
  167. void CefHandler::OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) {
  168.   Con::printf("!!! OnPopupSize");
  169. }
  170.  
  171. // Called when an element should be painted. Pixel values passed to this method are scaled relative to view coordinates based on the value of CefScreenInfo.device_scale_factor
  172. // returned from GetScreenInfo.
  173. // |type| indicates whether the element is the view or the popup widget.
  174. // |buffer| contains the pixel data for the whole image.
  175. // |dirtyRects| contains the set of rectangles in pixel coordinates that need to be repainted.
  176. // |buffer| will be |width|*|height|*4 bytes in size and represents a BGRA image with an upper-left origin.
  177. void CefHandler::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList& dirtyRects, const void* buffer, int width, int height) {
  178.   Con::printf("!!! OnPaint");
  179. }
  180.  
  181. // Called when the browser's cursor has changed. If |type| is CT_CUSTOM then |custom_cursor_info| will be populated with the custom cursor information.
  182. void CefHandler::OnCursorChange(CefRefPtr<CefBrowser> browser, CefCursorHandle cursor, CefRenderHandler::CursorType type, const CefCursorInfo& custom_cursor_info) {
  183.   Con::printf("!!! OnCursorChange");
  184. }
  185.  
  186. // Called when the user starts dragging content in the web view. Contextual information about the dragged content is supplied by |drag_data|.
  187. // (|x|, |y|) is the drag start location in screen coordinates. OS APIs that run a system message loop may be used within the StartDragging call.
  188. // Return false to abort the drag operation. Don't call any of CefBrowserHost::DragSource*Ended* methods after returning false.
  189. // Return true to handle the drag operation.
  190. // Call CefBrowserHost::DragSourceEndedAt and DragSourceSystemDragEnded either synchronously or asynchronously to inform the web view that the drag operation has ended.
  191. bool CefHandler::StartDragging(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDragData> drag_data, CefRenderHandler::DragOperationsMask allowed_ops, int x, int y) {
  192.   Con::printf("!!! StartDragging");
  193.   return false;
  194. }
  195.  
  196. // Called when the web view wants to update the mouse cursor during a drag & drop operation. |operation| describes the allowed operation (none, move, copy, link).
  197. void CefHandler::UpdateDragCursor(CefRefPtr<CefBrowser> browser, CefRenderHandler::DragOperation operation) {
  198.   Con::printf("!!! UpdateDragCursor");
  199. }
  200.  
  201. // Called when the IME composition range has changed. |selected_range| is the range of characters that have been selected. |character_bounds| is the bounds of each character in view coordinates.
  202. void CefHandler::OnImeCompositionRangeChanged(CefRefPtr<CefBrowser> browser, const CefRange& selection_range, const CefRenderHandler::RectList& character_bounds) {
  203.   Con::printf("!!! OnImeCompositionRangeChanged");
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement