Home | History | Annotate | Download | only in util
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_INSTALLER_UTIL_INSTALLATION_STATE_H_
      6 #define CHROME_INSTALLER_UTIL_INSTALLATION_STATE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/command_line.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "chrome/installer/util/app_commands.h"
     15 #include "chrome/installer/util/browser_distribution.h"
     16 #include "chrome/installer/util/channel_info.h"
     17 
     18 namespace base {
     19 class Version;
     20 namespace win {
     21 class RegKey;
     22 }
     23 }
     24 
     25 namespace installer {
     26 
     27 class InstallationState;
     28 
     29 // A representation of a product's state on the machine based on the contents
     30 // of the Windows registry.
     31 // TODO(grt): Pull this out into its own file.
     32 class ProductState {
     33  public:
     34   ProductState();
     35 
     36   // Returns true if the product is installed (i.e., the product's Clients key
     37   // exists and has a "pv" value); false otherwise.
     38   bool Initialize(bool system_install,
     39                   BrowserDistribution::Type type);
     40   bool Initialize(bool system_install,
     41                   BrowserDistribution* distribution);
     42 
     43   // Returns the product's channel info (i.e., the Google Update "ap" value).
     44   const ChannelInfo& channel() const { return channel_; }
     45 
     46   // Returns the path to the product's "setup.exe"; may be empty.
     47   base::FilePath GetSetupPath() const;
     48 
     49   // Returns the product's version.  This method may only be called on an
     50   // instance that has been initialized for an installed product.
     51   const base::Version& version() const;
     52 
     53   // Returns the current version of the product if a new version is awaiting
     54   // update; may be NULL.  Ownership of a returned value is not passed to the
     55   // caller.
     56   const base::Version* old_version() const { return old_version_.get(); }
     57 
     58   // Returns the brand code the product is currently installed with.
     59   const std::wstring& brand() const { return brand_; }
     60 
     61   // Returns the command to be used to update to the new version that is
     62   // awaiting update; may be empty.
     63   const std::wstring& rename_cmd() const { return rename_cmd_; }
     64 
     65   // Returns true and populates |eula_accepted| if the product has such a value;
     66   // otherwise, returns false and does not modify |eula_accepted|.  Expected
     67   // values are 0 (false) and 1 (true), although |eula_accepted| is given
     68   // whatever is found.
     69   bool GetEulaAccepted(DWORD* eula_accepted) const;
     70 
     71   // Returns true and populates |oem_install| if the product has such a value;
     72   // otherwise, returns false and does not modify |oem_install|.  Expected
     73   // value is "1", although |oem_install| is given whatever is found.
     74   bool GetOemInstall(std::wstring* oem_install) const;
     75 
     76   // Returns true and populates |usagestats| if the product has such a value;
     77   // otherwise, returns false and does not modify |usagestats|.  Expected values
     78   // are 0 (false) and 1 (true), although |usagestats| is given whatever is
     79   // found.
     80   bool GetUsageStats(DWORD* usagestats) const;
     81 
     82   // True if the "msi" value in the ClientState key is present and non-zero.
     83   bool is_msi() const { return msi_; }
     84 
     85   // The command to uninstall the product; may be empty.
     86   const CommandLine& uninstall_command() const { return uninstall_command_; }
     87 
     88   // True if |uninstall_command| contains --multi-install.
     89   bool is_multi_install() const { return multi_install_; }
     90 
     91   // Returns the set of Google Update commands.
     92   const AppCommands& commands() const { return commands_; }
     93 
     94   // Returns this object a la operator=().
     95   ProductState& CopyFrom(const ProductState& other);
     96 
     97   // Clears the state of this object.
     98   void Clear();
     99 
    100  protected:
    101   static bool InitializeCommands(const base::win::RegKey& version_key,
    102                                  AppCommands* commands);
    103 
    104   ChannelInfo channel_;
    105   scoped_ptr<Version> version_;
    106   scoped_ptr<Version> old_version_;
    107   std::wstring brand_;
    108   std::wstring rename_cmd_;
    109   std::wstring oem_install_;
    110   CommandLine uninstall_command_;
    111   AppCommands commands_;
    112   DWORD eula_accepted_;
    113   DWORD usagestats_;
    114   bool msi_ : 1;
    115   bool multi_install_ : 1;
    116   bool has_eula_accepted_ : 1;
    117   bool has_oem_install_ : 1;
    118   bool has_usagestats_ : 1;
    119 
    120  private:
    121   friend class InstallationState;
    122 
    123   DISALLOW_COPY_AND_ASSIGN(ProductState);
    124 };  // class ProductState
    125 
    126 // Encapsulates the state of all products on the system.
    127 // TODO(grt): Rename this to MachineState and put it in its own file.
    128 class InstallationState {
    129  public:
    130   InstallationState();
    131 
    132   // Initializes this object with the machine's current state.
    133   void Initialize();
    134 
    135   // Returns the state of a product or NULL if not installed.
    136   // Caller does NOT assume ownership of returned pointer.
    137   const ProductState* GetProductState(bool system_install,
    138                                       BrowserDistribution::Type type) const;
    139 
    140   // Returns the state of a product, even one that has not yet been installed.
    141   // This is useful during first install, when some but not all ProductState
    142   // information has been written by Omaha. Notably absent from the
    143   // ProductState returned here are the version numbers. Do NOT try to access
    144   // the version numbers from a ProductState returned by this method.
    145   // Caller does NOT assume ownership of returned pointer. This method will
    146   // never return NULL.
    147   const ProductState* GetNonVersionedProductState(
    148       bool system_install, BrowserDistribution::Type type) const;
    149 
    150  protected:
    151   enum {
    152     CHROME_BROWSER_INDEX,
    153     CHROME_FRAME_INDEX,
    154     CHROME_BINARIES_INDEX,
    155     CHROME_APP_HOST_INDEX,
    156     NUM_PRODUCTS
    157   };
    158 
    159   static int IndexFromDistType(BrowserDistribution::Type type);
    160 
    161   ProductState user_products_[NUM_PRODUCTS];
    162   ProductState system_products_[NUM_PRODUCTS];
    163 
    164  private:
    165   DISALLOW_COPY_AND_ASSIGN(InstallationState);
    166 };  // class InstallationState
    167 
    168 }  // namespace installer
    169 
    170 #endif  // CHROME_INSTALLER_UTIL_INSTALLATION_STATE_H_
    171