void FB::BrowserHost::initJS(const void* inst) { assertMainThread(); // Inject javascript helper function into the page; this is neccesary to help // with some browser compatibility issues. const char* javascriptMethod = "window.__FB_CALL_%1% = " "function(delay, f, args, fname) {" " if (arguments.length == 3)" " return setTimeout(function() { f.apply(null, args); }, delay);" " else" " return setTimeout(function() { f[fname].apply(f, args); }, delay);" "};"; // hash pointer to get a unique key for this plugin instance std::size_t inst_key = static_cast<std::size_t>( reinterpret_cast<std::ptrdiff_t>(inst)); inst_key += (inst_key >> 3); unique_key = boost::lexical_cast<std::string>(inst_key); call_delegate = (boost::format("__FB_CALL_%1%") % inst_key).str(); evaluateJavaScript((boost::format(javascriptMethod) % inst_key).str()); }
void FB::BrowserHost::initJS(const void* inst) { assertMainThread(); // Inject javascript helper function into the page; this is neccesary to help // with some browser compatibility issues. const char* javascriptMethod = "window.__FB_CALL_%1% = " "function(delay, f, args, fname) {" " if (arguments.length == 3)" " return setTimeout(function() { f.apply(null, args); }, delay);" " else" " return setTimeout(function() { f[fname].apply(f, args); }, delay);" "};"; // I'm open to suggestions on a better way to get a unique key for this plugin instance uint32_t inst_key; memcpy(&inst_key, &inst, 4); inst_key >>= 1; // Make sure nobody could use this to get a valid pointer inst_key *= 2.5; unique_key = boost::lexical_cast<std::string>(inst_key); call_delegate = (boost::format("__FB_CALL_%1%") % inst_key).str(); evaluateJavaScript((boost::format(javascriptMethod) % inst_key).str()); }
FB::BrowserStreamPtr FB::BrowserHost::createPostStream( const std::string& url, const PluginEventSinkPtr& callback, const std::string& postdata, bool cache /*= true*/, bool seekable /*= false*/, size_t internalBufferSize /*= 128 * 1024 */ ) const { assertMainThread(); FB::BrowserStreamPtr ptr(_createPostStream(url, callback, postdata, cache, seekable, internalBufferSize)); if (ptr) { m_streamMgr->retainStream(ptr); } return ptr; }
// We don't currently support return values as this makes it the responsibility // of the caller to clean up the stack, this can be changed if we automate void NativeDelegate::invoke() const { // Don't do this from non-main thread. assertMainThread(); // if we have no callbacks defined, the VM state will be NULL if (!L) { // Even if we don't do anything, need to reset arg count. _argumentCount = 0; return; } int top = lua_gettop(L); int numArgs = _argumentCount; // Reset argument count, so recursion is properly handled _argumentCount = 0; getCallbacks(L); int tidx = lua_gettop(L); if (!lua_istable(L, tidx)) { LSError("Error getting native delegate callback table"); } for (int i = 0; i < _callbackCount; i++) { lua_pushnumber(L, (double)i); lua_gettable(L, tidx); int t = lua_gettop(L); for (int i = top - numArgs; i < top; i++) { lua_pushvalue(L, i + 1); } lua_call(L, numArgs, 1); lua_settop(L, t); /* removes last lua_function called */ lua_pop(L, 1); } lua_settop(L, top); // clean up arguments off stack lua_pop(L, numArgs); }
int FB::BrowserHost::delayedInvoke(const int delayms, const FB::JSObjectPtr& func, const FB::VariantList& args, const std::string& fname) { assertMainThread(); FB::JSObjectPtr delegate = getDelayedInvokeDelegate(); assert(delegate); if (fname.empty()) return delegate->Invoke("", FB::variant_list_of(delayms)(func)(args)).convert_cast<int>(); else return delegate->Invoke("", FB::variant_list_of(delayms)(func)(args)(fname)).convert_cast<int>(); }
int FB::BrowserHost::delayedInvoke(const int delayms, const FB::JSObjectPtr& func, const FB::VariantList& args, const std::string& fname) { assertMainThread(); FB::JSObjectPtr delegate = getDelayedInvokeDelegate(); if (!delegate) return -1; // this is wrong (the return is meant to be the result of setTimeout) if (fname.empty()) return delegate->Invoke("", FB::variant_list_of(delayms)(func)(args)).convert_cast<int>(); else return delegate->Invoke("", FB::variant_list_of(delayms)(func)(args)(fname)).convert_cast<int>(); }
void NativeDelegate::registerDelegate(lua_State *L, NativeDelegate *delegate) { assertMainThread(); utArray<NativeDelegate *> *delegates = NULL; if (sActiveNativeDelegates.find(L) != UT_NPOS) { delegates = *(sActiveNativeDelegates.get(L)); } if (!delegates) { delegates = new utArray<NativeDelegate *>; sActiveNativeDelegates.insert(L, delegates); } delegates->push_back(delegate); }