Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / installer / util / app_commands.cc
blobcb6f617dfde580c3ad78924226fd0b2343479f1c
1 // Copyright (c) 2011 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.
5 #include "chrome/installer/util/app_commands.h"
7 #include "base/logging.h"
8 #include "base/win/registry.h"
9 #include "chrome/installer/util/google_update_constants.h"
10 #include "chrome/installer/util/work_item_list.h"
12 using base::win::RegKey;
14 namespace installer {
16 AppCommands::AppCommands() {
19 AppCommands::~AppCommands() {
22 bool AppCommands::Initialize(const base::win::RegKey& key, REGSAM wow64access) {
23 if (!key.Valid()) {
24 LOG(DFATAL) << "Cannot initialize AppCommands from an invalid key.";
25 return false;
28 if (wow64access != 0 && wow64access != KEY_WOW64_32KEY &&
29 wow64access != KEY_WOW64_64KEY) {
30 LOG(DFATAL) << "Invalid wow64access supplied to AppCommands.";
31 return false;
34 using base::win::RegistryKeyIterator;
35 static const wchar_t kEmptyString[] = L"";
37 commands_.clear();
39 RegKey cmd_key;
40 LONG result;
41 AppCommand command;
42 for (RegistryKeyIterator key_iterator(
43 key.Handle(), kEmptyString, wow64access);
44 key_iterator.Valid();
45 ++key_iterator) {
46 const wchar_t* name = key_iterator.Name();
47 result = cmd_key.Open(key.Handle(), name, KEY_QUERY_VALUE);
48 if (result != ERROR_SUCCESS) {
49 LOG(ERROR) << "Failed to open key \"" << name
50 << "\" with last-error code " << result;
51 } else if (command.Initialize(cmd_key)) {
52 commands_[name] = command;
53 } else {
54 VLOG(1) << "Skipping over key \"" << name
55 << "\" as it does not appear to hold a product command.";
59 return true;
62 AppCommands& AppCommands::CopyFrom(const AppCommands& other) {
63 commands_ = other.commands_;
65 return *this;
68 void AppCommands::Clear() {
69 commands_.clear();
72 bool AppCommands::Get(const std::wstring& command_id,
73 AppCommand* command) const {
74 DCHECK(command);
75 CommandMap::const_iterator it(commands_.find(command_id));
76 if (it == commands_.end())
77 return false;
78 *command = it->second;
79 return true;
82 bool AppCommands::Set(const std::wstring& command_id,
83 const AppCommand& command) {
84 std::pair<CommandMap::iterator, bool> result(
85 commands_.insert(std::make_pair(command_id, command)));
86 if (!result.second)
87 result.first->second = command;
88 return result.second;
91 bool AppCommands::Remove(const std::wstring& command_id) {
92 return commands_.erase(command_id) != 0;
95 AppCommands::CommandMapRange AppCommands::GetIterators() const {
96 return std::make_pair(commands_.begin(), commands_.end());
99 } // namespace installer