| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 continue; | 83 continue; |
| 84 items.push_back(item); | 84 items.push_back(item); |
| 85 } | 85 } |
| 86 return items; | 86 return items; |
| 87 } | 87 } |
| 88 | 88 |
| 89 scoped_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) { | 89 scoped_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) { |
| 90 scoped_ptr<base::ListValue> list(new base::ListValue()); | 90 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 91 for (size_t i = 0; i < items.size(); ++i) | 91 for (size_t i = 0; i < items.size(); ++i) |
| 92 list->Append(items[i]->ToValue().release()); | 92 list->Append(items[i]->ToValue().release()); |
| 93 return scoped_ptr<Value>(list.release()); | 93 return scoped_ptr<base::Value>(list.release()); |
| 94 } | 94 } |
| 95 | 95 |
| 96 bool GetStringList(const DictionaryValue& dict, | 96 bool GetStringList(const base::DictionaryValue& dict, |
| 97 const std::string& key, | 97 const std::string& key, |
| 98 std::vector<std::string>* out) { | 98 std::vector<std::string>* out) { |
| 99 if (!dict.HasKey(key)) | 99 if (!dict.HasKey(key)) |
| 100 return true; | 100 return true; |
| 101 | 101 |
| 102 const base::ListValue* list = NULL; | 102 const base::ListValue* list = NULL; |
| 103 if (!dict.GetListWithoutPathExpansion(key, &list)) | 103 if (!dict.GetListWithoutPathExpansion(key, &list)) |
| 104 return false; | 104 return false; |
| 105 | 105 |
| 106 for (size_t i = 0; i < list->GetSize(); ++i) { | 106 for (size_t i = 0; i < list->GetSize(); ++i) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 return false; | 184 return false; |
| 185 checked_ = checked; | 185 checked_ = checked; |
| 186 return true; | 186 return true; |
| 187 } | 187 } |
| 188 | 188 |
| 189 void MenuItem::AddChild(MenuItem* item) { | 189 void MenuItem::AddChild(MenuItem* item) { |
| 190 item->parent_id_.reset(new Id(id_)); | 190 item->parent_id_.reset(new Id(id_)); |
| 191 children_.push_back(item); | 191 children_.push_back(item); |
| 192 } | 192 } |
| 193 | 193 |
| 194 scoped_ptr<DictionaryValue> MenuItem::ToValue() const { | 194 scoped_ptr<base::DictionaryValue> MenuItem::ToValue() const { |
| 195 scoped_ptr<DictionaryValue> value(new DictionaryValue); | 195 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 196 // Should only be called for extensions with event pages, which only have | 196 // Should only be called for extensions with event pages, which only have |
| 197 // string IDs for items. | 197 // string IDs for items. |
| 198 DCHECK_EQ(0, id_.uid); | 198 DCHECK_EQ(0, id_.uid); |
| 199 value->SetString(kStringUIDKey, id_.string_uid); | 199 value->SetString(kStringUIDKey, id_.string_uid); |
| 200 value->SetBoolean(kIncognitoKey, id_.incognito); | 200 value->SetBoolean(kIncognitoKey, id_.incognito); |
| 201 value->SetInteger(kTypeKey, type_); | 201 value->SetInteger(kTypeKey, type_); |
| 202 if (type_ != SEPARATOR) | 202 if (type_ != SEPARATOR) |
| 203 value->SetString(kTitleKey, title_); | 203 value->SetString(kTitleKey, title_); |
| 204 if (type_ == CHECKBOX || type_ == RADIO) | 204 if (type_ == CHECKBOX || type_ == RADIO) |
| 205 value->SetBoolean(kCheckedKey, checked_); | 205 value->SetBoolean(kCheckedKey, checked_); |
| 206 value->SetBoolean(kEnabledKey, enabled_); | 206 value->SetBoolean(kEnabledKey, enabled_); |
| 207 value->Set(kContextsKey, contexts_.ToValue().release()); | 207 value->Set(kContextsKey, contexts_.ToValue().release()); |
| 208 if (parent_id_) { | 208 if (parent_id_) { |
| 209 DCHECK_EQ(0, parent_id_->uid); | 209 DCHECK_EQ(0, parent_id_->uid); |
| 210 value->SetString(kParentUIDKey, parent_id_->string_uid); | 210 value->SetString(kParentUIDKey, parent_id_->string_uid); |
| 211 } | 211 } |
| 212 value->Set(kDocumentURLPatternsKey, | 212 value->Set(kDocumentURLPatternsKey, |
| 213 document_url_patterns_.ToValue().release()); | 213 document_url_patterns_.ToValue().release()); |
| 214 value->Set(kTargetURLPatternsKey, target_url_patterns_.ToValue().release()); | 214 value->Set(kTargetURLPatternsKey, target_url_patterns_.ToValue().release()); |
| 215 return value.Pass(); | 215 return value.Pass(); |
| 216 } | 216 } |
| 217 | 217 |
| 218 // static | 218 // static |
| 219 MenuItem* MenuItem::Populate(const std::string& extension_id, | 219 MenuItem* MenuItem::Populate(const std::string& extension_id, |
| 220 const DictionaryValue& value, | 220 const base::DictionaryValue& value, |
| 221 std::string* error) { | 221 std::string* error) { |
| 222 bool incognito = false; | 222 bool incognito = false; |
| 223 if (!value.GetBoolean(kIncognitoKey, &incognito)) | 223 if (!value.GetBoolean(kIncognitoKey, &incognito)) |
| 224 return NULL; | 224 return NULL; |
| 225 Id id(incognito, extension_id); | 225 Id id(incognito, extension_id); |
| 226 if (!value.GetString(kStringUIDKey, &id.string_uid)) | 226 if (!value.GetString(kStringUIDKey, &id.string_uid)) |
| 227 return NULL; | 227 return NULL; |
| 228 int type_int; | 228 int type_int; |
| 229 Type type = NORMAL; | 229 Type type = NORMAL; |
| 230 if (!value.GetInteger(kTypeKey, &type_int)) | 230 if (!value.GetInteger(kTypeKey, &type_int)) |
| 231 return NULL; | 231 return NULL; |
| 232 type = static_cast<Type>(type_int); | 232 type = static_cast<Type>(type_int); |
| 233 std::string title; | 233 std::string title; |
| 234 if (type != SEPARATOR && !value.GetString(kTitleKey, &title)) | 234 if (type != SEPARATOR && !value.GetString(kTitleKey, &title)) |
| 235 return NULL; | 235 return NULL; |
| 236 bool checked = false; | 236 bool checked = false; |
| 237 if ((type == CHECKBOX || type == RADIO) && | 237 if ((type == CHECKBOX || type == RADIO) && |
| 238 !value.GetBoolean(kCheckedKey, &checked)) { | 238 !value.GetBoolean(kCheckedKey, &checked)) { |
| 239 return NULL; | 239 return NULL; |
| 240 } | 240 } |
| 241 bool enabled = true; | 241 bool enabled = true; |
| 242 if (!value.GetBoolean(kEnabledKey, &enabled)) | 242 if (!value.GetBoolean(kEnabledKey, &enabled)) |
| 243 return NULL; | 243 return NULL; |
| 244 ContextList contexts; | 244 ContextList contexts; |
| 245 const Value* contexts_value = NULL; | 245 const base::Value* contexts_value = NULL; |
| 246 if (!value.Get(kContextsKey, &contexts_value)) | 246 if (!value.Get(kContextsKey, &contexts_value)) |
| 247 return NULL; | 247 return NULL; |
| 248 if (!contexts.Populate(*contexts_value)) | 248 if (!contexts.Populate(*contexts_value)) |
| 249 return NULL; | 249 return NULL; |
| 250 | 250 |
| 251 scoped_ptr<MenuItem> result(new MenuItem( | 251 scoped_ptr<MenuItem> result(new MenuItem( |
| 252 id, title, checked, enabled, type, contexts)); | 252 id, title, checked, enabled, type, contexts)); |
| 253 | 253 |
| 254 std::vector<std::string> document_url_patterns; | 254 std::vector<std::string> document_url_patterns; |
| 255 if (!GetStringList(value, kDocumentURLPatternsKey, &document_url_patterns)) | 255 if (!GetStringList(value, kDocumentURLPatternsKey, &document_url_patterns)) |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 } | 581 } |
| 582 | 582 |
| 583 // Now iterate forwards from |item| and uncheck any adjacent radio items. | 583 // Now iterate forwards from |item| and uncheck any adjacent radio items. |
| 584 for (i = item_location + 1; i != list->end(); ++i) { | 584 for (i = item_location + 1; i != list->end(); ++i) { |
| 585 if ((*i)->type() != MenuItem::RADIO) | 585 if ((*i)->type() != MenuItem::RADIO) |
| 586 break; | 586 break; |
| 587 (*i)->SetChecked(false); | 587 (*i)->SetChecked(false); |
| 588 } | 588 } |
| 589 } | 589 } |
| 590 | 590 |
| 591 static void AddURLProperty(DictionaryValue* dictionary, | 591 static void AddURLProperty(base::DictionaryValue* dictionary, |
| 592 const std::string& key, const GURL& url) { | 592 const std::string& key, const GURL& url) { |
| 593 if (!url.is_empty()) | 593 if (!url.is_empty()) |
| 594 dictionary->SetString(key, url.possibly_invalid_spec()); | 594 dictionary->SetString(key, url.possibly_invalid_spec()); |
| 595 } | 595 } |
| 596 | 596 |
| 597 void MenuManager::ExecuteCommand(Profile* profile, | 597 void MenuManager::ExecuteCommand(Profile* profile, |
| 598 WebContents* web_contents, | 598 WebContents* web_contents, |
| 599 const content::ContextMenuParams& params, | 599 const content::ContextMenuParams& params, |
| 600 const MenuItem::Id& menu_item_id) { | 600 const MenuItem::Id& menu_item_id) { |
| 601 EventRouter* event_router = extensions::ExtensionSystem::Get(profile)-> | 601 EventRouter* event_router = extensions::ExtensionSystem::Get(profile)-> |
| 602 event_router(); | 602 event_router(); |
| 603 if (!event_router) | 603 if (!event_router) |
| 604 return; | 604 return; |
| 605 | 605 |
| 606 MenuItem* item = GetItemById(menu_item_id); | 606 MenuItem* item = GetItemById(menu_item_id); |
| 607 if (!item) | 607 if (!item) |
| 608 return; | 608 return; |
| 609 | 609 |
| 610 // ExtensionService/Extension can be NULL in unit tests :( | 610 // ExtensionService/Extension can be NULL in unit tests :( |
| 611 ExtensionService* service = | 611 ExtensionService* service = |
| 612 ExtensionSystem::Get(profile_)->extension_service(); | 612 ExtensionSystem::Get(profile_)->extension_service(); |
| 613 const Extension* extension = service ? | 613 const Extension* extension = service ? |
| 614 service->extensions()->GetByID(menu_item_id.extension_id) : NULL; | 614 service->extensions()->GetByID(menu_item_id.extension_id) : NULL; |
| 615 | 615 |
| 616 if (item->type() == MenuItem::RADIO) | 616 if (item->type() == MenuItem::RADIO) |
| 617 RadioItemSelected(item); | 617 RadioItemSelected(item); |
| 618 | 618 |
| 619 scoped_ptr<base::ListValue> args(new base::ListValue()); | 619 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 620 | 620 |
| 621 DictionaryValue* properties = new DictionaryValue(); | 621 base::DictionaryValue* properties = new base::DictionaryValue(); |
| 622 SetIdKeyValue(properties, "menuItemId", item->id()); | 622 SetIdKeyValue(properties, "menuItemId", item->id()); |
| 623 if (item->parent_id()) | 623 if (item->parent_id()) |
| 624 SetIdKeyValue(properties, "parentMenuItemId", *item->parent_id()); | 624 SetIdKeyValue(properties, "parentMenuItemId", *item->parent_id()); |
| 625 | 625 |
| 626 switch (params.media_type) { | 626 switch (params.media_type) { |
| 627 case blink::WebContextMenuData::MediaTypeImage: | 627 case blink::WebContextMenuData::MediaTypeImage: |
| 628 properties->SetString("mediaType", "image"); | 628 properties->SetString("mediaType", "image"); |
| 629 break; | 629 break; |
| 630 case blink::WebContextMenuData::MediaTypeVideo: | 630 case blink::WebContextMenuData::MediaTypeVideo: |
| 631 properties->SetString("mediaType", "video"); | 631 properties->SetString("mediaType", "video"); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 648 | 648 |
| 649 args->Append(properties); | 649 args->Append(properties); |
| 650 | 650 |
| 651 // Add the tab info to the argument list. | 651 // Add the tab info to the argument list. |
| 652 // No tab info in a platform app. | 652 // No tab info in a platform app. |
| 653 if (!extension || !extension->is_platform_app()) { | 653 if (!extension || !extension->is_platform_app()) { |
| 654 // Note: web_contents are NULL in unit tests :( | 654 // Note: web_contents are NULL in unit tests :( |
| 655 if (web_contents) { | 655 if (web_contents) { |
| 656 args->Append(ExtensionTabUtil::CreateTabValue(web_contents)); | 656 args->Append(ExtensionTabUtil::CreateTabValue(web_contents)); |
| 657 } else { | 657 } else { |
| 658 args->Append(new DictionaryValue()); | 658 args->Append(new base::DictionaryValue()); |
| 659 } | 659 } |
| 660 } | 660 } |
| 661 | 661 |
| 662 if (item->type() == MenuItem::CHECKBOX || | 662 if (item->type() == MenuItem::CHECKBOX || |
| 663 item->type() == MenuItem::RADIO) { | 663 item->type() == MenuItem::RADIO) { |
| 664 bool was_checked = item->checked(); | 664 bool was_checked = item->checked(); |
| 665 properties->SetBoolean("wasChecked", was_checked); | 665 properties->SetBoolean("wasChecked", was_checked); |
| 666 | 666 |
| 667 // RADIO items always get set to true when you click on them, but CHECKBOX | 667 // RADIO items always get set to true when you click on them, but CHECKBOX |
| 668 // items get their state toggled. | 668 // items get their state toggled. |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 if (uid < other.uid) | 888 if (uid < other.uid) |
| 889 return true; | 889 return true; |
| 890 if (uid == other.uid) | 890 if (uid == other.uid) |
| 891 return string_uid < other.string_uid; | 891 return string_uid < other.string_uid; |
| 892 } | 892 } |
| 893 } | 893 } |
| 894 return false; | 894 return false; |
| 895 } | 895 } |
| 896 | 896 |
| 897 } // namespace extensions | 897 } // namespace extensions |
| OLD | NEW |