OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/web_applications/web_app_win.h" | 5 #include "chrome/browser/web_applications/web_app_win.h" |
6 | 6 |
7 #include <shlobj.h> | 7 #include <shlobj.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/md5.h" | 13 #include "base/md5.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/strings/string16.h" | |
15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "base/win/shortcut.h" | 19 #include "base/win/shortcut.h" |
19 #include "base/win/windows_version.h" | 20 #include "base/win/windows_version.h" |
21 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/browser/web_applications/update_shortcut_worker_win.h" | 22 #include "chrome/browser/web_applications/update_shortcut_worker_win.h" |
21 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/installer/util/browser_distribution.h" | 24 #include "chrome/installer/util/browser_distribution.h" |
25 #include "chrome/installer/util/install_util.h" | |
23 #include "chrome/installer/util/shell_util.h" | 26 #include "chrome/installer/util/shell_util.h" |
24 #include "chrome/installer/util/util_constants.h" | 27 #include "chrome/installer/util/util_constants.h" |
25 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
29 #include "extensions/common/extension.h" | |
30 #include "net/base/mime_util.h" | |
26 #include "ui/base/win/shell.h" | 31 #include "ui/base/win/shell.h" |
27 #include "ui/gfx/icon_util.h" | 32 #include "ui/gfx/icon_util.h" |
28 #include "ui/gfx/image/image.h" | 33 #include "ui/gfx/image/image.h" |
29 #include "ui/gfx/image/image_family.h" | 34 #include "ui/gfx/image/image_family.h" |
30 | 35 |
31 namespace { | 36 namespace { |
32 | 37 |
33 const base::FilePath::CharType kIconChecksumFileExt[] = | 38 const base::FilePath::CharType kIconChecksumFileExt[] = |
34 FILE_PATH_LITERAL(".ico.md5"); | 39 FILE_PATH_LITERAL(".ico.md5"); |
35 | 40 |
41 const base::FilePath::CharType kAppShimExe[] = | |
42 FILE_PATH_LITERAL("app_shim.exe"); | |
43 | |
36 // Calculates checksum of an icon family using MD5. | 44 // Calculates checksum of an icon family using MD5. |
37 // The checksum is derived from all of the icons in the family. | 45 // The checksum is derived from all of the icons in the family. |
38 void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) { | 46 void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) { |
39 DCHECK(digest); | 47 DCHECK(digest); |
40 base::MD5Context md5_context; | 48 base::MD5Context md5_context; |
41 base::MD5Init(&md5_context); | 49 base::MD5Init(&md5_context); |
42 | 50 |
43 for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end(); | 51 for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end(); |
44 ++it) { | 52 ++it) { |
45 SkBitmap bitmap = it->AsBitmap(); | 53 SkBitmap bitmap = it->AsBitmap(); |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 const web_app::ShortcutInfo& shortcut_info) { | 364 const web_app::ShortcutInfo& shortcut_info) { |
357 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 365 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
358 | 366 |
359 // Set window's icon to the one we're about to create/update in the web app | 367 // Set window's icon to the one we're about to create/update in the web app |
360 // path. The icon cache will refresh on icon creation. | 368 // path. The icon cache will refresh on icon creation. |
361 base::FilePath web_app_path = | 369 base::FilePath web_app_path = |
362 web_app::GetWebAppDataDirectory(shortcut_info.profile_path, | 370 web_app::GetWebAppDataDirectory(shortcut_info.profile_path, |
363 shortcut_info.extension_id, | 371 shortcut_info.extension_id, |
364 shortcut_info.url); | 372 shortcut_info.url); |
365 base::FilePath icon_file = | 373 base::FilePath icon_file = |
366 web_app_path.Append(web_app::internals::GetSanitizedFileName( | 374 web_app::internals::GetIconFilePath(web_app_path, shortcut_info.title); |
367 shortcut_info.title)) | |
368 .ReplaceExtension(FILE_PATH_LITERAL(".ico")); | |
369 | |
370 content::BrowserThread::PostBlockingPoolTask( | 375 content::BrowserThread::PostBlockingPoolTask( |
371 FROM_HERE, | 376 FROM_HERE, |
372 base::Bind(&CreateIconAndSetRelaunchDetails, | 377 base::Bind(&CreateIconAndSetRelaunchDetails, |
373 web_app_path, | 378 web_app_path, |
374 icon_file, | 379 icon_file, |
375 shortcut_info, | 380 shortcut_info, |
376 hwnd)); | 381 hwnd)); |
377 } | 382 } |
378 | 383 |
384 // Creates an "app shim exe" by linking or copying the generic app shim exe. | |
385 // This is the binary that will be run when the user opens a file with this | |
386 // application. The name and icon of the binary will be used on the Open With | |
387 // menu. For this reason, we cannot simply launch chrome.exe. We give the app | |
388 // shim exe the same name as the application (with no ".exe" extension), so that | |
389 // the correct title will appear on the Open With menu. (Note: we also need a | |
390 // separate binary per app because Windows only allows a single association with | |
391 // each executable.) | |
392 // |path| is the full path of the shim binary to be created. | |
393 bool CreateAppShimBinary(const base::FilePath& path) { | |
394 // TODO(mgiuca): Hard-link instead of copying, if on the same file system. | |
395 base::FilePath chrome_binary_directory; | |
396 if (!PathService::Get(base::DIR_EXE, &chrome_binary_directory)) { | |
397 NOTREACHED(); | |
398 return false; | |
399 } | |
400 | |
401 base::FilePath generic_shim_path = | |
402 chrome_binary_directory.Append(kAppShimExe); | |
403 if (!base::CopyFile(generic_shim_path, path)) { | |
404 LOG(ERROR) << "Could not copy app shim exe to " << path.value(); | |
405 return false; | |
406 } | |
407 | |
408 return true; | |
409 } | |
410 | |
411 // Gets the full command line for calling the shim binary. This will include a | |
412 // placeholder "%1" argument, which Windows will substitute with the filename | |
413 // chosen by the user. | |
414 base::CommandLine GetAppShimCommandLine(const base::FilePath& app_shim_path, | |
415 const std::string& extension_id, | |
416 const base::FilePath& profile_path) { | |
417 // Get the command-line to pass to the shim (e.g., "chrome.exe --app-id=..."). | |
418 CommandLine chrome_cmd_line = ShellIntegration::CommandLineArgsForLauncher( | |
419 GURL(), extension_id, profile_path); | |
420 chrome_cmd_line.AppendArg("%1"); | |
421 | |
422 // Get the command-line for calling the shim (e.g., | |
423 // "app_shim [--chrome-sxs] -- --app-id=..."). | |
424 CommandLine shim_cmd_line(app_shim_path); | |
425 // If this is a canary build, launch the shim in canary mode. | |
426 if (InstallUtil::IsChromeSxSProcess()) | |
427 shim_cmd_line.AppendSwitch(installer::switches::kChromeSxS); | |
428 // Ensure all subsequent switches are treated as args to the shim. | |
429 shim_cmd_line.AppendArg("--"); | |
430 for (const auto& arg : chrome_cmd_line.GetArgs()) | |
431 shim_cmd_line.AppendArgNative(arg); | |
432 | |
433 return shim_cmd_line; | |
434 } | |
435 | |
436 // Gets the set of file extensions associated with a particular file handler. | |
437 // Uses both the MIME types and extensions. | |
438 void GetHandlerFileExtensions(const extensions::FileHandlerInfo& handler, | |
439 std::set<base::string16>* exts) { | |
440 for (const auto& mime : handler.types) { | |
441 std::vector<base::string16> mime_type_extensions; | |
442 net::GetExtensionsForMimeType(mime, &mime_type_extensions); | |
443 exts->insert(mime_type_extensions.begin(), mime_type_extensions.end()); | |
444 } | |
445 for (const auto& ext : handler.extensions) | |
446 exts->insert(base::UTF8ToUTF16(ext)); | |
447 } | |
448 | |
449 // Creates operating system file type associations for a given app. | |
450 // This is the platform specific implementation of the CreateFileAssociations | |
451 // function, and is executed on the FILE thread. | |
452 // Returns true on success, false on failure. | |
453 bool CreateFileAssociationsForApp( | |
454 const std::string& extension_id, | |
455 const base::string16& title, | |
456 const base::FilePath& profile_path, | |
457 const extensions::FileHandlersInfo& file_handlers_info) { | |
458 base::FilePath web_app_path = | |
459 web_app::GetWebAppDataDirectory(profile_path, extension_id, GURL()); | |
460 base::FilePath file_name = web_app::internals::GetSanitizedFileName(title); | |
461 | |
462 // The progid is "chrome-APPID-HANDLERID". This is the internal name Windows | |
463 // will use for file associations with this application. | |
464 base::string16 progid_base = L"chrome-"; | |
465 progid_base += base::UTF8ToUTF16(extension_id); | |
466 | |
467 // Create the app shim binary (see CreateAppShimBinary for rationale). Get the | |
468 // command line for the shim. | |
469 base::FilePath app_shim_path = web_app_path.Append(file_name); | |
470 if (!CreateAppShimBinary(app_shim_path)) | |
471 return false; | |
472 | |
473 CommandLine shim_cmd_line( | |
474 GetAppShimCommandLine(app_shim_path, extension_id, profile_path)); | |
475 | |
476 // TODO(mgiuca): Get the file type name from the manifest, or generate a | |
477 // default one. (If this is blank, Windows will generate one of the form | |
478 // '<EXT> file'.) | |
479 base::string16 file_type_name = L""; | |
480 | |
481 // TODO(mgiuca): Generate a new icon for this application's file associations | |
482 // that looks like a page with the application icon inside. | |
483 base::FilePath icon_file = | |
484 web_app::internals::GetIconFilePath(web_app_path, title); | |
485 | |
486 // Create a separate file association (ProgId) for each handler. This allows | |
487 // each handler to have its own filetype name and icon, and also a different | |
488 // command line (so the app can see which handler was invoked). | |
489 size_t num_successes = 0; | |
490 for (const auto& handler : file_handlers_info) { | |
491 base::string16 progid = progid_base + L"-" + base::UTF8ToUTF16(handler.id); | |
492 | |
493 std::set<base::string16> exts; | |
494 GetHandlerFileExtensions(handler, &exts); | |
495 | |
496 if (ShellUtil::AddFileAssociations(progid, shim_cmd_line, file_type_name, | |
497 icon_file, exts)) { | |
498 ++num_successes; | |
499 } | |
500 } | |
501 | |
502 if (num_successes == 0) { | |
503 // There were no successes; delete the shim. | |
504 base::DeleteFile(app_shim_path, false); | |
505 } else { | |
506 // There were some successes; tell Windows Explorer to update its cache. | |
507 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, | |
508 nullptr, nullptr); | |
509 } | |
510 | |
511 return num_successes == file_handlers_info.size(); | |
512 } | |
513 | |
379 } // namespace | 514 } // namespace |
380 | 515 |
381 namespace web_app { | 516 namespace web_app { |
382 | 517 |
383 base::FilePath CreateShortcutInWebAppDir(const base::FilePath& web_app_dir, | 518 base::FilePath CreateShortcutInWebAppDir(const base::FilePath& web_app_dir, |
384 const ShortcutInfo& shortcut_info) { | 519 const ShortcutInfo& shortcut_info) { |
385 std::vector<base::FilePath> paths; | 520 std::vector<base::FilePath> paths; |
386 paths.push_back(web_app_dir); | 521 paths.push_back(web_app_dir); |
387 std::vector<base::FilePath> out_filenames; | 522 std::vector<base::FilePath> out_filenames; |
388 base::FilePath web_app_dir_shortcut = | 523 base::FilePath web_app_dir_shortcut = |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
476 if (pin_to_taskbar) { | 611 if (pin_to_taskbar) { |
477 base::FilePath file_name = GetSanitizedFileName(shortcut_info.title); | 612 base::FilePath file_name = GetSanitizedFileName(shortcut_info.title); |
478 // Use the web app path shortcut for pinning to avoid having unique numbers | 613 // Use the web app path shortcut for pinning to avoid having unique numbers |
479 // in the application name. | 614 // in the application name. |
480 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). | 615 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). |
481 AddExtension(installer::kLnkExt); | 616 AddExtension(installer::kLnkExt); |
482 if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) | 617 if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) |
483 return false; | 618 return false; |
484 } | 619 } |
485 | 620 |
621 if (switches::kEnableAppsFileAssociations) { | |
Nico
2014/11/18 23:22:13
..\..\chrome\browser\web_applications\web_app_win.
| |
622 CreateFileAssociationsForApp( | |
623 shortcut_info.extension_id, shortcut_info.title, | |
624 shortcut_info.profile_path, file_handlers_info); | |
625 } | |
626 | |
486 return true; | 627 return true; |
487 } | 628 } |
488 | 629 |
489 void UpdatePlatformShortcuts( | 630 void UpdatePlatformShortcuts( |
490 const base::FilePath& web_app_path, | 631 const base::FilePath& web_app_path, |
491 const base::string16& old_app_title, | 632 const base::string16& old_app_title, |
492 const ShortcutInfo& shortcut_info, | 633 const ShortcutInfo& shortcut_info, |
493 const extensions::FileHandlersInfo& file_handlers_info) { | 634 const extensions::FileHandlersInfo& file_handlers_info) { |
494 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 635 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
495 | 636 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
619 | 760 |
620 } // namespace internals | 761 } // namespace internals |
621 | 762 |
622 void UpdateShortcutForTabContents(content::WebContents* web_contents) { | 763 void UpdateShortcutForTabContents(content::WebContents* web_contents) { |
623 // UpdateShortcutWorker will delete itself when it's done. | 764 // UpdateShortcutWorker will delete itself when it's done. |
624 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); | 765 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); |
625 worker->Run(); | 766 worker->Run(); |
626 } | 767 } |
627 | 768 |
628 } // namespace web_app | 769 } // namespace web_app |
OLD | NEW |