| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/plugin_uma.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cstring> | |
| 9 | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "base/string_util.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // String we will use to convert mime tyoe to plugin type. | |
| 16 const char kWindowsMediaPlayerType[] = "application/x-mplayer2"; | |
| 17 const char kSilverlightTypePrefix[] = "application/x-silverlight"; | |
| 18 const char kRealPlayerTypePrefix[] = "audio/x-pn-realaudio"; | |
| 19 const char kJavaTypeSubstring[] = "application/x-java-applet"; | |
| 20 const char kQuickTimeType[] = "video/quicktime"; | |
| 21 | |
| 22 // Arrays containing file extensions connected with specific plugins. | |
| 23 // The arrays must be sorted because binary search is used on them. | |
| 24 const char* kWindowsMediaPlayerExtensions[] = { | |
| 25 ".asx" | |
| 26 }; | |
| 27 | |
| 28 const char* kRealPlayerExtensions[] = { | |
| 29 ".ra", | |
| 30 ".ram", | |
| 31 ".rm", | |
| 32 ".rmm", | |
| 33 ".rmp", | |
| 34 ".rpm" | |
| 35 }; | |
| 36 | |
| 37 const char* kQuickTimeExtensions[] = { | |
| 38 ".moov", | |
| 39 ".mov", | |
| 40 ".qif", | |
| 41 ".qt", | |
| 42 ".qti", | |
| 43 ".qtif" | |
| 44 }; | |
| 45 | |
| 46 } // namespace. | |
| 47 | |
| 48 class UMASenderImpl : public MissingPluginReporter::UMASender { | |
| 49 virtual void SendPluginUMA(MissingPluginReporter::PluginType plugin_type) | |
| 50 OVERRIDE; | |
| 51 }; | |
| 52 | |
| 53 void UMASenderImpl::SendPluginUMA( | |
| 54 MissingPluginReporter::PluginType plugin_type) { | |
| 55 UMA_HISTOGRAM_ENUMERATION("Plugin.MissingPlugins", | |
| 56 plugin_type, | |
| 57 MissingPluginReporter::OTHER); | |
| 58 } | |
| 59 | |
| 60 // static. | |
| 61 MissingPluginReporter* MissingPluginReporter::GetInstance() { | |
| 62 return Singleton<MissingPluginReporter>::get(); | |
| 63 } | |
| 64 | |
| 65 void MissingPluginReporter::ReportPluginMissing( | |
| 66 std::string plugin_mime_type, const GURL& plugin_src) { | |
| 67 PluginType plugin_type; | |
| 68 // If we know plugin's mime type, we use it to determine plugin's type. Else, | |
| 69 // we try to determine plugin type using plugin source's extension. | |
| 70 if (!plugin_mime_type.empty()) { | |
| 71 StringToLowerASCII(&plugin_mime_type); | |
| 72 plugin_type = MimeTypeToPluginType(plugin_mime_type); | |
| 73 } else { | |
| 74 plugin_type = SrcToPluginType(plugin_src); | |
| 75 } | |
| 76 report_sender_->SendPluginUMA(plugin_type); | |
| 77 } | |
| 78 | |
| 79 void MissingPluginReporter::SetUMASender(UMASender* sender) { | |
| 80 report_sender_.reset(sender); | |
| 81 } | |
| 82 | |
| 83 MissingPluginReporter::MissingPluginReporter() | |
| 84 : report_sender_(new UMASenderImpl()) { | |
| 85 } | |
| 86 | |
| 87 MissingPluginReporter::~MissingPluginReporter() { | |
| 88 } | |
| 89 | |
| 90 // static. | |
| 91 bool MissingPluginReporter::CompareCStrings(const char* first, | |
| 92 const char* second) { | |
| 93 return strcmp(first, second) < 0; | |
| 94 } | |
| 95 | |
| 96 bool MissingPluginReporter::CStringArrayContainsCString(const char** array, | |
| 97 size_t array_size, | |
| 98 const char* str) { | |
| 99 return std::binary_search(array, array + array_size, str, CompareCStrings); | |
| 100 } | |
| 101 | |
| 102 void MissingPluginReporter::ExtractFileExtension(const GURL& src, | |
| 103 std::string* extension) { | |
| 104 std::string extension_file_path(src.ExtractFileName()); | |
| 105 if (extension_file_path.empty()) | |
| 106 extension_file_path = src.host(); | |
| 107 | |
| 108 size_t last_dot = extension_file_path.find_last_of('.'); | |
| 109 if (last_dot != std::string::npos) { | |
| 110 *extension = extension_file_path.substr(last_dot); | |
| 111 } else { | |
| 112 extension->clear(); | |
| 113 } | |
| 114 | |
| 115 StringToLowerASCII(extension); | |
| 116 } | |
| 117 | |
| 118 MissingPluginReporter::PluginType MissingPluginReporter::SrcToPluginType( | |
| 119 const GURL& src) { | |
| 120 std::string file_extension; | |
| 121 ExtractFileExtension(src, &file_extension); | |
| 122 if (CStringArrayContainsCString(kWindowsMediaPlayerExtensions, | |
| 123 arraysize(kWindowsMediaPlayerExtensions), | |
| 124 file_extension.c_str())) { | |
| 125 return WINDOWS_MEDIA_PLAYER; | |
| 126 } | |
| 127 | |
| 128 if (CStringArrayContainsCString(kQuickTimeExtensions, | |
| 129 arraysize(kQuickTimeExtensions), | |
| 130 file_extension.c_str())) { | |
| 131 return QUICKTIME; | |
| 132 } | |
| 133 | |
| 134 if (CStringArrayContainsCString(kRealPlayerExtensions, | |
| 135 arraysize(kRealPlayerExtensions), | |
| 136 file_extension.c_str())) { | |
| 137 return REALPLAYER; | |
| 138 } | |
| 139 | |
| 140 return OTHER; | |
| 141 } | |
| 142 | |
| 143 MissingPluginReporter::PluginType MissingPluginReporter::MimeTypeToPluginType( | |
| 144 const std::string& mime_type) { | |
| 145 if (strcmp(mime_type.c_str(), kWindowsMediaPlayerType) == 0) | |
| 146 return WINDOWS_MEDIA_PLAYER; | |
| 147 | |
| 148 size_t prefix_length = strlen(kSilverlightTypePrefix); | |
| 149 if (strncmp(mime_type.c_str(), kSilverlightTypePrefix, prefix_length) == 0) | |
| 150 return SILVERLIGHT; | |
| 151 | |
| 152 prefix_length = strlen(kRealPlayerTypePrefix); | |
| 153 if (strncmp(mime_type.c_str(), kRealPlayerTypePrefix, prefix_length) == 0) | |
| 154 return REALPLAYER; | |
| 155 | |
| 156 if (strstr(mime_type.c_str(), kJavaTypeSubstring)) | |
| 157 return JAVA; | |
| 158 | |
| 159 if (strcmp(mime_type.c_str(), kQuickTimeType) == 0) | |
| 160 return QUICKTIME; | |
| 161 | |
| 162 return OTHER; | |
| 163 } | |
| 164 | |
| OLD | NEW |