[Time] Refactoring part 1.
authorKarol Pawlowski <k.pawlowski@samsung.com>
Thu, 8 Jan 2015 12:05:22 +0000 (13:05 +0100)
committerKarol Pawlowski <k.pawlowski@samsung.com>
Fri, 9 Jan 2015 09:32:35 +0000 (10:32 +0100)
[Verification]
    TCT results should be self same

Change-Id: I376bfa5c6bcc7bc91c665d2f37625c300195c6d1
Signed-off-by: Karol Pawlowski <k.pawlowski@samsung.com>
src/time/time_api.js
src/time/time_extension.cc
src/time/time_extension.h
src/time/time_instance.cc
src/time/time_instance.h

index 99e00c40cc59b38428f6bd41038e48c94d928c27..32255687de4e254c5808523d5b10a594ee479f80 100644 (file)
@@ -1,16 +1,25 @@
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2015 Samsung Electronics Co, Ltd. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 var _minuteInMilliseconds = 60 * 1000;
 var _hourInMilliseconds = _minuteInMilliseconds * 60;
 
+var _common = xwalk.utils;
+var native_ = new _common.NativeManager(extension);
+
 exports.getCurrentDateTime = function() {
   return new tizen.TZDate();
 };
 
 exports.getLocalTimezone = function() {
-  return _sendSyncMessage('GetLocalTimeZone').value;
+  var result = native_.callSync('Time_getLocalTimeZone');
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+
+  return native_.getResultObject(result);
 };
 
 var _availableTimezonesCache = [];
@@ -18,7 +27,13 @@ var _availableTimezonesCache = [];
 exports.getAvailableTimezones = function() {
   if (_availableTimezonesCache.length)
     return _availableTimezonesCache;
-  _availableTimezonesCache = _sendSyncMessage('GetAvailableTimeZones').value;
+
+  var result = native_.callSync('Time_getAvailableTimeZones');
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+
+  _availableTimezonesCache = native_.getResultObject(result);
   return _availableTimezonesCache;
 };
 
