1 // 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.
5 #include "chrome/installer/test/resource_updater.h"
9 #include "base/files/file_path.h"
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
13 namespace upgrade_test
{
15 ResourceUpdater::ResourceUpdater() : handle_(NULL
) {
18 ResourceUpdater::~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";
26 bool ResourceUpdater::Initialize(const base::FilePath
& pe_image_path
) {
27 DCHECK(handle_
== NULL
);
28 handle_
= BeginUpdateResource(pe_image_path
.value().c_str(), FALSE
);
29 if (handle_
== NULL
) {
31 << "BeginUpdateResource failed on \"" << pe_image_path
.value() << "\"";
37 bool ResourceUpdater::Update(const std::wstring
& name
,
38 const std::wstring
& type
,
40 const base::FilePath
& input_file
) {
41 DCHECK(handle_
!= NULL
);
42 base::MemoryMappedFile input
;
44 if (input
.Initialize(input_file
)) {
45 if (UpdateResource(handle_
, type
.c_str(), name
.c_str(), language_id
,
46 const_cast<uint8
*>(input
.data()),
47 static_cast<DWORD
>(input
.length()))
51 PLOG(DFATAL
) << "UpdateResource failed for resource \"" << name
<< "\"";
53 PLOG(DFATAL
) << "Failed mapping \"" << input_file
.value() << "\"";
58 bool ResourceUpdater::Commit() {
59 DCHECK(handle_
!= NULL
);
61 if (EndUpdateResource(handle_
, FALSE
) == FALSE
) {
62 PLOG(DFATAL
) << "EndUpdateResource failed";
69 } // namespace upgrade_test