void FBTestPluginAPI::getURLCallback(const FB::JSObjectPtr& callback, bool success,
    const FB::HeaderMap& headers, const boost::shared_array<uint8_t>& data, const size_t size) {
    FB::VariantMap outHeaders;
    for (FB::HeaderMap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
        if (headers.count(it->first) > 1) {
            if (outHeaders.find(it->first) != outHeaders.end()) {
                outHeaders[it->first].cast<FB::VariantList>().push_back(it->second);
            } else {
                outHeaders[it->first] = FB::VariantList(FB::variant_list_of(it->second));
            }
        } else {
            outHeaders[it->first] = it->second;
        }
    }
    if (success) {
        std::string dstr(reinterpret_cast<const char*>(data.get()), size);
        callback->InvokeAsync("", FB::variant_list_of
            (true)
            (outHeaders)
            (dstr)
            );
    } else {
        callback->InvokeAsync("", FB::variant_list_of(false));
    }
}
Exemple #2
0
FB::VariantMap UploadQueue::getStatusDict(bool include_filenames) {
    FB::VariantMap d;
    // whole-queue stats
    d["queue_name"] = name;
    d["current_queue_bytes_remaining"] = current_queue_bytes;
    d["total_queue_bytes"] = total_queue_bytes;
    d["total_queue_files"] = total_queue_files;
    d["current_queue_files_remaining"] = files_waiting;
    d["batches_remaining"] = ceil(static_cast<float>(files_waiting) / static_cast<float>(batch_size));
    d["status"] = "Uploading";

    // this-batch stats
    if (include_filenames) {
        FB::VariantList cf;
        for (std::set<std::wstring>::const_iterator it = current_upload_files.begin();
            it != current_upload_files.end(); ++it)  {
                cf.push_back(*it);
        }
        d["current_files"] = cf;
    }
    d["current_batch_bytes_total"] = current_batch_bytes;

    double current_batch_pct_complete = 0.0;

    if (current_upload_request) {
        HTTP::Status st = current_upload_request->getStatus();
        FB::VariantMap status(st.asDict());
        d.insert(status.begin(), status.end());
        if (st.send_total) {
            current_batch_pct_complete = static_cast<double>(st.bytes_sent)
                / static_cast<double>(st.send_total);
        }
    }

    d["current_batch_pct_complete"] = current_batch_pct_complete;
    d["overall_progress_pct"] = static_cast<double>(
        (total_queue_bytes - (current_queue_bytes + current_batch_bytes))
            + (current_batch_pct_complete *  current_batch_bytes))
        / static_cast<double>(total_queue_bytes);

    return d;
}
void PluginCore::setParams(const FB::VariantMap& inParams)
{
    m_params.insert(inParams.begin(), inParams.end());
    for (FB::VariantMap::iterator it = m_params.begin(); it != m_params.end(); ++it)
    {
        std::string key(it->first);
        try {
            std::string value(it->second.convert_cast<std::string>());
            if (key.substr(0, 2) == "on") {
                FB::JSObjectPtr tmp;
                tmp = m_host->getDOMWindow()
                    ->getProperty<FB::JSObjectPtr>(value);

                FBLOG_TRACE("PluginCore", "Found <param> event handler: " << key);

                m_params[key] = tmp;
            }
        } catch (const std::exception &ex) {
            FBLOG_WARN("PluginCore", "Exception processing <param> " << key << ": " << ex.what());
        }
    }
}
Exemple #4
0
void btlauncherAPI::gotajax(const FB::JSObjectPtr& callback, 
							bool success,
						    const FB::HeaderMap& headers,
						    const boost::shared_array<uint8_t>& data,
						    const size_t size) {
	FBLOG_INFO("gotajax()", "START");

	FB::VariantMap response;
	response["allowed"] = true;
	response["success"] = success;
	
	if(!success) {
		FBLOG_INFO("gotajax()", "unsuccessful");
		callback->InvokeAsync("", FB::variant_list_of(response));
		FBLOG_INFO("gotajax()", "END");
		return;
	}
	
	FB::VariantMap outHeaders;
	for (FB::HeaderMap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
        if (headers.count(it->first) > 1) {
            if (outHeaders.find(it->first) != outHeaders.end()) {
                outHeaders[it->first].cast<FB::VariantList>().push_back(it->second);
            } else {
                outHeaders[it->first] = FB::VariantList(FB::variant_list_of(it->second));
            }
        } else {
            outHeaders[it->first] = it->second;
        }
    }
	response["headers"] = outHeaders;
	response["size"] = size;
	std::string result = std::string((const char*) data.get(), size);
	response["data"] = result;
	
	callback->InvokeAsync("", FB::variant_list_of(response));
	FBLOG_INFO("gotajax()", "END");
}
Exemple #5
0
bool BlabbleCall::Transfer(const FB::VariantMap &params)
{
	std::string destination;
	pj_str_t desturi;
	BlabbleAccountPtr p = parent_.lock();
	if (!p)
		return false;

	if (call_id_ == INVALID_CALL)
		return false;

	FB::VariantMap::const_iterator iter = params.find("destination");
	if (iter == params.end())
	{
		throw FB::script_error("No destination given!");
	}
	else
	{
		destination = iter->second.cast<std::string>();
		if (destination.size() <= 4 || destination.substr(0, 4) != "sip:")
			destination = "sip:" + destination + "@" + p->server();

		if (p->use_tls() && destination.find("transport=TLS") == std::string::npos)
			destination += ";transport=TLS";
	}

	if ((iter = params.find("onStatusChange")) != params.end() &&
		iter->second.is_of_type<FB::JSObjectPtr>())
	{
		set_on_transfer_status(iter->second.cast<FB::JSObjectPtr>());
	}

	desturi.ptr = const_cast<char*>(destination.c_str());
	desturi.slen = destination.length();
	return pjsua_call_xfer(call_id_, &desturi, NULL) == PJ_SUCCESS;
}
Json::Value variantToJsonValue(const FB::variant &val)
{
    if (val.is_of_type<std::string>()) {
        return Json::Value(val.convert_cast<std::string>());
    } else if (val.is_of_type<FB::VariantMap>()) {
        Json::Value retVal(Json::objectValue);
        FB::VariantMap map = val.cast<FB::VariantMap>();
        for (FB::VariantMap::iterator it = map.begin(); it != map.end(); ++it) {
            retVal[it->first] = variantToJsonValue(it->second);
        }
        return retVal;
    } else if (val.is_of_type<FB::VariantList>()) {
        Json::Value retVal(Json::arrayValue);
        FB::VariantList list = val.cast<FB::VariantList>();
        for (FB::VariantList::iterator it = list.begin(); it != list.end(); ++it) {
            retVal.append(variantToJsonValue(*it));
        }
        return retVal;
    } else if (val.is_of_type<int>()
               || val.is_of_type<char>()
               || val.is_of_type<short>()
               || val.is_of_type<long>()) {
        return Json::Value((Json::Int)val.convert_cast<long>());
    } else if (val.is_of_type<unsigned int>()
               || val.is_of_type<unsigned short>()
               || val.is_of_type<unsigned char>()
               || val.is_of_type<unsigned long>()) {
        return Json::Value((Json::UInt) val.convert_cast<unsigned long>());
    } else if (val.is_of_type<double>() || val.is_of_type<float>()) {
        return Json::Value(val.convert_cast<double>());
    } else if (val.is_of_type<bool>()) {
        return Json::Value(val.convert_cast<bool>());
    } else {
        return Json::Value(Json::nullValue);
    }
}
Exemple #7
0
void UploadQueue::start_next_upload() {
    unsigned int files_started = 0;

    boost::shared_ptr<HTTPRequestData> data(new HTTPRequestData);
    data->method = "POST";
    data->cookies = cookies;

    current_upload_files.clear();
    current_batch_bytes = 0;
    current_batch_retry = 0;

    for (std::list<UploadQueueEntry>::iterator it = queue.begin(); it != queue.end(); ++it) {
        if (it->status != UploadQueueEntry::ENTRY_WAITING) continue;
        try {
            UploadQueueEntry& qe = *it;

            // Open and process the image.
            // We do this now so we can catch errors and attribute them to individual images;
            // letting the HTTPRequest do it would fail the whole request if a single image
            // was unreadable.
            qe.datablock->resolve();

            // Pull in post params
            for (std::map<std::string, std::string>::iterator pvit = post_vars.begin();
                pvit != post_vars.end(); ++pvit) {
                qe.target.query_data[pvit->first] = pvit->second;
            }

            if (files_started == 0) {
                // First match sets the target uri
                data->uri = qe.target;
            } else {
                // Subsequently, we can only batch up more uploads to the same endpoint
                if (!(data->uri == qe.target)) continue;
            }

            it->setStatus(UploadQueueEntry::ENTRY_IN_PROGRESS);
            --files_waiting;
            current_queue_bytes -= qe.filesize;

            std::string s = "file" + files_started;
            qe.post_field = s;
            data->addFile(s, FB::wstring_to_utf8(qe.filename), "application/octet-stream", qe.datablock);

            current_upload_files.insert(qe.source_path);
            current_batch_bytes += qe.filesize;
            ++files_started;
            if (files_started >= batch_size) break;

        } catch (const std::exception& e) {
            it->result = e.what();
            it->setStatus(UploadQueueEntry::ENTRY_ERROR);
        }
    }

    if (files_started) {
        // We had enough images left in the queue for another request, so kick that off now.
        current_upload_request = HTTPRequest::create();
        current_upload_request->onStatusChanged(
            bind(&UploadQueue::upload_request_status_changed, this, _1)
            );
        current_upload_request->startRequest(data);

        // As long as we're doing uploads, we want to keep the HTTP server
        // up to provide progress to the chat bar widget -- so enable deferred shutdown.
        // TODOTODO
        //try {
        //    boost::shared_ptr<HTTPService> h = http_srv_inst_weak.lock();
        //    if (h) h->setDeferShutdown(true);
        //} catch (...) {}

    } else {
#ifndef NDEBUG
        FBLOG_DEBUG("UploadQueue",
            "start_next_upload() found no waiting files, running completion handlers\n");
#endif
        // All done, post upload finished callback to all instances
        FB::VariantMap d = getEmptyProgressDict();
        FB::VariantMap failures;

        for (std::list<UploadQueueEntry>::iterator it = queue.begin(); it != queue.end(); ++it) {
            if (it->status == UploadQueueEntry::ENTRY_ERROR) {
                failures[FB::wstring_to_utf8(it->source_path)] = it->result;
#ifndef NDEBUG
                FBLOG_WARN("UploadQueue", "Reporting file \"" << it->source_path.c_str()
                    << "\" as failed: " << it->result.c_str() << std::endl);
#endif        
            }
        }
        queue.clear();

        if (! failures.empty()) d["failed_files"] = failures;

        status = UploadQueue::UPLOAD_COMPLETE;
        // fire completion handlers, if available
        for (std::list<FB::URI>::iterator it = completion_handlers.begin();
            it != completion_handlers.end(); ++it) {
            boost::shared_ptr<HTTPRequestData> reqdata(new HTTPRequestData(*it));
            reqdata->cookies = cookies;
            HTTPRequest::asyncStartRequest(reqdata);
        }

        if (queue_finished_callback)
            queue_finished_callback(shared_ptr());
        
        StatusUpdateEvent evt(d);
        SendEvent(&evt);
    }
}
FB::VariantMap LocalVideoTrack::GetVideoDevices()
{
    static int deviceCount = 0;
    const size_t kMaxDeviceNameLength = 128;
    const size_t kMaxUniqueIdLength = 256;
    char deviceName[kMaxDeviceNameLength];
    char deviceUniqueId[kMaxUniqueIdLength];
    webrtc::VideoCaptureModule::DeviceInfo* pDevInfo;
    FB::VariantMap devices;
    std::string key;
    std::string val;

    pDevInfo = webrtc::VideoCaptureFactory::CreateDeviceInfo(0);
    for(size_t i=0; i<pDevInfo->NumberOfDevices(); i++)
    {
        pDevInfo->GetDeviceName(i, deviceName, kMaxDeviceNameLength,
                                deviceUniqueId, kMaxUniqueIdLength);
        key = deviceUniqueId;
        val = deviceName;
        devices[key] = val;

        if(videoDevices.end() == videoDevices.find(key))
        {
            videoDevices[key] = webrtc::VideoCaptureFactory::Create(deviceCount++, deviceUniqueId);

            std::string msg("Capture device [");
            std::stringstream devIdxStr;
            msg += deviceUniqueId;
            msg += "][idx = ";
            devIdxStr << (deviceCount-1);
            msg += (devIdxStr.str() + "]");

            if(NULL == videoDevices[key])
            {
                devices.erase(key);
                videoDevices.erase(key);
                msg += " (failed to open)";
                FBLOG_ERROR_CUSTOM("LocalVideoTrack::GetVideoDevices", msg);
            }
            else
            {
                msg += "...";
                FBLOG_INFO_CUSTOM("LocalVideoTrack::GetVideoDevices", msg);
            }
        }

        if((0 == i) && (videoDevices.end() != videoDevices.find(key)))
        {
            devices["default"] = key;
        }
    }

    std::stringstream offlineDevices;
    for(VideoDeviceList::iterator it = videoDevices.begin();
            it != videoDevices.end(); it++)
    {
        if(devices.end() == devices.find(it->first))
        {
            offlineDevices << (it->first);
        }
    }

    while(offlineDevices)
    {
        std::string deviceId;
        offlineDevices >> deviceId;

        if("" != deviceId)
        {
            videoDevices.erase(deviceId);
            std::string msg = "Deleting offline device [";
            msg += (deviceId + "]...");
            FBLOG_INFO_CUSTOM("LocalVideTrack::GetVideoDevices", msg);
        }
    }

    delete pDevInfo;
    return devices;
}
Exemple #9
0
void NpapiBrowserHost::getNPVariant(NPVariant *dst, const FB::variant &var)
{
    if (var.get_type() == typeid(FB::Npapi::NpapiNull)) {
        dst->type = NPVariantType_Null;

    } else if (var.get_type() == typeid(int)
        || var.get_type() == typeid(long)
        || var.get_type() == typeid(short)
        || var.get_type() == typeid(char)
        || var.get_type() == typeid(unsigned int)
        || var.get_type() == typeid(unsigned short)
        || var.get_type() == typeid(unsigned char)) {
        // Integer type
        dst->type = NPVariantType_Int32;
        dst->value.intValue = var.convert_cast<long>();

    } else if (var.get_type() == typeid(double)
        || var.get_type() == typeid(float)) {
        dst->type = NPVariantType_Double;
        dst->value.doubleValue = var.convert_cast<double>();

    } else if (var.get_type() == typeid(bool)) {
        dst->type = NPVariantType_Bool;
        dst->value.boolValue = var.convert_cast<bool>();

    } else if (var.get_type() == typeid(std::string)) {
        std::string str = var.convert_cast<std::string>();
        char *outStr = (char*)this->MemAlloc(str.size() + 1);
        memcpy(outStr, str.c_str(), str.size() + 1);
        dst->type = NPVariantType_String;
        dst->value.stringValue.UTF8Characters = outStr;
        dst->value.stringValue.UTF8Length = str.size();

    } else if (var.get_type() == typeid(FB::VariantList)) {
        JSAPI_DOMNode outArr = this->getDOMWindow().createArray();
        FB::VariantList inArr = var.cast<FB::VariantList>();
        for (FB::VariantList::iterator it = inArr.begin(); it != inArr.end(); it++) {
            outArr.callMethod<void>("push", variant_list_of(*it));
        }
        FB::AutoPtr<NPObjectAPI> api = dynamic_cast<NPObjectAPI*>(outArr.getJSObject().ptr());
        if (api.ptr() != NULL) {
            dst->type = NPVariantType_Object;
            dst->value.objectValue = api->getNPObject();
            this->RetainObject(dst->value.objectValue);
        }

    } else if (var.get_type() == typeid(FB::VariantMap)) {
        JSAPI_DOMNode out = this->getDOMWindow().createMap();
        FB::VariantMap inMap = var.cast<FB::VariantMap>();
        for (FB::VariantMap::iterator it = inMap.begin(); it != inMap.end(); it++) {
            out.setProperty(it->first, it->second);
        }
        FB::AutoPtr<NPObjectAPI> api = dynamic_cast<NPObjectAPI*>(out.getJSObject().ptr());
        if (api.ptr() != NULL) {
            dst->type = NPVariantType_Object;
            dst->value.objectValue = api->getNPObject();
            this->RetainObject(dst->value.objectValue);
        }

    } else if (var.get_type() == typeid(FB::JSOutObject)) {
        NPObject *outObj = NULL;
        FB::JSOutObject obj = var.cast<FB::JSOutObject>();
        NPObjectAPI *tmpObj = dynamic_cast<NPObjectAPI *>(obj.ptr());

        if (tmpObj == NULL) {
            outObj = NPJavascriptObject::NewObject(this, obj);
        } else {
            outObj = tmpObj->getNPObject();
            this->RetainObject(outObj);
        }

        dst->type = NPVariantType_Object;
        dst->value.objectValue = outObj;

    } else if (var.get_type() == typeid(FB::JSObject)) {
        NPObject *outObj = NULL;
        FB::JSObject obj = var.cast<JSObject>();
        NPObjectAPI *tmpObj = dynamic_cast<NPObjectAPI *>(obj.ptr());

        if (tmpObj == NULL) {
            outObj = NPJavascriptObject::NewObject(this, obj);
        } else {
            outObj = tmpObj->getNPObject();
            this->RetainObject(outObj);
        }

        dst->type = NPVariantType_Object;
        dst->value.objectValue = outObj;
    }
    // TODO: implement object types
}