OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/renderer/pepper/pepper_uma_host.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "base/sha1.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/renderer/chrome_content_renderer_client.h" |
| 12 #include "content/public/renderer/renderer_ppapi_host.h" |
| 13 #include "extensions/common/constants.h" |
| 14 #include "extensions/common/extension.h" |
| 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/host/dispatch_host_message.h" |
| 17 #include "ppapi/host/host_message_context.h" |
| 18 #include "ppapi/host/ppapi_host.h" |
| 19 #include "ppapi/proxy/ppapi_messages.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char* kPredefinedAllowedUMAOrigins[] = { |
| 24 "6EAED1924DB611B6EEF2A664BD077BE7EAD33B8F", // see crbug.com/317833 |
| 25 "4EB74897CB187C7633357C2FE832E0AD6A44883A" // see crbug.com/317833 |
| 26 }; |
| 27 |
| 28 const char* kWhitelistedHistogramHashes[] = { |
| 29 "F131550DAB7A7C6E6633EF81FB5998CC0482AC63", // see crbug.com/317833 |
| 30 "BFA80E7D71DE24D08B7363B108CD6DABFF24B9A0", // see crbug.com/317833 |
| 31 "13955AB4DAD798384DFB4304734FCF2A95F353CC", // see crbug.com/317833 |
| 32 "404E800582901F1B937B8E287235FC603A5DEDFB", // see crbug.com/317833 |
| 33 "D95DDB0F180CF797AD30E221D659A9E2B6404BC8" // see crbug.com/317833 |
| 34 }; |
| 35 |
| 36 std::string HashHistogram(const std::string& histogram) { |
| 37 const std::string id_hash = base::SHA1HashString(histogram); |
| 38 DCHECK_EQ(id_hash.length(), base::kSHA1Length); |
| 39 return base::HexEncode(id_hash.c_str(), id_hash.length()); |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 PepperUMAHost::PepperUMAHost( |
| 45 content::RendererPpapiHost* host, |
| 46 PP_Instance instance, |
| 47 PP_Resource resource) |
| 48 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 49 document_url_(host->GetDocumentURL(instance)), |
| 50 is_plugin_in_process_(host->IsRunningInProcess()) { |
| 51 for (size_t i = 0; i < arraysize(kPredefinedAllowedUMAOrigins); ++i) |
| 52 allowed_origins_.insert(kPredefinedAllowedUMAOrigins[i]); |
| 53 for (size_t i = 0; i < arraysize(kWhitelistedHistogramHashes); ++i) |
| 54 allowed_histograms_.insert(kWhitelistedHistogramHashes[i]); |
| 55 } |
| 56 |
| 57 PepperUMAHost::~PepperUMAHost() { |
| 58 } |
| 59 |
| 60 int32_t PepperUMAHost::OnResourceMessageReceived( |
| 61 const IPC::Message& msg, |
| 62 ppapi::host::HostMessageContext* context) { |
| 63 IPC_BEGIN_MESSAGE_MAP(PepperUMAHost, msg) |
| 64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_UMA_HistogramCustomTimes, |
| 65 OnHistogramCustomTimes); |
| 66 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_UMA_HistogramCustomCounts, |
| 67 OnHistogramCustomCounts); |
| 68 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_UMA_HistogramEnumeration, |
| 69 OnHistogramEnumeration); |
| 70 IPC_END_MESSAGE_MAP() |
| 71 return PP_ERROR_FAILED; |
| 72 } |
| 73 |
| 74 bool PepperUMAHost::IsHistogramAllowed(const std::string& histogram) { |
| 75 if (is_plugin_in_process_ && histogram.find("NaCl.") == 0) { |
| 76 return true; |
| 77 } |
| 78 |
| 79 bool is_whitelisted = |
| 80 ChromeContentRendererClient::IsExtensionOrSharedModuleWhitelisted( |
| 81 document_url_, allowed_origins_); |
| 82 if (is_whitelisted && |
| 83 allowed_histograms_.find(HashHistogram(histogram)) != |
| 84 allowed_histograms_.end()) { |
| 85 return true; |
| 86 } |
| 87 |
| 88 LOG(ERROR) << "Host or histogram name is not allowed to use the UMA API."; |
| 89 return false; |
| 90 } |
| 91 |
| 92 #define RETURN_IF_BAD_ARGS(_sample, _min, _max, _buckets) \ |
| 93 do { \ |
| 94 if (_min >= _max || _sample < _min || _sample > _max || _buckets <= 1) \ |
| 95 return PP_ERROR_BADARGUMENT; \ |
| 96 } while (0) |
| 97 |
| 98 int32_t PepperUMAHost::OnHistogramCustomTimes( |
| 99 ppapi::host::HostMessageContext* context, |
| 100 const std::string& name, |
| 101 int64_t sample, |
| 102 int64_t min, |
| 103 int64_t max, |
| 104 uint32_t bucket_count) { |
| 105 if (!IsHistogramAllowed(name)) { |
| 106 return PP_ERROR_NOACCESS; |
| 107 } |
| 108 RETURN_IF_BAD_ARGS(sample, min, max, bucket_count); |
| 109 |
| 110 base::HistogramBase* counter = |
| 111 base::Histogram::FactoryTimeGet( |
| 112 name, |
| 113 base::TimeDelta::FromMilliseconds(min), |
| 114 base::TimeDelta::FromMilliseconds(max), |
| 115 bucket_count, |
| 116 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 117 counter->AddTime(base::TimeDelta::FromMilliseconds(sample)); |
| 118 return PP_OK; |
| 119 } |
| 120 |
| 121 int32_t PepperUMAHost::OnHistogramCustomCounts( |
| 122 ppapi::host::HostMessageContext* context, |
| 123 const std::string& name, |
| 124 int32_t sample, |
| 125 int32_t min, |
| 126 int32_t max, |
| 127 uint32_t bucket_count) { |
| 128 if (!IsHistogramAllowed(name)) { |
| 129 return PP_ERROR_NOACCESS; |
| 130 } |
| 131 RETURN_IF_BAD_ARGS(sample, min, max, bucket_count); |
| 132 |
| 133 base::HistogramBase* counter = |
| 134 base::Histogram::FactoryGet( |
| 135 name, |
| 136 min, |
| 137 max, |
| 138 bucket_count, |
| 139 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 140 counter->Add(sample); |
| 141 return PP_OK; |
| 142 } |
| 143 |
| 144 int32_t PepperUMAHost::OnHistogramEnumeration( |
| 145 ppapi::host::HostMessageContext* context, |
| 146 const std::string& name, |
| 147 int32_t sample, |
| 148 int32_t boundary_value) { |
| 149 if (!IsHistogramAllowed(name)) { |
| 150 return PP_ERROR_NOACCESS; |
| 151 } |
| 152 RETURN_IF_BAD_ARGS(sample, 1, boundary_value, boundary_value + 1); |
| 153 |
| 154 base::HistogramBase* counter = |
| 155 base::Histogram::FactoryGet( |
| 156 name, |
| 157 1, |
| 158 boundary_value, |
| 159 boundary_value + 1, |
| 160 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 161 counter->Add(sample); |
| 162 return PP_OK; |
| 163 } |
| 164 |
OLD | NEW |