2011-03-24 21:36:47 +01:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "cefclient/osrplugin.h"
|
2012-05-16 18:56:38 +02:00
|
|
|
#include "cefclient/osrenderer.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
|
|
|
|
#include <windows.h>
|
2011-03-24 21:36:47 +01:00
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#include <math.h>
|
2012-01-10 00:46:23 +01:00
|
|
|
#include <gl/gl.h>
|
|
|
|
#include <gl/glu.h>
|
2011-03-24 21:36:47 +01:00
|
|
|
#include <sstream>
|
2012-01-10 00:46:23 +01:00
|
|
|
#include <string>
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_frame.h"
|
|
|
|
#include "cefclient/cefclient.h"
|
|
|
|
#include "cefclient/client_popup_handler.h"
|
|
|
|
#include "cefclient/resource.h"
|
|
|
|
#include "cefclient/resource_util.h"
|
|
|
|
#include "cefclient/string_util.h"
|
|
|
|
#include "cefclient/util.h"
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
// Initialized in NP_Initialize.
|
|
|
|
NPNetscapeFuncs* g_osrbrowser = NULL;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowser> g_offscreenBrowser;
|
|
|
|
|
2011-10-21 21:35:19 +02:00
|
|
|
// If set to true alpha transparency will be used.
|
|
|
|
bool g_offscreenTransparent = false;
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Class holding pointers for the client plugin window.
|
2012-01-10 00:46:23 +01:00
|
|
|
class ClientPlugin {
|
|
|
|
public:
|
2012-05-16 18:56:38 +02:00
|
|
|
ClientPlugin(bool transparent)
|
|
|
|
: renderer(transparent),
|
|
|
|
hWnd(NULL),
|
|
|
|
hDC(NULL),
|
|
|
|
hRC(NULL) {
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
ClientOSRenderer renderer;
|
2011-03-24 21:36:47 +01:00
|
|
|
HWND hWnd;
|
|
|
|
HDC hDC;
|
|
|
|
HGLRC hRC;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handler for off-screen rendering windows.
|
2011-05-20 16:42:25 +02:00
|
|
|
class ClientOSRHandler : public CefClient,
|
|
|
|
public CefLifeSpanHandler,
|
|
|
|
public CefLoadHandler,
|
2011-10-21 21:35:19 +02:00
|
|
|
public CefRequestHandler,
|
2011-05-20 16:42:25 +02:00
|
|
|
public CefDisplayHandler,
|
2012-01-10 00:46:23 +01:00
|
|
|
public CefRenderHandler {
|
|
|
|
public:
|
|
|
|
explicit ClientOSRHandler(ClientPlugin* plugin)
|
2012-08-16 21:21:47 +02:00
|
|
|
: plugin_(plugin),
|
|
|
|
want_pixel_value_(false) {
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
~ClientOSRHandler() {
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2012-08-15 01:33:57 +02:00
|
|
|
// Called immediately before the plugin is destroyed.
|
|
|
|
void Detach() {
|
|
|
|
plugin_ = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-16 21:21:47 +02:00
|
|
|
// Retrieve the pixel value at the requested coordinate the next time that
|
|
|
|
// the view is painted.
|
|
|
|
void WantPixelValue(int x, int y) {
|
|
|
|
if (!want_pixel_value_) {
|
|
|
|
want_pixel_value_ = true;
|
|
|
|
|
|
|
|
// Trigger a call to OnPaint.
|
|
|
|
if (g_offscreenBrowser.get())
|
|
|
|
g_offscreenBrowser->Invalidate(CefRect(0,0,1,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
coord_x_ = x;
|
|
|
|
coord_y_ = y;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
// CefClient methods
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE {
|
|
|
|
return this;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
// CefLifeSpanHandler methods
|
|
|
|
|
|
|
|
virtual bool OnBeforePopup(CefRefPtr<CefBrowser> parentBrowser,
|
|
|
|
const CefPopupFeatures& popupFeatures,
|
|
|
|
CefWindowInfo& windowInfo,
|
|
|
|
const CefString& url,
|
|
|
|
CefRefPtr<CefClient>& client,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefBrowserSettings& settings) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
windowInfo.m_bWindowRenderingDisabled = TRUE;
|
|
|
|
client = new ClientPopupHandler(g_offscreenBrowser);
|
|
|
|
return false;
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
g_offscreenBrowser = browser;
|
2012-05-16 18:56:38 +02:00
|
|
|
|
|
|
|
// Set the off-screen browser size to match the plugin window size.
|
|
|
|
RECT clientRect;
|
|
|
|
::GetClientRect(plugin_->hWnd, &clientRect);
|
|
|
|
g_offscreenBrowser->SetSize(PET_VIEW, clientRect.right, clientRect.bottom);
|
2011-05-20 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE {
|
2011-05-20 16:42:25 +02:00
|
|
|
g_offscreenBrowser = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefLoadHandler methods
|
|
|
|
|
|
|
|
virtual void OnLoadStart(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefFrame> frame) OVERRIDE {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!browser->IsPopup() && frame->IsMain()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// We've just started loading a page
|
|
|
|
SetLoading(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2012-01-10 00:46:23 +01:00
|
|
|
int httpStatusCode) OVERRIDE {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
2011-03-24 21:36:47 +01:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!browser->IsPopup() && frame->IsMain()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
// We've just finished loading a page
|
|
|
|
SetLoading(false);
|
|
|
|
}
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2011-10-21 21:35:19 +02:00
|
|
|
// CefRequestHandler methods
|
|
|
|
|
|
|
|
virtual bool OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefString& redirectUrl,
|
|
|
|
CefRefPtr<CefStreamReader>& resourceStream,
|
|
|
|
CefRefPtr<CefResponse> response,
|
2012-01-10 00:46:23 +01:00
|
|
|
int loadFlags) OVERRIDE {
|
2011-10-21 21:35:19 +02:00
|
|
|
REQUIRE_IO_THREAD();
|
|
|
|
|
|
|
|
std::string url = request->GetURL();
|
2012-01-10 00:46:23 +01:00
|
|
|
if (url == "http://tests/transparency") {
|
2011-10-21 21:35:19 +02:00
|
|
|
resourceStream = GetBinaryResourceReader(IDS_TRANSPARENCY);
|
|
|
|
response->SetMimeType("text/html");
|
|
|
|
response->SetStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
// CefDisplayHandler methods
|
|
|
|
|
|
|
|
virtual void OnNavStateChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool canGoBack,
|
2012-01-10 00:46:23 +01:00
|
|
|
bool canGoForward) OVERRIDE {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Set the "back" and "forward" button state in the HTML.
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "document.getElementById('back').disabled = "
|
|
|
|
<< (canGoBack?"false":"true") << ";";
|
|
|
|
ss << "document.getElementById('forward').disabled = "
|
|
|
|
<< (canGoForward?"false":"true") << ";";
|
|
|
|
AppGetBrowser()->GetMainFrame()->ExecuteJavaScript(ss.str(), "", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnAddressChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2012-01-10 00:46:23 +01:00
|
|
|
const CefString& url) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Set the "url" value in the HTML.
|
|
|
|
std::stringstream ss;
|
|
|
|
std::string urlStr = url;
|
|
|
|
StringReplace(urlStr, "'", "\\'");
|
|
|
|
ss << "document.getElementById('url').value = '" << urlStr.c_str() << "'";
|
|
|
|
AppGetBrowser()->GetMainFrame()->ExecuteJavaScript(ss.str(), "", 0);
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
const CefString& title) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Set the "title" value in the HTML.
|
|
|
|
std::stringstream ss;
|
|
|
|
std::string titleStr = title;
|
|
|
|
StringReplace(titleStr, "'", "\\'");
|
|
|
|
ss << "document.getElementById('title').innerHTML = '" <<
|
|
|
|
titleStr.c_str() << "'";
|
|
|
|
AppGetBrowser()->GetMainFrame()->ExecuteJavaScript(ss.str(), "", 0);
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
// CefRenderHandler methods
|
|
|
|
|
|
|
|
virtual bool GetViewRect(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRect& rect) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-08-15 01:33:57 +02:00
|
|
|
if (!plugin_)
|
|
|
|
return false;
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// The simulated screen and view rectangle are the same. This is necessary
|
|
|
|
// for popup menus to be located and sized inside the view.
|
2012-05-16 18:56:38 +02:00
|
|
|
RECT clientRect;
|
|
|
|
::GetClientRect(plugin_->hWnd, &clientRect);
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
rect.x = rect.y = 0;
|
2012-05-16 18:56:38 +02:00
|
|
|
rect.width = clientRect.right;
|
|
|
|
rect.height = clientRect.bottom;
|
2011-05-20 16:42:25 +02:00
|
|
|
return true;
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
virtual bool GetScreenRect(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRect& rect) OVERRIDE {
|
2011-05-20 16:42:25 +02:00
|
|
|
return GetViewRect(browser, rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool GetScreenPoint(CefRefPtr<CefBrowser> browser,
|
|
|
|
int viewX,
|
|
|
|
int viewY,
|
|
|
|
int& screenX,
|
2012-01-10 00:46:23 +01:00
|
|
|
int& screenY) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-08-15 01:33:57 +02:00
|
|
|
if (!plugin_)
|
|
|
|
return false;
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Convert the point from view coordinates to actual screen coordinates.
|
|
|
|
POINT screen_pt = {viewX, viewY};
|
|
|
|
MapWindowPoints(plugin_->hWnd, HWND_DESKTOP, &screen_pt, 1);
|
|
|
|
screenX = screen_pt.x;
|
|
|
|
screenY = screen_pt.y;
|
2011-05-20 16:42:25 +02:00
|
|
|
return true;
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void OnPopupShow(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
bool show) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
2012-08-15 01:33:57 +02:00
|
|
|
|
|
|
|
if (!plugin_)
|
|
|
|
return;
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin_->renderer.OnPopupShow(browser, show);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void OnPopupSize(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
const CefRect& rect) OVERRIDE {
|
2011-05-20 16:42:25 +02:00
|
|
|
REQUIRE_UI_THREAD();
|
2012-08-15 01:33:57 +02:00
|
|
|
|
|
|
|
if (!plugin_)
|
|
|
|
return;
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin_->renderer.OnPopupSize(browser, rect);
|
2011-05-20 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnPaint(CefRefPtr<CefBrowser> browser,
|
|
|
|
PaintElementType type,
|
2011-11-09 21:09:24 +01:00
|
|
|
const RectList& dirtyRects,
|
2012-01-10 00:46:23 +01:00
|
|
|
const void* buffer) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-08-15 01:33:57 +02:00
|
|
|
if (!plugin_)
|
|
|
|
return;
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
wglMakeCurrent(plugin_->hDC, plugin_->hRC);
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin_->renderer.OnPaint(browser, type, dirtyRects, buffer);
|
2012-08-16 21:21:47 +02:00
|
|
|
plugin_->renderer.Render();
|
|
|
|
|
|
|
|
SwapBuffers(plugin_->hDC);
|
|
|
|
|
|
|
|
if (type == PET_VIEW && want_pixel_value_) {
|
|
|
|
// Retrieve the pixel value at the requested coordinate.
|
|
|
|
int width = plugin_->renderer.GetViewWidth();
|
|
|
|
int height = plugin_->renderer.GetViewHeight();
|
|
|
|
|
|
|
|
if(coord_x_ >= 0 && coord_x_ < width &&
|
|
|
|
coord_y_ >= 0 && coord_y_ < height) {
|
|
|
|
bool transparent = plugin_->renderer.IsTransparent();
|
|
|
|
|
|
|
|
int index = (width * coord_y_ * 4) + (coord_x_ * 4);
|
|
|
|
const unsigned char *pBuf = static_cast<const unsigned char*>(buffer);
|
|
|
|
unsigned char r, g, b, a;
|
|
|
|
|
|
|
|
b = pBuf[index];
|
|
|
|
g = pBuf[index+1];
|
|
|
|
r = pBuf[index+2];
|
|
|
|
if (transparent)
|
|
|
|
a = pBuf[index+3];
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << coord_x_ << "," << coord_y_ << " = R:" << static_cast<int>(r)
|
|
|
|
<< " G:" << static_cast<int>(g) << " B:" << static_cast<int>(b);
|
|
|
|
if (transparent)
|
|
|
|
ss << " A:" << static_cast<int>(a);
|
|
|
|
|
|
|
|
std::string js =
|
|
|
|
"document.getElementById('pixel').innerText = '" + ss.str() + "';";
|
|
|
|
AppGetBrowser()->GetMainFrame()->ExecuteJavaScript(js, "", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
want_pixel_value_ = false;
|
|
|
|
}
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void OnCursorChange(CefRefPtr<CefBrowser> browser,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefCursorHandle cursor) OVERRIDE {
|
2011-03-24 21:36:47 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
|
|
|
|
2012-08-15 01:33:57 +02:00
|
|
|
if (!plugin_)
|
|
|
|
return;
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Change the plugin window's cursor.
|
|
|
|
SetClassLong(plugin_->hWnd, GCL_HCURSOR,
|
|
|
|
static_cast<LONG>(reinterpret_cast<LONG_PTR>(cursor)));
|
|
|
|
SetCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
|
|
|
void SetLoading(bool isLoading) {
|
2011-03-24 21:36:47 +01:00
|
|
|
// Set the "stop" and "reload" button state in the HTML.
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "document.getElementById('stop').disabled = "
|
|
|
|
<< (isLoading?"false":"true") << ";"
|
|
|
|
<< "document.getElementById('reload').disabled = "
|
|
|
|
<< (isLoading?"true":"false") << ";";
|
|
|
|
AppGetBrowser()->GetMainFrame()->ExecuteJavaScript(ss.str(), "", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientPlugin* plugin_;
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2012-08-16 21:21:47 +02:00
|
|
|
// Used for retrieving the pixel value.
|
|
|
|
bool want_pixel_value_;
|
|
|
|
int coord_x_;
|
|
|
|
int coord_y_;
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
// Include the default reference counting implementation.
|
|
|
|
IMPLEMENT_REFCOUNTING(ClientOSRPlugin);
|
2011-03-24 21:36:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Forward declarations of functions included in this code module:
|
|
|
|
LRESULT CALLBACK PluginWndProc(HWND hWnd, UINT message, WPARAM wParam,
|
|
|
|
LPARAM lParam);
|
|
|
|
|
|
|
|
// Enable GL.
|
2012-05-16 18:56:38 +02:00
|
|
|
void EnableGL(ClientPlugin* plugin) {
|
2011-03-24 21:36:47 +01:00
|
|
|
PIXELFORMATDESCRIPTOR pfd;
|
|
|
|
int format;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Get the device context.
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin->hDC = GetDC(plugin->hWnd);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Set the pixel format for the DC.
|
|
|
|
ZeroMemory(&pfd, sizeof(pfd));
|
|
|
|
pfd.nSize = sizeof(pfd);
|
|
|
|
pfd.nVersion = 1;
|
|
|
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
|
|
pfd.iPixelType = PFD_TYPE_RGBA;
|
|
|
|
pfd.cColorBits = 24;
|
|
|
|
pfd.cDepthBits = 16;
|
|
|
|
pfd.iLayerType = PFD_MAIN_PLANE;
|
2012-05-16 18:56:38 +02:00
|
|
|
format = ChoosePixelFormat(plugin->hDC, &pfd);
|
|
|
|
SetPixelFormat(plugin->hDC, format, &pfd);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Create and enable the render context.
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin->hRC = wglCreateContext(plugin->hDC);
|
|
|
|
wglMakeCurrent(plugin->hDC, plugin->hRC);
|
2011-10-21 21:35:19 +02:00
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin->renderer.Initialize();
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable GL.
|
2012-05-16 18:56:38 +02:00
|
|
|
void DisableGL(ClientPlugin* plugin) {
|
|
|
|
plugin->renderer.Cleanup();
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
wglMakeCurrent(NULL, NULL);
|
2012-05-16 18:56:38 +02:00
|
|
|
wglDeleteContext(plugin->hRC);
|
|
|
|
ReleaseDC(plugin->hWnd, plugin->hDC);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size the GL view.
|
2012-01-10 00:46:23 +01:00
|
|
|
void SizeGL(ClientPlugin* plugin, int width, int height) {
|
|
|
|
if (g_offscreenBrowser.get())
|
2011-03-24 21:36:47 +01:00
|
|
|
g_offscreenBrowser->SetSize(PET_VIEW, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
NPError NPP_NewImpl(NPMIMEType plugin_type, NPP instance, uint16 mode,
|
|
|
|
int16 argc, char* argn[], char* argv[],
|
|
|
|
NPSavedData* saved) {
|
|
|
|
if (instance == NULL)
|
|
|
|
return NPERR_INVALID_INSTANCE_ERROR;
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
ClientPlugin* plugin = new ClientPlugin(g_offscreenTransparent);
|
2011-03-24 21:36:47 +01:00
|
|
|
instance->pdata = reinterpret_cast<void*>(plugin);
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
NPError NPP_DestroyImpl(NPP instance, NPSavedData** save) {
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientPlugin* plugin = reinterpret_cast<ClientPlugin*>(instance->pdata);
|
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
if (plugin) {
|
2012-08-15 01:33:57 +02:00
|
|
|
if (g_offscreenBrowser.get()) {
|
|
|
|
CefRefPtr<ClientOSRHandler> handler =
|
|
|
|
static_cast<ClientOSRHandler*>(g_offscreenBrowser->GetClient().get());
|
|
|
|
handler->Detach();
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
if (plugin->hWnd) {
|
2011-03-24 21:36:47 +01:00
|
|
|
DestroyWindow(plugin->hWnd);
|
2012-05-16 18:56:38 +02:00
|
|
|
DisableGL(plugin);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
delete plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
NPError NPP_SetWindowImpl(NPP instance, NPWindow* window_info) {
|
|
|
|
if (instance == NULL)
|
|
|
|
return NPERR_INVALID_INSTANCE_ERROR;
|
|
|
|
|
|
|
|
if (window_info == NULL)
|
|
|
|
return NPERR_GENERIC_ERROR;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
ClientPlugin* plugin = reinterpret_cast<ClientPlugin*>(instance->pdata);
|
2011-03-24 21:36:47 +01:00
|
|
|
HWND parent_hwnd = reinterpret_cast<HWND>(window_info->window);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
if (plugin->hWnd == NULL) {
|
2011-03-24 21:36:47 +01:00
|
|
|
WNDCLASS wc;
|
|
|
|
HINSTANCE hInstance = GetModuleHandle(NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Register the window class.
|
|
|
|
wc.style = CS_OWNDC;
|
|
|
|
wc.lpfnWndProc = PluginWndProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = L"ClientOSRPlugin";
|
|
|
|
RegisterClass(&wc);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Create the main window.
|
|
|
|
plugin->hWnd = CreateWindow(L"ClientOSRPlugin", L"Client OSR Plugin",
|
|
|
|
WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, 0, 0, 0, 0, parent_hwnd, NULL,
|
|
|
|
hInstance, NULL);
|
|
|
|
|
|
|
|
SetWindowLongPtr(plugin->hWnd, GWLP_USERDATA,
|
|
|
|
reinterpret_cast<LONG_PTR>(plugin));
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
// Enable GL drawing for the window.
|
2012-05-16 18:56:38 +02:00
|
|
|
EnableGL(plugin);
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
// Create the off-screen rendering window.
|
|
|
|
CefWindowInfo windowInfo;
|
|
|
|
windowInfo.SetAsOffScreen(plugin->hWnd);
|
2011-10-21 21:35:19 +02:00
|
|
|
if (g_offscreenTransparent)
|
|
|
|
windowInfo.SetTransparentPainting(TRUE);
|
2012-08-15 17:13:11 +02:00
|
|
|
|
|
|
|
CefBrowserSettings settings;
|
|
|
|
AppGetBrowserSettings(settings);
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
CefBrowser::CreateBrowser(windowInfo, new ClientOSRHandler(plugin),
|
|
|
|
"http://www.google.com", settings);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position the plugin window and make sure it's visible.
|
|
|
|
RECT parent_rect;
|
|
|
|
GetClientRect(parent_hwnd, &parent_rect);
|
|
|
|
SetWindowPos(plugin->hWnd, NULL, parent_rect.left, parent_rect.top,
|
|
|
|
parent_rect.right - parent_rect.left,
|
|
|
|
parent_rect.bottom - parent_rect.top, SWP_SHOWWINDOW);
|
|
|
|
|
|
|
|
UpdateWindow(plugin->hWnd);
|
|
|
|
ShowWindow(plugin->hWnd, SW_SHOW);
|
|
|
|
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plugin window procedure.
|
|
|
|
LRESULT CALLBACK PluginWndProc(HWND hWnd, UINT message, WPARAM wParam,
|
2012-01-10 00:46:23 +01:00
|
|
|
LPARAM lParam) {
|
2011-03-24 21:36:47 +01:00
|
|
|
static POINT lastMousePos, curMousePos;
|
|
|
|
static bool mouseRotation = false;
|
|
|
|
static bool mouseTracking = false;
|
|
|
|
|
|
|
|
ClientPlugin* plugin =
|
|
|
|
reinterpret_cast<ClientPlugin*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
switch (message) {
|
2011-03-24 21:36:47 +01:00
|
|
|
case WM_DESTROY:
|
2011-10-19 18:51:21 +02:00
|
|
|
// Explicitly close the offscreen browser and release the reference.
|
|
|
|
g_offscreenBrowser->CloseBrowser();
|
2011-03-24 21:36:47 +01:00
|
|
|
g_offscreenBrowser = NULL;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
case WM_RBUTTONDOWN:
|
|
|
|
SetCapture(hWnd);
|
|
|
|
SetFocus(hWnd);
|
|
|
|
if (wParam & MK_SHIFT) {
|
|
|
|
// Start rotation effect.
|
|
|
|
lastMousePos.x = curMousePos.x = LOWORD(lParam);
|
|
|
|
lastMousePos.y = curMousePos.y = HIWORD(lParam);
|
|
|
|
mouseRotation = true;
|
2012-05-23 19:37:56 +02:00
|
|
|
} else if (wParam & MK_CONTROL) {
|
|
|
|
// Retrieve the pixel value.
|
|
|
|
int x = LOWORD(lParam);
|
|
|
|
int y = HIWORD(lParam);
|
2012-08-16 21:21:47 +02:00
|
|
|
CefRefPtr<ClientOSRHandler> handler =
|
|
|
|
static_cast<ClientOSRHandler*>(g_offscreenBrowser->GetClient().get());
|
|
|
|
handler->WantPixelValue(x, y);
|
2011-03-24 21:36:47 +01:00
|
|
|
} else {
|
|
|
|
if (g_offscreenBrowser.get()) {
|
|
|
|
g_offscreenBrowser->SendMouseClickEvent(LOWORD(lParam), HIWORD(lParam),
|
2012-01-10 00:46:23 +01:00
|
|
|
(message == WM_LBUTTONDOWN?MBT_LEFT:MBT_RIGHT), false, 1);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_LBUTTONUP:
|
|
|
|
case WM_RBUTTONUP:
|
|
|
|
if (GetCapture() == hWnd)
|
|
|
|
ReleaseCapture();
|
|
|
|
if (mouseRotation) {
|
|
|
|
// End rotation effect.
|
|
|
|
mouseRotation = false;
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin->renderer.SetSpin(0, 0);
|
2011-03-24 21:36:47 +01:00
|
|
|
} else {
|
|
|
|
if (g_offscreenBrowser.get()) {
|
|
|
|
g_offscreenBrowser->SendMouseClickEvent(LOWORD(lParam), HIWORD(lParam),
|
2012-01-10 00:46:23 +01:00
|
|
|
(message == WM_LBUTTONUP?MBT_LEFT:MBT_RIGHT), true, 1);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_MOUSEMOVE:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (mouseRotation) {
|
2011-03-24 21:36:47 +01:00
|
|
|
// Apply rotation effect.
|
|
|
|
curMousePos.x = LOWORD(lParam);
|
|
|
|
curMousePos.y = HIWORD(lParam);
|
2012-05-16 18:56:38 +02:00
|
|
|
plugin->renderer.IncrementSpin((curMousePos.x - lastMousePos.x),
|
|
|
|
(curMousePos.y - lastMousePos.y));
|
2011-03-24 21:36:47 +01:00
|
|
|
lastMousePos.x = curMousePos.x;
|
|
|
|
lastMousePos.y = curMousePos.y;
|
|
|
|
} else {
|
|
|
|
if (!mouseTracking) {
|
|
|
|
// Start tracking mouse leave. Required for the WM_MOUSELEAVE event to
|
|
|
|
// be generated.
|
|
|
|
TRACKMOUSEEVENT tme;
|
|
|
|
tme.cbSize = sizeof(TRACKMOUSEEVENT);
|
|
|
|
tme.dwFlags = TME_LEAVE;
|
|
|
|
tme.hwndTrack = hWnd;
|
|
|
|
TrackMouseEvent(&tme);
|
|
|
|
mouseTracking = true;
|
|
|
|
}
|
|
|
|
if (g_offscreenBrowser.get()) {
|
|
|
|
g_offscreenBrowser->SendMouseMoveEvent(LOWORD(lParam), HIWORD(lParam),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_MOUSELEAVE:
|
|
|
|
if (mouseTracking) {
|
|
|
|
// Stop tracking mouse leave.
|
|
|
|
TRACKMOUSEEVENT tme;
|
|
|
|
tme.cbSize = sizeof(TRACKMOUSEEVENT);
|
|
|
|
tme.dwFlags = TME_LEAVE & TME_CANCEL;
|
|
|
|
tme.hwndTrack = hWnd;
|
|
|
|
TrackMouseEvent(&tme);
|
|
|
|
mouseTracking = false;
|
|
|
|
}
|
|
|
|
if (g_offscreenBrowser.get())
|
|
|
|
g_offscreenBrowser->SendMouseMoveEvent(0, 0, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_MOUSEWHEEL:
|
|
|
|
if (g_offscreenBrowser.get()) {
|
|
|
|
g_offscreenBrowser->SendMouseWheelEvent(LOWORD(lParam), HIWORD(lParam),
|
2012-05-16 18:56:38 +02:00
|
|
|
0, GET_WHEEL_DELTA_WPARAM(wParam));
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
case WM_SIZE: {
|
2012-01-10 00:46:23 +01:00
|
|
|
int width = LOWORD(lParam);
|
2011-03-24 21:36:47 +01:00
|
|
|
int height = HIWORD(lParam);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (width > 0 && height > 0)
|
2011-03-24 21:36:47 +01:00
|
|
|
SizeGL(plugin, width, height);
|
2012-01-10 00:46:23 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
case WM_SETFOCUS:
|
|
|
|
case WM_KILLFOCUS:
|
|
|
|
if (g_offscreenBrowser.get())
|
2012-01-10 00:46:23 +01:00
|
|
|
g_offscreenBrowser->SendFocusEvent(message == WM_SETFOCUS);
|
2011-03-24 21:36:47 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_CAPTURECHANGED:
|
|
|
|
case WM_CANCELMODE:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!mouseRotation) {
|
2011-03-24 21:36:47 +01:00
|
|
|
if (g_offscreenBrowser.get())
|
|
|
|
g_offscreenBrowser->SendCaptureLostEvent();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_KEYDOWN:
|
|
|
|
case WM_KEYUP:
|
|
|
|
case WM_SYSKEYDOWN:
|
|
|
|
case WM_SYSKEYUP:
|
|
|
|
case WM_CHAR:
|
|
|
|
case WM_SYSCHAR:
|
|
|
|
case WM_IME_CHAR:
|
|
|
|
if (g_offscreenBrowser.get()) {
|
|
|
|
CefBrowser::KeyType type = KT_CHAR;
|
2012-05-16 18:56:38 +02:00
|
|
|
CefKeyInfo keyInfo;
|
|
|
|
keyInfo.key = wParam;
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
|
|
|
|
type = KT_KEYDOWN;
|
|
|
|
else if (message == WM_KEYUP || message == WM_SYSKEYUP)
|
|
|
|
type = KT_KEYUP;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-03-24 21:36:47 +01:00
|
|
|
if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP ||
|
|
|
|
message == WM_SYSCHAR)
|
2012-05-16 18:56:38 +02:00
|
|
|
keyInfo.sysChar = true;
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
if (message == WM_IME_CHAR)
|
2012-05-16 18:56:38 +02:00
|
|
|
keyInfo.imeChar = true;
|
2011-03-24 21:36:47 +01:00
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
g_offscreenBrowser->SendKeyEvent(type, keyInfo, lParam);
|
2011-03-24 21:36:47 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_PAINT: {
|
|
|
|
PAINTSTRUCT ps;
|
|
|
|
BeginPaint(hWnd, &ps);
|
|
|
|
EndPaint(hWnd, &ps);
|
2012-01-10 00:46:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-24 21:36:47 +01:00
|
|
|
|
|
|
|
case WM_ERASEBKGND:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2011-03-24 21:36:47 +01:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_OSRGetEntryPoints(NPPluginFuncs* pFuncs) {
|
2011-03-24 21:36:47 +01:00
|
|
|
pFuncs->newp = NPP_NewImpl;
|
|
|
|
pFuncs->destroy = NPP_DestroyImpl;
|
|
|
|
pFuncs->setwindow = NPP_SetWindowImpl;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_OSRInitialize(NPNetscapeFuncs* pFuncs) {
|
2011-03-24 21:36:47 +01:00
|
|
|
g_osrbrowser = pFuncs;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
NPError API_CALL NP_OSRShutdown(void) {
|
2011-03-24 21:36:47 +01:00
|
|
|
g_osrbrowser = NULL;
|
|
|
|
return NPERR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefBrowser> GetOffScreenBrowser() {
|
2011-03-24 21:36:47 +01:00
|
|
|
return g_offscreenBrowser;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void SetOffScreenTransparent(bool transparent) {
|
2011-10-21 21:35:19 +02:00
|
|
|
g_offscreenTransparent = transparent;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // OS_WIN
|