@@ -29,7 +44,12 @@ exports.getDateFormat = function(shortformat) {
 };
 
 exports.getTimeFormat = function() {
-  return _sendSyncMessage('GetTimeFormat').value;
+  var result = native_.callSync('Time_getTimeFormat');
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+
+  return native_.getResultObject(result);
 };
 
 exports.isLeapYear = function(year) {
@@ -51,17 +71,6 @@ function _throwProperTizenException(e) {
     throw new tizen.WebAPIException(tizen.WebAPIException.UNKNOWN_ERR);
 }
 
-function _sendSyncMessage(cmd, timezone, value, trans, locale) {
-  var msg = {
-    'cmd': cmd,
-    'timezone': timezone || '',
-    'value': value || '',
-    'trans': trans || '',
-    'locale': locale || false
-  };
-  return JSON.parse(extension.internal.sendSyncMessage(JSON.stringify(msg)));
-}
-
 var TimeDurationUnit = [
   'MSECS',
   'SECS',
@@ -201,8 +210,16 @@ tizen.TZDate = function(year, month, day, hours, minutes, seconds, milliseconds,
     throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
 };
 
-function getTimezoneOffset(timezone, timeInMs) {
-  return _sendSyncMessage('GetTimeZoneOffset', timezone, timeInMs).value;
+function getTimezoneOffset(_timezone, _timeInMs) {
+  var result = native_.callSync('Time_getTimeZoneOffset', {
+    timezone: _timezone,
+    value: _timeInMs
+  });
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+
+  return native_.getResultObject(result);
 }
 
 function _getTimeWithOffset(date) {
@@ -415,60 +432,99 @@ tizen.TZDate.prototype.addDuration = function(duration) {
 };
 
 tizen.TZDate.prototype.toLocaleDateString = function() {
-  var result = _sendSyncMessage('ToDateString', this.timezone_,
-                                this.date_.getTime(), '', true);
-  if (result.error)
+  var result = native_.callSync('Time_toDateString', {
+    timezone: this.timezone_,
+    value: this.date_.getTime(),
+    trans: '',
+    locale: true
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.toLocaleTimeString = function() {
-  var result = _sendSyncMessage('ToTimeString', this.timezone_,
-                                this.date_.getTime(), '', true);
-  if (result.error)
+  var result = native_.callSync('Time_toTimeString', {
+    timezone: this.timezone_,
+    value: this.date_.getTime(),
+    trans: '',
+    locale: true
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.toLocaleString = function() {
-  var result = _sendSyncMessage('ToString', this.timezone_,
-                                this.date_.getTime(), '', true);
-  if (result.error)
+  var result = native_.callSync('Time_toString', {
+    timezone: this.timezone_,
+    value: this.date_.getTime(),
+    trans: '',
+    locale: true
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.toDateString = function() {
-  var result = _sendSyncMessage('ToDateString', this.timezone_,
-                                this.date_.getTime(), '', false);
-  if (result.error)
+  var result = native_.callSync('Time_toDateString', {
+    timezone: this.timezone_,
+    value: this.date_.getTime(),
+    trans: '',
+    locale: false
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.toTimeString = function() {
-  var result = _sendSyncMessage('ToTimeString', this.timezone_,
-                                this.date_.getTime(), '', false);
-  if (result.error)
+  var result = native_.callSync('Time_toTimeString', {
+    timezone: this.timezone_,
+    value: this.date_.getTime(),
+    trans: '',
+    locale: false
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.toString = function() {
-  var result = _sendSyncMessage('ToString', this.timezone_,
-                                this.date_.getTime(), '', false);
-  if (result.error)
+  var result = native_.callSync('Time_toString', {
+    timezone: this.timezone_,
+    value: this.date_.getTime(),
+    trans: '',
+    locale: false
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.getTimezoneAbbreviation = function() {
-  var result = _sendSyncMessage('GetTimeZoneAbbreviation', this.timezone_,
-                                this.date_.getTime());
-
-  if (result.error)
+  var result = native_.callSync('Time_getTimeZoneAbbreviation', {
+    timezone: this.timezone_,
+    value: this.date_.getTime()
+  });
+  if (native_.isFailure(result)) {
     return '';
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.secondsFromUTC = function() {
@@ -476,30 +532,45 @@ tizen.TZDate.prototype.secondsFromUTC = function() {
 };
 
 tizen.TZDate.prototype.isDST = function() {
-  var result = _sendSyncMessage('IsDST', this.timezone_,
-                                _getTimeWithOffset(this.date_));
-
-  if (result.error)
+  var result = native_.callSync('Time_isDST', {
+    timezone: this.timezone_,
+    value: _getTimeWithOffset(this.date_)
+  });
+  if (native_.isFailure(result)) {
     return false;
-  return result.value;
+  }
+
+  return native_.getResultObject(result);
 };
 
 tizen.TZDate.prototype.getPreviousDSTTransition = function() {
-  var OffsetInMilliseconds = this.date_.getTimezoneOffset() * _minuteInMilliseconds * -1;
-  var result = _sendSyncMessage('GetDSTTransition', this.timezone_,
-                                _getTimeWithOffset(this.date_), 'NEXT_TRANSITION');
-
-  if (result.error || result.value == 0)
+  var result = native_.callSync('Time_getDSTTransition', {
+    "timezone": this.timezone_,
+    "value": _getTimeWithOffset(this.date_),
+    "trans": 'NEXT_TRANSITION'
+  });
+  if (native_.isFailure(result)) {
     return null;
-  return new tizen.TZDate(new Date(result.value - OffsetInMilliseconds), this.timezone_);
+  }
+  var _result = native_.getResultObject(result);
+  if (result.error || _result == 0)
+    return null;
+  var OffsetInMilliseconds = this.date_.getTimezoneOffset() * _minuteInMilliseconds * -1;
+  return new tizen.TZDate(new Date(_result - OffsetInMilliseconds), this.timezone_);
 };
 
 tizen.TZDate.prototype.getNextDSTTransition = function() {
-  var OffsetInMilliseconds = this.date_.getTimezoneOffset() * _minuteInMilliseconds * -1;
-  var result = _sendSyncMessage('GetDSTTransition', this.timezone_,
-                                _getTimeWithOffset(this.date_), 'PREV_TRANSITION');
-
-  if (result.error || result.value == 0)
+  var result = native_.callSync('Time_getDSTTransition', {
+    timezone: this.timezone_,
+    value: _getTimeWithOffset(this.date_),
+    trans: 'PREV_TRANSITION'
+  });
+  if (native_.isFailure(result)) {
     return null;
-  return new tizen.TZDate(new Date(result.value - OffsetInMilliseconds), this.timezone_);
+  }
+  var _result = native_.getResultObject(result);
+  if (result.error || _result == 0)
+    return null;
+  var OffsetInMilliseconds = this.date_.getTimezoneOffset() * _minuteInMilliseconds * -1;
+  return new tizen.TZDate(new Date(_result - OffsetInMilliseconds), this.timezone_);
 };
index 6a204793cd42384692e0bf2a09acf0f44cbf34d6..7cbaf73d97664ce15ea7e3f9ba1595a34871036b 100644 (file)
@@ -1,9 +1,9 @@
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2015 Samsung Electronics Co, Ltd. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 #include "time/time_extension.h"
-
 #include "time/time_instance.h"
 
 common::Extension* CreateExtension() {
@@ -28,5 +28,5 @@ TimeExtension::TimeExtension() {
 TimeExtension::~TimeExtension() {}
 
 common::Instance* TimeExtension::CreateInstance() {
-  return new TimeInstance;
+  return &extension::time::TimeInstance::GetInstance();
 }
index a7534e334fef4d99ea566f4d2adcd259c3922a50..df8b8d45fb827d4cbaeac6496c608f173c3df4e5 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2015 Samsung Electronics Co, Ltd. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
index 30c0daa39f5b24354a687e2e6688d32f06708e49..daf19765e48a9ab23c70483044a241f47e568f6e 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2015 Samsung Electronics Co, Ltd. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -9,7 +10,6 @@
 #endif
 
 #include <sstream>
-#include <string>
 #include <memory>
 #include <cerrno>
 
 #include "unicode/smpdtfmt.h"
 #include "unicode/dtptngen.h"
 
+namespace extension {
+namespace time {
+
 namespace {
 
 const int _hourInMilliseconds = 3600000;
 
 }  // namespace
 
-TimeInstance::TimeInstance() {}
-
-TimeInstance::~TimeInstance() {}
-
-void TimeInstance::HandleMessage(const char* message) {}
-
-void TimeInstance::HandleSyncMessage(const char* message) {
-  picojson::value v;
-
-  std::string err;
-  picojson::parse(v, message, message + strlen(message), &err);
-  if (!err.empty()) {
-    std::cout << "Ignoring Sync message.\n";
-    return;
-  }
+TimeInstance& TimeInstance::GetInstance() {
+    static TimeInstance instance;
+    return instance;
+}
 
-  std::string cmd = v.get("cmd").to_str();
-  picojson::value::object o;
-  if (cmd == "GetLocalTimeZone")
-    o = HandleGetLocalTimeZone(v);
-  else if (cmd == "GetAvailableTimeZones")
-    o = HandleGetAvailableTimeZones(v);
-  else if (cmd == "GetTimeZoneOffset")
-    o = HandleGetTimeZoneOffset(v);
-  else if (cmd == "GetTimeZoneAbbreviation")
-    o = HandleGetTimeZoneAbbreviation(v);
-  else if (cmd == "IsDST")
-    o = HandleIsDST(v);
-  else if (cmd == "GetDSTTransition")
-    o = HandleGetDSTTransition(v);
-  else if (cmd == "ToDateString")
-    o = HandleToString(v, TimeInstance::DATE_FORMAT);
-  else if (cmd == "ToTimeString")
-    o = HandleToString(v, TimeInstance::TIME_FORMAT);
-  else if (cmd == "ToString")
-    o = HandleToString(v, TimeInstance::DATETIME_FORMAT);
-  else if (cmd == "GetTimeFormat")
-    o = HandleGetTimeFormat(v);
-
-  if (o.empty())
-    o["error"] = picojson::value(true);
-
-  SendSyncReply(picojson::value(o).serialize().c_str());
+TimeInstance::TimeInstance() {
+  using namespace std::placeholders;
+
+#define REGISTER_SYNC(c, x) \
+  RegisterSyncHandler(c, std::bind(&TimeInstance::x, this, _1, _2));
+#define REGISTER_ASYNC(c, x) \
+  RegisterHandler(c, std::bind(&TimeInstance::x, this, _1, _2));
+
+  REGISTER_SYNC("Time_getAvailableTimeZones", Time_getAvailableTimeZones);
+  REGISTER_SYNC("Time_getDSTTransition", Time_getDSTTransition);
+  REGISTER_SYNC("Time_getLocalTimeZone", Time_getLocalTimeZone);
+  REGISTER_SYNC("Time_getTimeFormat", Time_getTimeFormat);
+  REGISTER_SYNC("Time_getTimeZoneOffset", Time_getTimeZoneOffset);
+  REGISTER_SYNC("Time_getTimeZoneAbbreviation", Time_getTimeZoneAbbreviation);
+  REGISTER_SYNC("Time_isDST", Time_isDST);
+  REGISTER_SYNC("Time_toString", Time_toString);
+  REGISTER_SYNC("Time_toDateString", Time_toDateString);
+  REGISTER_SYNC("Time_toTimeString", Time_toTimeString);
+
+#undef REGISTER_SYNC
+#undef REGISTER_ASYNC
 }
 
-const picojson::value::object TimeInstance::HandleGetLocalTimeZone(
-  const picojson::value& msg) {
-  picojson::value::object o;
+TimeInstance::~TimeInstance() {}
 
+void TimeInstance::Time_getLocalTimeZone(const JsonValue& /*args*/,
+                                         JsonObject& out) {
   UnicodeString local_timezone;
   TimeZone::createDefault()->getID(local_timezone);
 
   std::string localtz;
   local_timezone.toUTF8String(localtz);
 
-  o["value"] = picojson::value(localtz);
-
-  return o;
+  ReportSuccess(JsonValue(localtz), out);
 }
 
-const picojson::value::object TimeInstance::HandleGetAvailableTimeZones(
-  const picojson::value& msg) {
-  picojson::value::object o;
-
+void TimeInstance::Time_getAvailableTimeZones(const JsonValue& /*args*/,
+                                              JsonObject& out) {
   UErrorCode ec = U_ZERO_ERROR;
   std::unique_ptr<StringEnumeration> timezones(TimeZone::createEnumeration());
   int32_t count = timezones->count(ec);
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
-  if (U_FAILURE(ec))
-    return o;
-
-  picojson::value::array a;
+  JsonArray a;
   const char *timezone = NULL;
   int i = 0;
   do {
     int32_t resultLen = 0;
     timezone = timezones->next(&resultLen, ec);
     if (U_SUCCESS(ec)) {
-      a.push_back(picojson::value(timezone));
+      a.push_back(JsonValue(timezone));
       i++;
     }
   }while(timezone && i < count);
 
-  o["value"] = picojson::value(a);
-
-  return o;
+  ReportSuccess(JsonValue(a), out);
 }
 
-const picojson::value::object TimeInstance::HandleGetTimeZoneOffset(
-  const picojson::value& msg) {
-  picojson::value::object o;
-
+void TimeInstance::Time_getTimeZoneOffset(const JsonValue& args,
+                                          JsonObject& out) {
   std::unique_ptr<UnicodeString> id(
-    new UnicodeString(msg.get("timezone").to_str().c_str()));
-  UDate dateInMs = strtod(msg.get("value").to_str().c_str(), NULL);
+    new UnicodeString(args.get("timezone").to_str().c_str()));
+  UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
 
-  if (errno == ERANGE)
-    return o;
+  if (errno == ERANGE) {
+    ReportError(out);
+    return;
+  }
 
   UErrorCode ec = U_ZERO_ERROR;
   std::unique_ptr<TimeZone> timezone(TimeZone::createTimeZone(*id));
   std::unique_ptr<Calendar> cal(Calendar::createInstance(*timezone, ec));
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   cal->setTime(dateInMs, ec);
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   int32_t offset = timezone->getRawOffset();
 
@@ -145,152 +130,184 @@ const picojson::value::object TimeInstance::HandleGetTimeZoneOffset(
   std::stringstream offsetStr;
   offsetStr << offset;
 
-  o["value"] = picojson::value(offsetStr.str());
-
-  return o;
+  ReportSuccess(JsonValue(offsetStr.str()), out);
 }
 
-const picojson::value::object TimeInstance::HandleGetTimeZoneAbbreviation(
-  const picojson::value& msg) {
-  picojson::value::object o;
-
+void TimeInstance::Time_getTimeZoneAbbreviation(const JsonValue& args,
+                                                JsonObject& out) {
   std::unique_ptr<UnicodeString> id(
-    new UnicodeString(msg.get("timezone").to_str().c_str()));
-  UDate dateInMs = strtod(msg.get("value").to_str().c_str(), NULL);
+    new UnicodeString(args.get("timezone").to_str().c_str()));
+  UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
 
-  if (errno == ERANGE)
-    return o;
+  if (errno == ERANGE) {
+    ReportError(out);
+    return;
+  }
 
   UErrorCode ec = U_ZERO_ERROR;
   std::unique_ptr<Calendar> cal(
     Calendar::createInstance(TimeZone::createTimeZone(*id), ec));
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   cal->setTime(dateInMs, ec);
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   std::unique_ptr<DateFormat> fmt(
     new SimpleDateFormat(UnicodeString("z"), Locale::getEnglish(), ec));
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   UnicodeString uAbbreviation;
   fmt->setCalendar(*cal);
   fmt->format(cal->getTime(ec), uAbbreviation);
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   std::string abbreviation = "";
   uAbbreviation.toUTF8String(abbreviation);
 
-  o["value"] = picojson::value(abbreviation);
-
-  return o;
+  ReportSuccess(JsonValue(abbreviation), out);
 }
 
-const picojson::value::object TimeInstance::HandleIsDST(
-  const picojson::value& msg) {
-  picojson::value::object o;
-
+void TimeInstance::Time_isDST(const JsonValue& args, JsonObject& out) {
   std::unique_ptr<UnicodeString> id(
-    new UnicodeString(msg.get("timezone").to_str().c_str()));
-  UDate dateInMs = strtod(msg.get("value").to_str().c_str(), NULL);
+    new UnicodeString(args.get("timezone").to_str().c_str()));
+  UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
   dateInMs -= _hourInMilliseconds;
 
-  if (errno == ERANGE)
-    return o;
+  if (errno == ERANGE) {
+    ReportError(out);
+    return;
+  }
 
   UErrorCode ec = U_ZERO_ERROR;
   std::unique_ptr<Calendar> cal(
     Calendar::createInstance(TimeZone::createTimeZone(*id), ec));
-  if (U_FAILURE(ec))
-    return o;
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
   cal->setTime(dateInMs, ec);
-  if (U_FAILURE(ec))
-    return o;
-
-  o["value"] = picojson::value(static_cast<bool>(cal->inDaylightTime(ec)));
+  if (U_FAILURE(ec)) {
+    ReportError(out);
+    return;
+  }
 
-  return o;
+  ReportSuccess(JsonValue{static_cast<bool>(cal->inDaylightTime(ec))}, out);
 }
 
-const picojson::value::object TimeInstance::HandleGetDSTTransition(
-  const picojson::value& msg) {
-  picojson::value::object o;
-
+void TimeInstance::Time_getDSTTransition(const JsonValue& args,
+                                         JsonObject& out) {
   std::unique_ptr<UnicodeString> id(
-    new UnicodeString(msg.get("timezone").to_str().c_str()));
-  std::string trans = msg.get("trans").to_str();
-  UDate dateInMs = strtod(msg.get("value").to_str().c_str(), NULL);
+    new UnicodeString(args.get("timezone").to_str().c_str()));
+  std::string trans = args.get("trans").to_str();
+  UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
 
-  if (errno == ERANGE)
-    return o;
+  if (errno == ERANGE) {
+    ReportError(out);
+    return;
+  }
 
   std::unique_ptr<VTimeZone> vtimezone(VTimeZone::createVTimeZoneByID(*id));
 
-  if (!vtimezone->useDaylightTime())
-    return o;
+  if (!vtimezone->useDaylightTime()) {
+    ReportError(out);
+    return;
+  }
 
   TimeZoneTransition tzTransition;
   if (trans.compare("NEXT_TRANSITION") &&
       vtimezone->getNextTransition(dateInMs, FALSE, tzTransition))
-    o["value"] = picojson::value(tzTransition.getTime());
+    ReportSuccess(JsonValue{tzTransition.getTime()}, out);
   else if (vtimezone->getPreviousTransition(dateInMs, FALSE, tzTransition))
-    o["value"] = picojson::value(tzTransition.getTime());
+    ReportSuccess(JsonValue{tzTransition.getTime()}, out);
+  else
+    ReportError(out);
+}
+
+void TimeInstance::Time_toString(const JsonValue& args, JsonObject& out) {
+  JsonValue val;
+  if (!this->toStringByFormat(args, val, TimeInstance::DATETIME_FORMAT)) {
+    ReportError(out);
+    return;
+  }
 
-  return o;
+  ReportSuccess(val, out);
 }
 
-const picojson::value::object TimeInstance::HandleToString(
-  const picojson::value& msg, DateTimeFormatType format) {
-  picojson::value::object o;
+void TimeInstance::Time_toDateString(const JsonValue& args, JsonObject& out) {
+  JsonValue val;
+  if (!this->toStringByFormat(args, val, TimeInstance::DATE_FORMAT)) {
+    ReportError(out);
+    return;
+  }
+
+  ReportSuccess(val, out);
+}
 
+void TimeInstance::Time_toTimeString(const JsonValue& args, JsonObject& out) {
+  JsonValue val;
+  if(!this->toStringByFormat(args, val, TimeInstance::TIME_FORMAT)) {
+    ReportError(out);
+    return;
+  }
+
+  ReportSuccess(val, out);
+}
+
+bool TimeInstance::toStringByFormat(const JsonValue& args, JsonValue& out,
+                                     DateTimeFormatType format) {
   std::unique_ptr<UnicodeString> id(
-    new UnicodeString(msg.get("timezone").to_str().c_str()));
-  bool bLocale = msg.get("locale").evaluate_as_boolean();
+    new UnicodeString(args.get("timezone").to_str().c_str()));
+  bool bLocale = args.get("locale").evaluate_as_boolean();
 
-  UDate dateInMs = strtod(msg.get("value").to_str().c_str(), NULL);
+  UDate dateInMs = strtod(args.get("value").to_str().c_str(), NULL);
   if (errno == ERANGE)
-    return o;
+    return false;
 
   UErrorCode ec = U_ZERO_ERROR;
   std::unique_ptr<Calendar> cal(
     Calendar::createInstance(TimeZone::createTimeZone(*id), ec));
   if (U_FAILURE(ec))
-    return o;
+    return false;
 
   cal->setTime(dateInMs, ec);
   if (U_FAILURE(ec))
-    return o;
+    return false;
 
   std::unique_ptr<DateFormat> fmt(
     new SimpleDateFormat(getDateTimeFormat(format, bLocale),
                         (bLocale ? Locale::getDefault() : Locale::getEnglish()),
                          ec));
   if (U_FAILURE(ec))
-    return o;
+    return false;
 
   UnicodeString uResult;
   fmt->setCalendar(*cal);
   fmt->format(cal->getTime(ec), uResult);
   if (U_FAILURE(ec))
-    return o;
+    return false;
 
   std::string result = "";
   uResult.toUTF8String(result);
 
-  o["value"] = picojson::value(result);
-
-  return o;
+  out = JsonValue(result);
+  return true;
 }
 
-const picojson::value::object TimeInstance::HandleGetTimeFormat(
-  const picojson::value& msg) {
-  picojson::value::object o;
-
+void TimeInstance::Time_getTimeFormat(const JsonValue& /*args*/,
+                                      JsonObject& out) {
   UnicodeString timeFormat = getDateTimeFormat(TimeInstance::TIME_FORMAT, true);
 
   timeFormat = timeFormat.findAndReplace("H", "h");
@@ -303,9 +320,7 @@ const picojson::value::object TimeInstance::HandleGetTimeFormat(
   std::string result = "";
   timeFormat.toUTF8String(result);
 
-  o["value"] = picojson::value(result);
-
-  return o;
+  ReportSuccess(JsonValue(result), out);
 }
 
 
@@ -350,3 +365,6 @@ UnicodeString TimeInstance::getDateTimeFormat(DateTimeFormatType type,
 
   return pattern;
 }
+
+}  // namespace time
+}  // namespace extension
index ecf76b7a2965fcda6b3f284c51eccaa0920ebe7a..08a62d50f446bf87b51a1eff10e8cc7ac792a7c0 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2015 Samsung Electronics Co, Ltd. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 #include "common/picojson.h"
 #include "unicode/unistr.h"
 
-class TimeInstance : public common::Instance {
+#include <string>
+
+namespace extension {
+namespace time {
+
+typedef picojson::value JsonValue;
+typedef picojson::object JsonObject;
+typedef picojson::array JsonArray;
+typedef std::string JsonString;
+
+class TimeInstance : public common::ParsedInstance {
  public:
   TimeInstance();
+  static TimeInstance& GetInstance();
   virtual ~TimeInstance();
 
  private:
-  // common::Instance implementation.
-  virtual void HandleMessage(const char* msg);
-  virtual void HandleSyncMessage(const char* msg);
-
   enum DateTimeFormatType {
     TIME_FORMAT,
     DATE_FORMAT,
@@ -26,24 +34,22 @@ class TimeInstance : public common::Instance {
     DATETIME_FORMAT
   };
 
-  const picojson::value::object
-     HandleGetLocalTimeZone(const picojson::value& msg);
-  const picojson::value::object
-     HandleGetAvailableTimeZones(const picojson::value& msg);
-  const picojson::value::object
-     HandleGetTimeZoneOffset(const picojson::value& msg);
-  const picojson::value::object
-     HandleGetTimeZoneAbbreviation(const picojson::value& msg);
-  const picojson::value::object
-     HandleIsDST(const picojson::value& msg);
-  const picojson::value::object
-     HandleGetDSTTransition(const picojson::value& msg);
-  const picojson::value::object
-     HandleToString(const picojson::value& msg, DateTimeFormatType type);
-  const picojson::value::object
-     HandleGetTimeFormat(const picojson::value& msg);
+  void Time_getAvailableTimeZones(const JsonValue& args, JsonObject& out);
+  void Time_getDSTTransition(const JsonValue& args, JsonObject& out);
+  void Time_getLocalTimeZone(const JsonValue& args, JsonObject& out);
+  void Time_getTimeFormat(const JsonValue& args, JsonObject& out);
+  void Time_getTimeZoneOffset(const JsonValue& args, JsonObject& out);
+  void Time_getTimeZoneAbbreviation(const JsonValue& args, JsonObject& out);
+  void Time_isDST(const JsonValue& args, JsonObject& out);
+  void Time_toString(const JsonValue& args, JsonObject& out);
+  void Time_toDateString(const JsonValue& args, JsonObject& out);
+  void Time_toTimeString(const JsonValue& args, JsonObject& out);
 
   UnicodeString getDateTimeFormat(DateTimeFormatType type, bool bLocale);
+  bool toStringByFormat(const JsonValue& args, JsonValue& out,
+                        DateTimeFormatType format);
 };
+}  // namespace time
+}  // namespace extension
 
 #endif  // TIME_TIME_INSTANCE_H_