diff --git a/dom/system/gonk/mozstumbler/UploadStumbleRunnable.cpp b/dom/system/gonk/mozstumbler/UploadStumbleRunnable.cpp index 09f0834e341c..963109cb5397 100644 --- a/dom/system/gonk/mozstumbler/UploadStumbleRunnable.cpp +++ b/dom/system/gonk/mozstumbler/UploadStumbleRunnable.cpp @@ -7,14 +7,15 @@ #include "UploadStumbleRunnable.h" #include "StumblerLogging.h" #include "mozilla/dom/Event.h" +#include "nsIInputStream.h" #include "nsIScriptSecurityManager.h" #include "nsIURLFormatter.h" #include "nsIVariant.h" #include "nsIXMLHttpRequest.h" #include "nsNetUtil.h" -UploadStumbleRunnable::UploadStumbleRunnable(const nsACString& aUploadData) -: mUploadData(aUploadData) +UploadStumbleRunnable::UploadStumbleRunnable(nsIInputStream* aUploadData) +: mUploadInputStream(aUploadData) { } @@ -36,7 +37,7 @@ UploadStumbleRunnable::Upload() nsCOMPtr variant = do_CreateInstance("@mozilla.org/variant;1", &rv); NS_ENSURE_SUCCESS(rv, rv); - rv = variant->SetAsACString(mUploadData); + rv = variant->SetAsISupports(mUploadInputStream); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr xhr = do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); @@ -63,13 +64,13 @@ UploadStumbleRunnable::Upload() rv = xhr->Open(NS_LITERAL_CSTRING("POST"), NS_ConvertUTF16toUTF8(url), false, EmptyString(), EmptyString()); NS_ENSURE_SUCCESS(rv, rv); - xhr->SetRequestHeader(NS_LITERAL_CSTRING("Content-Type"), NS_LITERAL_CSTRING("application/json")); + xhr->SetRequestHeader(NS_LITERAL_CSTRING("Content-Type"), NS_LITERAL_CSTRING("gzip")); xhr->SetMozBackgroundRequest(true); // 60s timeout xhr->SetTimeout(60 * 1000); nsCOMPtr target(do_QueryInterface(xhr)); - nsRefPtr listener = new UploadEventListener(xhr, mUploadData.Length()); + nsRefPtr listener = new UploadEventListener(xhr); const char* const sEventStrings[] = { // nsIXMLHttpRequestEventTarget event types @@ -93,8 +94,8 @@ UploadStumbleRunnable::Upload() NS_IMPL_ISUPPORTS(UploadEventListener, nsIDOMEventListener) -UploadEventListener::UploadEventListener(nsIXMLHttpRequest* aXHR, int64_t aFileSize) -: mXHR(aXHR), mFileSize(aFileSize) +UploadEventListener::UploadEventListener(nsIXMLHttpRequest* aXHR) +: mXHR(aXHR) { } @@ -109,7 +110,7 @@ UploadEventListener::HandleEvent(nsIDOMEvent* aEvent) } if (type.EqualsLiteral("load")) { - STUMBLER_DBG("Got load Event : size %lld", mFileSize); + STUMBLER_DBG("Got load Event\n"); } else if (type.EqualsLiteral("error") && mXHR) { STUMBLER_ERR("Upload Error"); } else { diff --git a/dom/system/gonk/mozstumbler/UploadStumbleRunnable.h b/dom/system/gonk/mozstumbler/UploadStumbleRunnable.h index e05dc4409be9..a9ea83e37dbe 100644 --- a/dom/system/gonk/mozstumbler/UploadStumbleRunnable.h +++ b/dom/system/gonk/mozstumbler/UploadStumbleRunnable.h @@ -11,6 +11,7 @@ #include "nsIDOMEventListener.h" class nsIXMLHttpRequest; +class nsIInputStream; /* This runnable is managed by WriteStumbleOnThread only, see that class @@ -19,12 +20,12 @@ class nsIXMLHttpRequest; class UploadStumbleRunnable final : public nsRunnable { public: - explicit UploadStumbleRunnable(const nsACString& aUploadData); + explicit UploadStumbleRunnable(nsIInputStream* aUploadInputStream); NS_IMETHOD Run() override; private: virtual ~UploadStumbleRunnable() {} - const nsCString mUploadData; + nsCOMPtr mUploadInputStream; nsresult Upload(); }; @@ -32,7 +33,7 @@ private: class UploadEventListener : public nsIDOMEventListener { public: - UploadEventListener(nsIXMLHttpRequest* aXHR, int64_t aFileSize); + UploadEventListener(nsIXMLHttpRequest* aXHR); NS_DECL_ISUPPORTS NS_DECL_NSIDOMEVENTLISTENER @@ -40,7 +41,6 @@ public: protected: virtual ~UploadEventListener() {} nsCOMPtr mXHR; - int64_t mFileSize; }; #endif diff --git a/dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp b/dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp index 1cec6a5d4fb0..f5c59ec94099 100644 --- a/dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp +++ b/dom/system/gonk/mozstumbler/WriteStumbleOnThread.cpp @@ -21,8 +21,8 @@ mozilla::Atomic WriteStumbleOnThread::sIsUploading(false); mozilla::Atomic WriteStumbleOnThread::sIsAlreadyRunning(false); WriteStumbleOnThread::UploadFreqGuard WriteStumbleOnThread::sUploadFreqGuard = {0}; -#define FILENAME_INPROGRESS NS_LITERAL_CSTRING("stumbles.json") -#define FILENAME_COMPLETED NS_LITERAL_CSTRING("stumbles.done.json") +#define FILENAME_INPROGRESS NS_LITERAL_CSTRING("stumbles.json.gz") +#define FILENAME_COMPLETED NS_LITERAL_CSTRING("stumbles.done.json.gz") #define OUTPUT_DIR NS_LITERAL_CSTRING("mozstumbler") class DeleteRunnable : public nsRunnable @@ -64,15 +64,6 @@ WriteStumbleOnThread::UploadEnded(bool deleteUploadFile) target->Dispatch(event, NS_DISPATCH_NORMAL); } -#define DUMP(o, s) \ - do { \ - const char* s2 = (s); \ - uint32_t dummy; \ - nsresult rv = (o)->Write((s2), strlen(s2), &dummy); \ - if (NS_WARN_IF(NS_FAILED(rv))) \ - STUMBLER_ERR("write err"); \ - } while (0) - void WriteStumbleOnThread::WriteJSON(Partition aPart) { @@ -87,10 +78,10 @@ WriteStumbleOnThread::WriteJSON(Partition aPart) return; } - nsCOMPtr ostream = do_CreateInstance("@mozilla.org/network/file-output-stream;1"); - rv = ostream->Init(tmpFile, PR_WRONLY | PR_APPEND, 0666, 0); + nsRefPtr gzWriter = new nsGZFileWriter(nsGZFileWriter::Append); + rv = gzWriter->Init(tmpFile); if (NS_WARN_IF(NS_FAILED(rv))) { - STUMBLER_ERR("Open a file for stumble failed"); + STUMBLER_ERR("gzWriter init failed"); return; } @@ -105,8 +96,8 @@ WriteStumbleOnThread::WriteJSON(Partition aPart) // Need to add "]}" after the last item if (aPart == Partition::End) { - DUMP(ostream, "]}"); - rv = ostream->Close(); + gzWriter->Write("]}"); + rv = gzWriter->Finish(); if (NS_WARN_IF(NS_FAILED(rv))) { STUMBLER_ERR("ostream finish failed"); } @@ -136,14 +127,14 @@ WriteStumbleOnThread::WriteJSON(Partition aPart) // Need to add "{items:[" before the first item if (aPart == Partition::Begining) { - DUMP(ostream, "{\"items\":[{"); + gzWriter->Write("{\"items\":[{"); } else if (aPart == Partition::Middle) { - DUMP(ostream, ",{"); + gzWriter->Write(",{"); } - DUMP(ostream, mDesc.get()); + gzWriter->Write(mDesc.get()); // one item is ended with '}' (e.g. {item}) - DUMP(ostream, "}"); - rv = ostream->Close(); + gzWriter->Write("}"); + rv = gzWriter->Finish(); if (NS_WARN_IF(NS_FAILED(rv))) { STUMBLER_ERR("ostream finish failed"); } @@ -297,21 +288,12 @@ WriteStumbleOnThread::Upload() // prepare json into nsIInputStream nsCOMPtr inStream; - rv = NS_NewLocalFileInputStream(getter_AddRefs(inStream), tmpFile, -1, -1, - nsIFileInputStream::DEFER_OPEN); + rv = NS_NewLocalFileInputStream(getter_AddRefs(inStream), tmpFile); if (NS_FAILED(rv)) { sIsUploading = false; return; } - nsCString bufStr; - rv = NS_ReadInputStreamToString(inStream, bufStr, fileSize); - - if (NS_FAILED(rv)) { - sIsUploading = false; - return; - } - - nsRefPtr uploader = new UploadStumbleRunnable(bufStr); + nsRefPtr uploader = new UploadStumbleRunnable(inStream); NS_DispatchToMainThread(uploader); }