Events draft and other useless stuffs

import/tags/kmeleon75
boisso 8 years ago
parent f5025c6829
commit ac748ea156
  1. 178
      k-meleon/JSBridge/jscomp.cpp
  2. 7
      k-meleon/JSBridge/jscomp.h
  3. 37
      k-meleon/JSBridge/jsplugin.cpp
  4. 14
      k-meleon/JSBridge/nsIJSBridge.idl

@ -9,12 +9,45 @@
#include <nsIWebBrowserChrome.h>
#include <nsIEmbeddingSiteWindow.h>
#include <nsComponentManagerUtils.h>
#include <nsISimpleEnumerator.h>
#include <nsMemory.h>
#include <jsapi.h>
#include "jscomp.h"
#include "..\kmeleon_plugin.h"
class CWindowEnumerator : public nsISimpleEnumerator {
HWND* list;
int count;
int pos;
public:
CWindowEnumerator(HWND* hWnd, int count)
{
list = hWnd;
count = count;
}
virtual ~CWindowEnumerator()
{
if (list) delete [] list;
}
NS_IMETHOD GetNext(nsISupports **retval) {
}
NS_IMETHOD HasMoreElements(bool *retval) {
*retval = !(pos >= count);
return NS_OK;
}
NS_DECL_ISUPPORTS
};
extern kmeleonPlugin kPlugin;
extern CCmdList* cmdList;
CCmdList* GetCmdList() {
@ -212,13 +245,10 @@ NS_IMETHODIMP CJSBridge::GetCmdList(PRUint32 *length, kmICommand ***list)
return NS_OK;
}
NS_IMETHODIMP CJSBridge::RegisterCmd(const char * name, const char * desc,
kmICommandFunction *command, JS::HandleValue icon,
kmICallback *enabled, kmICallback *checked, JSContext* cx, int32_t *_retval)
nsresult GetPathAndRect(JSContext* cx, JS::HandleValue& icon, char** path, RECT* rect)
{
if (!kPlugin.kFuncs) return NS_ERROR_NOT_INITIALIZED;
char* iconPath = nullptr;
UINT id = 0;
*path = nullptr;
rect->bottom = rect->top = rect->left = rect->right = 0;
if (icon.isObject()) {
JS::RootedObject obj(cx);
obj = icon.toObjectOrNull();
@ -239,29 +269,57 @@ NS_IMETHODIMP CJSBridge::RegisterCmd(const char * name, const char * desc,
if (!JS_GetProperty(cx, obj, "right", &vr))
return NS_ERROR_INVALID_ARG;
RECT rect = {vl.toInt32(), vt.toInt32(), vr.toInt32(), vb.toInt32()};
id = kPlugin.kFuncs->RegisterCmd(name, desc, nullptr);
iconPath = JS_EncodeString(cx, vpath.toString());
kPlugin.kFuncs->SetCmdIcon(name, iconPath, &rect, nullptr, nullptr, nullptr, nullptr);
RECT r = {vl.toInt32(), vt.toInt32(), vr.toInt32(), vb.toInt32()};
*rect = r;
*path = JS_EncodeString(cx, vpath.toString());
} else {
if (icon.isString())
iconPath = JS_EncodeString(cx, icon.toString());
id = kPlugin.kFuncs->RegisterCmd(name, desc, iconPath);
*path = JS_EncodeString(cx, icon.toString());
}
return NS_OK;
}
NS_IMETHODIMP CJSBridge::RegisterCmd(const char * name, const char * desc,
kmICommandFunction *command, JS::HandleValue icon,
kmICallback *enabled, kmICallback *checked, JSContext* cx, int32_t *_retval)
{
if (!kPlugin.kFuncs) return NS_ERROR_NOT_INITIALIZED;
UINT id;
RECT rect;
char* iconPath = nullptr;
nsresult rv = GetPathAndRect(cx, icon, &iconPath, &rect);
NS_ENSURE_SUCCESS(rv, rv);
if (rect.bottom != 0 || rect.right != 0) {
id = kPlugin.kFuncs->RegisterCmd(name, desc, nullptr);
kPlugin.kFuncs->SetCmdIcon(name, iconPath, &rect, nullptr, nullptr, nullptr, nullptr);
} else
id = kPlugin.kFuncs->RegisterCmd(name, desc, iconPath);
::GetCmdList()->Add(id, command);
if (iconPath) JS_free(cx, iconPath);
return NS_OK;
}
NS_IMETHODIMP CJSBridge::AddButton(const char * name, const char *command, const char* menu)
NS_IMETHODIMP CJSBridge::SetCmdIcon(const char * name, JS::HandleValue icon, JSContext* cx)
{
RECT rect;
char* iconPath = nullptr;
nsresult rv = GetPathAndRect(cx, icon, &iconPath, &rect);
kPlugin.kFuncs->SetCmdIcon(name, iconPath, &rect, nullptr, nullptr, nullptr, nullptr);
if (iconPath) JS_free(cx, iconPath);
return NS_OK;
}
NS_IMETHODIMP CJSBridge::AddButton(const char * name, const char * command, const char * menu)
{
kPlugin.kFuncs->AddButton(name, command, menu);
return NS_OK;
}
NS_IMETHODIMP CJSBridge::RemoveButton(const char * name, const char *command)
NS_IMETHODIMP CJSBridge::RemoveButton(const char * name, const char * command)
{
kPlugin.kFuncs->RemoveButton(name, command);
return NS_OK;
@ -308,3 +366,93 @@ NS_IMETHODIMP CJSBridge::CreateCallbackButton(kmICommandFunction *command, const
::GetCmdList()->Add(button->id, command);
return rv;
}
class CWin : public kmIWindow {
public:
NS_DECL_ISUPPORTS
NS_DECL_KMIWINDOW
CWin() {};
virtual ~CWin() {};
HWND hWnd;
};
NS_IMPL_ISUPPORTS (CWin, kmIWindow)
NS_IMETHODIMP CWin::GetHandle(void **aHandle) {
*aHandle = hWnd;
return NS_OK;
}
NS_IMETHODIMP CJSBridge::GetWindows(uint32_t *length, kmIWindow * **list)
{
*list = nullptr;
*length = 0;
int size = kPlugin.kFuncs->GetWindowsList(NULL, 0);
if (size == 0) return NS_OK;
HWND* hList = new HWND[size];
kPlugin.kFuncs->GetWindowsList(hList, size);
kmIWindow** wins = static_cast<kmIWindow**>(NS_Alloc(size*sizeof(kmIWindow*)));
for (unsigned i = 0;i<size;i++) {
CWin* win = new CWin();
win->hWnd = hList[i];
void *result;
win->QueryInterface(NS_GET_TEMPLATE_IID(kmIWindow), &result);
wins[i] = static_cast<kmIWindow*>(result);
}
delete [] hList;
if (length) *length = size;
*list = wins;
return NS_OK;
}
NS_IMETHODIMP CJSBridge::AddListener(nsIObserver* listener)
{
NS_ENSURE_ARG_POINTER(listener);
mListeners.AppendObject(listener);
return NS_OK;
}
NS_IMETHODIMP CJSBridge::RemoveListener(nsIObserver* listener)
{
NS_ENSURE_ARG_POINTER(listener);
mListeners.RemoveObject(listener);
return NS_OK;
}
bool notifyOpenWindow(nsIObserver *aListener, void* aData)
{
kmIWindow* winData = static_cast<kmIWindow*>(aData);
aListener->Observe(winData, "kmeleon-createwindow", nullptr);
return true;
}
void CJSBridge::OnCreateWindow(HWND hWnd, int flag)
{
CWin* win = new CWin();
win->hWnd = hWnd;
void *result;
win->QueryInterface(NS_GET_TEMPLATE_IID(kmIWindow), &result);
mListeners.EnumerateForwards(notifyOpenWindow, result);
}
bool notifySwitchTab(nsIObserver *aListener, void* aData)
{
nsIDOMWindow* dom = static_cast<nsIDOMWindow*>(aData);
aListener->Observe(dom, "kmeleon-switchtab", nullptr);
return true;
}
void CJSBridge::OnSwitchTab(HWND oldhWnd, HWND newhWnd)
{
nsCOMPtr<nsIWebBrowser> browser;
kPlugin.kFuncs->GetMozillaWebBrowser(newhWnd, getter_AddRefs(browser));
if (!browser) return;
nsCOMPtr<nsIDOMWindow> dom;
browser->GetContentDOMWindow(getter_AddRefs(dom));
if (!dom) return;
mListeners.EnumerateForwards(notifySwitchTab, dom);
}

@ -5,8 +5,11 @@
#include <nsCOMPtr.h>
#include <nsISupports.h>
#include "nsIJSBridge.h"
#include "nsIObserver.h"
#include "nsEmbedString.h"
#include "nsCOMArray.h"
#include <map>
#include <list>
#define JSBRIDGE_CID { 0x842170a0, 0x5210, 0x11db, { 0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66}}
static NS_DEFINE_CID(kJSBridgeCID, JSBRIDGE_CID);
@ -61,5 +64,9 @@ public:
CJSBridge() {};
virtual ~CJSBridge() {};
void OnCreateWindow(HWND hWnd, int flag);
void OnSwitchTab(HWND oldhWnd, HWND newhWnd);
protected:
nsCOMArray<nsIObserver> mListeners;
};

@ -85,6 +85,8 @@ BOOL Init()
return NS_SUCCEEDED(rv) ? TRUE : FALSE;
}
static nsCOMPtr<CJSBridge> gJSB = nullptr;
BOOL Quit()
{
if (cmdList) delete cmdList;
@ -92,7 +94,8 @@ BOOL Quit()
nsCOMPtr<nsIComponentRegistrar> compReg;
rv = NS_GetComponentRegistrar(getter_AddRefs (compReg));
NS_ENSURE_SUCCESS(rv, FALSE);
gJSB = nullptr;
rv = compReg->UnregisterFactory(components.mCID, componentFactory);
return NS_SUCCEEDED(rv) ? TRUE : FALSE;
}
@ -102,19 +105,28 @@ BOOL Quit()
WNDPROC KMeleonWndProc;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
CJSBridge* getJSB() {
if (!gJSB) {
nsCOMPtr<nsIServiceManager> servMan;
nsresult rv = NS_GetServiceManager(getter_AddRefs(servMan));
NS_ENSURE_SUCCESS(rv, nullptr);
nsCOMPtr<nsIJSBridge> jsb;
rv = servMan->GetServiceByContractID("@kmeleon/jsbridge;1", NS_GET_IID(nsIJSBridge), getter_AddRefs(jsb));
NS_ENSURE_SUCCESS(rv, nullptr);
gJSB = reinterpret_cast<CJSBridge*>(jsb.get());
}
return gJSB;
}
void Create(HWND hWnd)
{
#ifdef _DEBUG
nsCOMPtr<nsIServiceManager> servMan;
nsresult rv = NS_GetServiceManager(getter_AddRefs(servMan));
NS_ENSURE_SUCCESS(rv, );
nsCOMPtr<nsIJSBridge> jsb;
rv = servMan->GetServiceByContractID("@kmeleon/jsbridge;1", NS_GET_IID(nsIJSBridge), getter_AddRefs(jsb));
NS_ENSURE_SUCCESS(rv, );
#endif
KMeleonWndProc = (WNDPROC) GetWindowLong(hWnd, GWL_WNDPROC);
SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProc);
if (getJSB()) getJSB()->OnCreateWindow(hWnd, 0);
}
long DoMessage(const char *to, const char *from, const char *subject,
@ -124,6 +136,7 @@ long DoMessage(const char *to, const char *from, const char *subject,
if (strcmp(subject, "Init") == 0) {
Init();
}
else if (strcmp(subject, "Load") == 0) {
Load();
}
@ -135,6 +148,10 @@ long DoMessage(const char *to, const char *from, const char *subject,
Quit();
}
else if (strcmp(subject, "SwitchTab") == 0) {
if (getJSB()) getJSB()->OnSwitchTab((HWND)data1, (HWND)data2);
}
else return 0;
return 1;
}

@ -4,8 +4,7 @@
interface nsISimpleEnumerator;
interface nsIArray;
interface nsIDOMWindow;
interface nsIObserver;
[scriptable,function, uuid(83910267-7670-4493-99c1-dd540a036ef3)]
interface kmICommandFunction:nsISupports {
@ -17,6 +16,11 @@ interface kmICallback:nsISupports {
void run();
};
[scriptable,function, uuid(83910267-7670-4493-99c1-dd540a036ef5)]
interface kmIWindow:nsISupports {
readonly attribute voidPtr handle;
};
[scriptable, uuid(3d8ce8f0-5214-11db-b0de-0800200c9a65)]
interface kmICommand : nsISupports
{
@ -67,9 +71,15 @@ interface nsIJSBridge : nsISupports
long RegisterCmd(in string name, in string desc,
in kmICommandFunction command, [optional] in jsval icon,
[optional] in kmICallback enabled, [optional] in kmICallback checked);
[implicit_jscontext]
void SetCmdIcon(in string name, in jsval icon);
void SetAccel(in string key, in string command);
nsIWebBrowser Open(in string url, in unsigned short state);
nsIWebBrowser GetActiveBrowser();
void GetWindows([optional] out unsigned long length, [array, size_is(length), retval] out kmIWindow list);
void AddListener(in nsIObserver listener);
void RemoveListener(in nsIObserver listener);
};

Loading…
Cancel
Save