1 // Copyright (c) 2012 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 // DataPack represents a read-only view onto an on-disk file that contains
6 // (key, value) pairs of data. It's used to store static resources like
7 // translation strings and images.
9 #ifndef UI_BASE_RESOURCE_DATA_PACK_H_
10 #define UI_BASE_RESOURCE_DATA_PACK_H_
14 #include "base/basictypes.h"
15 #include "base/files/file.h"
16 #include "base/files/memory_mapped_file.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/strings/string_piece.h"
20 #include "ui/base/resource/resource_handle.h"
21 #include "ui/base/ui_base_export.h"
25 class RefCountedStaticMemory
;
29 enum ScaleFactor
: int;
31 class UI_BASE_EXPORT DataPack
: public ResourceHandle
{
33 explicit DataPack(ui::ScaleFactor scale_factor
);
36 // Load a pack file from |path|, returning false on error.
37 bool LoadFromPath(const base::FilePath
& path
);
39 // Loads a pack file from |file|, returning false on error.
40 bool LoadFromFile(base::File file
);
42 // Loads a pack file from |region| of |file|, returning false on error.
43 bool LoadFromFileRegion(base::File file
,
44 const base::MemoryMappedFile::Region
& region
);
46 // Writes a pack file containing |resources| to |path|. If there are any
47 // text resources to be written, their encoding must already agree to the
48 // |textEncodingType| specified. If no text resources are present, please
50 static bool WritePack(const base::FilePath
& path
,
51 const std::map
<uint16
, base::StringPiece
>& resources
,
52 TextEncodingType textEncodingType
);
54 // ResourceHandle implementation:
55 bool HasResource(uint16 resource_id
) const override
;
56 bool GetStringPiece(uint16 resource_id
,
57 base::StringPiece
* data
) const override
;
58 base::RefCountedStaticMemory
* GetStaticMemory(
59 uint16 resource_id
) const override
;
60 TextEncodingType
GetTextEncodingType() const override
;
61 ui::ScaleFactor
GetScaleFactor() const override
;
64 // Checks to see if any resource in this DataPack already exists in the list
66 void CheckForDuplicateResources(const ScopedVector
<ResourceHandle
>& packs
);
70 // Does the actual loading of a pack file. Called by Load and LoadFromFile.
73 // The memory-mapped data.
74 scoped_ptr
<base::MemoryMappedFile
> mmap_
;
76 // Number of resources in the data.
77 size_t resource_count_
;
79 // Type of encoding for text resources.
80 TextEncodingType text_encoding_type_
;
82 // The scale of the image in this resource pack relative to images in the 1x
84 ui::ScaleFactor scale_factor_
;
86 DISALLOW_COPY_AND_ASSIGN(DataPack
);
91 #endif // UI_BASE_RESOURCE_DATA_PACK_H_