OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/menu_manager.h" | 5 #include "chrome/browser/extensions/menu_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return items; | 81 return items; |
82 } | 82 } |
83 | 83 |
84 scoped_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) { | 84 scoped_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) { |
85 scoped_ptr<base::ListValue> list(new ListValue()); | 85 scoped_ptr<base::ListValue> list(new ListValue()); |
86 for (size_t i = 0; i < items.size(); ++i) | 86 for (size_t i = 0; i < items.size(); ++i) |
87 list->Append(items[i]->ToValue().release()); | 87 list->Append(items[i]->ToValue().release()); |
88 return scoped_ptr<Value>(list.release()); | 88 return scoped_ptr<Value>(list.release()); |
89 } | 89 } |
90 | 90 |
| 91 bool GetStringList(const DictionaryValue& dict, |
| 92 const std::string& key, |
| 93 std::vector<std::string>* out) { |
| 94 if (!dict.HasKey(key)) |
| 95 return true; |
| 96 |
| 97 const ListValue* list = NULL; |
| 98 if (!dict.GetListWithoutPathExpansion(key, &list)) |
| 99 return false; |
| 100 |
| 101 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 102 std::string pattern; |
| 103 if (!list->GetString(i, &pattern)) |
| 104 return false; |
| 105 out->push_back(pattern); |
| 106 } |
| 107 |
| 108 return true; |
| 109 } |
| 110 |
91 } // namespace | 111 } // namespace |
92 | 112 |
93 MenuItem::MenuItem(const Id& id, | 113 MenuItem::MenuItem(const Id& id, |
94 const std::string& title, | 114 const std::string& title, |
95 bool checked, | 115 bool checked, |
96 bool enabled, | 116 bool enabled, |
97 Type type, | 117 Type type, |
98 const ContextList& contexts) | 118 const ContextList& contexts) |
99 : id_(id), | 119 : id_(id), |
100 title_(title), | 120 title_(title), |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 MenuItem* MenuItem::Populate(const std::string& extension_id, | 216 MenuItem* MenuItem::Populate(const std::string& extension_id, |
197 const DictionaryValue& value, | 217 const DictionaryValue& value, |
198 std::string* error) { | 218 std::string* error) { |
199 bool incognito = false; | 219 bool incognito = false; |
200 if (!value.GetBoolean(kIncognitoKey, &incognito)) | 220 if (!value.GetBoolean(kIncognitoKey, &incognito)) |
201 return NULL; | 221 return NULL; |
202 Id id(incognito, extension_id); | 222 Id id(incognito, extension_id); |
203 if (!value.GetString(kStringUIDKey, &id.string_uid)) | 223 if (!value.GetString(kStringUIDKey, &id.string_uid)) |
204 return NULL; | 224 return NULL; |
205 int type_int; | 225 int type_int; |
206 Type type; | 226 Type type = NORMAL; |
207 if (!value.GetInteger(kTypeKey, &type_int)) | 227 if (!value.GetInteger(kTypeKey, &type_int)) |
208 return NULL; | 228 return NULL; |
209 type = static_cast<Type>(type_int); | 229 type = static_cast<Type>(type_int); |
210 std::string title; | 230 std::string title; |
211 if (type != SEPARATOR && !value.GetString(kTitleKey, &title)) | 231 if (type != SEPARATOR && !value.GetString(kTitleKey, &title)) |
212 return NULL; | 232 return NULL; |
213 bool checked; | 233 bool checked = false; |
214 if ((type == CHECKBOX || type == RADIO) && | 234 if ((type == CHECKBOX || type == RADIO) && |
215 !value.GetBoolean(kCheckedKey, &checked)) { | 235 !value.GetBoolean(kCheckedKey, &checked)) { |
216 return NULL; | 236 return NULL; |
217 } | 237 } |
218 bool enabled = true; | 238 bool enabled = true; |
219 if (!value.GetBoolean(kEnabledKey, &enabled)) | 239 if (!value.GetBoolean(kEnabledKey, &enabled)) |
220 return NULL; | 240 return NULL; |
221 ContextList contexts; | 241 ContextList contexts; |
222 const Value* contexts_value = NULL; | 242 const Value* contexts_value = NULL; |
223 if (!value.Get(kContextsKey, &contexts_value)) | 243 if (!value.Get(kContextsKey, &contexts_value)) |
224 return NULL; | 244 return NULL; |
225 if (!contexts.Populate(*contexts_value)) | 245 if (!contexts.Populate(*contexts_value)) |
226 return NULL; | 246 return NULL; |
227 | 247 |
228 scoped_ptr<MenuItem> result(new MenuItem( | 248 scoped_ptr<MenuItem> result(new MenuItem( |
229 id, title, checked, enabled, type, contexts)); | 249 id, title, checked, enabled, type, contexts)); |
230 | 250 |
231 if (!result->PopulateURLPatterns( | 251 std::vector<std::string> document_url_patterns; |
232 value, kDocumentURLPatternsKey, kTargetURLPatternsKey, error)) | 252 if (!GetStringList(value, kDocumentURLPatternsKey, &document_url_patterns)) |
233 return NULL; | 253 return NULL; |
| 254 std::vector<std::string> target_url_patterns; |
| 255 if (!GetStringList(value, kTargetURLPatternsKey, &target_url_patterns)) |
| 256 return NULL; |
| 257 |
| 258 if (!result->PopulateURLPatterns(&document_url_patterns, |
| 259 &target_url_patterns, |
| 260 error)) { |
| 261 return NULL; |
| 262 } |
234 | 263 |
235 // parent_id is filled in from the value, but it might not be valid. It's left | 264 // parent_id is filled in from the value, but it might not be valid. It's left |
236 // to be validated upon being added (via AddChildItem) to the menu manager. | 265 // to be validated upon being added (via AddChildItem) to the menu manager. |
237 scoped_ptr<Id> parent_id(new Id(incognito, extension_id)); | 266 scoped_ptr<Id> parent_id(new Id(incognito, extension_id)); |
238 if (value.HasKey(kParentUIDKey)) { | 267 if (value.HasKey(kParentUIDKey)) { |
239 if (!value.GetString(kParentUIDKey, &parent_id->string_uid)) | 268 if (!value.GetString(kParentUIDKey, &parent_id->string_uid)) |
240 return NULL; | 269 return NULL; |
241 result->parent_id_.swap(parent_id); | 270 result->parent_id_.swap(parent_id); |
242 } | 271 } |
243 return result.release(); | 272 return result.release(); |
244 } | 273 } |
245 | 274 |
246 bool MenuItem::PopulateURLPatterns(const DictionaryValue& properties, | 275 bool MenuItem::PopulateURLPatterns( |
247 const char* document_url_patterns_key, | 276 std::vector<std::string>* document_url_patterns, |
248 const char* target_url_patterns_key, | 277 std::vector<std::string>* target_url_patterns, |
249 std::string* error) { | 278 std::string* error) { |
250 if (properties.HasKey(document_url_patterns_key)) { | 279 if (document_url_patterns) { |
251 const ListValue* list = NULL; | |
252 if (!properties.GetList(document_url_patterns_key, &list)) | |
253 return false; | |
254 if (!document_url_patterns_.Populate( | 280 if (!document_url_patterns_.Populate( |
255 *list, URLPattern::SCHEME_ALL, true, error)) { | 281 *document_url_patterns, URLPattern::SCHEME_ALL, true, error)) { |
256 return false; | 282 return false; |
257 } | 283 } |
258 } | 284 } |
259 if (properties.HasKey(target_url_patterns_key)) { | 285 if (target_url_patterns) { |
260 const ListValue* list = NULL; | |
261 if (!properties.GetList(target_url_patterns_key, &list)) | |
262 return false; | |
263 if (!target_url_patterns_.Populate( | 286 if (!target_url_patterns_.Populate( |
264 *list, URLPattern::SCHEME_ALL, true, error)) { | 287 *target_url_patterns, URLPattern::SCHEME_ALL, true, error)) { |
265 return false; | 288 return false; |
266 } | 289 } |
267 } | 290 } |
268 return true; | 291 return true; |
269 } | 292 } |
270 | 293 |
271 MenuManager::MenuManager(Profile* profile) | 294 MenuManager::MenuManager(Profile* profile) |
272 : profile_(profile) { | 295 : profile_(profile) { |
273 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 296 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
274 content::Source<Profile>(profile)); | 297 content::Source<Profile>(profile)); |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 if (uid < other.uid) | 838 if (uid < other.uid) |
816 return true; | 839 return true; |
817 if (uid == other.uid) | 840 if (uid == other.uid) |
818 return string_uid < other.string_uid; | 841 return string_uid < other.string_uid; |
819 } | 842 } |
820 } | 843 } |
821 return false; | 844 return false; |
822 } | 845 } |
823 | 846 |
824 } // namespace extensions | 847 } // namespace extensions |
OLD | NEW |