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 std::map<std::string, linked_ptr<ExtensionAction> >::iterator existing = | |
Matt Perry
2012/05/18 19:31:31
typedefing the map in the header would make this a
not at google - send to devlin
2012/05/18 22:51:27
Done.
| |
96 script_badges_.find(extension->id()); | |
97 if (existing != script_badges_.end()) | |
98 return existing->second.get(); | |
99 | |
100 linked_ptr<ExtensionAction> badge(new ExtensionAction(extension->id())); | |
101 badge->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); | |
102 badge->SetIsVisible(ExtensionAction::kDefaultTabId, true); | |
103 | |
104 // If there are existing actions, and they have default icon paths, use those. | |
105 // Otherwise we'll need to use the default icon and set it for all tabs. | |
106 std::string default_icon_path; | |
107 | |
108 if (extension->browser_action()) | |
109 default_icon_path = extension->browser_action()->default_icon_path(); | |
110 else if (extension->page_action()) | |
111 default_icon_path = extension->page_action()->default_icon_path(); | |
112 | |
113 if (!default_icon_path.empty()) { | |
114 badge->set_default_icon_path(default_icon_path); | |
115 } else { | |
116 badge->SetIcon( | |
117 ExtensionAction::kDefaultTabId, | |
118 *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
119 IDR_EXTENSIONS_FAVICON).ToSkBitmap()); | |
120 } | |
121 | |
122 script_badges_[extension->id()] = badge; | |
123 return badge.get(); | |
124 } | |
125 | |
126 ExtensionService* ScriptBadgeController::GetExtensionService() { | |
127 return ExtensionSystem::Get(tab_contents_->profile())->extension_service(); | |
128 } | |
129 | |
130 } // namespace extensions | |
OLD | NEW |