[Common] Added cache for JS privilege checking 48/239648/2
authorPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Tue, 28 Jul 2020 10:38:04 +0000 (12:38 +0200)
committerPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Wed, 29 Jul 2020 07:03:02 +0000 (09:03 +0200)
Commit improves the performance of asking for application privileges.
Values are cached.

[Verification]
Code compiles without errors.
Privilege TCT passrate 100%

Change-Id: Id1772d9b14e4d4ff07e6a7ca0993027b98a4f2f4

src/utils/utils_api.js

index 70424a535a67b02803c4147932ff3e3b66ffe625..6e4a3dfef97fc604a15fc5ccb07c304c9a8c7401 100644 (file)
@@ -253,12 +253,17 @@ Utils.prototype.repackFilter = function(filter) {
     return null;
 };
 
+var apiVersion = null;
 Utils.prototype.getPkgApiVersion = function() {
+    if (apiVersion) {
+        return apiVersion;
+    }
     var result = native_.callSync('UtilsGetPkgApiVersion');
     if (native_.isFailure(result)) {
         throw native_.getErrorObject(result);
     }
-    return native_.getResultObject(result);
+    apiVersion = native_.getResultObject(result);
+    return apiVersion;
 };
 
 var isPrivilege = function(toCheck) {
@@ -268,6 +273,7 @@ var isPrivilege = function(toCheck) {
     return true;
 };
 
+var cachedPrivileges = {};
 Utils.prototype.checkPrivilegeAccess = function(privilege) {
     if (!isPrivilege(privilege)) {
         xwalk.utils.error(
@@ -276,11 +282,15 @@ Utils.prototype.checkPrivilegeAccess = function(privilege) {
         throw new WebAPIException(WebAPIException.SECURITY_ERR);
     }
 
+    if (cachedPrivileges[privilege]) {
+        return;
+    }
     var result = native_.callSync('UtilsCheckPrivilegeAccess', {
         privilege: _toString(privilege)
     });
-
-    if (native_.isFailure(result)) {
+    var isFailure = native_.isFailure(result);
+    cachedPrivileges[privilege] = !isFailure;
+    if (isFailure) {
         throw native_.getErrorObject(result);
     }
 };