OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #ifndef CHROME_INSTALLER_SETUP_SETUP_SINGLETON_H_ |
| 6 #define CHROME_INSTALLER_SETUP_SETUP_SINGLETON_H_ |
| 7 |
| 8 #include <windows.h> |
| 9 |
| 10 #include <memory> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/strings/string16.h" |
| 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/win/scoped_handle.h" |
| 16 |
| 17 namespace base { |
| 18 class CommandLine; |
| 19 class TimeDelta; |
| 20 } |
| 21 |
| 22 namespace installer { |
| 23 |
| 24 class InstallationState; |
| 25 class InstallerState; |
| 26 class MasterPreferences; |
| 27 |
| 28 // Any modification to a Chrome installation should be done within the scope of |
| 29 // a SetupSingleton. There can be only one active SetupSingleton per Chrome |
| 30 // installation at a time. |
| 31 class SetupSingleton { |
| 32 public: |
| 33 // Returns a SetupSingleton which, throughout its lifetime, gives the current |
| 34 // process the exclusive right to modify the Chrome installation described by |
| 35 // |installer_state| (installation directory and associated registry keys). |
| 36 // May block. |original_state| and |installer_state| are updated using |
| 37 // |command_line| and |master_preferences| to reflect the new state of the |
| 38 // installation after acquisition. Returns nullptr on failure. |
| 39 static std::unique_ptr<SetupSingleton> Acquire( |
| 40 const base::CommandLine& command_line, |
| 41 const MasterPreferences& master_preferences, |
| 42 InstallationState* original_state, |
| 43 InstallerState* installer_state); |
| 44 |
| 45 // Releases the exclusive right to modify the Chrome installation. |
| 46 ~SetupSingleton(); |
| 47 |
| 48 // Waits until |max_time| has passed or another process tries to acquire a |
| 49 // SetupSingleton for the same Chrome installation. In the latter case, the |
| 50 // method returns true and this SetupSingleton should be released as soon as |
| 51 // possible to unblock the other process. |
| 52 bool WaitForInterrupt(const base::TimeDelta& max_time); |
| 53 |
| 54 private: |
| 55 class ScopedHoldMutex { |
| 56 public: |
| 57 ScopedHoldMutex(); |
| 58 ~ScopedHoldMutex(); |
| 59 |
| 60 // Waits up to a certain amount of time to acquire |mutex|. Returns true on |
| 61 // success. |mutex| will be released in the destructor. |
| 62 bool Acquire(HANDLE mutex); |
| 63 |
| 64 private: |
| 65 HANDLE mutex_ = INVALID_HANDLE_VALUE; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(ScopedHoldMutex); |
| 68 }; |
| 69 |
| 70 // |sync_primitive_name_suffix| is a suffix for the name of |setup_mutex_| and |
| 71 // |exit_event_|. |
| 72 explicit SetupSingleton(const base::string16& sync_primitive_name_suffix); |
| 73 |
| 74 // A mutex that must be held to modify the Chrome installation directory. |
| 75 base::win::ScopedHandle setup_mutex_; |
| 76 |
| 77 // Holds |setup_mutex_| throughout the lifetime of this SetupSingleton. |
| 78 ScopedHoldMutex scoped_hold_setup_mutex_; |
| 79 |
| 80 // An event signaled to ask the owner of |setup_mutex_| to release it as soon |
| 81 // as possible. |
| 82 base::WaitableEvent exit_event_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(SetupSingleton); |
| 85 }; |
| 86 |
| 87 } // namespace installer |
| 88 |
| 89 #endif // CHROME_INSTALLER_SETUP_SETUP_SINGLETON_H_ |
OLD | NEW |