Merge pull request #4515 from deepak1556/ipc_value_conversion_patch

ipc: allow passing date instances
This commit is contained in:
Cheng Zhao 2016-02-26 16:16:33 +08:00
commit a21e095a59
3 changed files with 20 additions and 15 deletions

View file

@ -76,15 +76,10 @@ class V8ValueConverter::FromV8ValueState {
};
V8ValueConverter::V8ValueConverter()
: date_allowed_(false),
reg_exp_allowed_(false),
: reg_exp_allowed_(false),
function_allowed_(false),
strip_null_from_objects_(false) {}
void V8ValueConverter::SetDateAllowed(bool val) {
date_allowed_ = val;
}
void V8ValueConverter::SetRegExpAllowed(bool val) {
reg_exp_allowed_ = val;
}
@ -243,12 +238,17 @@ base::Value* V8ValueConverter::FromV8ValueImpl(
return NULL;
if (val->IsDate()) {
if (!date_allowed_)
// JSON.stringify would convert this to a string, but an object is more
// consistent within this class.
return FromV8Object(val->ToObject(), state, isolate);
v8::Date* date = v8::Date::Cast(*val);
return new base::FundamentalValue(date->NumberValue() / 1000.0);
v8::Local<v8::Value> toISOString =
date->Get(v8::String::NewFromUtf8(isolate, "toISOString"));
if (toISOString->IsFunction()) {
v8::Local<v8::Value> result =
toISOString.As<v8::Function>()->Call(val, 0, nullptr);
if (!result.IsEmpty()) {
v8::String::Utf8Value utf8(result->ToString());
return new base::StringValue(std::string(*utf8, utf8.length()));
}
}
}
if (val->IsRegExp()) {

View file

@ -22,7 +22,6 @@ class V8ValueConverter {
public:
V8ValueConverter();
void SetDateAllowed(bool val);
void SetRegExpAllowed(bool val);
void SetFunctionAllowed(bool val);
void SetStripNullFromObjects(bool val);
@ -58,9 +57,6 @@ class V8ValueConverter {
FromV8ValueState* state,
v8::Isolate* isolate) const;
// If true, we will convert Date JavaScript objects to doubles.
bool date_allowed_;
// If true, we will convert RegExp JavaScript objects to string.
bool reg_exp_allowed_;

View file

@ -147,6 +147,15 @@ describe('ipc module', function() {
});
ipcRenderer.send('message', obj);
});
it('can send instance of Date', function(done) {
const currentDate = new Date();
ipcRenderer.once('message', function(event, value) {
assert.equal(value, currentDate.toISOString());
done();
});
ipcRenderer.send('message', currentDate);
});
});
describe('ipc.sendSync', function() {