Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest_handlers / tizen_appwidget_handler.cc
index f3ea966..03dcc1f 100644 (file)
@@ -33,6 +33,10 @@ const char kErrMsgInvalidKeyValue[] =
     "Invalid key value. Key name: ";
 const char kErrMsgMultipleKeys[] =
     "Too many keys found. Key name: ";
+const char kErrMsgNoNamespace[] =
+    "Element pointed by key has no namespace specified. Key name: ";
+const char kErrMsgInvalidNamespace[] =
+    "Invalid namespace of element pointed by key. Key name: ";
 const char kErrMsgAppWidgetInfoNotFound[] =
     "Cannot access app-widget info object.";
 const char kErrMsgApplicationInfoNotFound[] =
@@ -45,10 +49,6 @@ const char kErrMsgInvalidAppWidgetIdBeginning[] =
 const char kErrMsgInvalidAppWidgetIdFormat[] =
     "Invalid format of an id attribute value in app-widget element."
     " The value: ";
-const char kErrMsgNoPrimaryAppWidget[] =
-    "No primary app-widget element (primary='true').";
-const char kErrMsgToManyPrimaryAppWidgets[] =
-    "Too many primary app-widget elements (primary='true').";
 const char kErrMsgUpdatePeriodOutOfDomain[] =
     "Value of an update-period attribute in app-widget element out of domain."
     " The value: ";
@@ -69,18 +69,54 @@ const char kErrMsgContentDropViewHeightOutOfDomain[] =
     "Value of a height attribute in box-content element out of domain."
     " The value: ";
 
+// If the error parameter is specified, it is filled with the given message
+// otherwise it does nothing.
+void SetError(const std::string& message,
+    std::string* error) {
+  if (error)
+    *error = message;
+}
+
+// If the error parameter is specified, it is filled with concatenation
+// of message and arg parameters otherwise it does nothing.
 void SetError(const std::string& message,
     const std::string& arg, std::string* error) {
   if (error)
     *error = message + arg;
 }
 
+// If the error parameter is specified, it is filled with the given message
+// otherwise it does nothing.
+void SetError(const std::string& message,
+    base::string16* error) {
+  if (error)
+    *error = base::ASCIIToUTF16(message);
+}
+
+// If the error parameter is specified, it is filled with concatenation
+// of message and arg parameters otherwise it does nothing.
 void SetError(const std::string& message,
     const std::string& arg, base::string16* error) {
   if (error)
     *error = base::ASCIIToUTF16(message + arg);
 }
 
+// Retrieves a mandatory dictionary from specified manifest and specified key.
+// Returns true, if the ditionary is found or false otherwise. If the error
+// parameter is specified, it is also filled with proper message.
+bool GetMandatoryDictionary(const Manifest& manifest, const std::string& key,
+    const base::DictionaryValue** dict, base::string16* error) {
+  if (!manifest.HasPath(key)) {
+    SetError(kErrMsgNoMandatoryKey, key, error);
+    return false;
+  }
+  if (!manifest.GetDictionary(key, dict) || !*dict) {
+    SetError(kErrMsgInvalidDictionary, key, error);
+    return false;
+  }
+  return true;
+}
+
 // Converts given text value to a value of specific type. Returns true
 // if convertion is successful or false otherwise.
 template <typename ValueType>
@@ -132,8 +168,8 @@ bool ConvertValue(const std::string& str_value, double* value) {
 }
 
 // Retrieves a mandatory value from specified dictionary and specified key.
