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