Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 24 #include "ui/base/resource/resource_handle.h" | 24 #include "ui/base/resource/resource_handle.h" |
| 25 | 25 |
| 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 DataSource { | |
| 35 public: | |
| 36 virtual ~DataSource(){}; | |
| 37 | |
| 38 virtual size_t length() const = 0; | |
| 39 virtual const uint8_t* data() const = 0; | |
|
sadrul
2016/05/17 15:07:50
These are virtual methods, I believe the style-gui
altimin
2016/05/17 15:40:08
Done.
| |
| 40 }; | |
| 41 | |
| 34 class UI_DATA_PACK_EXPORT DataPack : public ResourceHandle { | 42 class UI_DATA_PACK_EXPORT DataPack : public ResourceHandle { |
| 35 public: | 43 public: |
| 36 explicit DataPack(ui::ScaleFactor scale_factor); | 44 explicit DataPack(ui::ScaleFactor scale_factor); |
| 37 ~DataPack() override; | 45 ~DataPack() override; |
| 38 | 46 |
| 39 void set_has_only_material_design_assets(bool has_only_material_assets) { | 47 void set_has_only_material_design_assets(bool has_only_material_assets) { |
| 40 has_only_material_design_assets_ = has_only_material_assets; | 48 has_only_material_design_assets_ = has_only_material_assets; |
| 41 } | 49 } |
| 42 | 50 |
| 43 // Load a pack file from |path|, returning false on error. | 51 // Load a pack file from |path|, returning false on error. |
| 44 bool LoadFromPath(const base::FilePath& path); | 52 bool LoadFromPath(const base::FilePath& path); |
| 45 | 53 |
| 46 // Loads a pack file from |file|, returning false on error. | 54 // Loads a pack file from |file|, returning false on error. |
| 47 bool LoadFromFile(base::File file); | 55 bool LoadFromFile(base::File file); |
| 48 | 56 |
| 49 // Loads a pack file from |region| of |file|, returning false on error. | 57 // Loads a pack file from |region| of |file|, returning false on error. |
| 50 bool LoadFromFileRegion(base::File file, | 58 bool LoadFromFileRegion(base::File file, |
| 51 const base::MemoryMappedFile::Region& region); | 59 const base::MemoryMappedFile::Region& region); |
| 52 | 60 |
| 61 // Loads a pack file from |buffer|, returning false on error. | |
| 62 bool LoadFromBuffer(base::StringPiece buffer); | |
|
sadrul
2016/05/17 15:07:50
const &?
altimin
2016/05/17 15:40:07
It's recommended to pass base::StringPiece by valu
| |
| 63 | |
| 53 // Writes a pack file containing |resources| to |path|. If there are any | 64 // Writes a pack file containing |resources| to |path|. If there are any |
| 54 // text resources to be written, their encoding must already agree to the | 65 // text resources to be written, their encoding must already agree to the |
| 55 // |textEncodingType| specified. If no text resources are present, please | 66 // |textEncodingType| specified. If no text resources are present, please |
| 56 // indicate BINARY. | 67 // indicate BINARY. |
| 57 static bool WritePack(const base::FilePath& path, | 68 static bool WritePack(const base::FilePath& path, |
| 58 const std::map<uint16_t, base::StringPiece>& resources, | 69 const std::map<uint16_t, base::StringPiece>& resources, |
| 59 TextEncodingType textEncodingType); | 70 TextEncodingType textEncodingType); |
| 60 | 71 |
| 61 // ResourceHandle implementation: | 72 // ResourceHandle implementation: |
| 62 bool HasResource(uint16_t resource_id) const override; | 73 bool HasResource(uint16_t resource_id) const override; |
| 63 bool GetStringPiece(uint16_t resource_id, | 74 bool GetStringPiece(uint16_t resource_id, |
| 64 base::StringPiece* data) const override; | 75 base::StringPiece* data) const override; |
| 65 base::RefCountedStaticMemory* GetStaticMemory( | 76 base::RefCountedStaticMemory* GetStaticMemory( |
| 66 uint16_t resource_id) const override; | 77 uint16_t resource_id) const override; |
| 67 TextEncodingType GetTextEncodingType() const override; | 78 TextEncodingType GetTextEncodingType() const override; |
| 68 ui::ScaleFactor GetScaleFactor() const override; | 79 ui::ScaleFactor GetScaleFactor() const override; |
| 69 bool HasOnlyMaterialDesignAssets() const override; | 80 bool HasOnlyMaterialDesignAssets() const override; |
| 70 | 81 |
| 71 #if DCHECK_IS_ON() | 82 #if DCHECK_IS_ON() |
| 72 // Checks to see if any resource in this DataPack already exists in the list | 83 // Checks to see if any resource in this DataPack already exists in the list |
| 73 // of resources. | 84 // of resources. |
| 74 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs); | 85 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs); |
| 75 #endif | 86 #endif |
| 76 | 87 |
| 77 private: | 88 private: |
| 78 // Does the actual loading of a pack file. Called by Load and LoadFromFile. | 89 // Does the actual loading of a pack file. |
| 90 // Called by Load and LoadFromFile and LoadFromBuffer. | |
| 79 bool LoadImpl(); | 91 bool LoadImpl(); |
| 80 | 92 |
| 81 // The memory-mapped data. | 93 std::unique_ptr<DataSource> data_source_; |
| 82 std::unique_ptr<base::MemoryMappedFile> mmap_; | |
| 83 | 94 |
| 84 // Number of resources in the data. | 95 // Number of resources in the data. |
| 85 size_t resource_count_; | 96 size_t resource_count_; |
| 86 | 97 |
| 87 // Type of encoding for text resources. | 98 // Type of encoding for text resources. |
| 88 TextEncodingType text_encoding_type_; | 99 TextEncodingType text_encoding_type_; |
| 89 | 100 |
| 90 // The scale of the image in this resource pack relative to images in the 1x | 101 // The scale of the image in this resource pack relative to images in the 1x |
| 91 // resource pak. | 102 // resource pak. |
| 92 ui::ScaleFactor scale_factor_; | 103 ui::ScaleFactor scale_factor_; |
| 93 | 104 |
| 94 // Set to true if the only resources contained within this DataPack are | 105 // Set to true if the only resources contained within this DataPack are |
| 95 // material design image assets. | 106 // material design image assets. |
| 96 bool has_only_material_design_assets_; | 107 bool has_only_material_design_assets_; |
| 97 | 108 |
| 98 DISALLOW_COPY_AND_ASSIGN(DataPack); | 109 DISALLOW_COPY_AND_ASSIGN(DataPack); |
| 99 }; | 110 }; |
| 100 | 111 |
| 101 } // namespace ui | 112 } // namespace ui |
| 102 | 113 |
| 103 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ | 114 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ |
| OLD | NEW |