2013-03-21 09:17:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "SaveProfileTask.h"
|
2013-09-27 16:08:45 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2013-03-21 09:17:23 +00:00
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool
|
2013-03-21 09:17:23 +00:00
|
|
|
WriteCallback(const jschar *buf, uint32_t len, void *data)
|
|
|
|
{
|
|
|
|
std::ofstream& stream = *static_cast<std::ofstream*>(data);
|
|
|
|
nsAutoCString profile = NS_ConvertUTF16toUTF8(buf, len);
|
|
|
|
stream << profile.Data();
|
2013-08-07 06:59:54 +00:00
|
|
|
return true;
|
2013-03-21 09:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SaveProfileTask::Run() {
|
|
|
|
// Get file path
|
|
|
|
#if defined(SPS_PLAT_arm_android) && !defined(MOZ_WIDGET_GONK)
|
|
|
|
nsCString tmpPath;
|
|
|
|
tmpPath.AppendPrintf("/sdcard/profile_%i_%i.txt", XRE_GetProcessType(), getpid());
|
|
|
|
#else
|
|
|
|
nsCOMPtr<nsIFile> tmpFile;
|
|
|
|
nsAutoCString tmpPath;
|
|
|
|
if (NS_FAILED(NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tmpFile)))) {
|
|
|
|
LOG("Failed to find temporary directory.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
tmpPath.AppendPrintf("profile_%i_%i.txt", XRE_GetProcessType(), getpid());
|
|
|
|
|
|
|
|
nsresult rv = tmpFile->AppendNative(tmpPath);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = tmpFile->GetNativePath(tmpPath);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create a JSContext to run a JSObjectBuilder :(
|
|
|
|
// Based on XPCShellEnvironment
|
|
|
|
JSRuntime *rt;
|
|
|
|
JSContext *cx;
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> rtsvc
|
|
|
|
= do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
|
|
|
|
if (!rtsvc || NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) {
|
|
|
|
LOG("failed to get RuntimeService");
|
|
|
|
return NS_ERROR_FAILURE;;
|
|
|
|
}
|
|
|
|
|
|
|
|
cx = JS_NewContext(rt, 8192);
|
|
|
|
if (!cx) {
|
|
|
|
LOG("Failed to get context");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
2013-09-11 12:49:05 +00:00
|
|
|
static const JSClass c = {
|
2013-03-21 09:17:23 +00:00
|
|
|
"global", JSCLASS_GLOBAL_FLAGS,
|
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub
|
|
|
|
};
|
2013-11-11 19:16:31 +00:00
|
|
|
JSObject *obj = JS_NewGlobalObject(cx, &c, nullptr, JS::FireOnNewGlobalHook);
|
2013-03-21 09:17:23 +00:00
|
|
|
|
|
|
|
std::ofstream stream;
|
|
|
|
stream.open(tmpPath.get());
|
|
|
|
if (stream.is_open()) {
|
|
|
|
JSAutoCompartment autoComp(cx, obj);
|
|
|
|
JSObject* profileObj = profiler_get_profile_jsobject(cx);
|
2013-05-30 21:46:48 +00:00
|
|
|
JS::Rooted<JS::Value> val(cx, OBJECT_TO_JSVAL(profileObj));
|
2013-09-17 01:33:40 +00:00
|
|
|
JS_Stringify(cx, &val, JS::NullPtr(), JS::NullHandleValue, WriteCallback, &stream);
|
2013-03-21 09:17:23 +00:00
|
|
|
stream.close();
|
|
|
|
LOGF("Saved to %s", tmpPath.get());
|
|
|
|
} else {
|
|
|
|
LOG("Fail to open profile log file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_DestroyContext(cx);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(ProfileSaveEvent, nsIProfileSaveEvent)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ProfileSaveEvent::AddSubProfile(const char* aProfile) {
|
|
|
|
mFunc(aProfile, mClosure);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|