NS_IMETHODIMP AutoMounterSetting::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData) { if (strcmp(aTopic, MOZSETTINGS_CHANGED) != 0) { return NS_OK; } // Note that this function gets called for any and all settings changes, // so we need to carefully check if we have the one we're interested in. // // The string that we're interested in will be a JSON string that looks like: // {"key":"ums.autoMount","value":true} nsCOMPtr<nsIThreadJSContextStack> stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1"); if (!stack) { ERR("Failed to get JSContextStack"); return NS_OK; } JSContext* cx = stack->GetSafeJSContext(); if (!cx) { ERR("Failed to GetSafeJSContext"); return NS_OK; } nsDependentString dataStr(aData); JS::Value val; if (!JS_ParseJSON(cx, dataStr.get(), dataStr.Length(), &val) || !val.isObject()) { return NS_OK; } JSObject& obj(val.toObject()); JS::Value key; if (!JS_GetProperty(cx, &obj, "key", &key) || !key.isString()) { return NS_OK; } JSBool match; if (!JS_StringEqualsAscii(cx, key.toString(), UMS_MODE, &match) || (match != JS_TRUE)) { return NS_OK; } JS::Value value; if (!JS_GetProperty(cx, &obj, "value", &value) || !value.isInt32()) { return NS_OK; } int32_t mode = value.toInt32(); SetAutoMounterMode(mode); return NS_OK; }
bool IdWrapper::equalsAscii(StringData sd) const { if (isString()) { auto str = JSID_TO_STRING(_value); if (!str) { uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JSID_TO_STRING"); } bool matched; if (!JS_StringEqualsAscii(_context, str, sd.rawData(), &matched)) { uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JS_StringEqualsAscii"); } return matched; } if (isInt()) { JSStringWrapper jsstr(toInt32()); return jsstr.toStringData().compare(sd) == 0; } uasserted(ErrorCodes::BadValue, "Cannot equalsAscii non-string non-integer jsid"); }
static bool CheckVector(JSContext* cx, Handle<ShapeVec> shapes) { for (size_t i = 0; i < 10; ++i) { // Check the shape to ensure it did not get collected. char buffer[2]; buffer[0] = 'a' + i; buffer[1] = '\0'; bool match; if (!JS_StringEqualsAscii(cx, JSID_TO_STRING(shapes[i]->propid()), buffer, &match)) return false; if (!match) return false; } // Ensure iterator enumeration works through the handle. for (auto shape : shapes) { if (!shape) return false; } return true; }
static void TestArgFormatter(JSContext* jscontext, JSObject* glob, nsIXPConnect* xpc) { JSBool ok = JS_TRUE; const char* a_in = "some string"; nsCOMPtr<nsITestXPCFoo> b_in = new nsTestXPCFoo(); nsCOMPtr<nsIWritableVariant> c_in = do_CreateInstance("@mozilla.org/variant;1"); static NS_NAMED_LITERAL_STRING(d_in, "foo bar"); const char* e_in = "another meaningless chunck of text"; JSBool a_match; nsCOMPtr<nsISupports> b_out; nsCOMPtr<nsIVariant> c_out; nsAutoString d_out; JSBool e_match; nsCOMPtr<nsITestXPCFoo> specified; PRInt32 val; printf("ArgumentFormatter test: "); if(!b_in || !c_in || NS_FAILED(c_in->SetAsInt32(5))) { printf(" failed to construct test objects -- FAILED!\n"); return; } do { JSAutoRequest ar(jscontext); // Prepare an array of arguments for JS_ConvertArguments jsval argv[5]; js::AutoArrayRooter tvr(jscontext, JS_ARRAY_LENGTH(argv), argv); if (!PushArguments(jscontext, 5, argv, "s %ip %iv %is s", a_in, &NS_GET_IID(nsITestXPCFoo2), b_in.get(), c_in.get(), static_cast<const nsAString*>(&d_in), e_in)) { printf(" could not convert from native to JS -- FAILED!\n"); return; } JSString *a_out, *e_out; ok = JS_ConvertArguments(jscontext, 5, argv, "S %ip %iv %is S", &a_out, static_cast<nsISupports**>(getter_AddRefs(b_out)), static_cast<nsIVariant**>(getter_AddRefs(c_out)), static_cast<nsAString*>(&d_out), &e_out); TAF_CHECK(ok, " could not convert from JS to native -- FAILED!\n"); TAF_CHECK(b_out, " JS to native for %%ip returned NULL -- FAILED!\n"); specified = do_QueryInterface(b_out); TAF_CHECK(specified, " could not QI value JS to native returned -- FAILED!\n"); ok = specified.get() == b_in.get(); TAF_CHECK(ok, " JS to native returned wrong value -- FAILED!\n"); TAF_CHECK(c_out, " JS to native for %%iv returned NULL -- FAILED!\n"); TAF_CHECK(NS_SUCCEEDED(c_out->GetAsInt32(&val)) && val == 5, " JS to native for %%iv holds wrong value -- FAILED!\n"); TAF_CHECK(d_in.Equals(d_out), " JS to native for %%is returned the wrong value -- FAILED!\n"); TAF_CHECK(JS_StringEqualsAscii(jscontext, a_out, a_in, &a_match), " oom -- FAILED!\n"); TAF_CHECK(JS_StringEqualsAscii(jscontext, e_out, e_in, &e_match), " oom -- FAILED!\n"); } while (0); if (!ok) return; if(a_match && e_match) printf("passed\n"); else printf(" conversion OK, but surrounding was mangled -- FAILED!\n"); }