blob: c308add4356de660a549b9f4f2edf7bc96c4a323 [file] [log] [blame]
[email protected]c744d292010-11-23 15:01:221// Copyright (c) 2010 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#include "chrome/installer/test/resource_updater.h"
6
7#include <windows.h>
8
[email protected]c744d292010-11-23 15:01:229#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]c744d292010-11-23 15:01:2211#include "base/logging.h"
12
13namespace upgrade_test {
14
15ResourceUpdater::ResourceUpdater() : handle_(NULL) {
16}
17
18ResourceUpdater::~ResourceUpdater() {
19 if (handle_ != NULL) {
20 // An update wasn't committed, so discard it.
21 BOOL result = EndUpdateResource(handle_, TRUE);
22 DPCHECK(result != FALSE) << "EndUpdateResource failed";
23 }
24}
25
[email protected]152ea302013-02-11 04:08:4026bool ResourceUpdater::Initialize(const base::FilePath& pe_image_path) {
[email protected]c744d292010-11-23 15:01:2227 DCHECK(handle_ == NULL);
28 handle_ = BeginUpdateResource(pe_image_path.value().c_str(), FALSE);
29 if (handle_ == NULL) {
30 PLOG(DFATAL)
31 << "BeginUpdateResource failed on \"" << pe_image_path.value() << "\"";
32 return false;
33 }
34 return true;
35}
36
37bool ResourceUpdater::Update(const std::wstring& name,
38 const std::wstring& type,
[email protected]152ea302013-02-11 04:08:4039 WORD language_id,
40 const base::FilePath& input_file) {
[email protected]c744d292010-11-23 15:01:2241 DCHECK(handle_ != NULL);
42 file_util::MemoryMappedFile input;
43
44 if (input.Initialize(input_file)) {
45 if (UpdateResource(handle_, type.c_str(), name.c_str(), language_id,
46 const_cast<uint8*>(input.data()), input.length())
47 != FALSE) {
48 return true;
49 }
50 PLOG(DFATAL) << "UpdateResource failed for resource \"" << name << "\"";
51 } else {
52 PLOG(DFATAL) << "Failed mapping \"" << input_file.value() << "\"";
53 }
54 return false;
55}
56
57bool ResourceUpdater::Commit() {
58 DCHECK(handle_ != NULL);
59 bool result = true;
60 if (EndUpdateResource(handle_, FALSE) == FALSE) {
61 PLOG(DFATAL) << "EndUpdateResource failed";
62 result = false;
63 }
64 handle_ = NULL;
65 return result;
66}
67
68} // namespace upgrade_test