1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Use of this source code is governed by a BSD-style license that can be
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// found in the LICENSE file.
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "chrome/installer/util/app_commands.h"
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/logging.h"
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "base/win/registry.h"
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "chrome/installer/util/google_update_constants.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "chrome/installer/util/work_item_list.h"
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
12baa3858d3f5d128a5c8466b700098109edcad5f2repo syncusing base::win::RegKey;
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
14baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace installer {
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
16baa3858d3f5d128a5c8466b700098109edcad5f2repo syncAppCommands::AppCommands() {
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
19baa3858d3f5d128a5c8466b700098109edcad5f2repo syncAppCommands::~AppCommands() {
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
22baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool AppCommands::Initialize(const base::win::RegKey& key) {
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!key.Valid()) {
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    LOG(DFATAL) << "Cannot initialize AppCommands from an invalid key.";
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  using base::win::RegistryKeyIterator;
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  static const wchar_t kEmptyString[] = L"";
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  commands_.clear();
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RegKey cmd_key;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  LONG result;
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  AppCommand command;
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (RegistryKeyIterator key_iterator(key.Handle(), kEmptyString);
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync       key_iterator.Valid(); ++key_iterator) {
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const wchar_t* name = key_iterator.Name();
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    result = cmd_key.Open(key.Handle(), name, KEY_QUERY_VALUE);
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (result != ERROR_SUCCESS) {
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      LOG(ERROR) << "Failed to open key \"" << name
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                 << "\" with last-error code " << result;
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    } else if (command.Initialize(cmd_key)) {
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      commands_[name] = command;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    } else {
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      VLOG(1) << "Skipping over key \"" << name
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync              << "\" as it does not appear to hold a product command.";
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
54baa3858d3f5d128a5c8466b700098109edcad5f2repo syncAppCommands& AppCommands::CopyFrom(const AppCommands& other) {
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  commands_ = other.commands_;
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return *this;
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
60baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid AppCommands::Clear() {
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  commands_.clear();
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
64baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool AppCommands::Get(const std::wstring& command_id,
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                      AppCommand* command) const {
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  DCHECK(command);
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CommandMap::const_iterator it(commands_.find(command_id));
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (it == commands_.end())
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  *command = it->second;
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
74baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool AppCommands::Set(const std::wstring& command_id,
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync                      const AppCommand& command) {
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  std::pair<CommandMap::iterator, bool> result(
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      commands_.insert(std::make_pair(command_id, command)));
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!result.second)
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    result.first->second = command;
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return result.second;
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
83baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool AppCommands::Remove(const std::wstring& command_id) {
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return commands_.erase(command_id) != 0;
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
86
87AppCommands::CommandMapRange AppCommands::GetIterators() const {
88  return std::make_pair(commands_.begin(), commands_.end());
89}
90
91}  // namespace installer
92