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" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/extensions/extension_tab_util.h" | 16 #include "chrome/browser/extensions/extension_tab_util.h" |
| 17 #include "chrome/browser/extensions/menu_manager_factory.h" | 17 #include "chrome/browser/extensions/menu_manager_factory.h" |
| 18 #include "chrome/browser/extensions/tab_helper.h" | 18 #include "chrome/browser/extensions/tab_helper.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/common/extensions/api/chrome_web_view_internal.h" | 20 #include "chrome/common/extensions/api/chrome_web_view_internal.h" |
| 21 #include "chrome/common/extensions/api/context_menus.h" | 21 #include "chrome/common/extensions/api/context_menus.h" |
| 22 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/common/child_process_host.h" | |
| 26 #include "content/public/common/context_menu_params.h" | 27 #include "content/public/common/context_menu_params.h" |
| 27 #include "extensions/browser/event_router.h" | 28 #include "extensions/browser/event_router.h" |
| 28 #include "extensions/browser/extension_registry.h" | 29 #include "extensions/browser/extension_registry.h" |
| 29 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 30 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
| 30 #include "extensions/browser/state_store.h" | 31 #include "extensions/browser/state_store.h" |
| 31 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
| 32 #include "extensions/common/manifest_handlers/background_info.h" | 33 #include "extensions/common/manifest_handlers/background_info.h" |
| 33 #include "ui/gfx/favicon_size.h" | 34 #include "ui/gfx/favicon_size.h" |
| 34 #include "ui/gfx/text_elider.h" | 35 #include "ui/gfx/text_elider.h" |
| 35 | 36 |
| 37 using content::ChildProcessHost; | |
| 36 using content::WebContents; | 38 using content::WebContents; |
| 39 using guest_view::kInstanceIDNone; | |
| 37 | 40 |
| 38 namespace extensions { | 41 namespace extensions { |
| 39 | 42 |
| 40 namespace { | 43 namespace { |
| 41 | 44 |
| 42 // Keys for serialization to and from Value to store in the preferences. | 45 // Keys for serialization to and from Value to store in the preferences. |
| 43 const char kContextMenusKey[] = "context_menus"; | 46 const char kContextMenusKey[] = "context_menus"; |
| 44 | 47 |
| 45 const char kCheckedKey[] = "checked"; | 48 const char kCheckedKey[] = "checked"; |
| 46 const char kContextsKey[] = "contexts"; | 49 const char kContextsKey[] = "contexts"; |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 const MenuItem::ExtensionKey& key) { | 337 const MenuItem::ExtensionKey& key) { |
| 335 MenuItemMap::iterator i = context_items_.find(key); | 338 MenuItemMap::iterator i = context_items_.find(key); |
| 336 if (i != context_items_.end()) { | 339 if (i != context_items_.end()) { |
| 337 return &(i->second); | 340 return &(i->second); |
| 338 } | 341 } |
| 339 return NULL; | 342 return NULL; |
| 340 } | 343 } |
| 341 | 344 |
| 342 bool MenuManager::AddContextItem(const Extension* extension, MenuItem* item) { | 345 bool MenuManager::AddContextItem(const Extension* extension, MenuItem* item) { |
| 343 const MenuItem::ExtensionKey& key = item->id().extension_key; | 346 const MenuItem::ExtensionKey& key = item->id().extension_key; |
| 344 // The item must have a non-empty extension id, and not have already been | 347 |
| 345 // added. | 348 // The item must have a non-empty key, and not have already been added. |
| 346 if (key.empty() || ContainsKey(items_by_id_, item->id())) | 349 if (key.empty() || ContainsKey(items_by_id_, item->id())) |
| 347 return false; | 350 return false; |
| 348 | 351 |
| 349 DCHECK_EQ(extension->id(), key.extension_id); | 352 DCHECK_EQ(extension->id(), key.extension_id); |
| 350 | 353 |
| 351 bool first_item = !ContainsKey(context_items_, key); | 354 bool first_item = !ContainsKey(context_items_, key); |
| 352 context_items_[key].push_back(item); | 355 context_items_[key].push_back(item); |
| 353 items_by_id_[item->id()] = item; | 356 items_by_id_[item->id()] = item; |
| 354 | 357 |
| 355 if (item->type() == MenuItem::RADIO) { | 358 if (item->type() == MenuItem::RADIO) { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 | 509 |
| 507 if (list.empty()) { | 510 if (list.empty()) { |
| 508 context_items_.erase(extension_key); | 511 context_items_.erase(extension_key); |
| 509 icon_manager_.RemoveIcon(extension_key.extension_id); | 512 icon_manager_.RemoveIcon(extension_key.extension_id); |
| 510 } | 513 } |
| 511 return result; | 514 return result; |
| 512 } | 515 } |
| 513 | 516 |
| 514 void MenuManager::RemoveAllContextItems( | 517 void MenuManager::RemoveAllContextItems( |
| 515 const MenuItem::ExtensionKey& extension_key) { | 518 const MenuItem::ExtensionKey& extension_key) { |
| 519 auto it = context_items_.find(extension_key); | |
| 520 if (it == context_items_.end()) | |
| 521 return; | |
| 522 | |
| 523 // We use the |extension_id| from the stored ExtensionKey, since the provided | |
| 524 // |extension_key| may leave it empty (if matching solely basted on the | |
| 525 // webview IDs). | |
|
Devlin
2015/07/08 21:10:01
// TODO(paulmeyer): We can get rid of this hack if
paulmeyer
2015/07/09 14:14:58
Done.
| |
| 526 std::string extension_id = it->first.extension_id; | |
| 527 MenuItem::List& context_items_for_key = it->second; | |
| 516 MenuItem::List::iterator i; | 528 MenuItem::List::iterator i; |
| 517 for (i = context_items_[extension_key].begin(); | 529 for (i = context_items_for_key.begin(); |
| 518 i != context_items_[extension_key].end(); | 530 i != context_items_for_key.end(); |
| 519 ++i) { | 531 ++i) { |
| 520 MenuItem* item = *i; | 532 MenuItem* item = *i; |
| 521 items_by_id_.erase(item->id()); | 533 items_by_id_.erase(item->id()); |
| 522 | 534 |
| 523 // Remove descendants from this item and erase them from the lookup cache. | 535 // Remove descendants from this item and erase them from the lookup cache. |
| 524 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants(); | 536 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants(); |
| 525 std::set<MenuItem::Id>::const_iterator j; | 537 std::set<MenuItem::Id>::const_iterator j; |
| 526 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) { | 538 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) { |
| 527 items_by_id_.erase(*j); | 539 items_by_id_.erase(*j); |
| 528 } | 540 } |
| 529 } | 541 } |
| 530 STLDeleteElements(&context_items_[extension_key]); | 542 STLDeleteElements(&context_items_for_key); |
| 531 context_items_.erase(extension_key); | 543 context_items_.erase(extension_key); |
| 532 icon_manager_.RemoveIcon(extension_key.extension_id); | 544 icon_manager_.RemoveIcon(extension_id); |
| 533 } | 545 } |
| 534 | 546 |
| 535 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const { | 547 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const { |
| 536 std::map<MenuItem::Id, MenuItem*>::const_iterator i = | 548 std::map<MenuItem::Id, MenuItem*>::const_iterator i = |
| 537 items_by_id_.find(id); | 549 items_by_id_.find(id); |
| 538 if (i != items_by_id_.end()) | 550 if (i != items_by_id_.end()) |
| 539 return i->second; | 551 return i->second; |
| 540 else | 552 else |
| 541 return NULL; | 553 return NULL; |
| 542 } | 554 } |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 879 items_to_remove.insert(iter->first); | 891 items_to_remove.insert(iter->first); |
| 880 } | 892 } |
| 881 | 893 |
| 882 std::set<MenuItem::Id>::iterator remove_iter; | 894 std::set<MenuItem::Id>::iterator remove_iter; |
| 883 for (remove_iter = items_to_remove.begin(); | 895 for (remove_iter = items_to_remove.begin(); |
| 884 remove_iter != items_to_remove.end(); | 896 remove_iter != items_to_remove.end(); |
| 885 ++remove_iter) | 897 ++remove_iter) |
| 886 RemoveContextMenuItem(*remove_iter); | 898 RemoveContextMenuItem(*remove_iter); |
| 887 } | 899 } |
| 888 | 900 |
| 889 MenuItem::ExtensionKey::ExtensionKey() : webview_instance_id(0) {} | 901 MenuItem::ExtensionKey::ExtensionKey() |
| 902 : webview_embedder_process_id(ChildProcessHost::kInvalidUniqueID), | |
| 903 webview_instance_id(kInstanceIDNone) {} | |
| 904 | |
| 905 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id) | |
| 906 : extension_id(extension_id), | |
| 907 webview_embedder_process_id(ChildProcessHost::kInvalidUniqueID), | |
| 908 webview_instance_id(kInstanceIDNone) { | |
| 909 DCHECK(!extension_id.empty()); | |
| 910 } | |
| 890 | 911 |
| 891 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id, | 912 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id, |
| 913 int webview_embedder_process_id, | |
| 892 int webview_instance_id) | 914 int webview_instance_id) |
| 893 : extension_id(extension_id), webview_instance_id(webview_instance_id) {} | 915 : extension_id(extension_id), |
| 894 | 916 webview_embedder_process_id(webview_embedder_process_id), |
| 895 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id) | 917 webview_instance_id(webview_instance_id) { |
| 896 : extension_id(extension_id), webview_instance_id(0) {} | 918 DCHECK(webview_embedder_process_id != ChildProcessHost::kInvalidUniqueID && |
| 919 webview_instance_id != kInstanceIDNone); | |
| 920 } | |
| 897 | 921 |
| 898 bool MenuItem::ExtensionKey::operator==(const ExtensionKey& other) const { | 922 bool MenuItem::ExtensionKey::operator==(const ExtensionKey& other) const { |
| 899 return extension_id == other.extension_id && | 923 bool webview_ids_match = webview_instance_id == other.webview_instance_id && |
| 900 webview_instance_id == other.webview_instance_id; | 924 webview_embedder_process_id == other.webview_embedder_process_id; |
| 925 | |
| 926 // If either extension ID is empty, then these ExtensionKeys will be matched | |
| 927 // only based on the other IDs. | |
| 928 if (extension_id.empty() || other.extension_id.empty()) | |
| 929 return webview_ids_match; | |
| 930 | |
| 931 return extension_id == other.extension_id && webview_ids_match; | |
| 901 } | 932 } |
| 902 | 933 |
| 903 bool MenuItem::ExtensionKey::operator<(const ExtensionKey& other) const { | 934 bool MenuItem::ExtensionKey::operator<(const ExtensionKey& other) const { |
| 904 if (extension_id != other.extension_id) | 935 if (webview_embedder_process_id != other.webview_embedder_process_id) |
| 905 return extension_id < other.extension_id; | 936 return webview_embedder_process_id < other.webview_embedder_process_id; |
| 906 | 937 |
| 907 return webview_instance_id < other.webview_instance_id; | 938 if (webview_instance_id != other.webview_instance_id) |
| 939 return webview_instance_id < other.webview_instance_id; | |
| 940 | |
| 941 // If either extension ID is empty, then these ExtensionKeys will be compared | |
| 942 // only based on the other IDs. | |
| 943 if (extension_id.empty() || other.extension_id.empty()) | |
| 944 return false; | |
| 945 | |
| 946 return extension_id < other.extension_id; | |
| 908 } | 947 } |
| 909 | 948 |
| 910 bool MenuItem::ExtensionKey::operator!=(const ExtensionKey& other) const { | 949 bool MenuItem::ExtensionKey::operator!=(const ExtensionKey& other) const { |
| 911 return !(*this == other); | 950 return !(*this == other); |
| 912 } | 951 } |
| 913 | 952 |
| 914 bool MenuItem::ExtensionKey::empty() const { | 953 bool MenuItem::ExtensionKey::empty() const { |
| 915 return extension_id.empty() && !webview_instance_id; | 954 return extension_id.empty() && |
| 955 webview_embedder_process_id == ChildProcessHost::kInvalidUniqueID && | |
| 956 webview_instance_id == kInstanceIDNone; | |
| 916 } | 957 } |
| 917 | 958 |
| 918 MenuItem::Id::Id() : incognito(false), uid(0) {} | 959 MenuItem::Id::Id() : incognito(false), uid(0) {} |
| 919 | 960 |
| 920 MenuItem::Id::Id(bool incognito, const MenuItem::ExtensionKey& extension_key) | 961 MenuItem::Id::Id(bool incognito, const MenuItem::ExtensionKey& extension_key) |
| 921 : incognito(incognito), extension_key(extension_key), uid(0) {} | 962 : incognito(incognito), extension_key(extension_key), uid(0) {} |
| 922 | 963 |
| 923 MenuItem::Id::~Id() { | 964 MenuItem::Id::~Id() { |
| 924 } | 965 } |
| 925 | 966 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 943 if (uid < other.uid) | 984 if (uid < other.uid) |
| 944 return true; | 985 return true; |
| 945 if (uid == other.uid) | 986 if (uid == other.uid) |
| 946 return string_uid < other.string_uid; | 987 return string_uid < other.string_uid; |
| 947 } | 988 } |
| 948 } | 989 } |
| 949 return false; | 990 return false; |
| 950 } | 991 } |
| 951 | 992 |
| 952 } // namespace extensions | 993 } // namespace extensions |
| OLD | NEW |