1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/application/common/manifest_handlers/widget_handler.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/string_split.h"
14 #include "xwalk/application/common/application_manifest_constants.h"
18 namespace keys = application_widget_keys;
21 // Below key names are readable from Javascript widget interface.
22 const char kAuthor[] = "author";
23 const char kDecription[] = "description";
24 const char kName[] = "name";
25 const char kShortName[] = "shortName";
26 const char kVersion[] = "version";
27 const char kID[] = "id";
28 const char kAuthorEmail[] = "authorEmail";
29 const char kAuthorHref[] = "authorHref";
30 const char kHeight[] = "height";
31 const char kWidth[] = "width";
32 const char kPreferences[] = "preferences";
34 // Child keys inside 'preferences' key.
35 const char kPreferencesName[] = "name";
36 const char kPreferencesValue[] = "value";
37 const char kPreferencesReadonly[] = "readonly";
39 typedef std::map<std::string, std::string> KeyMap;
40 typedef std::map<std::string, std::string>::const_iterator KeyMapIterator;
41 typedef std::pair<std::string, std::string> KeyPair;
43 const KeyMap& GetWidgetKeyPairs() {
46 map.insert(KeyPair(keys::kAuthorKey, kAuthor));
47 map.insert(KeyPair(keys::kDescriptionKey, kDecription));
48 map.insert(KeyPair(keys::kNameKey, kName));
49 map.insert(KeyPair(keys::kShortNameKey, kShortName));
50 map.insert(KeyPair(keys::kVersionKey, kVersion));
51 map.insert(KeyPair(keys::kIDKey, kID));
52 map.insert(KeyPair(keys::kAuthorEmailKey, kAuthorEmail));
53 map.insert(KeyPair(keys::kAuthorHrefKey, kAuthorHref));
54 map.insert(KeyPair(keys::kHeightKey, kHeight));
55 map.insert(KeyPair(keys::kWidthKey, kWidth));
61 void ParsePreferenceItem(const base::DictionaryValue* in_value,
62 base::DictionaryValue* out_value) {
63 DCHECK(in_value && in_value->IsType(base::Value::TYPE_DICTIONARY));
65 std::string pref_name;
66 std::string pref_value;
67 std::string pref_readonly;
68 if (in_value->GetString(keys::kPreferencesNameKey, &pref_name))
69 out_value->SetString(kPreferencesName, pref_name);
71 if (in_value->GetString(keys::kPreferencesValueKey, &pref_value))
72 out_value->SetString(kPreferencesValue, pref_value);
74 if (in_value->GetString(keys::kPreferencesReadonlyKey, &pref_readonly))
75 out_value->SetString(kPreferencesReadonly, pref_readonly);
80 namespace application {
82 WidgetInfo::WidgetInfo()
83 : value_(new base::DictionaryValue) {}
85 WidgetInfo::~WidgetInfo() {}
87 void WidgetInfo::SetString(const std::string& key, const std::string& value) {
88 value_->SetString(key, value);
91 void WidgetInfo::Set(const std::string& key, base::Value* value) {
92 value_->Set(key, value);
95 WidgetHandler::WidgetHandler() {}
97 WidgetHandler::~WidgetHandler() {}
99 bool WidgetHandler::Parse(scoped_refptr<ApplicationData> application,
100 base::string16* error) {
101 scoped_ptr<WidgetInfo> widget_info(new WidgetInfo);
102 const Manifest* manifest = application->GetManifest();
105 const KeyMap& map = GetWidgetKeyPairs();
107 for (KeyMapIterator iter = map.begin(); iter != map.end(); ++iter) {
109 manifest->GetString(iter->first, &string);
110 widget_info->SetString(iter->second, string);
113 base::Value* pref_value = NULL;
114 manifest->Get(keys::kPreferencesKey, &pref_value);
116 if (pref_value && pref_value->IsType(base::Value::TYPE_DICTIONARY)) {
117 base::DictionaryValue* preferences = new base::DictionaryValue;
118 base::DictionaryValue* dict;
119 pref_value->GetAsDictionary(&dict);
120 ParsePreferenceItem(dict, preferences);
121 widget_info->Set(kPreferences, preferences);
122 } else if (pref_value && pref_value->IsType(base::Value::TYPE_LIST)) {
123 base::ListValue* preferences = new base::ListValue;
124 base::ListValue* list;
125 pref_value->GetAsList(&list);
127 for (base::ListValue::iterator it = list->begin();
128 it != list->end(); ++it) {
129 base::DictionaryValue* pref = new base::DictionaryValue;
130 base::DictionaryValue* dict;
131 (*it)->GetAsDictionary(&dict);
132 ParsePreferenceItem(dict, pref);
133 preferences->Append(pref);
135 widget_info->Set(kPreferences, preferences);
138 application->SetManifestData(keys::kWidgetKey, widget_info.release());
142 bool WidgetHandler::AlwaysParseForType(Manifest::Type type) const {
146 std::vector<std::string> WidgetHandler::Keys() const {
147 return std::vector<std::string>(1, keys::kWidgetKey);
150 } // namespace application