[Package] Obtain total size and data size on demand.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 26 Jun 2015 09:01:50 +0000 (11:01 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 26 Jun 2015 11:58:02 +0000 (20:58 +0900)
These are time consuming operations, should not be executed
unless user explicitly asks for this data

Related issue: http://168.219.209.56/jira/browse/TNEF-2498

[Verification] TCT pass rate: 66/66/0/0/0.

Change-Id: I3c45a8f81f1406b36599f0709e91de5c1dcb99a2
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/package/package_api.js
src/package/package_info_provider.cc
src/package/package_info_provider.h
src/package/package_instance.cc
src/package/package_instance.h

index 0bd248f3518517976ef4dfbc0e67758188de619b..58e49b441b6673ca9b31f5f638b713d09031f28c 100644 (file)
@@ -56,7 +56,7 @@ function callNative(cmd, args) {
   }
 
   if (result['status'] == 'success') {
-    if (result['result']) {
+    if (undefined !== result['result']) {
       return result['result'];
     }
     return true;
@@ -90,18 +90,48 @@ function SetReadOnlyProperty(obj, n, v) {
 function PackageInformation(obj) {
   var lastModified = obj.lastModified;
   obj.lastModified = new Date(lastModified);
-  SetReadOnlyProperty(obj, 'id'); // read only property
-  SetReadOnlyProperty(obj, 'name'); // read only property
-  SetReadOnlyProperty(obj, 'iconPath'); // read only property
-  SetReadOnlyProperty(obj, 'version'); // read only property
-  SetReadOnlyProperty(obj, 'totalSize'); // read only property
-  SetReadOnlyProperty(obj, 'dataSize'); // read only property
-  SetReadOnlyProperty(obj, 'lastModified'); // read only property
-  SetReadOnlyProperty(obj, 'author'); // read only property
-  SetReadOnlyProperty(obj, 'description'); // read only property
-  SetReadOnlyProperty(obj, 'appIds'); // read only property
-
-  return obj;
+
+  SetReadOnlyProperty(this, 'id', obj.id); // read only property
+  SetReadOnlyProperty(this, 'name', obj.name); // read only property
+  SetReadOnlyProperty(this, 'iconPath', obj.iconPath); // read only property
+  SetReadOnlyProperty(this, 'version', obj.version); // read only property
+  SetReadOnlyProperty(this, 'lastModified', obj.lastModified); // read only property
+  SetReadOnlyProperty(this, 'author', obj.author); // read only property
+  SetReadOnlyProperty(this, 'description', obj.description); // read only property
+  SetReadOnlyProperty(this, 'appIds', obj.appIds); // read only property
+
+  var totalSize;
+  var dataSize;
+
+  Object.defineProperty(this, 'totalSize', {
+    enumerable: true,
+    set: function() {},
+    get: function() {
+      if (undefined === totalSize) {
+        try {
+          totalSize = callNative('PackageManager_getTotalSize', {id: this.id});
+        } catch (e) {
+          totalSize = -1;
+        }
+      }
+      return totalSize;
+    }
+  });
+
+  Object.defineProperty(this, 'dataSize', {
+    enumerable: true,
+    set: function() {},
+    get: function() {
+      if (undefined === dataSize) {
+        try {
+          dataSize = callNative('PackageManager_getDataSize', {id: this.id});
+        } catch (e) {
+          dataSize = -1;
+        }
+      }
+      return dataSize;
+    }
+  });
 }
 
 function PackageManager() {
@@ -210,7 +240,7 @@ PackageManager.prototype.getPackagesInfo = function(successCallback, errorCallba
         function(result) {
           if (result.status == 'success') {
             for (var i = 0; i < result.informationArray.length; i++) {
-              result.informationArray[i] = PackageInformation(result.informationArray[i]);
+              result.informationArray[i] = new PackageInformation(result.informationArray[i]);
             }
             args.successCallback(result.informationArray);
           } else if (result.status == 'error') {
@@ -244,7 +274,7 @@ PackageManager.prototype.getPackageInfo = function() {
 
   try {
     var syncResult = callNative('PackageManager_getPackageInfo', nativeParam);
-    return PackageInformation(syncResult);
+    return new PackageInformation(syncResult);
   } catch (e) {
     throw e;
   }
@@ -270,9 +300,9 @@ PackageManager.prototype.setPackageInfoEventListener = function(eventCallback) {
         nativeParam,
         function(result) {
           if (result.status == 'installed') {
-            args.eventCallback.oninstalled(PackageInformation(result.info));
+            args.eventCallback.oninstalled(new PackageInformation(result.info));
           } else if (result.status == 'updated') {
-            args.eventCallback.onupdated(PackageInformation(result.info));
+            args.eventCallback.onupdated(new PackageInformation(result.info));
           } else if (result.status == 'uninstalled') {
             args.eventCallback.onuninstalled(result.id);
           }
index 0c829d4297d0e52a55f61a4d6065c07b9cf7b74e..557293e1643e7ecb01a920c536e342766e25df6b 100644 (file)
@@ -27,6 +27,7 @@
 #include <functional>
 
 #include "common/logger.h"
+#include "common/tools.h"
 
 namespace extension {
 namespace package {
@@ -34,6 +35,11 @@ namespace package {
 using common::UnknownException;
 using common::NotFoundException;
 
+using common::ErrorCode;
+using common::PlatformResult;
+using common::tools::ReportError;
+using common::tools::ReportSuccess;
+
 #define REPORT_ERROR(out, exception) \
   out["status"] = picojson::value("error"); \
   out["error"] = exception.ToJSON();
@@ -212,32 +218,6 @@ bool PackageInfoProvider:: ConvertToPackageToObject(
     return false;
   }
 
-  pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST);
-  if ( pc == NULL ) {
-    LoggerE("Fail to create pkgmgr client");
-    return false;
-  } else {
-    int ret = pkgmgr_client_request_service(PM_REQUEST_GET_SIZE,
-        PM_GET_TOTAL_SIZE, pc, NULL, id, NULL, NULL, NULL);
-    if ( ret < 0 ) {
-      LoggerE("Fail to get total size");
-      return false;
-    } else {
-      LoggerD("totalSize: [%d]", ret);
-      out["totalSize"] = picojson::value(static_cast<double>(ret));
-    }
-
-    ret = pkgmgr_client_request_service(PM_REQUEST_GET_SIZE, PM_GET_DATA_SIZE,
-        pc, NULL, id, NULL, NULL, NULL);
-    if ( ret < 0 ) {
-      LoggerE("Fail to get data size");
-      return false;
-    } else {
-      LoggerD("dataSize: [%d]", ret);
-      out["dataSize"] = picojson::value(static_cast<double>(ret));
-    }
-  }
-
   picojson::array array_data;
   ret = package_info_foreach_app_from_package(package_info,
       PACKAGE_INFO_ALLAPP, PackageAppInfoCb, &array_data);
@@ -255,6 +235,39 @@ bool PackageInfoProvider:: ConvertToPackageToObject(
   return true;
 }
 
+namespace {
+
+void GetSize(const std::string& id, int service_mode, picojson::object* out) {
+  LoggerD("Enter");
+  pkgmgr_client* pc = pkgmgr_client_new(PC_REQUEST);
+  int size = pkgmgr_client_request_service(PM_REQUEST_GET_SIZE, service_mode,
+                                           pc,
+                                           NULL,
+                                           id.c_str(), NULL, NULL, NULL);
+  pkgmgr_client_free(pc);
+
+  if (size < 0) {
+    ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to get size"),
+                out);
+  } else {
+    ReportSuccess(picojson::value(static_cast<double>(size)), *out);
+  }
+}
+
+}  // namespace
+
+void PackageInfoProvider::GetTotalSize(const std::string& id,
+                                       picojson::object* out) {
+  LoggerD("Enter");
+  GetSize(id, PM_GET_TOTAL_SIZE, out);
+}
+
+void PackageInfoProvider::GetDataSize(const std::string& id,
+                                      picojson::object* out) {
+  LoggerD("Enter");
+  GetSize(id, PM_GET_DATA_SIZE, out);
+}
+
 bool PackageInfoProvider::GetCurrentPackageId(
     char** package_id) {
   LoggerD("Enter");
index 1e28e410ee5a65142f288a3d149a6f6fc77f644a..e08cfee374d3fe61bc393b642d3c5aadbbe15cdf 100644 (file)
@@ -52,6 +52,9 @@ class PackageInfoProvider {
   static bool ConvertToPackageToObject(
       const pkgmgrinfo_pkginfo_h info, picojson::object& out);
 
+  static void GetTotalSize(const std::string& id, picojson::object* out);
+  static void GetDataSize(const std::string& id, picojson::object* out);
+
  private:
   static bool GetCurrentPackageId(char** package_id);
 };
index 9cdc5b16af88a5b6e24e2e4d99eda2517856b70c..d4a23c7c6fef32355822e2665b818de3196d55bf 100644 (file)
@@ -34,6 +34,9 @@ using common::NotFoundException;
 using common::TypeMismatchException;
 using common::SecurityException;
 
+using common::ErrorCode;
+using common::PlatformResult;
+
 typedef enum _PackageThreadWorkType {
   PackageThreadWorkNone = 0,
   PackageThreadWorkGetPackagesInfo,
@@ -246,6 +249,10 @@ PackageInstance::PackageInstance() {
       PackageManagerUnsetpackageinfoeventlistener);
   REGISTER_SYNC("PackageManager_getPackageInfo",
       PackageManagerGetpackageinfo);
+  REGISTER_SYNC("PackageManager_getTotalSize",
+      PackageManagerGetTotalSize);
+  REGISTER_SYNC("PackageManager_getDataSize",
+      PackageManagerGetDataSize);
   #undef REGISTER_SYNC
 }
 
@@ -394,6 +401,34 @@ void PackageInstance::PackageManagerGetpackageinfo(
   }
 }
 
+void PackageInstance::PackageManagerGetTotalSize(const picojson::value& args,
+                                                 picojson::object& out) {
+  LoggerD("Enter");
+
+  const auto& id = args.get("id");
+
+  if (id.is<std::string>()) {
+    PackageInfoProvider::GetTotalSize(id.get<std::string>(), &out);
+  } else {
+    ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Missing id parameter"),
+                &out);
+  }
+}
+
+void PackageInstance::PackageManagerGetDataSize(const picojson::value& args,
+                                                picojson::object& out) {
+  LoggerD("Enter");
+
+  const auto& id = args.get("id");
+
+  if (id.is<std::string>()) {
+    PackageInfoProvider::GetDataSize(id.get<std::string>(), &out);
+  } else {
+    ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Missing id parameter"),
+                &out);
+  }
+}
+
 void PackageInstance::InvokeListener(picojson::object& param) {
   LoggerD("Enter");
   picojson::value result = picojson::value(param);
index c1c2426ce7afeccfed328563ab8bdf1d296b19bf..2d22d81fe1f1d3c24f10e3e4dac3356b90edae93 100644 (file)
@@ -54,6 +54,10 @@ class PackageInstance : public common::ParsedInstance {
     (const picojson::value& args, picojson::object& out);
   void PackageManagerGetpackageinfo
     (const picojson::value& args, picojson::object& out);
+  void PackageManagerGetTotalSize(const picojson::value& args,
+                                  picojson::object& out);
+  void PackageManagerGetDataSize(const picojson::value& args,
+                                 picojson::object& out);
   void PackageManagerSetpackageinfoeventlistener
     (const picojson::value& args, picojson::object& out);
   void PackageManagerUnsetpackageinfoeventlistener