Chromium Code Reviews| 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 MenuItem* MenuItem::Populate(const std::string& extension_id, | 196 MenuItem* MenuItem::Populate(const std::string& extension_id, |
| 197 const DictionaryValue& value, | 197 const DictionaryValue& value, |
| 198 std::string* error) { | 198 std::string* error) { |
| 199 bool incognito = false; | 199 bool incognito = false; |
| 200 if (!value.GetBoolean(kIncognitoKey, &incognito)) | 200 if (!value.GetBoolean(kIncognitoKey, &incognito)) |
| 201 return NULL; | 201 return NULL; |
| 202 Id id(incognito, extension_id); | 202 Id id(incognito, extension_id); |
| 203 if (!value.GetString(kStringUIDKey, &id.string_uid)) | 203 if (!value.GetString(kStringUIDKey, &id.string_uid)) |
| 204 return NULL; | 204 return NULL; |
| 205 int type_int; | 205 int type_int; |
| 206 Type type; | 206 Type type = NORMAL; |
| 207 if (!value.GetInteger(kTypeKey, &type_int)) | 207 if (!value.GetInteger(kTypeKey, &type_int)) |
| 208 return NULL; | 208 return NULL; |
| 209 type = static_cast<Type>(type_int); | 209 type = static_cast<Type>(type_int); |
| 210 std::string title; | 210 std::string title; |
| 211 if (type != SEPARATOR && !value.GetString(kTitleKey, &title)) | 211 if (type != SEPARATOR && !value.GetString(kTitleKey, &title)) |
| 212 return NULL; | 212 return NULL; |
| 213 bool checked; | 213 bool checked = false; |
| 214 if ((type == CHECKBOX || type == RADIO) && | 214 if ((type == CHECKBOX || type == RADIO) && |
| 215 !value.GetBoolean(kCheckedKey, &checked)) { | 215 !value.GetBoolean(kCheckedKey, &checked)) { |
| 216 return NULL; | 216 return NULL; |
| 217 } | 217 } |
| 218 bool enabled = true; | 218 bool enabled = true; |
| 219 if (!value.GetBoolean(kEnabledKey, &enabled)) | 219 if (!value.GetBoolean(kEnabledKey, &enabled)) |
| 220 return NULL; | 220 return NULL; |
| 221 ContextList contexts; | 221 ContextList contexts; |
| 222 const Value* contexts_value = NULL; | 222 const Value* contexts_value = NULL; |
| 223 if (!value.Get(kContextsKey, &contexts_value)) | 223 if (!value.Get(kContextsKey, &contexts_value)) |
| 224 return NULL; | 224 return NULL; |
| 225 if (!contexts.Populate(*contexts_value)) | 225 if (!contexts.Populate(*contexts_value)) |
| 226 return NULL; | 226 return NULL; |
| 227 | 227 |
| 228 scoped_ptr<MenuItem> result(new MenuItem( | 228 scoped_ptr<MenuItem> result(new MenuItem( |
| 229 id, title, checked, enabled, type, contexts)); | 229 id, title, checked, enabled, type, contexts)); |
| 230 | 230 |
| 231 if (!result->PopulateURLPatterns( | 231 std::vector<std::string> document_url_patterns; |
| 232 value, kDocumentURLPatternsKey, kTargetURLPatternsKey, error)) | 232 if (value.HasKey(kDocumentURLPatternsKey)) { |
|
not at google - send to devlin
2012/08/07 02:00:11
code is basically repeated. Could you pull this ou
chebert
2012/08/13 22:41:52
Done.
| |
| 233 const ListValue* list = NULL; | |
| 234 if (!value.GetList(kDocumentURLPatternsKey, &list)) | |
| 235 return NULL; | |
| 236 | |
| 237 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 238 std::string pattern; | |
| 239 if (!list->GetString(i, &pattern)) | |
| 240 return NULL; | |
| 241 document_url_patterns.push_back(pattern); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 std::vector<std::string> target_url_patterns; | |
| 246 if (value.HasKey(kTargetURLPatternsKey)) { | |
| 247 const ListValue* list = NULL; | |
| 248 if (!value.GetList(kTargetURLPatternsKey, &list)) | |
| 249 return NULL; | |
| 250 | |
| 251 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 252 std::string pattern; | |
| 253 if (!list->GetString(i, &pattern)) | |
| 254 return NULL; | |
| 255 target_url_patterns.push_back(pattern); | |
| 256 } | |
| 257 } | |
| 258 if (!result->PopulateURLPatterns(&document_url_patterns, | |
| 259 &target_url_patterns, | |
| 260 error)) { | |
| 233 return NULL; | 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 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 817 if (uid < other.uid) | 840 if (uid < other.uid) |
| 818 return true; | 841 return true; |
| 819 if (uid == other.uid) | 842 if (uid == other.uid) |
| 820 return string_uid < other.string_uid; | 843 return string_uid < other.string_uid; |
| 821 } | 844 } |
| 822 } | 845 } |
| 823 return false; | 846 return false; |
| 824 } | 847 } |
| 825 | 848 |
| 826 } // namespace extensions | 849 } // namespace extensions |
| OLD | NEW |