Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / component_updater / pepper_flash_component_installer.cc
blob2ebbb31854080d51bc574bc9aebdb89ad23aa79e
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.
5 #include <stdint.h>
6 #include <string.h>
7 #include <vector>
9 #include "base/base_paths.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/version.h"
22 #include "build/build_config.h"
23 #include "chrome/browser/component_updater/flash_component_installer.h"
24 #include "chrome/browser/plugins/plugin_prefs.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pepper_flash.h"
29 #include "chrome/common/ppapi_utils.h"
30 #include "components/component_updater/component_updater_service.h"
31 #include "components/update_client/update_client.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/plugin_service.h"
34 #include "content/public/common/content_constants.h"
35 #include "content/public/common/pepper_plugin_info.h"
36 #include "flapper_version.h" // In SHARED_INTERMEDIATE_DIR. NOLINT
37 #include "ppapi/shared_impl/ppapi_permissions.h"
39 using content::BrowserThread;
40 using content::PluginService;
42 namespace component_updater {
44 namespace {
46 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
47 // CRX hash. The extension id is: mimojjlkmoijpicakmndhoigimigcmbb.
48 const uint8_t kSha2Hash[] = {0xc8, 0xce, 0x99, 0xba, 0xce, 0x89, 0xf8, 0x20,
49 0xac, 0xd3, 0x7e, 0x86, 0x8c, 0x86, 0x2c, 0x11,
50 0xb9, 0x40, 0xc5, 0x55, 0xaf, 0x08, 0x63, 0x70,
51 0x54, 0xf9, 0x56, 0xd3, 0xe7, 0x88, 0xba, 0x8c};
53 // If we don't have a Pepper Flash component, this is the version we claim.
54 const char kNullVersion[] = "0.0.0.0";
56 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
58 // The base directory on Windows looks like:
59 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\.
60 base::FilePath GetPepperFlashBaseDirectory() {
61 base::FilePath result;
62 PathService::Get(chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &result);
63 return result;
66 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
67 // Pepper Flash plugins have the version encoded in the path itself
68 // so we need to enumerate the directories to find the full path.
69 // On success, |latest_dir| returns something like:
70 // <profile>\AppData\Local\Google\Chrome\User Data\PepperFlash\10.3.44.555\.
71 // |latest_version| returns the corresponding version number. |older_dirs|
72 // returns directories of all older versions.
73 bool GetPepperFlashDirectory(base::FilePath* latest_dir,
74 Version* latest_version,
75 std::vector<base::FilePath>* older_dirs) {
76 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
77 base::FilePath base_dir = GetPepperFlashBaseDirectory();
78 bool found = false;
79 base::FileEnumerator file_enumerator(
80 base_dir, false, base::FileEnumerator::DIRECTORIES);
81 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
82 path = file_enumerator.Next()) {
83 Version version(path.BaseName().MaybeAsASCII());
84 if (!version.IsValid())
85 continue;
86 if (found) {
87 if (version.CompareTo(*latest_version) > 0) {
88 older_dirs->push_back(*latest_dir);
89 *latest_dir = path;
90 *latest_version = version;
91 } else {
92 older_dirs->push_back(path);
94 } else {
95 *latest_dir = path;
96 *latest_version = version;
97 found = true;
100 return found;
102 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
104 bool MakePepperFlashPluginInfo(const base::FilePath& flash_path,
105 const Version& flash_version,
106 bool out_of_process,
107 content::PepperPluginInfo* plugin_info) {
108 if (!flash_version.IsValid())
109 return false;
110 const std::vector<uint32_t> ver_nums = flash_version.components();
111 if (ver_nums.size() < 3)
112 return false;
114 plugin_info->is_internal = false;
115 plugin_info->is_out_of_process = out_of_process;
116 plugin_info->path = flash_path;
117 plugin_info->name = content::kFlashPluginName;
118 plugin_info->permissions = chrome::kPepperFlashPermissions;
120 // The description is like "Shockwave Flash 10.2 r154".
121 plugin_info->description = base::StringPrintf("%s %d.%d r%d",
122 content::kFlashPluginName,
123 ver_nums[0],
124 ver_nums[1],
125 ver_nums[2]);
127 plugin_info->version = flash_version.GetString();
129 content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
130 content::kFlashPluginSwfExtension,
131 content::kFlashPluginName);
132 plugin_info->mime_types.push_back(swf_mime_type);
133 content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
134 content::kFlashPluginSplExtension,
135 content::kFlashPluginName);
136 plugin_info->mime_types.push_back(spl_mime_type);
137 return true;
140 bool IsPepperFlash(const content::WebPluginInfo& plugin) {
141 // We try to recognize Pepper Flash by the following criteria:
142 // * It is a Pepper plugin.
143 // * It has the special Flash permissions.
144 return plugin.is_pepper_plugin() &&
145 (plugin.pepper_permissions & ppapi::PERMISSION_FLASH);
148 void RegisterPepperFlashWithChrome(const base::FilePath& path,
149 const Version& version) {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI);
151 content::PepperPluginInfo plugin_info;
152 if (!MakePepperFlashPluginInfo(path, version, true, &plugin_info))
153 return;
155 std::vector<content::WebPluginInfo> plugins;
156 PluginService::GetInstance()->GetInternalPlugins(&plugins);
157 for (std::vector<content::WebPluginInfo>::const_iterator it =
158 plugins.begin();
159 it != plugins.end();
160 ++it) {
161 if (!IsPepperFlash(*it))
162 continue;
164 // Do it only if the version we're trying to register is newer.
165 Version registered_version(base::UTF16ToUTF8(it->version));
166 if (registered_version.IsValid() &&
167 version.CompareTo(registered_version) <= 0) {
168 return;
171 // If the version is newer, remove the old one first.
172 PluginService::GetInstance()->UnregisterInternalPlugin(it->path);
173 break;
176 PluginService::GetInstance()->RegisterInternalPlugin(
177 plugin_info.ToWebPluginInfo(), true);
178 PluginService::GetInstance()->RefreshPlugins();
181 } // namespace
183 class PepperFlashComponentInstaller : public update_client::CrxInstaller {
184 public:
185 explicit PepperFlashComponentInstaller(const Version& version);
187 // ComponentInstaller implementation:
188 void OnUpdateError(int error) override;
190 bool Install(const base::DictionaryValue& manifest,
191 const base::FilePath& unpack_path) override;
193 bool GetInstalledFile(const std::string& file,
194 base::FilePath* installed_file) override;
196 bool Uninstall() override;
198 private:
199 ~PepperFlashComponentInstaller() override {}
201 Version current_version_;
204 PepperFlashComponentInstaller::PepperFlashComponentInstaller(
205 const Version& version)
206 : current_version_(version) {
207 DCHECK(version.IsValid());
210 void PepperFlashComponentInstaller::OnUpdateError(int error) {
211 NOTREACHED() << "Pepper Flash update error: " << error;
214 bool PepperFlashComponentInstaller::Install(
215 const base::DictionaryValue& manifest,
216 const base::FilePath& unpack_path) {
217 Version version;
218 if (!chrome::CheckPepperFlashManifest(manifest, &version))
219 return false;
220 if (current_version_.CompareTo(version) > 0)
221 return false;
222 if (!base::PathExists(unpack_path.Append(chrome::kPepperFlashPluginFilename)))
223 return false;
224 // Passed the basic tests. Time to install it.
225 base::FilePath path =
226 GetPepperFlashBaseDirectory().AppendASCII(version.GetString());
227 if (base::PathExists(path))
228 return false;
229 if (!base::Move(unpack_path, path))
230 return false;
231 // Installation is done. Now tell the rest of chrome. Both the path service
232 // and to the plugin service.
233 current_version_ = version;
234 PathService::Override(chrome::DIR_PEPPER_FLASH_PLUGIN, path);
235 path = path.Append(chrome::kPepperFlashPluginFilename);
236 BrowserThread::PostTask(
237 BrowserThread::UI,
238 FROM_HERE,
239 base::Bind(&RegisterPepperFlashWithChrome, path, version));
240 return true;
243 bool PepperFlashComponentInstaller::GetInstalledFile(
244 const std::string& file,
245 base::FilePath* installed_file) {
246 return false;
249 bool PepperFlashComponentInstaller::Uninstall() {
250 return false;
255 namespace {
257 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
258 void FinishPepperFlashUpdateRegistration(ComponentUpdateService* cus,
259 const Version& version) {
260 DCHECK_CURRENTLY_ON(BrowserThread::UI);
261 update_client::CrxComponent pepflash;
262 pepflash.name = "pepper_flash";
263 pepflash.installer = new PepperFlashComponentInstaller(version);
264 pepflash.version = version;
265 pepflash.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
266 if (!cus->RegisterComponent(pepflash))
267 NOTREACHED() << "Pepper Flash component registration failed.";
270 void StartPepperFlashUpdateRegistration(ComponentUpdateService* cus) {
271 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
272 base::FilePath path = GetPepperFlashBaseDirectory();
273 if (!base::PathExists(path)) {
274 if (!base::CreateDirectory(path)) {
275 NOTREACHED() << "Could not create Pepper Flash directory.";
276 return;
280 Version version(kNullVersion);
281 std::vector<base::FilePath> older_dirs;
282 if (GetPepperFlashDirectory(&path, &version, &older_dirs)) {
283 path = path.Append(chrome::kPepperFlashPluginFilename);
284 if (base::PathExists(path)) {
285 BrowserThread::PostTask(
286 BrowserThread::UI,
287 FROM_HERE,
288 base::Bind(&RegisterPepperFlashWithChrome, path, version));
289 } else {
290 version = Version(kNullVersion);
294 #if defined(FLAPPER_AVAILABLE)
295 // If a version of Flash is bundled with Chrome, and it's a higher version
296 // than the version of the component, or the component has never been updated,
297 // then set the bundled version as the current version.
298 if (version.CompareTo(Version(FLAPPER_VERSION_STRING)) < 0)
299 version = Version(FLAPPER_VERSION_STRING);
300 #endif
302 BrowserThread::PostTask(
303 BrowserThread::UI,
304 FROM_HERE,
305 base::Bind(&FinishPepperFlashUpdateRegistration, cus, version));
307 // Remove older versions of Pepper Flash.
308 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
309 iter != older_dirs.end();
310 ++iter) {
311 base::DeleteFile(*iter, true);
314 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
316 } // namespace
318 void RegisterPepperFlashComponent(ComponentUpdateService* cus) {
319 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_LINUX)
320 // Component updated flash supersedes bundled flash therefore if that one
321 // is disabled then this one should never install.
322 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
323 if (cmd_line->HasSwitch(switches::kDisableBundledPpapiFlash))
324 return;
325 BrowserThread::PostTask(BrowserThread::FILE,
326 FROM_HERE,
327 base::Bind(&StartPepperFlashUpdateRegistration, cus));
328 #endif
331 } // namespace component_updater