예제 #1
0
bool CaffeineClientApp::PostCrashLogs(CefString CrashLog, CefString AppVersion, CefString UserDescription)
{
    CefRefPtr<CefRequest> request = CefRequest::Create();
    wstring url(DIAGNOSTICS_URL);

    request->SetURL(url);
    request->SetMethod("POST");
    request->SetFlags(UR_FLAG_ALLOW_CACHED_CREDENTIALS|UR_FLAG_REPORT_UPLOAD_PROGRESS);
    
    // Add post data to the request.  The correct method and content-
    // type headers will be set by CEF.
    CefRefPtr<CefPostDataElement> Data = CefPostDataElement::Create();
//    Data->SetToFile(CrashLog);
    //  TODO:  Consider creating a memory mapped file
    HANDLE hZipFile = CreateFile(CrashLog.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD BigBufferSize = GetFileSize(hZipFile, NULL);
    if (BigBufferSize != INVALID_FILE_SIZE)
    {
        LPBYTE BigBuffer = new BYTE[BigBufferSize];

        DWORD dwBytesRead = 0;
        ReadFile(hZipFile, BigBuffer, BigBufferSize, &dwBytesRead, NULL);
        CloseHandle(hZipFile);
        Data->SetToBytes(dwBytesRead, BigBuffer);
    
        CefRefPtr<CefPostData> POSTBody = CefPostData::Create();
        POSTBody->AddElement(Data);
        request->SetPostData(POSTBody);

        CefRequest::HeaderMap headerMap;
        request->GetHeaderMap(headerMap);
        headerMap.insert(make_pair("Content-Type", "application/zip"));
        request->SetHeaderMap(headerMap);
    
//        CefV8Context::GetCurrentContext()->GetFrame()->LoadRequest(request);

        // Create the client instance.
        CefRefPtr<CaffeineRequestClient> client = new CaffeineRequestClient();
    
        // Start the request. MyRequestClient callbacks will be executed asynchronously.
        CefRefPtr<CefURLRequest> url_request = CefURLRequest::Create(request, client.get());
        
        delete [] BigBuffer;
    }

    return true;
}
void LoadHandler::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl)
{
	if (errorCode == ERR_ABORTED)
		return;

	std::string errorMsg;
	std::map<int, std::string>::iterator it = g_mErrorMsgMap.find(errorCode);
	
	CefStringUTF8 strUrl = ConvertToUtf8(failedUrl);
	CefStringUTF8 et = ConvertToUtf8(errorText);
	cef3Trace("ErrId: %d, ErrMsg: [%s], Url: [%s]", errorCode, et.c_str(), strUrl.c_str());

	if (it != g_mErrorMsgMap.end())
	{
		std::stringstream stream;
		stream << g_mErrorMsgMap[errorCode] << " [" << errorCode << "]";
		errorMsg = stream.str();
	}
	else
	{
		std::stringstream stream;
		stream << "Error Code " << errorCode;
		errorMsg = stream.str();
	}

	std::string out;

	//if no frame its the whole page
	if (GetCallback())
	{
		const size_t size = 100*1024;
		char buff[size];
		buff[0] = 0;

		if (GetCallback()->onLoadError(errorMsg.c_str(), strUrl.c_str(), buff, size) && buff[0])
		{
			size_t nSize = strlen(buff);

			if (nSize > size)
				nSize = size;

			out = std::string(buff, nSize);
		}
	}

	if (out.empty())
	{
		// All other messages.
		std::stringstream ss;
		ss << "<html><head><title>Load Failed</title></head>"
			"<body><h1>Load Failed</h1>"
			"<h2>Load of URL " << strUrl.c_str() <<
			" failed: " << errorMsg <<
			".</h2></body>"
			"</html>";

		out = ss.str();
	}

	CefRefPtr<CefPostData> postData = CefPostData::Create();

	CefRefPtr<CefPostDataElement> el = CefPostDataElement::Create();
	el->SetToBytes(out.size(), out.c_str());

	postData->AddElement(el);

	CefRefPtr<CefRequest> req = CefRequest::Create();
	req->SetMethod("POST");
	req->SetURL("internal://loaderror");
	req->SetPostData(postData);
	frame->LoadRequest(req);
}