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_
14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/platform_file.h"
17 #include "base/strings/string_piece.h"
18 #include "ui/base/layout.h"
19 #include "ui/base/resource/resource_handle.h"
20 #include "ui/base/ui_export.h"
24 class MemoryMappedFile;
25 class RefCountedStaticMemory;
30 class UI_EXPORT DataPack : public ResourceHandle {
32 DataPack(ui::ScaleFactor scale_factor);
35 // Load a pack file from |path|, returning false on error.
36 bool LoadFromPath(const base::FilePath& path);
38 // Loads a pack file from |file|, returning false on error.
39 bool LoadFromFile(base::PlatformFile file);
41 // Writes a pack file containing |resources| to |path|. If there are any
42 // text resources to be written, their encoding must already agree to the
43 // |textEncodingType| specified. If no text resources are present, please
45 static bool WritePack(const base::FilePath& path,
46 const std::map<uint16, base::StringPiece>& resources,
47 TextEncodingType textEncodingType);
49 // ResourceHandle implementation:
50 virtual bool HasResource(uint16 resource_id) const OVERRIDE;
51 virtual bool GetStringPiece(uint16 resource_id,
52 base::StringPiece* data) const OVERRIDE;
53 virtual base::RefCountedStaticMemory* GetStaticMemory(
54 uint16 resource_id) const OVERRIDE;
55 virtual TextEncodingType GetTextEncodingType() const OVERRIDE;
56 virtual ui::ScaleFactor GetScaleFactor() const OVERRIDE;
59 // Does the actual loading of a pack file. Called by Load and LoadFromFile.
62 // The memory-mapped data.
63 scoped_ptr<base::MemoryMappedFile> mmap_;
65 // Number of resources in the data.
66 size_t resource_count_;
68 // Type of encoding for text resources.
69 TextEncodingType text_encoding_type_;
71 // The scale of the image in this resource pack relative to images in the 1x
73 ui::ScaleFactor scale_factor_;
75 DISALLOW_COPY_AND_ASSIGN(DataPack);
80 #endif // UI_BASE_RESOURCE_DATA_PACK_H_