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/event_names.h" | 16 #include "chrome/browser/extensions/event_names.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
19 #include "chrome/browser/extensions/menu_manager_factory.h" | 19 #include "chrome/browser/extensions/menu_manager_factory.h" |
20 #include "chrome/browser/extensions/state_store.h" | 20 #include "chrome/browser/extensions/state_store.h" |
21 #include "chrome/browser/extensions/tab_helper.h" | 21 #include "chrome/browser/extensions/tab_helper.h" |
22 #include "chrome/browser/guestview/guestview.h" | |
22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/extensions/api/context_menus.h" | 24 #include "chrome/common/extensions/api/context_menus.h" |
24 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
26 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
28 #include "content/public/common/context_menu_params.h" | 29 #include "content/public/common/context_menu_params.h" |
29 #include "extensions/browser/event_router.h" | 30 #include "extensions/browser/event_router.h" |
30 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
31 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 | 316 |
316 // static | 317 // static |
317 MenuManager* MenuManager::Get(Profile* profile) { | 318 MenuManager* MenuManager::Get(Profile* profile) { |
318 return MenuManagerFactory::GetForProfile(profile); | 319 return MenuManagerFactory::GetForProfile(profile); |
319 } | 320 } |
320 | 321 |
321 std::set<std::string> MenuManager::ExtensionIds() { | 322 std::set<std::string> MenuManager::ExtensionIds() { |
322 std::set<std::string> id_set; | 323 std::set<std::string> id_set; |
323 for (MenuItemMap::const_iterator i = context_items_.begin(); | 324 for (MenuItemMap::const_iterator i = context_items_.begin(); |
324 i != context_items_.end(); ++i) { | 325 i != context_items_.end(); ++i) { |
325 id_set.insert(i->first); | 326 id_set.insert(i->first.extension_id); |
326 } | 327 } |
327 return id_set; | 328 return id_set; |
328 } | 329 } |
329 | 330 |
330 const MenuItem::List* MenuManager::MenuItems( | 331 const MenuItem::List* MenuManager::MenuItems( |
331 const std::string& extension_id) { | 332 const MenuItem::ExtensionKey& key) { |
332 MenuItemMap::iterator i = context_items_.find(extension_id); | 333 MenuItemMap::iterator i = context_items_.find(key); |
333 if (i != context_items_.end()) { | 334 if (i != context_items_.end()) { |
334 return &(i->second); | 335 return &(i->second); |
335 } | 336 } |
336 return NULL; | 337 return NULL; |
337 } | 338 } |
338 | 339 |
339 bool MenuManager::AddContextItem( | 340 bool MenuManager::AddContextItem(const Extension* extension, MenuItem* item) { |
340 const Extension* extension, | 341 const MenuItem::ExtensionKey key(item->id()); |
341 MenuItem* item) { | |
342 const std::string& extension_id = item->extension_id(); | |
343 // The item must have a non-empty extension id, and not have already been | 342 // The item must have a non-empty extension id, and not have already been |
344 // added. | 343 // added. |
345 if (extension_id.empty() || ContainsKey(items_by_id_, item->id())) | 344 if (key.empty() || ContainsKey(items_by_id_, item->id())) |
346 return false; | 345 return false; |
347 | 346 |
348 DCHECK_EQ(extension->id(), extension_id); | 347 DCHECK_EQ(extension->id(), key.extension_id); |
349 | 348 |
350 bool first_item = !ContainsKey(context_items_, extension_id); | 349 bool first_item = !ContainsKey(context_items_, key); |
351 context_items_[extension_id].push_back(item); | 350 context_items_[key].push_back(item); |
352 items_by_id_[item->id()] = item; | 351 items_by_id_[item->id()] = item; |
353 | 352 |
354 if (item->type() == MenuItem::RADIO) { | 353 if (item->type() == MenuItem::RADIO) { |
355 if (item->checked()) | 354 if (item->checked()) |
356 RadioItemSelected(item); | 355 RadioItemSelected(item); |
357 else | 356 else |
358 SanitizeRadioList(context_items_[extension_id]); | 357 SanitizeRadioList(context_items_[key]); |
359 } | 358 } |
360 | 359 |
361 // If this is the first item for this extension, start loading its icon. | 360 // If this is the first item for this extension, start loading its icon. |
362 if (first_item) | 361 if (first_item) |
363 icon_manager_.LoadIcon(profile_, extension); | 362 icon_manager_.LoadIcon(profile_, extension); |
364 | 363 |
365 return true; | 364 return true; |
366 } | 365 } |
367 | 366 |
368 bool MenuManager::AddChildItem(const MenuItem::Id& parent_id, | 367 bool MenuManager::AddChildItem(const MenuItem::Id& parent_id, |
369 MenuItem* child) { | 368 MenuItem* child) { |
370 MenuItem* parent = GetItemById(parent_id); | 369 MenuItem* parent = GetItemById(parent_id); |
371 if (!parent || parent->type() != MenuItem::NORMAL || | 370 if (!parent || parent->type() != MenuItem::NORMAL || |
372 parent->incognito() != child->incognito() || | 371 parent->incognito() != child->incognito() || |
373 parent->extension_id() != child->extension_id() || | |
374 ContainsKey(items_by_id_, child->id())) | 372 ContainsKey(items_by_id_, child->id())) |
375 return false; | 373 return false; |
374 | |
375 const MenuItem::ExtensionKey parent_key(parent_id); | |
376 const MenuItem::ExtensionKey child_key(child->id()); | |
377 if (parent_key != child_key) | |
378 return false; | |
379 | |
376 parent->AddChild(child); | 380 parent->AddChild(child); |
377 items_by_id_[child->id()] = child; | 381 items_by_id_[child->id()] = child; |
378 | 382 |
379 if (child->type() == MenuItem::RADIO) | 383 if (child->type() == MenuItem::RADIO) |
380 SanitizeRadioList(parent->children()); | 384 SanitizeRadioList(parent->children()); |
381 return true; | 385 return true; |
382 } | 386 } |
383 | 387 |
384 bool MenuManager::DescendantOf(MenuItem* item, | 388 bool MenuManager::DescendantOf(MenuItem* item, |
385 const MenuItem::Id& ancestor_id) { | 389 const MenuItem::Id& ancestor_id) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
417 NOTREACHED(); | 421 NOTREACHED(); |
418 return false; | 422 return false; |
419 } | 423 } |
420 MenuItem* taken = | 424 MenuItem* taken = |
421 old_parent->ReleaseChild(child_id, false /* non-recursive search*/); | 425 old_parent->ReleaseChild(child_id, false /* non-recursive search*/); |
422 DCHECK(taken == child); | 426 DCHECK(taken == child); |
423 SanitizeRadioList(old_parent->children()); | 427 SanitizeRadioList(old_parent->children()); |
424 } else { | 428 } else { |
425 // This is a top-level item, so we need to pull it out of our list of | 429 // This is a top-level item, so we need to pull it out of our list of |
426 // top-level items. | 430 // top-level items. |
427 MenuItemMap::iterator i = context_items_.find(child->extension_id()); | 431 MenuItem::ExtensionKey child_key(child->id()); |
432 MenuItemMap::iterator i = context_items_.find(child_key); | |
428 if (i == context_items_.end()) { | 433 if (i == context_items_.end()) { |
429 NOTREACHED(); | 434 NOTREACHED(); |
430 return false; | 435 return false; |
431 } | 436 } |
432 MenuItem::List& list = i->second; | 437 MenuItem::List& list = i->second; |
433 MenuItem::List::iterator j = std::find(list.begin(), list.end(), | 438 MenuItem::List::iterator j = std::find(list.begin(), list.end(), child); |
434 child); | |
435 if (j == list.end()) { | 439 if (j == list.end()) { |
436 NOTREACHED(); | 440 NOTREACHED(); |
437 return false; | 441 return false; |
438 } | 442 } |
439 list.erase(j); | 443 list.erase(j); |
440 SanitizeRadioList(list); | 444 SanitizeRadioList(list); |
441 } | 445 } |
442 | 446 |
443 if (new_parent) { | 447 if (new_parent) { |
444 new_parent->AddChild(child); | 448 new_parent->AddChild(child); |
445 SanitizeRadioList(new_parent->children()); | 449 SanitizeRadioList(new_parent->children()); |
446 } else { | 450 } else { |
447 context_items_[child->extension_id()].push_back(child); | 451 MenuItem::ExtensionKey child_key(child->id()); |
452 context_items_[child_key].push_back(child); | |
448 child->parent_id_.reset(NULL); | 453 child->parent_id_.reset(NULL); |
449 SanitizeRadioList(context_items_[child->extension_id()]); | 454 SanitizeRadioList(context_items_[child_key]); |
450 } | 455 } |
451 return true; | 456 return true; |
452 } | 457 } |
453 | 458 |
454 bool MenuManager::RemoveContextMenuItem(const MenuItem::Id& id) { | 459 bool MenuManager::RemoveContextMenuItem(const MenuItem::Id& id) { |
455 if (!ContainsKey(items_by_id_, id)) | 460 if (!ContainsKey(items_by_id_, id)) |
456 return false; | 461 return false; |
457 | 462 |
458 MenuItem* menu_item = GetItemById(id); | 463 MenuItem* menu_item = GetItemById(id); |
459 DCHECK(menu_item); | 464 DCHECK(menu_item); |
460 std::string extension_id = menu_item->extension_id(); | 465 MenuItem::ExtensionKey key(id); |
461 MenuItemMap::iterator i = context_items_.find(extension_id); | 466 MenuItemMap::iterator i = context_items_.find(key); |
462 if (i == context_items_.end()) { | 467 if (i == context_items_.end()) { |
463 NOTREACHED(); | 468 NOTREACHED(); |
464 return false; | 469 return false; |
465 } | 470 } |
466 | 471 |
467 bool result = false; | 472 bool result = false; |
468 std::set<MenuItem::Id> items_removed; | 473 std::set<MenuItem::Id> items_removed; |
469 MenuItem::List& list = i->second; | 474 MenuItem::List& list = i->second; |
470 MenuItem::List::iterator j; | 475 MenuItem::List::iterator j; |
471 for (j = list.begin(); j < list.end(); ++j) { | 476 for (j = list.begin(); j < list.end(); ++j) { |
(...skipping 24 matching lines...) Expand all Loading... | |
496 | 501 |
497 // Clear entries from the items_by_id_ map. | 502 // Clear entries from the items_by_id_ map. |
498 std::set<MenuItem::Id>::iterator removed_iter; | 503 std::set<MenuItem::Id>::iterator removed_iter; |
499 for (removed_iter = items_removed.begin(); | 504 for (removed_iter = items_removed.begin(); |
500 removed_iter != items_removed.end(); | 505 removed_iter != items_removed.end(); |
501 ++removed_iter) { | 506 ++removed_iter) { |
502 items_by_id_.erase(*removed_iter); | 507 items_by_id_.erase(*removed_iter); |
503 } | 508 } |
504 | 509 |
505 if (list.empty()) { | 510 if (list.empty()) { |
506 context_items_.erase(extension_id); | 511 context_items_.erase(key); |
507 icon_manager_.RemoveIcon(extension_id); | 512 icon_manager_.RemoveIcon(key.extension_id); |
508 } | 513 } |
509 return result; | 514 return result; |
510 } | 515 } |
511 | 516 |
512 void MenuManager::RemoveAllContextItems(const std::string& extension_id) { | 517 void MenuManager::RemoveAllContextItems( |
518 const MenuItem::ExtensionKey& extension_key) { | |
513 MenuItem::List::iterator i; | 519 MenuItem::List::iterator i; |
514 for (i = context_items_[extension_id].begin(); | 520 for (i = context_items_[extension_key].begin(); |
515 i != context_items_[extension_id].end(); ++i) { | 521 i != context_items_[extension_key].end(); |
522 ++i) { | |
516 MenuItem* item = *i; | 523 MenuItem* item = *i; |
517 items_by_id_.erase(item->id()); | 524 items_by_id_.erase(item->id()); |
518 | 525 |
519 // Remove descendants from this item and erase them from the lookup cache. | 526 // Remove descendants from this item and erase them from the lookup cache. |
520 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants(); | 527 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants(); |
521 std::set<MenuItem::Id>::const_iterator j; | 528 std::set<MenuItem::Id>::const_iterator j; |
522 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) { | 529 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) { |
523 items_by_id_.erase(*j); | 530 items_by_id_.erase(*j); |
524 } | 531 } |
525 } | 532 } |
526 STLDeleteElements(&context_items_[extension_id]); | 533 STLDeleteElements(&context_items_[extension_key]); |
527 context_items_.erase(extension_id); | 534 context_items_.erase(extension_key); |
528 icon_manager_.RemoveIcon(extension_id); | 535 icon_manager_.RemoveIcon(extension_key.extension_id); |
529 } | 536 } |
530 | 537 |
531 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const { | 538 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const { |
532 std::map<MenuItem::Id, MenuItem*>::const_iterator i = | 539 std::map<MenuItem::Id, MenuItem*>::const_iterator i = |
533 items_by_id_.find(id); | 540 items_by_id_.find(id); |
534 if (i != items_by_id_.end()) | 541 if (i != items_by_id_.end()) |
535 return i->second; | 542 return i->second; |
536 else | 543 else |
537 return NULL; | 544 return NULL; |
538 } | 545 } |
539 | 546 |
540 void MenuManager::RadioItemSelected(MenuItem* item) { | 547 void MenuManager::RadioItemSelected(MenuItem* item) { |
541 // If this is a child item, we need to get a handle to the list from its | 548 // If this is a child item, we need to get a handle to the list from its |
542 // parent. Otherwise get a handle to the top-level list. | 549 // parent. Otherwise get a handle to the top-level list. |
543 const MenuItem::List* list = NULL; | 550 const MenuItem::List* list = NULL; |
544 if (item->parent_id()) { | 551 if (item->parent_id()) { |
545 MenuItem* parent = GetItemById(*item->parent_id()); | 552 MenuItem* parent = GetItemById(*item->parent_id()); |
546 if (!parent) { | 553 if (!parent) { |
547 NOTREACHED(); | 554 NOTREACHED(); |
548 return; | 555 return; |
549 } | 556 } |
550 list = &(parent->children()); | 557 list = &(parent->children()); |
551 } else { | 558 } else { |
552 if (context_items_.find(item->extension_id()) == context_items_.end()) { | 559 MenuItem::ExtensionKey item_extension_key(item->id()); |
560 if (context_items_.find(item_extension_key) == context_items_.end()) { | |
553 NOTREACHED(); | 561 NOTREACHED(); |
554 return; | 562 return; |
555 } | 563 } |
556 list = &context_items_[item->extension_id()]; | 564 list = &context_items_[item_extension_key]; |
557 } | 565 } |
558 | 566 |
559 // Find where |item| is in the list. | 567 // Find where |item| is in the list. |
560 MenuItem::List::const_iterator item_location; | 568 MenuItem::List::const_iterator item_location; |
561 for (item_location = list->begin(); item_location != list->end(); | 569 for (item_location = list->begin(); item_location != list->end(); |
562 ++item_location) { | 570 ++item_location) { |
563 if (*item_location == item) | 571 if (*item_location == item) |
564 break; | 572 break; |
565 } | 573 } |
566 if (item_location == list->end()) { | 574 if (item_location == list->end()) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
639 AddURLProperty(properties, "linkUrl", params.unfiltered_link_url); | 647 AddURLProperty(properties, "linkUrl", params.unfiltered_link_url); |
640 AddURLProperty(properties, "srcUrl", params.src_url); | 648 AddURLProperty(properties, "srcUrl", params.src_url); |
641 AddURLProperty(properties, "pageUrl", params.page_url); | 649 AddURLProperty(properties, "pageUrl", params.page_url); |
642 AddURLProperty(properties, "frameUrl", params.frame_url); | 650 AddURLProperty(properties, "frameUrl", params.frame_url); |
643 | 651 |
644 if (params.selection_text.length() > 0) | 652 if (params.selection_text.length() > 0) |
645 properties->SetString("selectionText", params.selection_text); | 653 properties->SetString("selectionText", params.selection_text); |
646 | 654 |
647 properties->SetBoolean("editable", params.is_editable); | 655 properties->SetBoolean("editable", params.is_editable); |
648 | 656 |
657 GuestView* guest_view = GuestView::FromWebContents(web_contents); | |
Fady Samuel
2014/03/04 21:14:59
Can we make this WebViewGuest::FromWebContents ins
lazyboy
2014/03/04 23:07:02
Done.
Also, the webviewInstanceId thing is someth
| |
658 if (guest_view) | |
659 properties->SetInteger("webviewInstanceId", guest_view->view_instance_id()); | |
660 | |
649 args->Append(properties); | 661 args->Append(properties); |
650 | 662 |
651 // Add the tab info to the argument list. | 663 // Add the tab info to the argument list. |
652 // No tab info in a platform app. | 664 // No tab info in a platform app. |
653 if (!extension || !extension->is_platform_app()) { | 665 if (!extension || !extension->is_platform_app()) { |
654 // Note: web_contents are NULL in unit tests :( | 666 // Note: web_contents are NULL in unit tests :( |
655 if (web_contents) { | 667 if (web_contents) { |
656 args->Append(ExtensionTabUtil::CreateTabValue(web_contents)); | 668 args->Append(ExtensionTabUtil::CreateTabValue(web_contents)); |
657 } else { | 669 } else { |
658 args->Append(new base::DictionaryValue()); | 670 args->Append(new base::DictionaryValue()); |
(...skipping 17 matching lines...) Expand all Loading... | |
676 WriteToStorage(extension); | 688 WriteToStorage(extension); |
677 } | 689 } |
678 | 690 |
679 // Note: web_contents are NULL in unit tests :( | 691 // Note: web_contents are NULL in unit tests :( |
680 if (web_contents && extensions::TabHelper::FromWebContents(web_contents)) { | 692 if (web_contents && extensions::TabHelper::FromWebContents(web_contents)) { |
681 extensions::TabHelper::FromWebContents(web_contents)-> | 693 extensions::TabHelper::FromWebContents(web_contents)-> |
682 active_tab_permission_granter()->GrantIfRequested(extension); | 694 active_tab_permission_granter()->GrantIfRequested(extension); |
683 } | 695 } |
684 | 696 |
685 { | 697 { |
698 // Dispatch to menu item's .onclick handler. | |
686 scoped_ptr<Event> event(new Event( | 699 scoped_ptr<Event> event(new Event( |
687 event_names::kOnContextMenus, | 700 event_names::kOnContextMenus, |
688 scoped_ptr<base::ListValue>(args->DeepCopy()))); | 701 scoped_ptr<base::ListValue>(args->DeepCopy()))); |
689 event->restrict_to_browser_context = profile; | 702 event->restrict_to_browser_context = profile; |
690 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; | 703 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; |
691 event_router->DispatchEventToExtension(item->extension_id(), event.Pass()); | 704 event_router->DispatchEventToExtension(item->extension_id(), event.Pass()); |
692 } | 705 } |
693 { | 706 { |
694 scoped_ptr<Event> event(new Event(context_menus::OnClicked::kEventName, | 707 // Dispatch to .contextMenus.onClicked handler. |
695 args.Pass())); | 708 scoped_ptr<Event> event( |
709 new Event(guest_view ? event_names::kOnWebviewContextMenus | |
710 : context_menus::OnClicked::kEventName, | |
711 args.Pass())); | |
696 event->restrict_to_browser_context = profile; | 712 event->restrict_to_browser_context = profile; |
697 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; | 713 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; |
714 if (guest_view) | |
715 event->filter_info.SetInstanceID(guest_view->view_instance_id()); | |
698 event_router->DispatchEventToExtension(item->extension_id(), event.Pass()); | 716 event_router->DispatchEventToExtension(item->extension_id(), event.Pass()); |
699 } | 717 } |
700 } | 718 } |
701 | 719 |
702 void MenuManager::SanitizeRadioList(const MenuItem::List& item_list) { | 720 void MenuManager::SanitizeRadioList(const MenuItem::List& item_list) { |
703 MenuItem::List::const_iterator i = item_list.begin(); | 721 MenuItem::List::const_iterator i = item_list.begin(); |
704 while (i != item_list.end()) { | 722 while (i != item_list.end()) { |
705 if ((*i)->type() != MenuItem::RADIO) { | 723 if ((*i)->type() != MenuItem::RADIO) { |
706 ++i; | 724 ++i; |
707 break; | 725 break; |
(...skipping 28 matching lines...) Expand all Loading... | |
736 bool MenuManager::ItemUpdated(const MenuItem::Id& id) { | 754 bool MenuManager::ItemUpdated(const MenuItem::Id& id) { |
737 if (!ContainsKey(items_by_id_, id)) | 755 if (!ContainsKey(items_by_id_, id)) |
738 return false; | 756 return false; |
739 | 757 |
740 MenuItem* menu_item = GetItemById(id); | 758 MenuItem* menu_item = GetItemById(id); |
741 DCHECK(menu_item); | 759 DCHECK(menu_item); |
742 | 760 |
743 if (menu_item->parent_id()) { | 761 if (menu_item->parent_id()) { |
744 SanitizeRadioList(GetItemById(*menu_item->parent_id())->children()); | 762 SanitizeRadioList(GetItemById(*menu_item->parent_id())->children()); |
745 } else { | 763 } else { |
746 std::string extension_id = menu_item->extension_id(); | 764 MenuItem::ExtensionKey key(menu_item->id()); |
747 MenuItemMap::iterator i = context_items_.find(extension_id); | 765 MenuItemMap::iterator i = context_items_.find(key); |
748 if (i == context_items_.end()) { | 766 if (i == context_items_.end()) { |
749 NOTREACHED(); | 767 NOTREACHED(); |
750 return false; | 768 return false; |
751 } | 769 } |
752 SanitizeRadioList(i->second); | 770 SanitizeRadioList(i->second); |
753 } | 771 } |
754 | 772 |
755 return true; | 773 return true; |
756 } | 774 } |
757 | 775 |
758 void MenuManager::WriteToStorage(const Extension* extension) { | 776 void MenuManager::WriteToStorage(const Extension* extension) { |
759 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) | 777 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) |
760 return; | 778 return; |
761 const MenuItem::List* top_items = MenuItems(extension->id()); | 779 const MenuItem::List* top_items = |
780 MenuItems(MenuItem::ExtensionKey(extension->id())); | |
762 MenuItem::List all_items; | 781 MenuItem::List all_items; |
763 if (top_items) { | 782 if (top_items) { |
764 for (MenuItem::List::const_iterator i = top_items->begin(); | 783 for (MenuItem::List::const_iterator i = top_items->begin(); |
765 i != top_items->end(); ++i) { | 784 i != top_items->end(); ++i) { |
785 DCHECK(!(*i)->id().webview_instance_id); | |
766 (*i)->GetFlattenedSubtree(&all_items); | 786 (*i)->GetFlattenedSubtree(&all_items); |
767 } | 787 } |
768 } | 788 } |
769 | 789 |
770 if (store_) | 790 if (store_) { |
771 store_->SetExtensionValue(extension->id(), kContextMenusKey, | 791 store_->SetExtensionValue(extension->id(), kContextMenusKey, |
772 MenuItemsToValue(all_items)); | 792 MenuItemsToValue(all_items)); |
793 } | |
773 } | 794 } |
774 | 795 |
775 void MenuManager::ReadFromStorage(const std::string& extension_id, | 796 void MenuManager::ReadFromStorage(const std::string& extension_id, |
776 scoped_ptr<base::Value> value) { | 797 scoped_ptr<base::Value> value) { |
777 const Extension* extension = | 798 const Extension* extension = |
778 ExtensionSystem::Get(profile_)->extension_service()->extensions()-> | 799 ExtensionSystem::Get(profile_)->extension_service()->extensions()-> |
779 GetByID(extension_id); | 800 GetByID(extension_id); |
780 if (!extension) | 801 if (!extension) |
781 return; | 802 return; |
782 | 803 |
(...skipping 14 matching lines...) Expand all Loading... | |
797 } | 818 } |
798 | 819 |
799 void MenuManager::Observe(int type, | 820 void MenuManager::Observe(int type, |
800 const content::NotificationSource& source, | 821 const content::NotificationSource& source, |
801 const content::NotificationDetails& details) { | 822 const content::NotificationDetails& details) { |
802 switch (type) { | 823 switch (type) { |
803 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 824 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
804 // Remove menu items for disabled/uninstalled extensions. | 825 // Remove menu items for disabled/uninstalled extensions. |
805 const Extension* extension = | 826 const Extension* extension = |
806 content::Details<UnloadedExtensionInfo>(details)->extension; | 827 content::Details<UnloadedExtensionInfo>(details)->extension; |
807 if (ContainsKey(context_items_, extension->id())) { | 828 MenuItem::ExtensionKey extension_key(extension->id()); |
808 RemoveAllContextItems(extension->id()); | 829 if (ContainsKey(context_items_, extension_key)) { |
830 RemoveAllContextItems(extension_key); | |
809 } | 831 } |
810 break; | 832 break; |
811 } | 833 } |
812 case chrome::NOTIFICATION_EXTENSION_LOADED: { | 834 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
813 const Extension* extension = | 835 const Extension* extension = |
814 content::Details<const Extension>(details).ptr(); | 836 content::Details<const Extension>(details).ptr(); |
815 if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) { | 837 if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
816 store_->GetExtensionValue(extension->id(), kContextMenusKey, | 838 store_->GetExtensionValue(extension->id(), kContextMenusKey, |
817 base::Bind(&MenuManager::ReadFromStorage, | 839 base::Bind(&MenuManager::ReadFromStorage, |
818 AsWeakPtr(), extension->id())); | 840 AsWeakPtr(), extension->id())); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
852 items_to_remove.insert(iter->first); | 874 items_to_remove.insert(iter->first); |
853 } | 875 } |
854 | 876 |
855 std::set<MenuItem::Id>::iterator remove_iter; | 877 std::set<MenuItem::Id>::iterator remove_iter; |
856 for (remove_iter = items_to_remove.begin(); | 878 for (remove_iter = items_to_remove.begin(); |
857 remove_iter != items_to_remove.end(); | 879 remove_iter != items_to_remove.end(); |
858 ++remove_iter) | 880 ++remove_iter) |
859 RemoveContextMenuItem(*remove_iter); | 881 RemoveContextMenuItem(*remove_iter); |
860 } | 882 } |
861 | 883 |
862 MenuItem::Id::Id() : incognito(false), uid(0) {} | 884 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id, |
885 int webview_instance_id) | |
886 : extension_id(extension_id), webview_instance_id(webview_instance_id) {} | |
887 | |
888 MenuItem::ExtensionKey::ExtensionKey(const MenuItem::Id& item_id) | |
Fady Samuel
2014/03/04 21:14:59
We can probably get rid of this if MenuItem::Id ha
lazyboy
2014/03/04 23:07:02
Done.
| |
889 : extension_id(item_id.extension_id), | |
890 webview_instance_id(item_id.webview_instance_id) {} | |
891 | |
892 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id) | |
893 : extension_id(extension_id), webview_instance_id(0) {} | |
894 | |
895 bool MenuItem::ExtensionKey::operator==(const ExtensionKey& other) const { | |
896 return extension_id == other.extension_id && | |
897 webview_instance_id == other.webview_instance_id; | |
898 } | |
899 | |
900 bool MenuItem::ExtensionKey::operator<(const ExtensionKey& other) const { | |
901 if (webview_instance_id != other.webview_instance_id) | |
902 return webview_instance_id < other.webview_instance_id; | |
903 | |
904 return extension_id < other.extension_id; | |
905 } | |
906 | |
907 bool MenuItem::ExtensionKey::operator!=(const ExtensionKey& other) const { | |
908 return !(*this == other); | |
909 } | |
910 | |
911 bool MenuItem::ExtensionKey::empty() const { | |
912 return extension_id.empty() && !webview_instance_id; | |
913 } | |
914 | |
915 MenuItem::Id::Id() : incognito(false), uid(0), webview_instance_id(0) {} | |
863 | 916 |
864 MenuItem::Id::Id(bool incognito, const std::string& extension_id) | 917 MenuItem::Id::Id(bool incognito, const std::string& extension_id) |
865 : incognito(incognito), extension_id(extension_id), uid(0) {} | 918 : incognito(incognito), |
919 extension_id(extension_id), | |
Fady Samuel
2014/03/04 21:14:59
So MenuItem now has an extension_id, and a webview
lazyboy
2014/03/04 23:07:02
Done.
| |
920 uid(0), | |
921 webview_instance_id(0) {} | |
866 | 922 |
867 MenuItem::Id::~Id() { | 923 MenuItem::Id::~Id() { |
868 } | 924 } |
869 | 925 |
870 bool MenuItem::Id::operator==(const Id& other) const { | 926 bool MenuItem::Id::operator==(const Id& other) const { |
871 return (incognito == other.incognito && | 927 return (incognito == other.incognito && extension_id == other.extension_id && |
872 extension_id == other.extension_id && | 928 uid == other.uid && string_uid == other.string_uid && |
873 uid == other.uid && | 929 webview_instance_id == other.webview_instance_id); |
Fady Samuel
2014/03/04 21:14:59
one less condition to check if this thing was an E
lazyboy
2014/03/04 23:07:02
Done.
| |
874 string_uid == other.string_uid); | |
875 } | 930 } |
876 | 931 |
877 bool MenuItem::Id::operator!=(const Id& other) const { | 932 bool MenuItem::Id::operator!=(const Id& other) const { |
878 return !(*this == other); | 933 return !(*this == other); |
879 } | 934 } |
880 | 935 |
881 bool MenuItem::Id::operator<(const Id& other) const { | 936 bool MenuItem::Id::operator<(const Id& other) const { |
882 if (incognito < other.incognito) | 937 if (incognito < other.incognito) |
883 return true; | 938 return true; |
884 if (incognito == other.incognito) { | 939 if (incognito == other.incognito) { |
885 if (extension_id < other.extension_id) | 940 if (extension_id < other.extension_id) |
886 return true; | 941 return true; |
887 if (extension_id == other.extension_id) { | 942 if (extension_id == other.extension_id) { |
888 if (uid < other.uid) | 943 if (webview_instance_id < other.webview_instance_id) |
Fady Samuel
2014/03/04 21:14:59
If this was an ExtensionKey you could do both comp
lazyboy
2014/03/04 23:07:02
Done.
| |
889 return true; | 944 return true; |
890 if (uid == other.uid) | 945 |
891 return string_uid < other.string_uid; | 946 if (webview_instance_id == other.webview_instance_id) { |
947 if (uid < other.uid) | |
948 return true; | |
949 if (uid == other.uid) | |
950 return string_uid < other.string_uid; | |
951 } | |
892 } | 952 } |
893 } | 953 } |
894 return false; | 954 return false; |
895 } | 955 } |
896 | 956 |
897 } // namespace extensions | 957 } // namespace extensions |
OLD | NEW |