OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/active_script_controller.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/extensions/extension_action.h" | |
10 #include "chrome/common/extensions/api/extension_action/action_info.h" | |
11 #include "content/public/browser/notification_service.h" | |
12 #include "content/public/browser/web_contents.h" | |
13 #include "extensions/browser/extension_registry.h" | |
14 #include "extensions/common/extension.h" | |
15 #include "extensions/common/extension_set.h" | |
16 #include "extensions/common/feature_switch.h" | |
17 #include "extensions/common/permissions/permissions_data.h" | |
18 #include "grit/theme_resources.h" | |
19 #include "ui/base/resource/resource_bundle.h" | |
20 | |
21 namespace extensions { | |
22 | |
23 namespace { | |
24 | |
25 void LogActiveScriptUMA(size_t number_of_active_scripts) { | |
26 UMA_HISTOGRAM_COUNTS_100( | |
27 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage", | |
28 number_of_active_scripts); | |
29 } | |
30 | |
31 } | |
32 | |
33 ActiveScriptController::ActiveScriptController( | |
34 content::WebContents* web_contents) | |
35 : LocationBarControllerProvider(web_contents), | |
36 enabled_(FeatureSwitch::active_script_enforcement()->IsEnabled()) { | |
not at google - send to devlin
2014/05/07 22:49:02
if it's not enabled then just don't create it. thi
Devlin
2014/05/08 18:15:46
Can't just not create it, since we need it for the
| |
37 } | |
38 | |
39 ActiveScriptController::~ActiveScriptController() { | |
40 LogActiveScriptUMA(extensions_executing_scripts_.size()); | |
41 } | |
42 | |
43 void ActiveScriptController::NotifyScriptExecuting( | |
44 const std::string& extension_id) { | |
45 if (!extensions_executing_scripts_.count(extension_id)) { | |
not at google - send to devlin
2014/05/07 22:49:02
nit: early return.
Devlin
2014/05/08 18:15:46
Done.
| |
46 const Extension* extension = | |
47 GetExtensionRegistry()->enabled_extensions().GetByID(extension_id); | |
48 if (!extension || | |
49 !PermissionsData::ShouldNotifyForScriptExecution(extension)) { | |
50 return; | |
51 } | |
52 extensions_executing_scripts_.insert(extension_id); | |
53 NotifyChange(); | |
54 } | |
55 } | |
56 | |
57 bool ActiveScriptController::HasActionForExtensionId( | |
not at google - send to devlin
2014/05/07 22:49:02
eventually we'll want to separate the metrics for
Devlin
2014/05/08 18:15:46
Agreed, but not at that point yet (since right now
| |
58 const std::string& extension_id) { | |
59 return extensions_executing_scripts_.count(extension_id) > 0; | |
60 } | |
61 | |
62 std::vector<ExtensionAction*> ActiveScriptController::GetCurrentActions() { | |
63 std::vector<ExtensionAction*> current_actions; | |
64 if (!enabled_) | |
65 return current_actions; | |
66 | |
67 const ExtensionSet& extensions = GetExtensionRegistry()->enabled_extensions(); | |
68 | |
69 for (ExtensionSet::const_iterator iter = extensions.begin(); | |
70 iter != extensions.end(); | |
71 ++iter) { | |
72 if (extensions_executing_scripts_.count((*iter)->id())) | |
73 current_actions.push_back(GetActionForExtension(*iter)); | |
74 } | |
75 return current_actions; | |
76 } | |
77 | |
78 LocationBarController::Action ActiveScriptController::OnClicked( | |
79 const Extension* extension, | |
80 LocationBarController::MouseButton button) { | |
81 return LocationBarController::ACTION_NONE; | |
82 } | |
83 | |
84 void ActiveScriptController::NavigatedMainFrame( | |
85 const content::LoadCommittedDetails& details, | |
86 const content::FrameNavigateParams& params) { | |
87 LogActiveScriptUMA(extensions_executing_scripts_.size()); | |
88 extensions_executing_scripts_.clear(); | |
not at google - send to devlin
2014/05/07 23:27:51
also need to make sure not to clear these on a has
Devlin
2014/05/08 18:15:46
Done.
| |
89 } | |
90 | |
91 ExtensionAction* ActiveScriptController::GetActionForExtension( | |
not at google - send to devlin
2014/05/07 22:49:02
nit: GetOrCreateExtensionAction
Devlin
2014/05/08 18:15:46
Moot.
| |
92 const Extension* extension) { | |
93 ActiveScriptMap::iterator existing = script_badges_.find(extension->id()); | |
94 if (existing != script_badges_.end()) | |
95 return existing->second.get(); | |
96 | |
97 linked_ptr<ExtensionAction> badge( | |
98 new ExtensionAction(extension->id(), | |
99 ActionInfo::TYPE_ACTIVE_SCRIPT, | |
100 ActionInfo())); | |
101 badge->SetTitle(ExtensionAction::kDefaultTabId, extension->name()); | |
102 badge->SetIsVisible(ExtensionAction::kDefaultTabId, true); | |
103 | |
104 const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension); | |
not at google - send to devlin
2014/05/07 22:49:02
see above
Devlin
2014/05/08 18:15:46
Assuming you're referencing the "GetOrCreate" comm
| |
105 if (!action_info) | |
106 action_info = ActionInfo::GetBrowserActionInfo(extension); | |
107 | |
108 if (action_info && !action_info->default_icon.empty()) { | |
109 scoped_ptr<ExtensionIconSet> icon_set( | |
110 new ExtensionIconSet(action_info->default_icon)); | |
111 badge->set_default_icon(icon_set.Pass()); | |
not at google - send to devlin
2014/05/07 22:49:02
I swear we already have code somewhere to do this
Devlin
2014/05/08 18:15:46
It's similar to the code in the ctor for Extension
| |
112 } | |
113 | |
114 script_badges_[extension->id()] = badge; | |
115 return badge.get(); | |
116 } | |
117 | |
118 } // namespace extensions | |
OLD | NEW |