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

Side by Side Diff: extensions/browser/api/declarative/declarative_api.cc

Issue 2863053002: Extensions: Add metrics to distinguish between the different kinds of declarative API function call… (Closed)
Patch Set: Address comments. Created 3 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
OLDNEW
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 "extensions/browser/api/declarative/declarative_api.h" 5 #include "extensions/browser/api/declarative/declarative_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/metrics/histogram_macros.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/strings/string_util.h"
13 #include "base/task_runner_util.h" 15 #include "base/task_runner_util.h"
14 #include "base/values.h" 16 #include "base/values.h"
15 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/render_frame_host.h" 18 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/api/declarative/rules_registry_service.h" 21 #include "extensions/browser/api/declarative/rules_registry_service.h"
20 #include "extensions/browser/api/extensions_api_client.h" 22 #include "extensions/browser/api/extensions_api_client.h"
21 #include "extensions/browser/extension_system.h" 23 #include "extensions/browser/extension_system.h"
22 #include "extensions/browser/guest_view/web_view/web_view_constants.h" 24 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
23 #include "extensions/browser/guest_view/web_view/web_view_guest.h" 25 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
24 #include "extensions/common/api/events.h" 26 #include "extensions/common/api/events.h"
25 #include "extensions/common/extension_api.h" 27 #include "extensions/common/extension_api.h"
26 #include "extensions/common/permissions/permissions_data.h" 28 #include "extensions/common/permissions/permissions_data.h"
27 29
28 using extensions::api::events::Rule; 30 using extensions::api::events::Rule;
29 31
30 namespace AddRules = extensions::api::events::Event::AddRules; 32 namespace AddRules = extensions::api::events::Event::AddRules;
31 namespace GetRules = extensions::api::events::Event::GetRules; 33 namespace GetRules = extensions::api::events::Event::GetRules;
32 namespace RemoveRules = extensions::api::events::Event::RemoveRules; 34 namespace RemoveRules = extensions::api::events::Event::RemoveRules;
33 35
34 namespace extensions { 36 namespace extensions {
35 37
36 namespace { 38 namespace {
37 39
38 const char kDeclarativeEventPrefix[] = "declarative"; 40 constexpr char kDeclarativeEventPrefix[] = "declarative";
41 constexpr char kDeclarativeContentEventPrefix[] = "declarativeContent.";
42 constexpr char kDeclarativeWebRequestEventPrefix[] = "declarativeWebRequest.";
43 constexpr char kDeclarativeWebRequestWebViewEventPrefix[] =
44 "webViewInternal.declarativeWebRequest.";
45
46 // The type of Declarative API. To collect more granular metrics, a distinction
47 // is made when the declarative web request API is used from a webview.
48 enum class DeclarativeAPIType {
49 kDeclarativeContent,
50 kDeclarativeWebRequest,
51 kDeclarativeWebRequestWebview,
52 kUnknown,
53 };
54
55 // Describes the possible types of declarative API function calls.
56 // These values are written to logs. New enum values can be added, but existing
57 // enum values must never be renumbered or deleted and reused.
58 enum DeclarativeAPIFunctionCallType {
59 kDeclarativeContentAddRules = 0,
60 kDeclarativeContentRemoveRules = 1,
61 kDeclarativeContentGetRules = 2,
62 kDeclarativeWebRequestAddRules = 3,
63 kDeclarativeWebRequestRemoveRules = 4,
64 kDeclarativeWebRequestGetRules = 5,
65 kDeclarativeWebRequestWebviewAddRules = 6,
66 kDeclarativeWebRequestWebviewRemoveRules = 7,
67 kDeclarativeWebRequestWebviewGetRules = 8,
68 kDeclarativeApiFunctionCallTypeMax,
69 };
70
71 DeclarativeAPIType GetDeclarativeAPITypeFromEventName(
lazyboy 2017/05/05 23:11:16 nit: FromEventName is redundant since it should be
karandeepb 2017/05/05 23:35:05 Done.
72 const std::string& event_name) {
73 if (base::StartsWith(event_name, kDeclarativeContentEventPrefix,
74 base::CompareCase::SENSITIVE))
75 return DeclarativeAPIType::kDeclarativeContent;
76 if (base::StartsWith(event_name, kDeclarativeWebRequestEventPrefix,
77 base::CompareCase::SENSITIVE))
78 return DeclarativeAPIType::kDeclarativeWebRequest;
79 if (base::StartsWith(event_name, kDeclarativeWebRequestWebViewEventPrefix,
80 base::CompareCase::SENSITIVE))
81 return DeclarativeAPIType::kDeclarativeWebRequestWebview;
82 return DeclarativeAPIType::kUnknown;
83 }
84
85 void RecordUMAHelper(DeclarativeAPIFunctionCallType call_type) {
86 DCHECK_LT(call_type, kDeclarativeApiFunctionCallTypeMax);
87 UMA_HISTOGRAM_ENUMERATION("Extensions.DeclarativeAPIFunctionCalls", call_type,
88 kDeclarativeApiFunctionCallTypeMax);
89 }
39 90
40 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict); 91 void ConvertBinaryDictionaryValuesToBase64(base::DictionaryValue* dict);
41 92
42 // Encodes |binary| as base64 and returns a new StringValue populated with the 93 // Encodes |binary| as base64 and returns a new StringValue populated with the
43 // encoded string. 94 // encoded string.
44 std::unique_ptr<base::Value> ConvertBinaryToBase64(base::Value* binary) { 95 std::unique_ptr<base::Value> ConvertBinaryToBase64(base::Value* binary) {
45 std::string binary_data(binary->GetBlob().data(), binary->GetBlob().size()); 96 std::string binary_data(binary->GetBlob().data(), binary->GetBlob().size());
46 std::string data64; 97 std::string data64;
47 base::Base64Encode(binary_data, &data64); 98 base::Base64Encode(binary_data, &data64);
48 return std::unique_ptr<base::Value>(new base::Value(data64)); 99 return std::unique_ptr<base::Value>(new base::Value(data64));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 } 169 }
119 170
120 bool RulesFunction::RunAsync() { 171 bool RulesFunction::RunAsync() {
121 std::string event_name; 172 std::string event_name;
122 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); 173 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
123 174
124 int web_view_instance_id = 0; 175 int web_view_instance_id = 0;
125 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &web_view_instance_id)); 176 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &web_view_instance_id));
126 int embedder_process_id = render_frame_host()->GetProcess()->GetID(); 177 int embedder_process_id = render_frame_host()->GetProcess()->GetID();
127 178
179 RecordUMA(event_name);
lazyboy 2017/05/05 23:11:16 Since this requires us to search for 3 prefixes in
karandeepb 2017/05/05 23:35:05 Acknowledged.
Ilya Sherman 2017/05/05 23:45:15 It's possible, but you should not do this. We do
lazyboy 2017/05/05 23:59:12 Good to know that! Thanks. The code here isn't a b
180
128 bool from_web_view = web_view_instance_id != 0; 181 bool from_web_view = web_view_instance_id != 0;
129 // If we are not operating on a particular <webview>, then the key is 0. 182 // If we are not operating on a particular <webview>, then the key is 0.
130 int rules_registry_id = RulesRegistryService::kDefaultRulesRegistryID; 183 int rules_registry_id = RulesRegistryService::kDefaultRulesRegistryID;
131 if (from_web_view) { 184 if (from_web_view) {
132 // Sample event names: 185 // Sample event names:
133 // webViewInternal.declarativeWebRequest.onRequest. 186 // webViewInternal.declarativeWebRequest.onRequest.
134 // webViewInternal.declarativeWebRequest.onMessage. 187 // webViewInternal.declarativeWebRequest.onMessage.
135 // The "webViewInternal." prefix is removed from the event name. 188 // The "webViewInternal." prefix is removed from the event name.
136 std::size_t found = event_name.find(kDeclarativeEventPrefix); 189 std::size_t found = event_name.find(kDeclarativeEventPrefix);
137 EXTENSION_FUNCTION_VALIDATE(found != std::string::npos); 190 EXTENSION_FUNCTION_VALIDATE(found != std::string::npos);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 if (error_.empty()) { 235 if (error_.empty()) {
183 std::unique_ptr<base::ListValue> rules_value(new base::ListValue()); 236 std::unique_ptr<base::ListValue> rules_value(new base::ListValue());
184 for (const auto& rule : linked_rules) 237 for (const auto& rule : linked_rules)
185 rules_value->Append(rule->ToValue()); 238 rules_value->Append(rule->ToValue());
186 SetResult(std::move(rules_value)); 239 SetResult(std::move(rules_value));
187 } 240 }
188 241
189 return error_.empty(); 242 return error_.empty();
190 } 243 }
191 244
245 void EventsEventAddRulesFunction::RecordUMA(
246 const std::string& event_name) const {
247 DeclarativeAPIFunctionCallType call_type = kDeclarativeApiFunctionCallTypeMax;
248 switch (GetDeclarativeAPITypeFromEventName(event_name)) {
249 case DeclarativeAPIType::kDeclarativeContent:
250 call_type = kDeclarativeContentAddRules;
251 break;
252 case DeclarativeAPIType::kDeclarativeWebRequest:
253 call_type = kDeclarativeWebRequestAddRules;
254 break;
255 case DeclarativeAPIType::kDeclarativeWebRequestWebview:
256 call_type = kDeclarativeWebRequestWebviewAddRules;
257 break;
258 case DeclarativeAPIType::kUnknown:
259 NOTREACHED();
260 return;
261 }
262 RecordUMAHelper(call_type);
263 }
264
192 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() { 265 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() {
193 std::unique_ptr<RemoveRules::Params> params( 266 std::unique_ptr<RemoveRules::Params> params(
194 RemoveRules::Params::Create(*args_)); 267 RemoveRules::Params::Create(*args_));
195 EXTENSION_FUNCTION_VALIDATE(params.get()); 268 EXTENSION_FUNCTION_VALIDATE(params.get());
196 269
197 if (params->rule_identifiers.get()) { 270 if (params->rule_identifiers.get()) {
198 error_ = rules_registry_->RemoveRules(extension_id(), 271 error_ = rules_registry_->RemoveRules(extension_id(),
199 *params->rule_identifiers); 272 *params->rule_identifiers);
200 } else { 273 } else {
201 error_ = rules_registry_->RemoveAllRules(extension_id()); 274 error_ = rules_registry_->RemoveAllRules(extension_id());
202 } 275 }
203 276
204 return error_.empty(); 277 return error_.empty();
205 } 278 }
206 279
280 void EventsEventRemoveRulesFunction::RecordUMA(
281 const std::string& event_name) const {
282 DeclarativeAPIFunctionCallType call_type = kDeclarativeApiFunctionCallTypeMax;
283 switch (GetDeclarativeAPITypeFromEventName(event_name)) {
284 case DeclarativeAPIType::kDeclarativeContent:
285 call_type = kDeclarativeContentRemoveRules;
286 break;
287 case DeclarativeAPIType::kDeclarativeWebRequest:
288 call_type = kDeclarativeWebRequestRemoveRules;
289 break;
290 case DeclarativeAPIType::kDeclarativeWebRequestWebview:
291 call_type = kDeclarativeWebRequestWebviewRemoveRules;
292 break;
293 case DeclarativeAPIType::kUnknown:
294 NOTREACHED();
295 return;
296 }
297 RecordUMAHelper(call_type);
298 }
299
207 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() { 300 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() {
208 std::unique_ptr<GetRules::Params> params(GetRules::Params::Create(*args_)); 301 std::unique_ptr<GetRules::Params> params(GetRules::Params::Create(*args_));
209 EXTENSION_FUNCTION_VALIDATE(params.get()); 302 EXTENSION_FUNCTION_VALIDATE(params.get());
210 303
211 std::vector<linked_ptr<Rule> > rules; 304 std::vector<linked_ptr<Rule> > rules;
212 if (params->rule_identifiers.get()) { 305 if (params->rule_identifiers.get()) {
213 rules_registry_->GetRules( 306 rules_registry_->GetRules(
214 extension_id(), *params->rule_identifiers, &rules); 307 extension_id(), *params->rule_identifiers, &rules);
215 } else { 308 } else {
216 rules_registry_->GetAllRules(extension_id(), &rules); 309 rules_registry_->GetAllRules(extension_id(), &rules);
217 } 310 }
218 311
219 std::unique_ptr<base::ListValue> rules_value(new base::ListValue()); 312 std::unique_ptr<base::ListValue> rules_value(new base::ListValue());
220 for (const auto& rule : rules) 313 for (const auto& rule : rules)
221 rules_value->Append(rule->ToValue()); 314 rules_value->Append(rule->ToValue());
222 SetResult(std::move(rules_value)); 315 SetResult(std::move(rules_value));
223 316
224 return true; 317 return true;
225 } 318 }
226 319
320 void EventsEventGetRulesFunction::RecordUMA(
321 const std::string& event_name) const {
322 DeclarativeAPIFunctionCallType call_type = kDeclarativeApiFunctionCallTypeMax;
323 switch (GetDeclarativeAPITypeFromEventName(event_name)) {
324 case DeclarativeAPIType::kDeclarativeContent:
325 call_type = kDeclarativeContentGetRules;
326 break;
327 case DeclarativeAPIType::kDeclarativeWebRequest:
328 call_type = kDeclarativeWebRequestGetRules;
329 break;
330 case DeclarativeAPIType::kDeclarativeWebRequestWebview:
331 call_type = kDeclarativeWebRequestWebviewGetRules;
332 break;
333 case DeclarativeAPIType::kUnknown:
334 NOTREACHED();
335 return;
336 }
337 RecordUMAHelper(call_type);
338 }
339
227 } // namespace extensions 340 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/declarative/declarative_api.h ('k') | tools/metrics/histograms/enums.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698