Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: chrome/browser/extensions/active_script_controller.cc

Issue 270153004: Introduce ActiveScriptController; track active extension scripts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Kalman's Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/extensions/extension_action.h"
9 #include "chrome/common/extensions/api/extension_action/action_info.h"
10 #include "content/public/browser/navigation_controller.h"
11 #include "content/public/browser/navigation_entry.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
19 namespace extensions {
20
21 namespace {
22
23 void LogActiveScriptUMA(size_t number_of_active_scripts) {
24 UMA_HISTOGRAM_COUNTS_100(
25 "Extensions.ActiveScriptController.ShownActiveScriptsOnPage",
26 number_of_active_scripts);
27 }
28
29 }
30
31 ActiveScriptController::ActiveScriptController(
32 content::WebContents* web_contents)
33 : web_contents_(web_contents),
34 enabled_(FeatureSwitch::scripts_require_action()->IsEnabled()) {
35 }
36
37 ActiveScriptController::~ActiveScriptController() {
38 LogActiveScriptUMA(extensions_executing_scripts_.size());
not at google - send to devlin 2014/05/08 20:47:09 you should only log UMA if it's enabled
Devlin 2014/05/08 23:01:00 Free (still good data) even if not enabled.
39 }
40
41 void ActiveScriptController::NotifyScriptExecuting(
42 const std::string& extension_id, int page_id) {
43 if (extensions_executing_scripts_.count(extension_id) ||
44 web_contents_->GetController().GetVisibleEntry()->GetPageID()
45 != page_id) {
46 return;
47 }
48
49 const Extension* extension =
50 ExtensionRegistry::Get(web_contents_->GetBrowserContext())
51 ->enabled_extensions().GetByID(extension_id);
52 if (extension &&
53 PermissionsData::RequiresActionForScriptExecution(extension)) {
54 extensions_executing_scripts_.insert(extension_id);
55 LocationBarController::NotifyChange(web_contents_);
56 }
57 }
58
59 bool ActiveScriptController::HasActionForExtensionId(
60 const std::string& extension_id) {
61 return extensions_executing_scripts_.count(extension_id) > 0;
not at google - send to devlin 2014/05/08 20:47:09 If this isn't |enabled_| then you'll be giving mis
62 }
63
64 ExtensionAction* ActiveScriptController::GetActionForExtension(
65 const Extension* extension) {
not at google - send to devlin 2014/05/08 20:47:09 check |enabled_|
Devlin 2014/05/08 23:01:00 Done.
66 if (extensions_executing_scripts_.count(extension->id()) == 0)
67 return NULL; // No action for this extension.
68
69 ActiveScriptMap::iterator existing =
70 active_script_actions_.find(extension->id());
71 if (existing != active_script_actions_.end())
72 return existing->second.get();
73
74 linked_ptr<ExtensionAction> action(
75 new ExtensionAction(extension->id(),
76 ActionInfo::TYPE_PAGE,
77 ActionInfo()));
78 action->SetTitle(ExtensionAction::kDefaultTabId, extension->name());
79 action->SetIsVisible(ExtensionAction::kDefaultTabId, true);
80
81 const ActionInfo* action_info = ActionInfo::GetPageActionInfo(extension);
82 if (!action_info)
83 action_info = ActionInfo::GetBrowserActionInfo(extension);
84
85 if (action_info && !action_info->default_icon.empty()) {
86 scoped_ptr<ExtensionIconSet> icon_set(
87 new ExtensionIconSet(action_info->default_icon));
88 action->set_default_icon(icon_set.Pass());
not at google - send to devlin 2014/05/08 20:47:09 easier on the eyes: action->set_default_icon(
Devlin 2014/05/08 23:01:00 Done.
89 }
90
91 active_script_actions_[extension->id()] = action;
92 return action.get();
93 }
94
95 LocationBarController::Action ActiveScriptController::OnClicked(
96 const Extension* extension) {
not at google - send to devlin 2014/05/08 20:47:09 DCHECK(enabled_)
Devlin 2014/05/08 23:01:00 We can get OnClicked even if not enabled, since th
97 return LocationBarController::ACTION_NONE;
98 }
99
100 void ActiveScriptController::OnNavigated() {
101 LogActiveScriptUMA(extensions_executing_scripts_.size());
not at google - send to devlin 2014/05/08 20:47:09 only log UMA if enable Generally there is so much
Devlin 2014/05/08 23:01:00 UMA if not enabled is fine. Only need to check en
102 extensions_executing_scripts_.clear();
103 }
104
105 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698