Make AppLifetimeHelper a member of ApplicationImpl.
[chromium-blink-merge.git] / ui / base / resource / data_pack.h
blob1ca3f101240786bec2d07e15bde0772af3430277
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 // Load a pack file from |path|, returning false on error.
37 bool LoadFromPath(const base::FilePath& path);
39 // Loads a pack file from |file|, returning false on error.
40 bool LoadFromFile(base::File file);
42 // Loads a pack file from |region| of |file|, returning false on error.
43 bool LoadFromFileRegion(base::File file,
44 const base::MemoryMappedFile::Region& region);
46 // Writes a pack file containing |resources| to |path|. If there are any
47 // text resources to be written, their encoding must already agree to the
48 // |textEncodingType| specified. If no text resources are present, please
49 // indicate BINARY.
50 static bool WritePack(const base::FilePath& path,
51 const std::map<uint16, base::StringPiece>& resources,
52 TextEncodingType textEncodingType);
54 // ResourceHandle implementation:
55 bool HasResource(uint16 resource_id) const override;
56 bool GetStringPiece(uint16 resource_id,
57 base::StringPiece* data) const override;
58 base::RefCountedStaticMemory* GetStaticMemory(
59 uint16 resource_id) const override;
60 TextEncodingType GetTextEncodingType() const override;
61 ui::ScaleFactor GetScaleFactor() const override;
63 #if DCHECK_IS_ON()
64 // Checks to see if any resource in this DataPack already exists in the list
65 // of resources.
66 void CheckForDuplicateResources(const ScopedVector<ResourceHandle>& packs);
67 #endif
69 private:
70 // Does the actual loading of a pack file. Called by Load and LoadFromFile.
71 bool LoadImpl();
73 // The memory-mapped data.
74 scoped_ptr<base::MemoryMappedFile> mmap_;
76 // Number of resources in the data.
77 size_t resource_count_;
79 // Type of encoding for text resources.
80 TextEncodingType text_encoding_type_;
82 // The scale of the image in this resource pack relative to images in the 1x
83 // resource pak.
84 ui::ScaleFactor scale_factor_;
86 DISALLOW_COPY_AND_ASSIGN(DataPack);
89 } // namespace ui
91 #endif // UI_BASE_RESOURCE_DATA_PACK_H_