-// Returns true if the value is found or false otherwise. If the error parameter
-// is specified it is also filled with proper message.
+// Returns true, if the value is found or false otherwise. If the error
+// parameter is specified, it is also filled with proper message.
 template <typename ValueType>
 bool GetMandatoryValue(const base::DictionaryValue& dict,
     const std::string& key, ValueType* value, base::string16* error) {
@@ -152,9 +188,9 @@ bool GetMandatoryValue(const base::DictionaryValue& dict,
 }
 
 // Retrieves an optional value from specified dictionary and specified key.
-// If the value is found the function returns true and fills value
-// parameter. If the value is not found the function returns true and fills
-// value parameter with default value. If an error occurs it returns false
+// If the value is found, the function returns true and fills value
+// parameter. If the value is not found, the function returns true and fills
+// value parameter with default value. If an error occurs, it returns false
 // and fills error parameter if it is set.
 template <typename ValueType>
 bool GetOptionalValue(const base::DictionaryValue& dict,
@@ -230,11 +266,34 @@ bool ParseEach(const base::DictionaryValue& dict, const std::string& key,
   return true;
 }
 
+// Verifies whether specified dictionary represents an element in specified
+// namespace. Returns true, if the namespace is set and equal to the specified
+// one or false otherwise. If the error parameter is specified, it is also
+// filled with proper message.
+bool VerifyElementNamespace(const base::DictionaryValue& dict,
+    const std::string& key, const std::string& desired_namespace_value,
+    base::string16* error) {
+  std::string namespace_value;
+  if (!GetMandatoryValue(dict, keys::kNamespaceKey,
+      &namespace_value, nullptr)) {
+    SetError(kErrMsgNoNamespace, key, error);
+    return false;
+  }
+  if (namespace_value != desired_namespace_value) {
+    SetError(kErrMsgInvalidNamespace, key, error);
+    return false;
+  }
+  return true;
+}
+
 // Parses box-label part
 bool ParseLabel(const base::DictionaryValue& dict,
     const std::string& key, TizenAppWidget* app_widget, base::string16* error) {
   DCHECK(app_widget);
 
+  if (!VerifyElementNamespace(dict, key, kTizenNamespacePrefix, error))
+    return false;
+
   std::string lang;
   if (!GetOptionalValue(dict, keys::kTizenAppWidgetBoxLabelLangKey,
       std::string(), &lang, error))
@@ -263,6 +322,9 @@ bool ParseIcon(const base::DictionaryValue& dict,
     const std::string& key, TizenAppWidget* app_widget, base::string16* error) {
   DCHECK(app_widget);
 
+  if (!VerifyElementNamespace(dict, key, kTizenNamespacePrefix, error))
+    return false;
+
   if (!app_widget->icon_src.empty()) {
     SetError(kErrMsgMultipleKeys, key, error);
     return false;
@@ -298,6 +360,9 @@ bool ParseContentSizes(const base::DictionaryValue& dict,
     const std::string& key, TizenAppWidget* app_widget, base::string16* error) {
   DCHECK(app_widget);
 
+  if (!VerifyElementNamespace(dict, key, kTizenNamespacePrefix, error))
+    return false;
+
   TizenAppWidgetSize size;
 
   std::string str_type;
@@ -332,6 +397,9 @@ bool ParseContentDropView(const base::DictionaryValue& dict,
     const std::string& key, TizenAppWidget* app_widget, base::string16* error) {
   DCHECK(app_widget);
 
+  if (!VerifyElementNamespace(dict, key, kTizenNamespacePrefix, error))
+    return false;
+
   if (!app_widget->content_drop_view.empty()) {
     SetError(kErrMsgMultipleKeys, key, error);
     return false;
@@ -363,6 +431,9 @@ bool ParseContent(const base::DictionaryValue& dict,
     const std::string& key, TizenAppWidget* app_widget, base::string16* error) {
   DCHECK(app_widget);
 
+  if (!VerifyElementNamespace(dict, key, kTizenNamespacePrefix, error))
+    return false;
+
   if (!app_widget->content_src.empty()) {
     SetError(kErrMsgMultipleKeys, key, error);
     return false;
@@ -396,6 +467,9 @@ bool ParseAppWidget(const base::DictionaryValue& dict,
     base::string16* error) {
   DCHECK(app_widgets);
 
+  if (!VerifyElementNamespace(dict, key, kTizenNamespacePrefix, error))
+    return false;
+
   TizenAppWidget app_widget;
 
   if (!GetMandatoryValue(dict, keys::kTizenAppWidgetIdKey,
@@ -423,7 +497,7 @@ bool ParseAppWidget(const base::DictionaryValue& dict,
     return false;
 
   if (!ParseEach(dict, keys::kTizenAppWidgetBoxIconKey,
-      true, ParseIcon, &app_widget, error))
+      false, ParseIcon, &app_widget, error))
     return false;
 
   if (!ParseEach(dict, keys::kTizenAppWidgetBoxContentKey,
@@ -463,28 +537,6 @@ bool ValidateEachId(const TizenAppWidgetVector& app_widgets,
   return true;
 }
 
-// Validates all app-widget primary attributes
-bool ValidateEachPrimary(const TizenAppWidgetVector& app_widgets,
-    std::string* error) {
-  int primary_count = 0;
-
-  for (const TizenAppWidget& app_widget : app_widgets)
-    if (app_widget.primary)
-      ++primary_count;
-
-  if (!primary_count) {
-    SetError(kErrMsgNoPrimaryAppWidget, "", error);
-    return false;
-  }
-
-  if (primary_count > 1) {
-    SetError(kErrMsgToManyPrimaryAppWidgets, "", error);
-    return false;
-  }
-
-  return true;
-}
-
 // Tests if specified string represents valid remote url
 bool IsValidUrl(const std::string& value) {
   // TODO(tweglarski): implement me (it's not crucial atm)
@@ -517,7 +569,7 @@ bool ValidateContentSize(const TizenAppWidgetSizeVector& content_size,
   }
 
   if (!mandatory_1x1_found) {
-    SetError(kErrMsgNoMandatoryContentSize1x1, "", error);
+    SetError(kErrMsgNoMandatoryContentSize1x1, error);
     return false;
   }
 
@@ -544,16 +596,9 @@ bool TizenAppWidgetHandler::Parse(scoped_refptr<ApplicationData> application,
   const Manifest* manifest = application->GetManifest();
   DCHECK(manifest);
 
-  if (!manifest->HasPath(keys::kTizenWidgetKey)) {
-    SetError(kErrMsgNoMandatoryKey, keys::kTizenWidgetKey, error);
-    return false;
-  }
-
   const base::DictionaryValue* dict = nullptr;
-  if (!manifest->GetDictionary(keys::kTizenWidgetKey, &dict) || !dict) {
-    SetError(kErrMsgInvalidDictionary, keys::kTizenWidgetKey, error);
+  if (!GetMandatoryDictionary(*manifest, keys::kTizenWidgetKey, &dict, error))
     return false;
-  }
 
   TizenAppWidgetVector app_widgets;
 
@@ -578,11 +623,11 @@ bool TizenAppWidgetHandler::Validate(
           application->GetManifestData(keys::kTizenApplicationKey));
 
   if (!app_widget_info) {
-    SetError(kErrMsgAppWidgetInfoNotFound, "", error);
+    SetError(kErrMsgAppWidgetInfoNotFound, error);
     return false;
   }
   if (!app_info) {
-    SetError(kErrMsgApplicationInfoNotFound, "", error);
+    SetError(kErrMsgApplicationInfoNotFound, error);
     return false;
   }
 
@@ -591,9 +636,6 @@ bool TizenAppWidgetHandler::Validate(
   if (!ValidateEachId(app_widgets, app_info->id(), error))
     return false;
 
-  if (!ValidateEachPrimary(app_widgets, error))
-    return false;
-
   for (const TizenAppWidget& app_widget : app_widgets) {
     if (!app_widget.update_period.empty()
         && app_widget.update_period.front() < 1800) {
@@ -604,7 +646,7 @@ bool TizenAppWidgetHandler::Validate(
 
     if (app_widget.label.default_value.empty()
         && app_widget.label.lang_value_map.empty()) {
-      SetError(kErrMsgNoLabel, "", error);
+      SetError(kErrMsgNoLabel, error);
       return false;
     }