FB::SimpleStreamHelperPtr FB::SimpleStreamHelper::AsyncPost( const FB::BrowserHostPtr& host, const FB::URI& uri, const std::string& postdata, const HttpCallback& callback, bool cache /*= true*/, size_t bufferSize /*= 256*1024*/ ) { if (!host->isMainThread()) { // This must be run from the main thread return host->CallOnMainThread(boost::bind(&FB::SimpleStreamHelper::AsyncPost, host, uri, postdata, callback, cache, bufferSize)); } FB::SimpleStreamHelperPtr ptr(boost::make_shared<FB::SimpleStreamHelper>(host, callback, bufferSize)); // This is kinda a weird trick; it's responsible for freeing itself, unless something decides // to hold a reference to it. ptr->keepReference(ptr); FB::BrowserStreamPtr stream(host->createPostStream(uri.toString(), ptr, postdata, true, false, bufferSize)); return ptr; }
FB::HttpStreamResponsePtr FB::SimpleStreamHelper::SynchronousPost( const FB::BrowserHostPtr& host, const FB::URI& uri, const std::string& postdata, const bool cache /*= true*/, const size_t bufferSize /*= 128*1024*/ ) { // Do not call this on the main thread. assert(!host->isMainThread()); SyncHTTPHelper helper; try { FB::HttpCallback cb(boost::bind(&SyncHTTPHelper::getURLCallback, &helper, _1, _2, _3, _4)); FB::SimpleStreamHelperPtr ptr = AsyncPost(host, uri, postdata, cb, cache, bufferSize); helper.setPtr(ptr); helper.waitForDone(); } catch (const std::exception&) { // If anything weird happens, just return NULL (to indicate failure) return FB::HttpStreamResponsePtr(); } return helper.m_response; }
FB::HttpStreamResponsePtr FB::SimpleStreamHelper::SynchronousGet( const FB::BrowserHostPtr& host, const FB::URI& uri, const bool cache /*= true*/, const size_t bufferSize /*= 128*1024*/ ) { // We can't ever block on the main thread, so SynchronousGet can't be called from there. // Also, if you could block the main thread, that still wouldn't work because the request // is processed on the main thread! assert(!host->isMainThread()); SyncGetHelper helper; try { FB::HttpCallback cb(boost::bind(&SyncGetHelper::getURLCallback, &helper, _1, _2, _3, _4)); FB::SimpleStreamHelperPtr ptr = AsyncGet(host, uri, cb, cache, bufferSize); helper.setPtr(ptr); helper.waitForDone(); } catch (const std::exception&) { // If anything weird happens, just return NULL (to indicate failure) return FB::HttpStreamResponsePtr(); } return helper.m_response; }