| 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 28 matching lines...) Expand all Loading... |
| 39 // Load a pack file from |path|, returning false on error. | 39 // Load a pack file from |path|, returning false on error. |
| 40 bool LoadFromPath(const base::FilePath& path); | 40 bool LoadFromPath(const base::FilePath& path); |
| 41 | 41 |
| 42 // Loads a pack file from |file|, returning false on error. | 42 // Loads a pack file from |file|, returning false on error. |
| 43 bool LoadFromFile(base::File file); | 43 bool LoadFromFile(base::File file); |
| 44 | 44 |
| 45 // Loads a pack file from |region| of |file|, returning false on error. | 45 // Loads a pack file from |region| of |file|, returning false on error. |
| 46 bool LoadFromFileRegion(base::File file, | 46 bool LoadFromFileRegion(base::File file, |
| 47 const base::MemoryMappedFile::Region& region); | 47 const base::MemoryMappedFile::Region& region); |
| 48 | 48 |
| 49 // Loads a pack file from |buffer|, returning false on error. |
| 50 // Data is not copied, |buffer| should stay alive during |DataPack| lifetime. |
| 51 bool LoadFromBuffer(base::StringPiece buffer); |
| 52 |
| 49 // Writes a pack file containing |resources| to |path|. If there are any | 53 // 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 | 54 // text resources to be written, their encoding must already agree to the |
| 51 // |textEncodingType| specified. If no text resources are present, please | 55 // |textEncodingType| specified. If no text resources are present, please |
| 52 // indicate BINARY. | 56 // indicate BINARY. |
| 53 static bool WritePack(const base::FilePath& path, | 57 static bool WritePack(const base::FilePath& path, |
| 54 const std::map<uint16_t, base::StringPiece>& resources, | 58 const std::map<uint16_t, base::StringPiece>& resources, |
| 55 TextEncodingType textEncodingType); | 59 TextEncodingType textEncodingType); |
| 56 | 60 |
| 57 // ResourceHandle implementation: | 61 // ResourceHandle implementation: |
| 58 bool HasResource(uint16_t resource_id) const override; | 62 bool HasResource(uint16_t resource_id) const override; |
| 59 bool GetStringPiece(uint16_t resource_id, | 63 bool GetStringPiece(uint16_t resource_id, |
| 60 base::StringPiece* data) const override; | 64 base::StringPiece* data) const override; |
| 61 base::RefCountedStaticMemory* GetStaticMemory( | 65 base::RefCountedStaticMemory* GetStaticMemory( |
| 62 uint16_t resource_id) const override; | 66 uint16_t resource_id) const override; |
| 63 TextEncodingType GetTextEncodingType() const override; | 67 TextEncodingType GetTextEncodingType() const override; |
| 64 ui::ScaleFactor GetScaleFactor() const override; | 68 ui::ScaleFactor GetScaleFactor() const override; |
| 65 | 69 |
| 66 #if DCHECK_IS_ON() | 70 #if DCHECK_IS_ON() |
| 67 // Checks to see if any resource in this DataPack already exists in the list | 71 // Checks to see if any resource in this DataPack already exists in the list |
| 68 // of resources. | 72 // of resources. |
| 69 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs); | 73 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs); |
| 70 #endif | 74 #endif |
| 71 | 75 |
| 72 private: | 76 private: |
| 73 // Does the actual loading of a pack file. Called by Load and LoadFromFile. | 77 class DataSource; |
| 74 bool LoadImpl(); | 78 class BufferDataSource; |
| 79 class MemoryMappedDataSource; |
| 75 | 80 |
| 76 // The memory-mapped data. | 81 // Does the actual loading of a pack file. |
| 77 std::unique_ptr<base::MemoryMappedFile> mmap_; | 82 // Called by Load and LoadFromFile and LoadFromBuffer. |
| 83 bool LoadImpl(std::unique_ptr<DataSource> data_source); |
| 84 |
| 85 std::unique_ptr<DataSource> data_source_; |
| 78 | 86 |
| 79 // Number of resources in the data. | 87 // Number of resources in the data. |
| 80 size_t resource_count_; | 88 size_t resource_count_; |
| 81 | 89 |
| 82 // Type of encoding for text resources. | 90 // Type of encoding for text resources. |
| 83 TextEncodingType text_encoding_type_; | 91 TextEncodingType text_encoding_type_; |
| 84 | 92 |
| 85 // The scale of the image in this resource pack relative to images in the 1x | 93 // The scale of the image in this resource pack relative to images in the 1x |
| 86 // resource pak. | 94 // resource pak. |
| 87 ui::ScaleFactor scale_factor_; | 95 ui::ScaleFactor scale_factor_; |
| 88 | 96 |
| 89 DISALLOW_COPY_AND_ASSIGN(DataPack); | 97 DISALLOW_COPY_AND_ASSIGN(DataPack); |
| 90 }; | 98 }; |
| 91 | 99 |
| 92 } // namespace ui | 100 } // namespace ui |
| 93 | 101 |
| 94 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ | 102 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ |
| OLD | NEW |