OLD | NEW |
(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/ui/pdf/adobe_reader_info_win.h" |
| 6 |
| 7 #include <shlwapi.h> |
| 8 |
| 9 #include <algorithm> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/callback.h" |
| 14 #include "base/file_version_info.h" |
| 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/win/registry.h" |
| 19 #include "base/win/windows_version.h" |
| 20 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/plugins/plugin_finder.h" |
| 22 #include "chrome/browser/plugins/plugin_metadata.h" |
| 23 #include "chrome/browser/plugins/plugin_prefs.h" |
| 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "content/public/browser/plugin_service.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Hardcoded value for the secure version of Acrobat Reader. |
| 31 const char kSecureVersion[] = "11.0.7.79"; |
| 32 |
| 33 const char kAdobeReaderIdentifier[] = "adobe-reader"; |
| 34 const char kPdfMimeType[] = "application/pdf"; |
| 35 const base::char16 kRegistryAcrobat[] = L"Acrobat.exe"; |
| 36 const base::char16 kRegistryAcrobatReader[] = L"AcroRd32.exe"; |
| 37 const base::char16 kRegistryApps[] = |
| 38 L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"; |
| 39 const base::char16 kRegistryPath[] = L"Path"; |
| 40 |
| 41 // Gets the installed path for a registered app. |
| 42 base::FilePath GetInstalledPath(const base::char16* app) { |
| 43 base::string16 reg_path(kRegistryApps); |
| 44 reg_path.append(L"\\"); |
| 45 reg_path.append(app); |
| 46 |
| 47 base::FilePath filepath; |
| 48 base::win::RegKey hkcu_key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ); |
| 49 base::string16 path; |
| 50 // As of Win7 AppPaths can also be registered in HKCU: http://goo.gl/UgFOf. |
| 51 if (base::win::GetVersion() >= base::win::VERSION_WIN7 && |
| 52 hkcu_key.ReadValue(kRegistryPath, &path) == ERROR_SUCCESS) { |
| 53 filepath = base::FilePath(path); |
| 54 } else { |
| 55 base::win::RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ); |
| 56 if (hklm_key.ReadValue(kRegistryPath, &path) == ERROR_SUCCESS) { |
| 57 filepath = base::FilePath(path); |
| 58 } |
| 59 } |
| 60 |
| 61 return filepath; |
| 62 } |
| 63 |
| 64 bool IsPdfMimeType(const content::WebPluginMimeType& plugin_mime_type) { |
| 65 return plugin_mime_type.mime_type == kPdfMimeType; |
| 66 } |
| 67 |
| 68 AdobeReaderPluginInfo GetReaderPlugin( |
| 69 Profile* profile, |
| 70 const std::vector<content::WebPluginInfo>& plugins) { |
| 71 AdobeReaderPluginInfo reader_info; |
| 72 reader_info.is_installed = false; |
| 73 reader_info.is_enabled = false; |
| 74 reader_info.is_secure = false; |
| 75 |
| 76 PluginFinder* plugin_finder = PluginFinder::GetInstance(); |
| 77 for (size_t i = 0; i < plugins.size(); ++i) { |
| 78 const content::WebPluginInfo& plugin = plugins[i]; |
| 79 if (plugin.is_pepper_plugin()) |
| 80 continue; |
| 81 if (std::find_if(plugin.mime_types.begin(), plugin.mime_types.end(), |
| 82 IsPdfMimeType) == plugin.mime_types.end()) |
| 83 continue; |
| 84 scoped_ptr<PluginMetadata> plugin_metadata( |
| 85 plugin_finder->GetPluginMetadata(plugins[i])); |
| 86 if (plugin_metadata->identifier() != kAdobeReaderIdentifier) |
| 87 continue; |
| 88 |
| 89 reader_info.is_installed = true; |
| 90 |
| 91 if (profile) { |
| 92 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile); |
| 93 PluginPrefs::PolicyStatus plugin_status = |
| 94 plugin_prefs->PolicyStatusForPlugin(plugin_metadata->name()); |
| 95 reader_info.is_enabled = plugin_status != PluginPrefs::POLICY_DISABLED; |
| 96 } |
| 97 |
| 98 PluginMetadata::SecurityStatus security_status = |
| 99 plugin_metadata->GetSecurityStatus(plugins[i]); |
| 100 reader_info.is_secure = |
| 101 security_status == PluginMetadata::SECURITY_STATUS_UP_TO_DATE; |
| 102 |
| 103 reader_info.plugin_info = plugins[i]; |
| 104 break; |
| 105 } |
| 106 return reader_info; |
| 107 } |
| 108 |
| 109 void OnGotPluginInfo(Profile* profile, |
| 110 const GetAdobeReaderPluginInfoCallback& callback, |
| 111 const std::vector<content::WebPluginInfo>& plugins) { |
| 112 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) |
| 113 profile = NULL; |
| 114 callback.Run(GetReaderPlugin(profile, plugins)); |
| 115 } |
| 116 |
| 117 bool IsAdobeReaderDefaultPDFViewerInternal(base::FilePath* path) { |
| 118 base::char16 app_cmd_buf[MAX_PATH]; |
| 119 DWORD app_cmd_buf_len = MAX_PATH; |
| 120 HRESULT hr = AssocQueryString(ASSOCF_NONE, ASSOCSTR_COMMAND, L".pdf", L"open", |
| 121 app_cmd_buf, &app_cmd_buf_len); |
| 122 if (FAILED(hr)) |
| 123 return false; |
| 124 |
| 125 // Looks for the install paths for Acrobat / Reader. |
| 126 base::FilePath install_path = GetInstalledPath(kRegistryAcrobatReader); |
| 127 if (install_path.empty()) |
| 128 install_path = GetInstalledPath(kRegistryAcrobat); |
| 129 if (install_path.empty()) |
| 130 return false; |
| 131 |
| 132 base::string16 app_cmd(app_cmd_buf); |
| 133 bool found = app_cmd.find(install_path.value()) != base::string16::npos; |
| 134 if (found && path) |
| 135 *path = install_path; |
| 136 return found; |
| 137 } |
| 138 |
| 139 } // namespace |
| 140 |
| 141 void GetAdobeReaderPluginInfoAsync( |
| 142 Profile* profile, |
| 143 const GetAdobeReaderPluginInfoCallback& callback) { |
| 144 DCHECK(!callback.is_null()); |
| 145 content::PluginService::GetInstance()->GetPlugins( |
| 146 base::Bind(&OnGotPluginInfo, profile, callback)); |
| 147 } |
| 148 |
| 149 bool GetAdobeReaderPluginInfo(Profile* profile, |
| 150 AdobeReaderPluginInfo* reader_info) { |
| 151 DCHECK(reader_info); |
| 152 std::vector<content::WebPluginInfo> plugins; |
| 153 bool up_to_date = content::PluginService::GetInstance()->GetPluginInfoArray( |
| 154 GURL(), kPdfMimeType, false, &plugins, NULL); |
| 155 *reader_info = GetReaderPlugin(profile, plugins); |
| 156 return up_to_date; |
| 157 } |
| 158 |
| 159 bool IsAdobeReaderDefaultPDFViewer() { |
| 160 return IsAdobeReaderDefaultPDFViewerInternal(NULL); |
| 161 } |
| 162 |
| 163 bool IsAdobeReaderUpToDate() { |
| 164 base::FilePath install_path; |
| 165 bool is_default = IsAdobeReaderDefaultPDFViewerInternal(&install_path); |
| 166 if (!is_default) |
| 167 return false; |
| 168 |
| 169 scoped_ptr<FileVersionInfo> file_version_info( |
| 170 FileVersionInfo::CreateFileVersionInfo(install_path)); |
| 171 if (!file_version_info) |
| 172 return false; |
| 173 |
| 174 std::string reader_version = |
| 175 base::UTF16ToUTF8(file_version_info->product_version()); |
| 176 // Convert 1.2.03.45 to 1.2.3.45 so base::Version considers it as valid. |
| 177 for (int i = 1; i <= 9; ++i) { |
| 178 std::string from = base::StringPrintf(".0%d", i); |
| 179 std::string to = base::StringPrintf(".%d", i); |
| 180 ReplaceSubstringsAfterOffset(&reader_version, 0, from, to); |
| 181 } |
| 182 base::Version file_version(reader_version); |
| 183 return file_version.IsValid() && !file_version.IsOlderThan(kSecureVersion); |
| 184 } |
OLD | NEW |