OLD | NEW |
(Empty) | |
| 1 // Copyright 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. |
| 4 |
| 5 #include "cloud_print/service/win/installer.h" |
| 6 |
| 7 #include <winerror.h> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/win/scoped_com_initializer.h" |
| 13 #include "base/win/shortcut.h" |
| 14 #include "cloud_print/common/win/cloud_print_utils.h" |
| 15 #include "cloud_print/common/win/install_utils.h" |
| 16 #include "cloud_print/service/service_constants.h" |
| 17 #include "cloud_print/service/service_switches.h" |
| 18 #include "cloud_print/service/win/service_controller.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 base::FilePath GetShortcutPath(int dir_key, bool with_subdir) { |
| 23 base::FilePath path; |
| 24 if (!PathService::Get(dir_key, &path)) |
| 25 return base::FilePath(); |
| 26 path = path.Append(cloud_print::LoadLocalString(IDS_FULL_PRODUCT_NAME)); |
| 27 if (with_subdir) |
| 28 path = path.Append(cloud_print::LoadLocalString(IDS_FULL_PRODUCT_NAME)); |
| 29 return path.InsertBeforeExtension(L".lnk"); |
| 30 } |
| 31 |
| 32 void CreateShortcut(int dir_key, bool with_subdir, |
| 33 base::win::ShortcutOperation operation) { |
| 34 base::FilePath path = GetShortcutPath(dir_key, with_subdir); |
| 35 if (path.empty()) |
| 36 return; |
| 37 file_util::CreateDirectory(path.DirName()); |
| 38 base::win::ShortcutProperties properties; |
| 39 |
| 40 base::FilePath exe_path; |
| 41 if (!PathService::Get(base::FILE_EXE, &exe_path)) |
| 42 return; |
| 43 properties.set_target(exe_path); |
| 44 properties.set_working_dir(exe_path.DirName()); |
| 45 CreateOrUpdateShortcutLink(path, properties, operation); |
| 46 } |
| 47 |
| 48 void CreateShortcuts(bool create_always) { |
| 49 base::win::ScopedCOMInitializer co_init; |
| 50 base::win::ShortcutOperation operation = |
| 51 create_always ? base::win::SHORTCUT_CREATE_ALWAYS : |
| 52 base::win::SHORTCUT_REPLACE_EXISTING; |
| 53 CreateShortcut(base::DIR_COMMON_START_MENU, true, operation); |
| 54 CreateShortcut(base::DIR_COMMON_DESKTOP, false, operation); |
| 55 } |
| 56 |
| 57 void DeleteShortcut(int dir_key, bool with_subdir) { |
| 58 base::FilePath path = GetShortcutPath(dir_key, with_subdir); |
| 59 if (path.empty()) |
| 60 return; |
| 61 if (with_subdir) |
| 62 file_util::Delete(path.DirName(), true); |
| 63 else |
| 64 file_util::Delete(path, false); |
| 65 } |
| 66 |
| 67 void DeleteShortcuts() { |
| 68 DeleteShortcut(base::DIR_COMMON_START_MENU, true); |
| 69 DeleteShortcut(base::DIR_COMMON_DESKTOP, false); |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 HRESULT ProcessInstallerSwitches() { |
| 75 const CommandLine& command_line(*CommandLine::ForCurrentProcess()); |
| 76 |
| 77 if (command_line.HasSwitch(kInstallSwitch)) { |
| 78 base::FilePath old_location = |
| 79 cloud_print::GetInstallLocation(kGoogleUpdateId); |
| 80 |
| 81 cloud_print::CreateUninstallKey( |
| 82 kGoogleUpdateId, cloud_print::LoadLocalString(IDS_FULL_PRODUCT_NAME), |
| 83 kUninstallSwitch); |
| 84 |
| 85 ServiceController controller(cloud_print::LoadLocalString(IDS_SERVICENAME)); |
| 86 HRESULT hr = controller.UpdateBinaryPath(); |
| 87 if (FAILED(hr)) |
| 88 return hr; |
| 89 |
| 90 if (!old_location.empty() && |
| 91 cloud_print::IsProgramsFilesParent(old_location) && |
| 92 old_location != cloud_print::GetInstallLocation(kGoogleUpdateId)) { |
| 93 file_util::Delete(old_location, true); |
| 94 } |
| 95 |
| 96 cloud_print::SetGoogleUpdateKeys( |
| 97 kGoogleUpdateId, cloud_print::LoadLocalString(IDS_FULL_PRODUCT_NAME)); |
| 98 |
| 99 CreateShortcuts(old_location.empty()); |
| 100 |
| 101 return S_OK; |
| 102 } else if (command_line.HasSwitch(kUninstallSwitch)) { |
| 103 ServiceController controller(cloud_print::LoadLocalString(IDS_SERVICENAME)); |
| 104 HRESULT hr = controller.UninstallService(); |
| 105 if (FAILED(hr)) |
| 106 return hr; |
| 107 |
| 108 DeleteShortcuts(); |
| 109 |
| 110 cloud_print::DeleteGoogleUpdateKeys(kGoogleUpdateId); |
| 111 cloud_print::DeleteUninstallKey(kGoogleUpdateId); |
| 112 cloud_print::DeleteProgramDir(kDeleteSwitch); |
| 113 return S_OK; |
| 114 } else if (command_line.HasSwitch(kDeleteSwitch)) { |
| 115 base::FilePath delete_path = command_line.GetSwitchValuePath(kDeleteSwitch); |
| 116 if (!delete_path.empty() && |
| 117 cloud_print::IsProgramsFilesParent(delete_path)) { |
| 118 file_util::Delete(delete_path, true); |
| 119 } |
| 120 return S_OK; |
| 121 } |
| 122 |
| 123 return S_FALSE; |
| 124 } |
| 125 |
OLD | NEW |