Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / resource / data_pack.h
blobaed737b2d0c7ddfc0199ac347367c01d801dcd1a
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_
12 #include <map>
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"
23 namespace base {
24 class FilePath;
25 class RefCountedStaticMemory;
28 namespace ui {
29 enum ScaleFactor : int;
31 class UI_BASE_EXPORT DataPack : public ResourceHandle {
32 public:
33 explicit DataPack(ui::ScaleFactor scale_factor);
34 ~DataPack() override;
36 void set_has_only_material_design_assets(bool has_only_material_assets) {
37 has_only_material_design_assets_ = has_only_material_assets;
40 // Load a pack file from |path|, returning false on error.
41 bool LoadFromPath(const base::FilePath& path);
43 // Loads a pack file from |file|, returning false on error.
44 bool LoadFromFile(base::File file);
46 // Loads a pack file from |region| of |file|, returning false on error.
47 bool LoadFromFileRegion(base::File file,
48 const base::MemoryMappedFile::Region& region);
50 // Writes a pack file containing |resources| to |path|. If there are any
51 // text resources to be written, their encoding must already agree to the
52 // |textEncodingType| specified. If no text resources are present, please
53 // indicate BINARY.
54 static bool WritePack(const base::FilePath& path,
55 const std::map<uint16, base::StringPiece>& resources,
56 TextEncodingType textEncodingType);
58 // ResourceHandle implementation:
59 bool HasResource(uint16 resource_id) const override;
60 bool GetStringPiece(uint16 resource_id,
61 base::StringPiece* data) const override;
62 base::RefCountedStaticMemory* GetStaticMemory(
63 uint16 resource_id) const override;
64 TextEncodingType GetTextEncodingType() const override;
65 ui::ScaleFactor GetScaleFactor() const override;
66 bool HasOnlyMaterialDesignAssets() const override;
68 #if DCHECK_IS_ON()
69 // Checks to see if any resource in this DataPack already exists in the list
70 // of resources.
71 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs);
72 #endif
74 private:
75 // Does the actual loading of a pack file. Called by Load and LoadFromFile.
76 bool LoadImpl();
78 // The memory-mapped data.
79 scoped_ptr<base::MemoryMappedFile> mmap_;
81 // Number of resources in the data.
82 size_t resource_count_;
84 // Type of encoding for text resources.
85 TextEncodingType text_encoding_type_;
87 // The scale of the image in this resource pack relative to images in the 1x
88 // resource pak.
89 ui::ScaleFactor scale_factor_;
91 // Set to true if the only resources contained within this DataPack are
92 // material design image assets.
93 bool has_only_material_design_assets_;
95 DISALLOW_COPY_AND_ASSIGN(DataPack);
98 } // namespace ui
100 #endif // UI_BASE_RESOURCE_DATA_PACK_H_