15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_INSTALLER_UTIL_INSTALLATION_STATE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_INSTALLER_UTIL_INSTALLATION_STATE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/app_commands.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/browser_distribution.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/channel_info.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class Version;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace win {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RegKey;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace installer {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class InstallationState;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A representation of a product's state on the machine based on the contents
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the Windows registry.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(grt): Pull this out into its own file.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProductState {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProductState();
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the product is installed (i.e., the product's Clients key
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exists and has a "pv" value); false otherwise.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Initialize(bool system_install,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  BrowserDistribution::Type type);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Initialize(bool system_install,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  BrowserDistribution* distribution);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the product's channel info (i.e., the Google Update "ap" value).
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ChannelInfo& channel() const { return channel_; }
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the path to the product's "setup.exe"; may be empty.
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath GetSetupPath() const;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the product's version.  This method may only be called on an
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instance that has been initialized for an installed product.
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::Version& version() const;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current version of the product if a new version is awaiting
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // update; may be NULL.  Ownership of a returned value is not passed to the
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // caller.
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::Version* old_version() const { return old_version_.get(); }
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the brand code the product is currently installed with.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::wstring& brand() const { return brand_; }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the command to be used to update to the new version that is
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // awaiting update; may be empty.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::wstring& rename_cmd() const { return rename_cmd_; }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and populates |eula_accepted| if the product has such a value;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // otherwise, returns false and does not modify |eula_accepted|.  Expected
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // values are 0 (false) and 1 (true), although |eula_accepted| is given
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // whatever is found.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetEulaAccepted(DWORD* eula_accepted) const;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and populates |oem_install| if the product has such a value;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // otherwise, returns false and does not modify |oem_install|.  Expected
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // value is "1", although |oem_install| is given whatever is found.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetOemInstall(std::wstring* oem_install) const;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true and populates |usagestats| if the product has such a value;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // otherwise, returns false and does not modify |usagestats|.  Expected values
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are 0 (false) and 1 (true), although |usagestats| is given whatever is
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // found.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetUsageStats(DWORD* usagestats) const;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the "msi" value in the ClientState key is present and non-zero.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_msi() const { return msi_; }
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The command to uninstall the product; may be empty.
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const base::CommandLine& uninstall_command() const {
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return uninstall_command_;
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if |uninstall_command| contains --multi-install.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_multi_install() const { return multi_install_; }
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the set of Google Update commands.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const AppCommands& commands() const { return commands_; }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns this object a la operator=().
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProductState& CopyFrom(const ProductState& other);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clears the state of this object.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear();
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool InitializeCommands(const base::win::RegKey& version_key,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 AppCommands* commands);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChannelInfo channel_;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<Version> version_;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<Version> old_version_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::wstring brand_;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::wstring rename_cmd_;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::wstring oem_install_;
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::CommandLine uninstall_command_;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppCommands commands_;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DWORD eula_accepted_;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DWORD usagestats_;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool msi_ : 1;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool multi_install_ : 1;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_eula_accepted_ : 1;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_oem_install_ : 1;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_usagestats_ : 1;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class InstallationState;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ProductState);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};  // class ProductState
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encapsulates the state of all products on the system.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(grt): Rename this to MachineState and put it in its own file.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class InstallationState {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InstallationState();
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes this object with the machine's current state.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Initialize();
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the state of a product or NULL if not installed.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Caller does NOT assume ownership of returned pointer.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ProductState* GetProductState(bool system_install,
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      BrowserDistribution::Type type) const;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the state of a product, even one that has not yet been installed.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is useful during first install, when some but not all ProductState
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // information has been written by Omaha. Notably absent from the
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ProductState returned here are the version numbers. Do NOT try to access
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the version numbers from a ProductState returned by this method.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Caller does NOT assume ownership of returned pointer. This method will
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // never return NULL.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ProductState* GetNonVersionedProductState(
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool system_install, BrowserDistribution::Type type) const;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum {
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHROME_BROWSER_INDEX,
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHROME_FRAME_INDEX,
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHROME_BINARIES_INDEX,
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHROME_APP_HOST_INDEX,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NUM_PRODUCTS
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int IndexFromDistType(BrowserDistribution::Type type);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProductState user_products_[NUM_PRODUCTS];
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProductState system_products_[NUM_PRODUCTS];
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(InstallationState);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};  // class InstallationState
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace installer
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_INSTALLER_UTIL_INSTALLATION_STATE_H_
173