void FileManagerAPI::exists(std::string const &url, FB::JSObjectPtr const &callback) {
	FBLOG_DEBUG("FileManagerAPI::exists", "this=" << this << "\t" << "url=" << url << "\t" << "callback=" << callback);
	FB::URI uri(url);
	if(!FileManagerAPI::isFile(uri)) {
		FBLOG_DEBUG("FileManagerAPI::exists", "Can't check Remote resource");
		callback->InvokeAsync("", FB::variant_list_of(false)("Can't check Remote resource"));
		return;
	}
	try {
		std::string pathStr = uriToFile(uri);
		if(pathStr.empty()) {
			FBLOG_DEBUG("FileManagerAPI::exists", "Invalid path");
			callback->InvokeAsync("", FB::variant_list_of(false)("Invalid path"));
			return;
		}
		boost::filesystem::path path(pathStr);
		
		FBLOG_DEBUG("FileManagerAPI::exists", "Test \"" << pathStr << "\"");
		bool b = boost::filesystem::exists(path);
		callback->InvokeAsync("", FB::variant_list_of(b)(""));
	} catch(boost::filesystem::filesystem_error &) {
		FBLOG_DEBUG("FileManagerAPI::exists", "Internal error");
		callback->InvokeAsync("", FB::variant_list_of(false)("Internal error"));
	}
}
예제 #2
0
void btlauncherAPI::gotDownloadProgram(const FB::JSObjectPtr& callback, 
									   std::wstring& program,
									   std::string& version,
									   bool success,
									   const FB::HeaderMap& headers,
									   const boost::shared_array<uint8_t>& data,
									   const size_t size) {
	TCHAR temppath[500];
	DWORD gettempresult = GetTempPath(500, temppath);
	if (! gettempresult) {
		callback->InvokeAsync("", FB::variant_list_of(false)("GetTempPath")(GetLastError()));
		return;
	}
	std::wstring syspath(temppath);
	syspath.append( program.c_str() );
	boost::uuids::random_generator gen;
	boost::uuids::uuid u = gen();
	syspath.append( _T("_") );
	std::wstring wversion;
	wversion.assign( version.begin(), version.end() );
	syspath.append( wversion );
	syspath.append( _T("_") );
	syspath.append( boost::uuids::to_wstring(u) );
	syspath.append(_T(".exe"));

	HANDLE hFile = CreateFile( syspath.c_str(), GENERIC_WRITE | GENERIC_EXECUTE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL );
	if (hFile == INVALID_HANDLE_VALUE) {
		
		callback->InvokeAsync("", FB::variant_list_of(false)(GetLastError()));
		return;
	}
	PVOID ptr = (VOID*) data.get();
	DWORD written = 0;
	BOOL RESULT = WriteFile( hFile, ptr, size, &written, NULL);
	CloseHandle(hFile);
	
	if (! RESULT) {
		callback->InvokeAsync("", FB::variant_list_of("FILE")(false)(GetLastError()));
		return;
	}
	std::wstring installcommand = std::wstring(syspath);
	installcommand.append(_T(" /NOINSTALL /MINIMIZED /HIDE"));
	STARTUPINFO info;
	PROCESS_INFORMATION procinfo;
	memset(&info,0,sizeof(info));
	info.cb = sizeof(STARTUPINFO);
	 
	/* CreateProcessW can modify installcommand thus we allocate needed memory */ 
	wchar_t * pwszParam = new wchar_t[installcommand.size() + 1]; 
	const wchar_t* pchrTemp = installcommand.c_str(); 
    wcscpy_s(pwszParam, installcommand.size() + 1, pchrTemp); 

	BOOL bProc = CreateProcess(NULL, pwszParam, NULL, NULL, FALSE, 0, NULL, NULL, &info, &procinfo);
	if(bProc) {
		callback->InvokeAsync("", FB::variant_list_of("PROCESS")(true)(installcommand.c_str())(GetLastError()));
	} else {
		callback->InvokeAsync("", FB::variant_list_of("PROCESS")(false)(installcommand.c_str())(GetLastError()));
	}

}
FileTransferAPIPtr FileManagerAPI::copy(std::string const &sourceUrl, std::string const &targetUrl, FB::JSObjectPtr const &callback) {
	FBLOG_DEBUG("FileManagerAPI::copy", "this=" << this << "\t" << "sourceUrl=" << sourceUrl << "\t" << "targetUrl=" << targetUrl << "\t" << "callback=" << callback);
	FB::URI sourceUri(sourceUrl);
	FB::URI targetUri(targetUrl);
	if(FileManagerAPI::isHttp(sourceUri) && FileManagerAPI::isHttp(targetUri)) {
		FBLOG_DEBUG("FileManagerAPI::copy", "Can't copy two Remote resources");
		callback->InvokeAsync("", FB::variant_list_of(false)("Can't copy two Remote resources"));
		return FileTransferAPIPtr();
	}
	if(FileManagerAPI::isInternal(targetUri)) {
		FBLOG_DEBUG("FileManagerAPI::copy", "Can't copy file into Internal resources");
		callback->InvokeAsync("", FB::variant_list_of(false)("Can't copy file into Internal resources"));
		return FileTransferAPIPtr();
	}
	if(FileManagerAPI::isHttp(sourceUri) && !isSameHost(sourceUri)) {
		FBLOG_DEBUG("FileManagerAPI::copy", "Can't use different host with remote resource (XSS)");
		callback->InvokeAsync("", FB::variant_list_of(false)("Can't use different host with remote resource (XSS)"));
		return FileTransferAPIPtr();
	}
	if(FileManagerAPI::isHttp(targetUri) && !isSameHost(targetUri)) {
		FBLOG_DEBUG("FileManagerAPI::copy", "Can't use different host with remote resource (XSS)");
		callback->InvokeAsync("", FB::variant_list_of(false)("Can't use different host with remote resource (XSS)"));
		return FileTransferAPIPtr();
	}
	FileTransferAPIPtr fileTransfer = getFactory()->getFileTransfer(sourceUri, targetUri, callback);
	return fileTransfer;
}
예제 #4
0
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));
    }
}
예제 #5
0
void ibrowserAPI::installCallback(const char *operation, plist_t status, void *user_data) {
    char *xml_doc=NULL;
    uint32_t xml_length;
    
    Callback *cb = (Callback *)user_data;
    
    FB::JSObjectPtr pcb = cb->get("pcb");
    FB::JSObjectPtr scb = cb->get("scb");
    FB::JSObjectPtr ecb = cb->get("ecb");
    
    if(pcb)
    {
        plist_to_xml(status, &xml_doc, &xml_length);
        pcb->InvokeAsync("", FB::variant_list_of(xml_doc));
        free(xml_doc);
    }
    
    plist_dict_iter it = NULL;
	char* key = NULL;
    char*   s = NULL;
	plist_t subnode = NULL;
	plist_dict_new_iter(status, &it);
	plist_dict_next_item(status, it, &key, &subnode);
	while (subnode)
	{
		if(strcmp(key, "Error") == 0)
        {
            plist_get_string_val(subnode, &s);
            if(ecb)
                ecb->InvokeAsync("", FB::variant_list_of(s));
            
        }else if(strcmp(key, "Status") == 0){
            plist_get_string_val(subnode, &s);
            if(strcmp(s, "Complete") == 0){
                if(scb)
                    scb->InvokeAsync("", FB::variant_list_of(NULL));
            }
        }
		plist_dict_next_item(status, it, &key, &subnode);
        
	}

    if(key)
        free(key);
    if(s)
        free(s);
	free(it);
    return;
}
예제 #6
0
void GCPAPI::SetRemoteDescription(const FB::variant& action,
                                  const FB::variant& sdp,
                                  const FB::JSObjectPtr& succCb,
                                  const FB::JSObjectPtr& failCb)
{
    GoCast::RtcCenter* pCtr = GoCast::RtcCenter::Instance();
    m_onsetsdpsuccessCb = succCb;
    m_onsetsdpfailureCb = failCb;
    
    if(false == pCtr->Inited())
    {
        std::string msg = m_htmlId.convert_cast<std::string>();
        msg += ": Failed to init RtcCenter singleton...";
        FBLOG_ERROR_CUSTOM("GCPAPI::SetRemoteDescription", msg);
        
        if(NULL != failCb.get())
        {
            failCb->InvokeAsync("", FB::variant_list_of("RtcCenter init failed"));
        }
        return;
    }
    
    pCtr->SetRemoteDescription(m_htmlId.convert_cast<std::string>(),
                               m_pSetRemoteSDPObserver,
                               action.convert_cast<std::string>(),
                               sdp.convert_cast<std::string>());
}
예제 #7
0
FB::variant btlauncherAPI::runProgram(const std::string& program, const FB::JSObjectPtr& callback) {
	FBLOG_INFO("runProgram()", "START");
	string exe = this->installPath;
	if (program == "SoShare")
		exe += SOSHARE_EXE_PATH;
	else if (program == "Torque")
		exe += TORQUE_EXE_PATH;
	else
		exe += BTLIVE_EXE_PATH;
		
	FBLOG_INFO("runProgram()", exe.c_str());

	struct stat st;
	if (stat(exe.c_str(), &st) == -1 || !S_ISREG(st.st_mode)) {
		FBLOG_INFO("runProgram()", "stat check problem");
		callback->InvokeAsync("", FB::variant_list_of(false)(0));
		FBLOG_INFO("runProgram()", "END");
		return 0;
	}
	
	FB::VariantList list = isRunning(program);
	if (list.empty()) {
		switch(fork()) 
		{
			case -1: {
				perror("BTLauncher Run Program Fork");
				FBLOG_INFO("runProgram()", "fork - failure");
				break;
			}
			case 0: {
				FBLOG_INFO("runProgram()", "fork - child process");
				FBLOG_INFO("runProgram()", exe.c_str());
				execlp(exe.c_str(), exe.c_str(), NULL);
				FBLOG_INFO("runProgram()", "child process exit");
				exit(1);
			}
			default: {
				break;
			}
		}
	}
	callback->InvokeAsync("", FB::variant_list_of(true)(1));
	FBLOG_INFO("runProgram()", "END");
	return 0;
}
예제 #8
0
void btlauncherAPI::gotDownloadProgram(const FB::JSObjectPtr& callback, 
									   std::string& program,
									   bool success,
									   const FB::HeaderMap& headers,
									   const boost::shared_array<uint8_t>& data,
									   const size_t size) {
	FBLOG_INFO("gotDownloadProgram()", "START");
	FBLOG_INFO("gotDownloadProgram()", program.c_str());
	

	char *tmpname = strdup("/tmp/btlauncherXXXXXX");
	mkstemp(tmpname);
	ofstream f(tmpname);
	
	if (f.fail()) {
		FBLOG_INFO("gotDownloadProgram()", "f.fail");
		callback->InvokeAsync("", FB::variant_list_of(false)(-1));
		return;
	}
	f.write((char *)data.get(), size);
	f.close();
	
	std::string url;
	if (program == "SoShare")
		url = SOSHARE_DOWNLOAD_URL;
	else if (program == "Torque")
		url = TORQUE_DOWNLOAD_URL;
	else
		url = BTLIVE_DOWNLOAD_URL;
	
	FBLOG_INFO("gotDownloadProgram()", url.c_str());
	
	const char *tarFlags = "-xf";
	if (url.find(".gz") != std::string::npos)
		tarFlags = "-xzf";
	
	pid_t tarPid;
	int status;
	switch(tarPid = fork()) 
	{
		case -1:
			FBLOG_INFO("gotDownloadProgram()", "fork failed");
			break;
		case 0:
			FBLOG_INFO("gotDownloadProgram()", "running tar");
			execl("/usr/bin/tar", "tar", tarFlags, tmpname, "-C", this->installPath.c_str(), NULL);
			break;
		default:
			FBLOG_INFO("gotDownloadProgram()", "waitpid");
			waitpid(tarPid, &status, 0);
			break;
	}
	
	runProgram(program, callback);
	FBLOG_INFO("gotDownloadProgram()", "END");
}
예제 #9
0
void btlauncherAPI::gotCheckForUpdate(const FB::JSObjectPtr& callback,
                                   bool success,
                                   const FB::HeaderMap& header,
                                   const boost::shared_array<uint8_t>& data,
                                   const size_t size) {
	FBLOG_INFO("gotCheckForUpdate()", "START");
    
	char *tmpname = strdup("/tmp/btlauncherXXXXXX.pkg");
	mkstemp(tmpname);
	ofstream f(tmpname);
	
	if (f.fail()) {
		FBLOG_INFO("gotCheckForUpdate()", "f.fail");
		callback->InvokeAsync("", FB::variant_list_of(false)(-1));
		return;
	}
	f.write((char *)data.get(), size);
	f.close();
    
    switch(fork())
    {
        case -1: {
            FBLOG_INFO("gotCheckForUpdate()", "fork - failure");
            break;
        }
        case 0: {
            FBLOG_INFO("gotCheckForUpdate()", "fork - child process");
            FBLOG_INFO("gotCheckForUpdate()", tmpname);
            execlp("installer", "installer", "-pkg", tmpname, "-target", "CurrentUserHomeDirectory", NULL);
            FBLOG_INFO("gotCheckForUpdate()", "child process exit");
            exit(1);
        }
        default: {
            FBLOG_INFO("gotCheckForUpdate()", "fork - parent process");
            break;
        }
    }
    
    callback->InvokeAsync("", FB::variant_list_of(true)(1));
	FBLOG_INFO("gotCheckForUpdate()", "END");
}
예제 #10
0
FB::variant btlauncherAPI::runProgram(const std::wstring& program, const FB::JSObjectPtr& callback) {
	if (!this->isSupported(program)) {
		return _T(NOT_SUPPORTED_MESSAGE);
	}
	
	HINSTANCE ret = (HINSTANCE)0;
	if (isRunning(program).size() == 0) {
		ret = launch_program(program);
		callback->InvokeAsync("", FB::variant_list_of(false)(ret));
	}
	return ret;
}
void FileManagerAPI::mkdir(std::string const &url, FB::JSObjectPtr const &callback) {
	FBLOG_DEBUG("FileManagerAPI::mkdir", "this=" << this << "\t" << "url=" << url << "\t" << "callback=" << callback);
	FB::URI uri(url);
	try {
		if(!FileManagerAPI::isFile(uri)) {
			FBLOG_DEBUG("FileManagerAPI::mkdir", "Can't create Remote directories");
			callback->InvokeAsync("", FB::variant_list_of(false)("Can't create Remote directories"));
			return;
		}
		if(FileManagerAPI::isInternal(uri)) {
			FBLOG_DEBUG("FileManagerAPI::mkdir", "Can't create Internal directories");
			callback->InvokeAsync("", FB::variant_list_of(false)("Can't create Internal directories"));
			return;
		}
		std::string pathStr = uriToFile(uri);
		if(pathStr.empty()) {
			FBLOG_DEBUG("FileManagerAPI::mkdir", "Invalid path");
			callback->InvokeAsync("", FB::variant_list_of(false)("Invalid path"));
			return;
		}
		boost::filesystem::path path(pathStr);
		if(boost::filesystem::exists(path)) {
			FBLOG_DEBUG("FileManagerAPI::mkdir", "The path \"" << pathStr << "\" already exists");
			callback->InvokeAsync("", FB::variant_list_of(false)("Invalid file"));
			return;
		}
		
		FBLOG_DEBUG("FileManagerAPI::mkdir", "Make directories \"" << pathStr << "\"");
		boost::filesystem::create_directories(path);
		callback->InvokeAsync("", FB::variant_list_of(true)(""));
	} catch(boost::filesystem::filesystem_error &) {
		FBLOG_DEBUG("FileManagerAPI::mkdir", "Internal error");
		callback->InvokeAsync("", FB::variant_list_of(false)("Internal error"));
	}
}
void FileManagerAPI::remove(std::string const &url, FB::JSObjectPtr const &callback) {
	FBLOG_DEBUG("FileManagerAPI::remove", "this=" << this << "\t" << "url=" << url << "\t" << "callback=" << callback);
	FB::URI uri(url);
	try {
		if(!FileManagerAPI::isFile(uri)) {
			FBLOG_DEBUG("FileManagerAPI::remove", "Can't remove Remote resource");
			callback->InvokeAsync("", FB::variant_list_of(false)("Can't remove Remote resource"));
			return;
		}
		if(FileManagerAPI::isInternal(uri)) {
			FBLOG_DEBUG("FileManagerAPI::remove", "Can't remove Internal resource");
			callback->InvokeAsync("", FB::variant_list_of(false)("Can't remove Internal resource"));
			return;
		}
		std::string pathStr = uriToFile(uri);
		if(pathStr.empty()) {
			FBLOG_DEBUG("FileManagerAPI::remove", "Invalid path");
			callback->InvokeAsync("", FB::variant_list_of(false)("Invalid path"));
			return;
		}
		boost::filesystem::path path(pathStr);
		if(!boost::filesystem::exists(path)) {
			FBLOG_DEBUG("FileManagerAPI::remove", "The path \"" << pathStr << "\" doesn't exist");
			callback->InvokeAsync("", FB::variant_list_of(false)("Invalid file"));
			return;
		}
		
		FBLOG_DEBUG("FileManagerAPI::remove", "Remove \"" << pathStr << "\"");
		boost::filesystem::remove_all(path);
		callback->InvokeAsync("", FB::variant_list_of(true)(""));
	} catch(boost::filesystem::filesystem_error &) {
		FBLOG_DEBUG("FileManagerAPI::remove", "Internal error");
		callback->InvokeAsync("", FB::variant_list_of(false)("Internal error"));
	}
}
예제 #13
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");
}
예제 #14
0
void btlauncherAPI::ajax(const std::string& url, const FB::JSObjectPtr& callback) {
	FBLOG_INFO("ajax()", "START");
	FBLOG_INFO("ajax()", url.c_str());
	if (FB::URI::fromString(url).domain != "127.0.0.1") {
		FB::VariantMap response;
		response["allowed"] = false;
		response["success"] = false;
		callback->InvokeAsync("", FB::variant_list_of(response));
		return;
	}
	FB::SimpleStreamHelper::AsyncGet(m_host, FB::URI::fromString(url), 
		boost::bind(&btlauncherAPI::gotajax, this, callback, _1, _2, _3, _4), false
		);
	FBLOG_INFO("ajax()", "END");
}
예제 #15
0
// If you override this, you probably want to call it again, since this is what calls back into the page
// to indicate that we're done.
void PluginCore::setReady()
{
    FBLOG_INFO("PluginCore", "Plugin Ready");
    // Ensure that the JSAPI object has been created, in case the browser hasn't requested it yet.
    getRootJSAPI(); 
    try {
        FB::VariantMap::iterator fnd = m_params.find("onload");
        if (fnd != m_params.end()) {
            FB::JSObjectPtr method = fnd->second.convert_cast<FB::JSObjectPtr>();
            method->InvokeAsync("", FB::variant_list_of(getRootJSAPI()));
        }
    } catch(...) {
        // Usually this would be if it isn't a JSObjectPtr or the object can't be called
    }
    onPluginReady();
}
예제 #16
0
/// Closes the device.
void PluginAPI::close(const FB::JSAPIPtr &device,
                      const FB::JSObjectPtr &callbacks)
{
    try {
        FBLOG_INFO("close()", "Closing device");
        
        acquire_plugin()
            ->communicator(boost::static_pointer_cast<DeviceAPI>(device)->device)
            ->close(callbacks);
        
    } catch (const std::exception &e) {
        FBLOG_ERROR("close()", "Exception caught");
        FBLOG_ERROR("close()", e.what());
        callbacks->InvokeAsync("error", FB::variant_list_of(e.what()));
    }
}
예제 #17
0
/// Calls the device.
void PluginAPI::call(const FB::JSAPIPtr &device,
                     bool use_timeout,
                     const std::string &type_name,
                     const FB::VariantMap &message_map,
                     const FB::JSObjectPtr &callbacks)
{
    try {
        FBLOG_INFO("call()", "Calling device");
        
        acquire_plugin()
            ->communicator(boost::static_pointer_cast<DeviceAPI>(device)->device)
            ->call(use_timeout, type_name, message_map, callbacks);
        
    } catch (const std::exception &e) {
        FBLOG_ERROR("call()", "Exception caught");
        FBLOG_ERROR("call()", e.what());
        callbacks->InvokeAsync("error", FB::variant_list_of(e.what()));
    }
}
예제 #18
0
void GCPAPI::GetUserMedia(const FB::JSObjectPtr& mediaHints,
                          const FB::JSObjectPtr& succCb,
                          const FB::JSObjectPtr& failCb)
{
    GoCast::RtcCenter* pCtr = GoCast::RtcCenter::Instance();
	m_htmlId = "localPlayer";
    
    if(false == pCtr->Inited())
    {
        std::string msg = m_htmlId.convert_cast<std::string>();
        msg += ": Failed to init RtcCenter singleton...";
        FBLOG_ERROR_CUSTOM("GCPAPI::GetUserMedia", msg);
        
        if(NULL != failCb.get())
        {
            failCb->InvokeAsync("", FB::variant_list_of("RtcCenter init failed"));
        }
        return;
    }
    
    pCtr->GetUserMedia(mediaHints, succCb, failCb, false);
}