| 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). |
| 526 // TODO(paulmeyer): We can get rid of this hack if/when we reliably track |
| 527 // extension IDs at WebView cleanup. |
| 528 std::string extension_id = it->first.extension_id; |
| 529 MenuItem::List& context_items_for_key = it->second; |
| 516 MenuItem::List::iterator i; | 530 MenuItem::List::iterator i; |
| 517 for (i = context_items_[extension_key].begin(); | 531 for (i = context_items_for_key.begin(); |
| 518 i != context_items_[extension_key].end(); | 532 i != context_items_for_key.end(); |
| 519 ++i) { | 533 ++i) { |
| 520 MenuItem* item = *i; | 534 MenuItem* item = *i; |
| 521 items_by_id_.erase(item->id()); | 535 items_by_id_.erase(item->id()); |
| 522 | 536 |
| 523 // Remove descendants from this item and erase them from the lookup cache. | 537 // Remove descendants from this item and erase them from the lookup cache. |
| 524 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants(); | 538 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants(); |
| 525 std::set<MenuItem::Id>::const_iterator j; | 539 std::set<MenuItem::Id>::const_iterator j; |
| 526 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) { | 540 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) { |
| 527 items_by_id_.erase(*j); | 541 items_by_id_.erase(*j); |
| 528 } | 542 } |
| 529 } | 543 } |
| 530 STLDeleteElements(&context_items_[extension_key]); | 544 STLDeleteElements(&context_items_for_key); |
| 531 context_items_.erase(extension_key); | 545 context_items_.erase(extension_key); |
| 532 icon_manager_.RemoveIcon(extension_key.extension_id); | 546 icon_manager_.RemoveIcon(extension_id); |
| 533 } | 547 } |
| 534 | 548 |
| 535 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const { | 549 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const { |
| 536 std::map<MenuItem::Id, MenuItem*>::const_iterator i = | 550 std::map<MenuItem::Id, MenuItem*>::const_iterator i = |
| 537 items_by_id_.find(id); | 551 items_by_id_.find(id); |
| 538 if (i != items_by_id_.end()) | 552 if (i != items_by_id_.end()) |
| 539 return i->second; | 553 return i->second; |
| 540 else | 554 else |
| 541 return NULL; | 555 return NULL; |
| 542 } | 556 } |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 items_to_remove.insert(iter->first); | 893 items_to_remove.insert(iter->first); |
| 880 } | 894 } |
| 881 | 895 |
| 882 std::set<MenuItem::Id>::iterator remove_iter; | 896 std::set<MenuItem::Id>::iterator remove_iter; |
| 883 for (remove_iter = items_to_remove.begin(); | 897 for (remove_iter = items_to_remove.begin(); |
| 884 remove_iter != items_to_remove.end(); | 898 remove_iter != items_to_remove.end(); |
| 885 ++remove_iter) | 899 ++remove_iter) |
| 886 RemoveContextMenuItem(*remove_iter); | 900 RemoveContextMenuItem(*remove_iter); |
| 887 } | 901 } |
| 888 | 902 |
| 889 MenuItem::ExtensionKey::ExtensionKey() : webview_instance_id(0) {} | 903 MenuItem::ExtensionKey::ExtensionKey() |
| 904 : webview_embedder_process_id(ChildProcessHost::kInvalidUniqueID), |
| 905 webview_instance_id(kInstanceIDNone) {} |
| 906 |
| 907 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id) |
| 908 : extension_id(extension_id), |
| 909 webview_embedder_process_id(ChildProcessHost::kInvalidUniqueID), |
| 910 webview_instance_id(kInstanceIDNone) { |
| 911 DCHECK(!extension_id.empty()); |
| 912 } |
| 890 | 913 |
| 891 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id, | 914 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id, |
| 915 int webview_embedder_process_id, |
| 892 int webview_instance_id) | 916 int webview_instance_id) |
| 893 : extension_id(extension_id), webview_instance_id(webview_instance_id) {} | 917 : extension_id(extension_id), |
| 894 | 918 webview_embedder_process_id(webview_embedder_process_id), |
| 895 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id) | 919 webview_instance_id(webview_instance_id) { |
| 896 : extension_id(extension_id), webview_instance_id(0) {} | 920 DCHECK(webview_embedder_process_id != ChildProcessHost::kInvalidUniqueID && |
| 921 webview_instance_id != kInstanceIDNone); |
| 922 } |
| 897 | 923 |
| 898 bool MenuItem::ExtensionKey::operator==(const ExtensionKey& other) const { | 924 bool MenuItem::ExtensionKey::operator==(const ExtensionKey& other) const { |
| 899 return extension_id == other.extension_id && | 925 bool webview_ids_match = webview_instance_id == other.webview_instance_id && |
| 900 webview_instance_id == other.webview_instance_id; | 926 webview_embedder_process_id == other.webview_embedder_process_id; |
| 927 |
| 928 // If either extension ID is empty, then these ExtensionKeys will be matched |
| 929 // only based on the other IDs. |
| 930 if (extension_id.empty() || other.extension_id.empty()) |
| 931 return webview_ids_match; |
| 932 |
| 933 return extension_id == other.extension_id && webview_ids_match; |
| 901 } | 934 } |
| 902 | 935 |
| 903 bool MenuItem::ExtensionKey::operator<(const ExtensionKey& other) const { | 936 bool MenuItem::ExtensionKey::operator<(const ExtensionKey& other) const { |
| 904 if (extension_id != other.extension_id) | 937 if (webview_embedder_process_id != other.webview_embedder_process_id) |
| 905 return extension_id < other.extension_id; | 938 return webview_embedder_process_id < other.webview_embedder_process_id; |
| 906 | 939 |
| 907 return webview_instance_id < other.webview_instance_id; | 940 if (webview_instance_id != other.webview_instance_id) |
| 941 return webview_instance_id < other.webview_instance_id; |
| 942 |
| 943 // If either extension ID is empty, then these ExtensionKeys will be compared |
| 944 // only based on the other IDs. |
| 945 if (extension_id.empty() || other.extension_id.empty()) |
| 946 return false; |
| 947 |
| 948 return extension_id < other.extension_id; |
| 908 } | 949 } |
| 909 | 950 |
| 910 bool MenuItem::ExtensionKey::operator!=(const ExtensionKey& other) const { | 951 bool MenuItem::ExtensionKey::operator!=(const ExtensionKey& other) const { |
| 911 return !(*this == other); | 952 return !(*this == other); |
| 912 } | 953 } |
| 913 | 954 |
| 914 bool MenuItem::ExtensionKey::empty() const { | 955 bool MenuItem::ExtensionKey::empty() const { |
| 915 return extension_id.empty() && !webview_instance_id; | 956 return extension_id.empty() && |
| 957 webview_embedder_process_id == ChildProcessHost::kInvalidUniqueID && |
| 958 webview_instance_id == kInstanceIDNone; |
| 916 } | 959 } |
| 917 | 960 |
| 918 MenuItem::Id::Id() : incognito(false), uid(0) {} | 961 MenuItem::Id::Id() : incognito(false), uid(0) {} |
| 919 | 962 |
| 920 MenuItem::Id::Id(bool incognito, const MenuItem::ExtensionKey& extension_key) | 963 MenuItem::Id::Id(bool incognito, const MenuItem::ExtensionKey& extension_key) |
| 921 : incognito(incognito), extension_key(extension_key), uid(0) {} | 964 : incognito(incognito), extension_key(extension_key), uid(0) {} |
| 922 | 965 |
| 923 MenuItem::Id::~Id() { | 966 MenuItem::Id::~Id() { |
| 924 } | 967 } |
| 925 | 968 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 943 if (uid < other.uid) | 986 if (uid < other.uid) |
| 944 return true; | 987 return true; |
| 945 if (uid == other.uid) | 988 if (uid == other.uid) |
| 946 return string_uid < other.string_uid; | 989 return string_uid < other.string_uid; |
| 947 } | 990 } |
| 948 } | 991 } |
| 949 return false; | 992 return false; |
| 950 } | 993 } |
| 951 | 994 |
| 952 } // namespace extensions | 995 } // namespace extensions |
| OLD | NEW |