OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/script_badge_controller.h" |
| 6 |
| 7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/extension_action.h" |
| 12 #include "chrome/common/extensions/extension_set.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "grit/theme_resources.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 ScriptBadgeController::ScriptBadgeController(TabContentsWrapper* tab_contents) |
| 22 : content::WebContentsObserver(tab_contents->web_contents()), |
| 23 script_executor_(tab_contents->web_contents()), |
| 24 tab_contents_(tab_contents) { |
| 25 } |
| 26 |
| 27 ScriptBadgeController::~ScriptBadgeController() { |
| 28 } |
| 29 |
| 30 scoped_ptr<std::vector<ExtensionAction*> > |
| 31 ScriptBadgeController::GetCurrentActions() { |
| 32 const GURL& current_url = tab_contents_->web_contents()->GetURL(); |
| 33 const ExtensionSet* extensions = GetExtensionService()->extensions(); |
| 34 |
| 35 scoped_ptr<std::vector<ExtensionAction*> > current_actions( |
| 36 new std::vector<ExtensionAction*>()); |
| 37 for (ExtensionSet::const_iterator it = extensions->begin(); |
| 38 it != extensions->end(); ++it) { |
| 39 const Extension* extension = *it; |
| 40 if (extension->HasContentScriptAtURL(current_url) || |
| 41 extensions_executing_scripts_.count(extension->id())) { |
| 42 current_actions->push_back(GetScriptBadge(extension)); |
| 43 } |
| 44 } |
| 45 return current_actions.Pass(); |
| 46 } |
| 47 |
| 48 ActionBoxController::Action ScriptBadgeController::OnClicked( |
| 49 const std::string& extension_id, int mouse_button) { |
| 50 const Extension* extension = |
| 51 GetExtensionService()->extensions()->GetByID(extension_id); |
| 52 CHECK(extension); |
| 53 |
| 54 switch (mouse_button) { |
| 55 case 3: // right |
| 56 return extension->ShowConfigureContextMenus() ? |
| 57 ACTION_SHOW_CONTEXT_MENU : ACTION_NONE; |
| 58 } |
| 59 |
| 60 return ACTION_NONE; |
| 61 } |
| 62 |
| 63 void ScriptBadgeController::ExecuteScript( |
| 64 const std::string& extension_id, |
| 65 ScriptExecutor::ScriptType script_type, |
| 66 const std::string& code, |
| 67 ScriptExecutor::FrameScope frame_scope, |
| 68 UserScript::RunLocation run_at, |
| 69 ScriptExecutor::WorldType world_type, |
| 70 const ExecuteScriptCallback& callback) { |
| 71 script_executor_.ExecuteScript(extension_id, |
| 72 script_type, |
| 73 code, |
| 74 frame_scope, |
| 75 run_at, |
| 76 world_type, |
| 77 callback); |
| 78 |
| 79 // This tab should now show that the extension executing a script. |
| 80 extensions_executing_scripts_.insert(extension_id); |
| 81 content::NotificationService::current()->Notify( |
| 82 chrome::NOTIFICATION_EXTENSION_ACTION_BOX_UPDATED, |
| 83 content::Source<Profile>(tab_contents_->profile()), |
| 84 content::Details<TabContentsWrapper>(tab_contents_)); |
| 85 } |
| 86 |
| 87 void ScriptBadgeController::DidNavigateMainFrame( |
| 88 const content::LoadCommittedDetails& details, |
| 89 const content::FrameNavigateParams& params) { |
| 90 extensions_executing_scripts_.clear(); |
| 91 } |
| 92 |
| 93 ExtensionAction* ScriptBadgeController::GetScriptBadge( |
| 94 const Extension* extension) { |
| 95 ScriptBadgeMap::iterator existing = script_badges_.find(extension->id()); |
| 96 if (existing != script_badges_.end()) |
| 97 return existing->second.get(); |
| 98 |
| 99 linked_ptr<ExtensionAction> badge(new ExtensionAction(extension->id())); |
| 100 badge->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); |
| 101 badge->SetIsVisible(ExtensionAction::kDefaultTabId, true); |
| 102 |
| 103 // If there are existing actions, and they have default icon paths, use those. |
| 104 // Otherwise we'll need to use the default icon and set it for all tabs. |
| 105 std::string default_icon_path; |
| 106 |
| 107 if (extension->browser_action()) |
| 108 default_icon_path = extension->browser_action()->default_icon_path(); |
| 109 else if (extension->page_action()) |
| 110 default_icon_path = extension->page_action()->default_icon_path(); |
| 111 |
| 112 if (!default_icon_path.empty()) { |
| 113 badge->set_default_icon_path(default_icon_path); |
| 114 } else { |
| 115 badge->SetIcon( |
| 116 ExtensionAction::kDefaultTabId, |
| 117 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 118 IDR_EXTENSIONS_FAVICON).ToSkBitmap()); |
| 119 } |
| 120 |
| 121 script_badges_[extension->id()] = badge; |
| 122 return badge.get(); |
| 123 } |
| 124 |
| 125 ExtensionService* ScriptBadgeController::GetExtensionService() { |
| 126 return ExtensionSystem::Get(tab_contents_->profile())->extension_service(); |
| 127 } |
| 128 |
| 129 } // namespace extensions |
OLD | NEW |