diff --git a/cef1/tests/cefclient/clientplugin.cpp b/cef1/tests/cefclient/clientplugin.cpp index cfb14762c..ee9858dd3 100644 --- a/cef1/tests/cefclient/clientplugin.cpp +++ b/cef1/tests/cefclient/clientplugin.cpp @@ -1,57 +1,166 @@ -// Copyright (c) 2008 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. +// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that +// can be found in the LICENSE file. #include "cefclient/clientplugin.h" #if defined(OS_WIN) -// Initialized in NP_Initialize. -NPNetscapeFuncs* g_browser = NULL; +#include namespace { -NPError NPP_ClientNew(NPMIMEType plugin_type, NPP instance, uint16_t mode, - int16_t argc, char* argn[], char* argv[], NPSavedData* saved) { +// Client plugin window implementation. +class ClientPlugin { + public: + ClientPlugin() + : hwnd_(NULL) { + } + + ~ClientPlugin() { + if (IsWindow(hwnd_)) + DestroyWindow(hwnd_); + } + + void Initialize(HWND parent_hwnd) { + if (hwnd_ != NULL) + return; + + HINSTANCE hInstance = GetModuleHandle(NULL); + + static bool class_registered = false; + if (!class_registered) { + // Register the window class. + WNDCLASS wc = {0}; + 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"ClientPlugin"; + RegisterClass(&wc); + + class_registered = true; + } + + // Create the main window. + hwnd_ = CreateWindow(L"ClientPlugin", L"Client Plugin", + WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 0, 0, parent_hwnd, + NULL, hInstance, NULL); + + SetWindowLongPtr(hwnd_, GWLP_USERDATA, reinterpret_cast(this)); + + // Size and display the plugin window. + RECT parent_rect; + GetClientRect(parent_hwnd, &parent_rect); + SetWindowPos(hwnd_, NULL, parent_rect.left, parent_rect.top, + parent_rect.right - parent_rect.left, + parent_rect.bottom - parent_rect.top, SWP_SHOWWINDOW); + } + + void EraseBackground(HDC hdc) { + RECT erase_rect; + GetClipBox(hdc, &erase_rect); + HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0)); + FillRect(hdc, &erase_rect, brush); + DeleteObject(brush); + } + + void Paint(HDC hdc) { + static LPCWSTR text = L"Left click in the green area for a message box!"; + + RECT client_rect; + GetClientRect(hwnd_, &client_rect); + + int old_mode = SetBkMode(hdc, TRANSPARENT); + COLORREF old_color = SetTextColor(hdc, RGB(0, 0, 255)); + + RECT text_rect = client_rect; + DrawText(hdc, text, -1, &text_rect, DT_CENTER | DT_CALCRECT); + + client_rect.top = ((client_rect.bottom - client_rect.top) - + (text_rect.bottom - text_rect.top)) / 2; + DrawText(hdc, text, -1, &client_rect, DT_CENTER); + + SetBkMode(hdc, old_mode); + SetTextColor(hdc, old_color); + } + + HWND GetWindow() const { return hwnd_; } + + // Plugin window procedure. + static LRESULT CALLBACK PluginWndProc(HWND hwnd_, UINT message, WPARAM wParam, + LPARAM lParam) { + ClientPlugin* plugin = + reinterpret_cast(GetWindowLongPtr(hwnd_, GWLP_USERDATA)); + + switch (message) { + case WM_DESTROY: + return 0; + + case WM_LBUTTONDOWN: + MessageBox(plugin->GetWindow(), + L"You clicked on the client plugin!", L"Client Plugin", MB_OK); + return 0; + + case WM_PAINT: { + PAINTSTRUCT ps; + BeginPaint(hwnd_, &ps); + plugin->Paint(ps.hdc); + EndPaint(hwnd_, &ps); + return 0; + } + + case WM_PRINTCLIENT: + plugin->Paint(reinterpret_cast(wParam)); + return 0; + + case WM_ERASEBKGND: + plugin->EraseBackground(reinterpret_cast(wParam)); + return 1; + } + + return DefWindowProc(hwnd_, message, wParam, lParam); + } + + private: + HWND hwnd_; +}; + +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; - ClientPlugin* plugin_impl = new ClientPlugin(mode); - plugin_impl->Initialize(GetModuleHandle(NULL), instance, plugin_type, argc, - argn, argv); - instance->pdata = reinterpret_cast(plugin_impl); - return NPERR_NO_ERROR; -} - -NPError NPP_ClientDestroy(NPP instance, NPSavedData** save) { - ClientPlugin* plugin_impl = reinterpret_cast(instance->pdata); - - if (plugin_impl) { - plugin_impl->Shutdown(); - delete plugin_impl; - } + ClientPlugin* plugin = new ClientPlugin(); + instance->pdata = reinterpret_cast(plugin); return NPERR_NO_ERROR; } -NPError NPP_ClientSetWindow(NPP instance, NPWindow* window_info) { +NPError NPP_DestroyImpl(NPP instance, NPSavedData** save) { + ClientPlugin* plugin = reinterpret_cast(instance->pdata); + if (plugin) + 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; - ClientPlugin* plugin_impl = reinterpret_cast(instance->pdata); - - if (plugin_impl == NULL) - return NPERR_GENERIC_ERROR; - - HWND window_handle = reinterpret_cast(window_info->window); - if (!plugin_impl->SetWindow(window_handle)) { - delete plugin_impl; - return NPERR_GENERIC_ERROR; - } + ClientPlugin* plugin = reinterpret_cast(instance->pdata); + HWND parent_hwnd = reinterpret_cast(window_info->window); + plugin->Initialize(parent_hwnd); return NPERR_NO_ERROR; } @@ -59,131 +168,18 @@ NPError NPP_ClientSetWindow(NPP instance, NPWindow* window_info) { } // namespace NPError API_CALL NP_ClientGetEntryPoints(NPPluginFuncs* pFuncs) { - pFuncs->newp = NPP_ClientNew; - pFuncs->destroy = NPP_ClientDestroy; - pFuncs->setwindow = NPP_ClientSetWindow; + pFuncs->newp = NPP_NewImpl; + pFuncs->destroy = NPP_DestroyImpl; + pFuncs->setwindow = NPP_SetWindowImpl; return NPERR_NO_ERROR; } NPError API_CALL NP_ClientInitialize(NPNetscapeFuncs* pFuncs) { - g_browser = pFuncs; return NPERR_NO_ERROR; } NPError API_CALL NP_ClientShutdown(void) { - g_browser = NULL; return NPERR_NO_ERROR; } - -// ClientPlugin Implementation - -ClientPlugin::ClientPlugin(int16 mode) - : mode_(mode) { -} - -ClientPlugin::~ClientPlugin() { -} - -bool ClientPlugin::Initialize(HINSTANCE module_handle, NPP instance, - NPMIMEType mime_type, int16 argc, char* argn[], - char* argv[]) { - RefreshDisplay(); - return true; -} - -bool ClientPlugin::SetWindow(HWND parent_window) { - if (!::IsWindow(parent_window)) { - // No window created yet. Ignore this call. - if (!IsWindow()) - return true; - // Parent window has been destroyed. - Shutdown(); - return true; - } - - RECT parent_rect; - - if (IsWindow()) { - ::GetClientRect(parent_window, &parent_rect); - SetWindowPos(NULL, &parent_rect, SWP_SHOWWINDOW); - return true; - } - // First time in -- no window created by plugin yet. - ::GetClientRect(parent_window, &parent_rect); - Create(parent_window, parent_rect, NULL, WS_CHILD | WS_BORDER); - - UpdateWindow(); - ShowWindow(SW_SHOW); - - return true; -} - -void ClientPlugin::Shutdown() { - if (IsWindow()) { - DestroyWindow(); - } -} - -LRESULT ClientPlugin::OnPaint(UINT message, WPARAM wparam, LPARAM lparam, - BOOL& handled) { - PAINTSTRUCT paint_struct; - BeginPaint(&paint_struct); - Paint(paint_struct.hdc); - EndPaint(&paint_struct); - return 0; -} - -// PrintClient is necessary to support off-screen rendering. -LRESULT ClientPlugin::OnPrintClient(UINT message, WPARAM wparam, LPARAM lparam, - BOOL& handled) { - Paint(reinterpret_cast(wparam)); - return 0; -} - -LRESULT ClientPlugin::OnEraseBackGround(UINT message, WPARAM wparam, - LPARAM lparam, BOOL& handled) { - HDC paint_device_context = reinterpret_cast(wparam); - RECT erase_rect; - GetClipBox(paint_device_context, &erase_rect); - HBRUSH brush = CreateSolidBrush(RGB(0, 255, 0)); - FillRect(paint_device_context, &erase_rect, brush); - DeleteObject(brush); - return 1; -} - -LRESULT ClientPlugin::OnLButtonDown(UINT message, WPARAM wparam, LPARAM lparam, - BOOL& handled) { - MessageBox(L"You clicked on the client plugin!", L"Client Plugin", MB_OK); - return 0; -} - -void ClientPlugin::RefreshDisplay() { - if (!IsWindow()) - return; - - InvalidateRect(NULL, TRUE); - UpdateWindow(); -} - -void ClientPlugin::Paint(HDC hdc) { - static LPCWSTR text = L"Left click in the green area for a message box!"; - - RECT client_rect; - GetClientRect(&client_rect); - - int old_mode = SetBkMode(hdc, TRANSPARENT); - COLORREF old_color = SetTextColor(hdc, RGB(0, 0, 255)); - - RECT text_rect = client_rect; - DrawText(hdc, text, -1, &text_rect, DT_CENTER | DT_CALCRECT); - - client_rect.top = ((client_rect.bottom - client_rect.top) - - (text_rect.bottom - text_rect.top)) / 2; - DrawText(hdc, text, -1, &client_rect, DT_CENTER); - - SetBkMode(hdc, old_mode); - SetTextColor(hdc, old_color); -} - #endif // OS_WIN diff --git a/cef1/tests/cefclient/clientplugin.h b/cef1/tests/cefclient/clientplugin.h index a324ee36c..27b5de762 100644 --- a/cef1/tests/cefclient/clientplugin.h +++ b/cef1/tests/cefclient/clientplugin.h @@ -1,10 +1,6 @@ -// Copyright (c) 2008 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. - -// Portions of this implementation are borrowed from webkit\default_plugin\ -// plugin_impl.h +// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that +// can be found in the LICENSE file. #ifndef CEF_TESTS_CEFCLIENT_CLIENTPLUGIN_H_ #define CEF_TESTS_CEFCLIENT_CLIENTPLUGIN_H_ @@ -14,90 +10,12 @@ #if defined(OS_WIN) -#include // NOLINT(build/include_order) -#include // NOLINT(build/include_order) #include "include/cef_nplugin.h" -extern NPNetscapeFuncs* g_browser; - NPError API_CALL NP_ClientGetEntryPoints(NPPluginFuncs* pFuncs); NPError API_CALL NP_ClientInitialize(NPNetscapeFuncs* pFuncs); NPError API_CALL NP_ClientShutdown(void); - -// Provides the client plugin functionality. -class ClientPlugin : public CWindowImpl { - public: - // mode is the plugin instantiation mode, i.e. whether it is a full - // page plugin (NP_FULL) or an embedded plugin (NP_EMBED) - explicit ClientPlugin(int16 mode); - virtual ~ClientPlugin(); - - BEGIN_MSG_MAP(ClientPlugin) - MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackGround) - MESSAGE_HANDLER(WM_PAINT, OnPaint) - MESSAGE_HANDLER(WM_PRINTCLIENT, OnPrintClient) - MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown) - END_MSG_MAP() - - // Initializes the plugin with the instance information, mime type - // and the list of parameters passed down to the plugin from the webpage. - // - // Parameters: - // module_handle - // The handle to the dll in which this object is instantiated. - // instance - // The plugins opaque instance handle. - // mime_type - // Identifies the third party plugin which would be eventually installed. - // argc - // Indicates the count of arguments passed in from the webpage. - // argv - // Pointer to the arguments. - // Returns true on success. - bool Initialize(HINSTANCE module_handle, NPP instance, NPMIMEType mime_type, - int16 argc, char* argn[], char* argv[]); - - // Displays the default plugin UI. - // - // Parameters: - // parent_window - // Handle to the parent window. - bool SetWindow(HWND parent_window); - - // Destroys the install dialog and the plugin window. - void Shutdown(); - - HWND window() const { return m_hWnd; } - - // Getter for the NPP instance member. - const NPP instance() const { - return instance_; - } - - protected: - // Window message handlers. - LRESULT OnPaint(UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled); - LRESULT OnPrintClient(UINT message, WPARAM wparam, LPARAM lparam, - BOOL& handled); - LRESULT OnEraseBackGround(UINT message, WPARAM wparam, LPARAM lparam, - BOOL& handled); - LRESULT OnLButtonDown(UINT message, WPARAM wparam, LPARAM lparam, - BOOL& handled); - - // Enables the plugin window if required and initiates an update of the - // the plugin window. - void RefreshDisplay(); - - void Paint(HDC hdc); - - private: - // The plugins opaque instance handle - NPP instance_; - // The plugin instantiation mode (NP_FULL or NP_EMBED) - int16 mode_; -}; - #endif // OS_WIN #endif // CEF_TESTS_CEFCLIENT_CLIENTPLUGIN_H_ diff --git a/cef1/tests/cefclient/osrplugin.cpp b/cef1/tests/cefclient/osrplugin.cpp index f1d11ae26..d1c4568e2 100644 --- a/cef1/tests/cefclient/osrplugin.cpp +++ b/cef1/tests/cefclient/osrplugin.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Embedded Framework Authors. +// Copyright (c) 2013 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. @@ -18,6 +18,7 @@ #include "include/cef_browser.h" #include "include/cef_frame.h" #include "cefclient/cefclient.h" +#include "cefclient/client_handler.h" #include "cefclient/client_popup_handler.h" #include "cefclient/resource.h" #include "cefclient/resource_util.h" @@ -165,14 +166,12 @@ class ClientOSRHandler : public CefClient, int loadFlags) OVERRIDE { REQUIRE_IO_THREAD(); - std::string url = request->GetURL(); - if (url == "http://tests/transparency") { - resourceStream = GetBinaryResourceReader(IDS_TRANSPARENCY); - response->SetMimeType("text/html"); - response->SetStatus(200); - } - - return false; + // Proxy the request to the main ClientHandler. + CefRefPtr main_browser = AppGetBrowser(); + CefRefPtr handler = + static_cast(main_browser->GetClient().get()); + return handler->OnBeforeResourceLoad(browser, request, redirectUrl, + resourceStream, response, loadFlags); } // CefDisplayHandler methods diff --git a/cef1/tests/cefclient/plugin_test.cpp b/cef1/tests/cefclient/plugin_test.cpp index 4bb3e00b9..7c695dc5e 100644 --- a/cef1/tests/cefclient/plugin_test.cpp +++ b/cef1/tests/cefclient/plugin_test.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2008-2009 The Chromium Embedded Framework Authors. All rights +// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. diff --git a/cef1/tools/distrib/win/README.txt b/cef1/tools/distrib/win/README.txt index 08d5b7a41..770bfe91c 100644 --- a/cef1/tools/distrib/win/README.txt +++ b/cef1/tools/distrib/win/README.txt @@ -47,8 +47,6 @@ USAGE Visual Studio 2010: Open the cefclient2010.sln solution and build. Visual Studio 2008: Open the cefclient2008.sln solution and build. - * If using VS2008 Express Edition add atlthunk.lib to the cefclient - Configuration Properties > Linker > Input > Additional Dependencies Visual Studio 2005: Open the cefclient2005.sln solution and build. Please visit the CEF Website for additional usage information.