Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / pdf / adobe_reader_info_win.cc
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.8.4";
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   return filepath.Append(app);
61 }
62
63 bool IsPdfMimeType(const content::WebPluginMimeType& plugin_mime_type) {
64   return plugin_mime_type.mime_type == kPdfMimeType;
65 }
66
67 AdobeReaderPluginInfo GetReaderPlugin(
68     Profile* profile,
69     const std::vector<content::WebPluginInfo>& plugins) {
70   AdobeReaderPluginInfo reader_info;
71   reader_info.is_installed = false;
72   reader_info.is_enabled = false;
73   reader_info.is_secure = false;
74
75   PluginFinder* plugin_finder = PluginFinder::GetInstance();
76   for (size_t i = 0; i < plugins.size(); ++i) {
77     const content::WebPluginInfo& plugin = plugins[i];
78     if (plugin.is_pepper_plugin())
79       continue;
80     if (std::find_if(plugin.mime_types.begin(), plugin.mime_types.end(),
81                      IsPdfMimeType) == plugin.mime_types.end())
82       continue;
83     scoped_ptr<PluginMetadata> plugin_metadata(
84         plugin_finder->GetPluginMetadata(plugins[i]));
85     if (plugin_metadata->identifier() != kAdobeReaderIdentifier)
86       continue;
87
88     reader_info.is_installed = true;
89
90     if (profile) {
91       PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile);
92       PluginPrefs::PolicyStatus plugin_status =
93           plugin_prefs->PolicyStatusForPlugin(plugin_metadata->name());
94       reader_info.is_enabled = plugin_status != PluginPrefs::POLICY_DISABLED;
95     }
96
97     // Adobe Reader will likely always come up as "requires_authorization".
98     // See http://crbug.com/311655.
99     PluginMetadata::SecurityStatus security_stat =
100         plugin_metadata->GetSecurityStatus(plugins[i]);
101     reader_info.is_secure =
102         security_stat == PluginMetadata::SECURITY_STATUS_UP_TO_DATE ||
103         security_stat == PluginMetadata::SECURITY_STATUS_REQUIRES_AUTHORIZATION;
104
105     reader_info.plugin_info = plugins[i];
106     break;
107   }
108   return reader_info;
109 }
110
111 void OnGotPluginInfo(Profile* profile,
112                      const GetAdobeReaderPluginInfoCallback& callback,
113                      const std::vector<content::WebPluginInfo>& plugins) {
114   if (!g_browser_process->profile_manager()->IsValidProfile(profile))
115     profile = NULL;
116   callback.Run(GetReaderPlugin(profile, plugins));
117 }
118
119 bool IsAdobeReaderDefaultPDFViewerInternal(base::FilePath* path) {
120   base::char16 app_cmd_buf[MAX_PATH];
121   DWORD app_cmd_buf_len = MAX_PATH;
122   HRESULT hr = AssocQueryString(ASSOCF_NONE, ASSOCSTR_COMMAND, L".pdf", L"open",
123                                 app_cmd_buf, &app_cmd_buf_len);
124   if (FAILED(hr))
125     return false;
126
127   // Looks for the install paths for Acrobat / Reader.
128   base::FilePath install_path = GetInstalledPath(kRegistryAcrobatReader);
129   if (install_path.empty())
130     install_path = GetInstalledPath(kRegistryAcrobat);
131   if (install_path.empty())
132     return false;
133
134   base::string16 app_cmd(app_cmd_buf);
135   bool found = app_cmd.find(install_path.value()) != base::string16::npos;
136   if (found && path)
137     *path = install_path;
138   return found;
139 }
140
141 }  // namespace
142
143 void GetAdobeReaderPluginInfoAsync(
144     Profile* profile,
145     const GetAdobeReaderPluginInfoCallback& callback) {
146   DCHECK(!callback.is_null());
147   content::PluginService::GetInstance()->GetPlugins(
148       base::Bind(&OnGotPluginInfo, profile, callback));
149 }
150
151 bool GetAdobeReaderPluginInfo(Profile* profile,
152                               AdobeReaderPluginInfo* reader_info) {
153   DCHECK(reader_info);
154   std::vector<content::WebPluginInfo> plugins;
155   bool up_to_date = content::PluginService::GetInstance()->GetPluginInfoArray(
156       GURL(), kPdfMimeType, false, &plugins, NULL);
157   *reader_info = GetReaderPlugin(profile, plugins);
158   return up_to_date;
159 }
160
161 bool IsAdobeReaderDefaultPDFViewer() {
162   return IsAdobeReaderDefaultPDFViewerInternal(NULL);
163 }
164
165 bool IsAdobeReaderUpToDate() {
166   base::FilePath install_path;
167   bool is_default = IsAdobeReaderDefaultPDFViewerInternal(&install_path);
168   if (!is_default)
169     return false;
170
171   scoped_ptr<FileVersionInfo> file_version_info(
172       FileVersionInfo::CreateFileVersionInfo(install_path));
173   if (!file_version_info)
174     return false;
175
176   std::string reader_version =
177       base::UTF16ToUTF8(file_version_info->product_version());
178   // Convert 1.2.03.45 to 1.2.3.45 so base::Version considers it as valid.
179   for (int i = 1; i <= 9; ++i) {
180     std::string from = base::StringPrintf(".0%d", i);
181     std::string to = base::StringPrintf(".%d", i);
182     ReplaceSubstringsAfterOffset(&reader_version, 0, from, to);
183   }
184   base::Version file_version(reader_version);
185   return file_version.IsValid() && !file_version.IsOlderThan(kSecureVersion);
186 }