OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // DataPack represents a read-only view onto an on-disk file that contains | 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 | 6 // (key, value) pairs of data. It's used to store static resources like |
7 // translation strings and images. | 7 // translation strings and images. |
8 | 8 |
9 #ifndef UI_BASE_RESOURCE_DATA_PACK_H_ | 9 #ifndef UI_BASE_RESOURCE_DATA_PACK_H_ |
10 #define UI_BASE_RESOURCE_DATA_PACK_H_ | 10 #define UI_BASE_RESOURCE_DATA_PACK_H_ |
(...skipping 15 matching lines...) Expand all Loading... | |
26 namespace base { | 26 namespace base { |
27 class FilePath; | 27 class FilePath; |
28 class RefCountedStaticMemory; | 28 class RefCountedStaticMemory; |
29 } | 29 } |
30 | 30 |
31 namespace ui { | 31 namespace ui { |
32 enum ScaleFactor : int; | 32 enum ScaleFactor : int; |
33 | 33 |
34 class UI_DATA_PACK_EXPORT DataPack : public ResourceHandle { | 34 class UI_DATA_PACK_EXPORT DataPack : public ResourceHandle { |
35 public: | 35 public: |
36 class DataSource; | |
sky
2017/02/08 22:22:58
Move to private section.
altimin
2017/02/09 00:13:52
Done.
| |
37 | |
36 explicit DataPack(ui::ScaleFactor scale_factor); | 38 explicit DataPack(ui::ScaleFactor scale_factor); |
37 ~DataPack() override; | 39 ~DataPack() override; |
38 | 40 |
39 // Load a pack file from |path|, returning false on error. | 41 // Load a pack file from |path|, returning false on error. |
40 bool LoadFromPath(const base::FilePath& path); | 42 bool LoadFromPath(const base::FilePath& path); |
41 | 43 |
42 // Loads a pack file from |file|, returning false on error. | 44 // Loads a pack file from |file|, returning false on error. |
43 bool LoadFromFile(base::File file); | 45 bool LoadFromFile(base::File file); |
44 | 46 |
45 // Loads a pack file from |region| of |file|, returning false on error. | 47 // Loads a pack file from |region| of |file|, returning false on error. |
46 bool LoadFromFileRegion(base::File file, | 48 bool LoadFromFileRegion(base::File file, |
47 const base::MemoryMappedFile::Region& region); | 49 const base::MemoryMappedFile::Region& region); |
48 | 50 |
51 // Loads a pack file from |buffer|, returning false on error. | |
52 // Data is not copied, |buffer| should stay alive during |DataPack| lifetime. | |
53 bool LoadFromBuffer(base::StringPiece buffer); | |
54 | |
49 // Writes a pack file containing |resources| to |path|. If there are any | 55 // Writes a pack file containing |resources| to |path|. If there are any |
50 // text resources to be written, their encoding must already agree to the | 56 // text resources to be written, their encoding must already agree to the |
51 // |textEncodingType| specified. If no text resources are present, please | 57 // |textEncodingType| specified. If no text resources are present, please |
52 // indicate BINARY. | 58 // indicate BINARY. |
53 static bool WritePack(const base::FilePath& path, | 59 static bool WritePack(const base::FilePath& path, |
54 const std::map<uint16_t, base::StringPiece>& resources, | 60 const std::map<uint16_t, base::StringPiece>& resources, |
55 TextEncodingType textEncodingType); | 61 TextEncodingType textEncodingType); |
56 | 62 |
57 // ResourceHandle implementation: | 63 // ResourceHandle implementation: |
58 bool HasResource(uint16_t resource_id) const override; | 64 bool HasResource(uint16_t resource_id) const override; |
59 bool GetStringPiece(uint16_t resource_id, | 65 bool GetStringPiece(uint16_t resource_id, |
60 base::StringPiece* data) const override; | 66 base::StringPiece* data) const override; |
61 base::RefCountedStaticMemory* GetStaticMemory( | 67 base::RefCountedStaticMemory* GetStaticMemory( |
62 uint16_t resource_id) const override; | 68 uint16_t resource_id) const override; |
63 TextEncodingType GetTextEncodingType() const override; | 69 TextEncodingType GetTextEncodingType() const override; |
64 ui::ScaleFactor GetScaleFactor() const override; | 70 ui::ScaleFactor GetScaleFactor() const override; |
65 | 71 |
66 #if DCHECK_IS_ON() | 72 #if DCHECK_IS_ON() |
67 // Checks to see if any resource in this DataPack already exists in the list | 73 // Checks to see if any resource in this DataPack already exists in the list |
68 // of resources. | 74 // of resources. |
69 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs); | 75 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs); |
70 #endif | 76 #endif |
71 | 77 |
72 private: | 78 private: |
73 // Does the actual loading of a pack file. Called by Load and LoadFromFile. | 79 // Does the actual loading of a pack file. |
80 // Called by Load and LoadFromFile and LoadFromBuffer. | |
74 bool LoadImpl(); | 81 bool LoadImpl(); |
75 | 82 |
76 // The memory-mapped data. | 83 std::unique_ptr<DataSource> data_source_; |
77 std::unique_ptr<base::MemoryMappedFile> mmap_; | |
78 | 84 |
79 // Number of resources in the data. | 85 // Number of resources in the data. |
80 size_t resource_count_; | 86 size_t resource_count_; |
81 | 87 |
82 // Type of encoding for text resources. | 88 // Type of encoding for text resources. |
83 TextEncodingType text_encoding_type_; | 89 TextEncodingType text_encoding_type_; |
84 | 90 |
85 // The scale of the image in this resource pack relative to images in the 1x | 91 // The scale of the image in this resource pack relative to images in the 1x |
86 // resource pak. | 92 // resource pak. |
87 ui::ScaleFactor scale_factor_; | 93 ui::ScaleFactor scale_factor_; |
88 | 94 |
89 DISALLOW_COPY_AND_ASSIGN(DataPack); | 95 DISALLOW_COPY_AND_ASSIGN(DataPack); |
90 }; | 96 }; |
91 | 97 |
92 } // namespace ui | 98 } // namespace ui |
93 | 99 |
94 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ | 100 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ |
OLD | NEW |