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_ |
11 #pragma once | 11 #pragma once |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 | 14 |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/platform_file.h" |
17 #include "base/string_piece.h" | 18 #include "base/string_piece.h" |
18 #include "ui/base/layout.h" | 19 #include "ui/base/layout.h" |
19 #include "ui/base/resource/resource_handle.h" | 20 #include "ui/base/resource/resource_handle.h" |
20 #include "ui/base/ui_export.h" | 21 #include "ui/base/ui_export.h" |
21 | 22 |
22 class FilePath; | 23 class FilePath; |
23 | 24 |
24 namespace base { | 25 namespace base { |
25 class RefCountedStaticMemory; | 26 class RefCountedStaticMemory; |
26 } | 27 } |
27 | 28 |
28 namespace file_util { | 29 namespace file_util { |
29 class MemoryMappedFile; | 30 class MemoryMappedFile; |
30 } | 31 } |
31 | 32 |
32 namespace ui { | 33 namespace ui { |
33 | 34 |
34 class UI_EXPORT DataPack : public ResourceHandle { | 35 class UI_EXPORT DataPack : public ResourceHandle { |
35 public: | 36 public: |
36 DataPack(ui::ScaleFactor scale_factor); | 37 DataPack(ui::ScaleFactor scale_factor); |
37 virtual ~DataPack(); | 38 virtual ~DataPack(); |
38 | 39 |
39 // Load a pack file from |path|, returning false on error. | 40 // Load a pack file from |path|, returning false on error. |
40 bool Load(const FilePath& path); | 41 bool LoadFromPath(const FilePath& path); |
| 42 |
| 43 // Loads a pack file from |file|, returning false on error. |
| 44 bool LoadFromFile(base::PlatformFile file); |
41 | 45 |
42 // Writes a pack file containing |resources| to |path|. If there are any | 46 // Writes a pack file containing |resources| to |path|. If there are any |
43 // text resources to be written, their encoding must already agree to the | 47 // text resources to be written, their encoding must already agree to the |
44 // |textEncodingType| specified. If no text resources are present, please | 48 // |textEncodingType| specified. If no text resources are present, please |
45 // indicate BINARY. | 49 // indicate BINARY. |
46 static bool WritePack(const FilePath& path, | 50 static bool WritePack(const FilePath& path, |
47 const std::map<uint16, base::StringPiece>& resources, | 51 const std::map<uint16, base::StringPiece>& resources, |
48 TextEncodingType textEncodingType); | 52 TextEncodingType textEncodingType); |
49 | 53 |
50 // ResourceHandle implementation: | 54 // ResourceHandle implementation: |
51 virtual bool HasResource(uint16 resource_id) const OVERRIDE; | 55 virtual bool HasResource(uint16 resource_id) const OVERRIDE; |
52 virtual bool GetStringPiece(uint16 resource_id, | 56 virtual bool GetStringPiece(uint16 resource_id, |
53 base::StringPiece* data) const OVERRIDE; | 57 base::StringPiece* data) const OVERRIDE; |
54 virtual base::RefCountedStaticMemory* GetStaticMemory( | 58 virtual base::RefCountedStaticMemory* GetStaticMemory( |
55 uint16 resource_id) const OVERRIDE; | 59 uint16 resource_id) const OVERRIDE; |
56 virtual TextEncodingType GetTextEncodingType() const OVERRIDE; | 60 virtual TextEncodingType GetTextEncodingType() const OVERRIDE; |
57 virtual ui::ScaleFactor GetScaleFactor() const OVERRIDE; | 61 virtual ui::ScaleFactor GetScaleFactor() const OVERRIDE; |
58 | 62 |
59 private: | 63 private: |
| 64 // Does the actual loading of a pack file. Called by Load and LoadFromFile. |
| 65 bool LoadImpl(); |
| 66 |
60 // The memory-mapped data. | 67 // The memory-mapped data. |
61 scoped_ptr<file_util::MemoryMappedFile> mmap_; | 68 scoped_ptr<file_util::MemoryMappedFile> mmap_; |
62 | 69 |
63 // Number of resources in the data. | 70 // Number of resources in the data. |
64 size_t resource_count_; | 71 size_t resource_count_; |
65 | 72 |
66 // Type of encoding for text resources. | 73 // Type of encoding for text resources. |
67 TextEncodingType text_encoding_type_; | 74 TextEncodingType text_encoding_type_; |
68 | 75 |
69 // The scale of the image in this resource pack relative to images in the 1x | 76 // The scale of the image in this resource pack relative to images in the 1x |
70 // resource pak. | 77 // resource pak. |
71 ui::ScaleFactor scale_factor_; | 78 ui::ScaleFactor scale_factor_; |
72 | 79 |
73 DISALLOW_COPY_AND_ASSIGN(DataPack); | 80 DISALLOW_COPY_AND_ASSIGN(DataPack); |
74 }; | 81 }; |
75 | 82 |
76 } // namespace ui | 83 } // namespace ui |
77 | 84 |
78 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ | 85 #endif // UI_BASE_RESOURCE_DATA_PACK_H_ |
OLD | NEW |