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/extension_menu_manager.h" | 5 #include "chrome/browser/extensions/extension_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" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/extensions/extension_event_names.h" | 15 #include "chrome/browser/extensions/extension_event_names.h" |
16 #include "chrome/browser/extensions/extension_event_router.h" | 16 #include "chrome/browser/extensions/extension_event_router.h" |
17 #include "chrome/browser/extensions/extension_prefs.h" | 17 #include "chrome/browser/extensions/extension_prefs.h" |
18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
19 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
| 20 #include "chrome/browser/extensions/extension_tab_helper.h" |
20 #include "chrome/browser/extensions/extension_tab_util.h" | 21 #include "chrome/browser/extensions/extension_tab_util.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
22 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
23 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
24 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 28 #include "content/public/browser/web_contents.h" |
26 #include "content/public/common/context_menu_params.h" | 29 #include "content/public/common/context_menu_params.h" |
27 #include "ui/base/text/text_elider.h" | 30 #include "ui/base/text/text_elider.h" |
28 #include "ui/gfx/favicon_size.h" | 31 #include "ui/gfx/favicon_size.h" |
29 | 32 |
30 using content::WebContents; | 33 using content::WebContents; |
31 | 34 |
32 namespace { | 35 namespace { |
33 | 36 |
34 // Keys for serialization to and from Value to store in the preferences. | 37 // Keys for serialization to and from Value to store in the preferences. |
35 const char kCheckedKey[] = "checked"; | 38 const char kCheckedKey[] = "checked"; |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 static void AddURLProperty(DictionaryValue* dictionary, | 529 static void AddURLProperty(DictionaryValue* dictionary, |
527 const std::string& key, const GURL& url) { | 530 const std::string& key, const GURL& url) { |
528 if (!url.is_empty()) | 531 if (!url.is_empty()) |
529 dictionary->SetString(key, url.possibly_invalid_spec()); | 532 dictionary->SetString(key, url.possibly_invalid_spec()); |
530 } | 533 } |
531 | 534 |
532 void ExtensionMenuManager::ExecuteCommand( | 535 void ExtensionMenuManager::ExecuteCommand( |
533 Profile* profile, | 536 Profile* profile, |
534 WebContents* web_contents, | 537 WebContents* web_contents, |
535 const content::ContextMenuParams& params, | 538 const content::ContextMenuParams& params, |
536 const ExtensionMenuItem::Id& menuItemId) { | 539 const ExtensionMenuItem::Id& menu_item_id) { |
537 ExtensionEventRouter* event_router = profile->GetExtensionEventRouter(); | 540 ExtensionEventRouter* event_router = profile->GetExtensionEventRouter(); |
538 if (!event_router) | 541 if (!event_router) |
539 return; | 542 return; |
540 | 543 |
541 ExtensionMenuItem* item = GetItemById(menuItemId); | 544 ExtensionMenuItem* item = GetItemById(menu_item_id); |
542 if (!item) | 545 if (!item) |
543 return; | 546 return; |
544 | 547 |
| 548 const extensions::Extension* extension = ExtensionSystem::Get(profile_)-> |
| 549 extension_service()->extensions()->GetByID(menu_item_id.extension_id); |
| 550 if (!extension) |
| 551 return; |
| 552 |
545 if (item->type() == ExtensionMenuItem::RADIO) | 553 if (item->type() == ExtensionMenuItem::RADIO) |
546 RadioItemSelected(item); | 554 RadioItemSelected(item); |
547 | 555 |
548 ListValue args; | 556 ListValue args; |
549 | 557 |
550 DictionaryValue* properties = new DictionaryValue(); | 558 DictionaryValue* properties = new DictionaryValue(); |
551 SetIdKeyValue(properties, "menuItemId", item->id()); | 559 SetIdKeyValue(properties, "menuItemId", item->id()); |
552 if (item->parent_id()) | 560 if (item->parent_id()) |
553 SetIdKeyValue(properties, "parentMenuItemId", item->id()); | 561 SetIdKeyValue(properties, "parentMenuItemId", item->id()); |
554 | 562 |
(...skipping 16 matching lines...) Expand all Loading... |
571 AddURLProperty(properties, "frameUrl", params.frame_url); | 579 AddURLProperty(properties, "frameUrl", params.frame_url); |
572 | 580 |
573 if (params.selection_text.length() > 0) | 581 if (params.selection_text.length() > 0) |
574 properties->SetString("selectionText", params.selection_text); | 582 properties->SetString("selectionText", params.selection_text); |
575 | 583 |
576 properties->SetBoolean("editable", params.is_editable); | 584 properties->SetBoolean("editable", params.is_editable); |
577 | 585 |
578 args.Append(properties); | 586 args.Append(properties); |
579 | 587 |
580 // Add the tab info to the argument list. | 588 // Add the tab info to the argument list. |
581 if (web_contents) { | 589 // Note: web_contents only NULL in unit tests :( |
| 590 if (web_contents) |
582 args.Append(ExtensionTabUtil::CreateTabValue(web_contents)); | 591 args.Append(ExtensionTabUtil::CreateTabValue(web_contents)); |
583 } else { | 592 else |
584 args.Append(new DictionaryValue()); | 593 args.Append(new DictionaryValue()); |
585 } | |
586 | 594 |
587 if (item->type() == ExtensionMenuItem::CHECKBOX || | 595 if (item->type() == ExtensionMenuItem::CHECKBOX || |
588 item->type() == ExtensionMenuItem::RADIO) { | 596 item->type() == ExtensionMenuItem::RADIO) { |
589 bool was_checked = item->checked(); | 597 bool was_checked = item->checked(); |
590 properties->SetBoolean("wasChecked", was_checked); | 598 properties->SetBoolean("wasChecked", was_checked); |
591 | 599 |
592 // RADIO items always get set to true when you click on them, but CHECKBOX | 600 // RADIO items always get set to true when you click on them, but CHECKBOX |
593 // items get their state toggled. | 601 // items get their state toggled. |
594 bool checked = | 602 bool checked = |
595 (item->type() == ExtensionMenuItem::RADIO) ? true : !was_checked; | 603 (item->type() == ExtensionMenuItem::RADIO) ? true : !was_checked; |
596 | 604 |
597 item->SetChecked(checked); | 605 item->SetChecked(checked); |
598 properties->SetBoolean("checked", item->checked()); | 606 properties->SetBoolean("checked", item->checked()); |
599 | 607 |
600 const extensions::Extension* extension = ExtensionSystem::Get(profile_)-> | |
601 extension_service()->GetExtensionById(menuItemId.extension_id, false); | |
602 WriteToPrefs(extension); | 608 WriteToPrefs(extension); |
603 } | 609 } |
604 | 610 |
| 611 TabContents* tab_contents = web_contents ? |
| 612 TabContents::FromWebContents(web_contents) : NULL; |
| 613 if (tab_contents) { |
| 614 tab_contents->extension_tab_helper()->active_tab_permission_manager()-> |
| 615 GrantIfRequested(extension); |
| 616 } |
| 617 |
605 std::string json_args; | 618 std::string json_args; |
606 base::JSONWriter::Write(&args, &json_args); | 619 base::JSONWriter::Write(&args, &json_args); |
607 event_router->DispatchEventToExtension( | 620 event_router->DispatchEventToExtension( |
608 item->extension_id(), extension_event_names::kOnContextMenus, | 621 item->extension_id(), extension_event_names::kOnContextMenus, |
609 json_args, profile, GURL(), | 622 json_args, profile, GURL(), |
610 ExtensionEventRouter::USER_GESTURE_ENABLED); | 623 ExtensionEventRouter::USER_GESTURE_ENABLED); |
611 event_router->DispatchEventToExtension( | 624 event_router->DispatchEventToExtension( |
612 item->extension_id(), extension_event_names::kOnContextMenuClicked, | 625 item->extension_id(), extension_event_names::kOnContextMenuClicked, |
613 json_args, profile, GURL(), | 626 json_args, profile, GURL(), |
614 ExtensionEventRouter::USER_GESTURE_ENABLED); | 627 ExtensionEventRouter::USER_GESTURE_ENABLED); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 return true; | 776 return true; |
764 if (extension_id == other.extension_id) { | 777 if (extension_id == other.extension_id) { |
765 if (uid < other.uid) | 778 if (uid < other.uid) |
766 return true; | 779 return true; |
767 if (uid == other.uid) | 780 if (uid == other.uid) |
768 return string_uid < other.string_uid; | 781 return string_uid < other.string_uid; |
769 } | 782 } |
770 } | 783 } |
771 return false; | 784 return false; |
772 } | 785 } |
OLD | NEW |