Wildcard character support in app-control uri.
authorTaejeong Lee <taejeong.lee@samsung.com>
Mon, 11 Mar 2013 10:12:30 +0000 (19:12 +0900)
committerGerrit Code Review <gerrit2@kim11>
Tue, 12 Mar 2013 07:20:14 +0000 (16:20 +0900)
[Issue#]   N/A
[Bug]      Wrt app-control service doesn't support a uri which has wildcard character(*).
[Cause]    N/A
[Solution] Implementation to support wildcard character was added.

Change-Id: Idf61a5089a774081c10563003cfd994a46939223

src/view/common/view_logic_uri_support.cpp

index 92374d2..b636eea 100644 (file)
@@ -33,6 +33,7 @@
 #include <wrt-commons/custom-handler-dao-ro/CustomHandlerDatabase.h>
 #include <wrt-commons/custom-handler-dao-ro/custom_handler_dao_read_only.h>
 #include <stdio.h>
+#include <pcrecpp.h>
 
 namespace ViewModule {
 namespace UriSupport {
@@ -40,29 +41,13 @@ namespace {
 enum ServiceDataType
 {
     SERVICE_DATA_TYPE_OPERATION,
+    SERVICE_DATA_TYPE_URI,
     SERVICE_DATA_TYPE_URI_SCHEME,
-    SERVICE_DATA_TYPE_MIME,
-    SERVICE_DATA_TYPE_MIME_ELEMENT
+    SERVICE_DATA_TYPE_MIME
 };
 
 char const * const SCHEME_TYPE_FILE = "file";
 char const * const SCHEME_TYPE_WIDGET = "widget";
-char const * const SERVICE_DATA_WILD_CHAR = "*";
-char const * const SERVICE_DATA_MIME_TOKEN = "/";
-const unsigned int SIZE_OF_MIME_ELEMENT = 2;
-
-std::list<std::string> parsingMimeData(std::string origin, std::string token)
-{
-    std::list<std::string> parsedData;
-    size_t current = 0;
-    size_t next = 0;
-    do {
-        next = origin.find_first_of(token, current);
-        parsedData.push_back(origin.substr(current, next - current));
-        current = next + 1;
-    } while (next != std::string::npos && current < origin.length());
-    return parsedData;
-}
 
 /**
  * Parses scheme from appsvc. From mailto:test@wacapps.net
@@ -82,47 +67,53 @@ std::vector<std::string> parseScheme(const std::string &scheme)
     return schemeParts;
 }
 
+bool wildcardCompare(std::string wildcardString, std::string target)
+{
+    std::string re = wildcardString;
+
+    // replace special character to meaning character
+    pcrecpp::RE("\\\\").GlobalReplace("\\\\\\\\", &re);
+    pcrecpp::RE("\\.").GlobalReplace("\\\\.", &re);
+    pcrecpp::RE("\\+").GlobalReplace("\\\\+", &re);
+    pcrecpp::RE("\\?").GlobalReplace("\\\\?", &re);
+    pcrecpp::RE("\\^").GlobalReplace("\\\\^", &re);
+    pcrecpp::RE("\\$").GlobalReplace("\\\\$", &re);
+    pcrecpp::RE("\\[").GlobalReplace("\\\\[", &re);
+    pcrecpp::RE("\\]").GlobalReplace("\\\\]", &re);
+    pcrecpp::RE("\\{").GlobalReplace("\\\\{", &re);
+    pcrecpp::RE("\\}").GlobalReplace("\\\\}", &re);
+    pcrecpp::RE("\\(").GlobalReplace("\\\\(", &re);
+    pcrecpp::RE("\\)").GlobalReplace("\\\\)", &re);
+    pcrecpp::RE("\\|").GlobalReplace("\\\\|", &re);
+
+    // replace wildcard character to regex type
+    pcrecpp::RE("\\\*").GlobalReplace(".*", &re);
+
+    return pcrecpp::RE(re).FullMatch(target);
+}
+
 bool compareServiceData(ServiceDataType type,
                         std::string origin,
                         std::string other)
 {
-    if (SERVICE_DATA_TYPE_OPERATION == type) {
+    if (SERVICE_DATA_TYPE_OPERATION == type)
+    {
         return origin == other;
-    } else if (SERVICE_DATA_TYPE_URI_SCHEME == type
-               || SERVICE_DATA_TYPE_MIME_ELEMENT == type)
+    }
+    else if (SERVICE_DATA_TYPE_URI == type)
+    {
+        return wildcardCompare(origin, other);
+    }
+    else if (SERVICE_DATA_TYPE_URI_SCHEME == type)
+    {
+        return origin == other;
+    }
+    else if (SERVICE_DATA_TYPE_MIME == type)
+    {
+        return wildcardCompare(origin, other);
+    }
+    else
     {
-        if (SERVICE_DATA_WILD_CHAR == origin) {
-            return true;
-        } else {
-            return origin == other;
-        }
-    } else if (SERVICE_DATA_TYPE_MIME == type) {
-        if (other == "") {
-            return origin == other;
-        }
-
-        std::list<std::string> vectorOrigin =
-            parsingMimeData(origin, SERVICE_DATA_MIME_TOKEN);
-        if (SIZE_OF_MIME_ELEMENT != vectorOrigin.size()) {
-            return false;
-        }
-        std::list<std::string> vectorOther =
-            parsingMimeData(other, SERVICE_DATA_MIME_TOKEN);
-        if (SIZE_OF_MIME_ELEMENT != vectorOther.size()) {
-            return false;
-        }
-
-        FOREACH(it, vectorOrigin) {
-            if (!compareServiceData(SERVICE_DATA_TYPE_MIME_ELEMENT,
-                                    *it,
-                                    vectorOther.front()))
-            {
-                return false;
-            }
-            vectorOther.pop_front();
-        }
-        return true;
-    } else {
         LogError("Wrong data type");
         return false;
     }
@@ -182,7 +173,8 @@ std::string getCustomHandlerContentUri(
 
 std::string getAppServiceUri(bundle *bundle, WidgetModel *widgetModel)
 {
-    if (!bundle) {
+    if (!bundle)
+    {
         LogError("Bundle is empty");
         return std::string("");
     }
@@ -196,35 +188,43 @@ std::string getAppServiceUri(bundle *bundle, WidgetModel *widgetModel)
 
     value = appsvc_get_uri(bundle);
     std::string uri = value ? value : "";
+
     std::vector<std::string> schemeParts = parseScheme(uri);
     std::string scheme = (schemeParts.empty()) ? "" : schemeParts[0];
 
     std::string ignoreScheme = "file";
-
     if (scheme == ignoreScheme)
     {
         LogInfo("exception : 'file' scheme is ignored.");
+        uri = "";
         scheme = "";
     }
 
-    LogDebug("operation : " << operation);
-    LogDebug("schemeType : " << scheme);
-    LogDebug("mimetype : " << mime);
-
-    WidgetApplicationServiceList appServiceList =
-        widgetModel->AppServiceList.Get();
-    FOREACH(appServiceIt, appServiceList) {
-        if (compareServiceData(SERVICE_DATA_TYPE_OPERATION,
-                               DPL::ToUTF8String(appServiceIt->operation),
-                               operation) &&
-            compareServiceData(SERVICE_DATA_TYPE_URI_SCHEME,
-                               DPL::ToUTF8String(appServiceIt->scheme),
-                               scheme) &&
-            compareServiceData(SERVICE_DATA_TYPE_MIME,
-                               DPL::ToUTF8String(appServiceIt->mime),
-                               mime))
+    LogDebug("AppService Information");
+    LogDebug(" - operation : " << operation);
+    LogDebug(" - uri : " << uri);
+    LogDebug(" - scheme : " << scheme);
+    LogDebug(" - mimetype : " << mime);
+
+    WidgetApplicationServiceList appServiceList = widgetModel->AppServiceList.Get();
+
+    FOREACH(appServiceIt, appServiceList)
+    {
+        if (compareServiceData(SERVICE_DATA_TYPE_OPERATION, DPL::ToUTF8String(appServiceIt->operation), operation))
         {
-            return DPL::ToUTF8String(appServiceIt->src);
+            if (compareServiceData(SERVICE_DATA_TYPE_URI_SCHEME, DPL::ToUTF8String(appServiceIt->scheme), scheme) ||
+                compareServiceData(SERVICE_DATA_TYPE_URI, DPL::ToUTF8String(appServiceIt->scheme), uri) )
+            {
+                if (compareServiceData(SERVICE_DATA_TYPE_MIME, DPL::ToUTF8String(appServiceIt->mime), mime))
+                {
+                    LogDebug("AppService Matched with : " << appServiceIt->src);
+                    LogDebug(" - operation : " << appServiceIt->operation);
+                    LogDebug(" - uri : " << appServiceIt->scheme);
+                    LogDebug(" - mimetype : " << appServiceIt->mime);
+
+                    return DPL::ToUTF8String(appServiceIt->src);
+                }
+            }
         }
     }