Autofill: Use the title attribute as a last resort for finding labels.
[chromium-blink-merge.git] / ui / base / resource / data_pack.h
blob1bf87365d93dfa7970ba13796cf2595c7adb3349
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/strings/string_piece.h"
19 #include "ui/base/resource/resource_handle.h"
20 #include "ui/base/ui_base_export.h"
22 namespace base {
23 class FilePath;
24 class RefCountedStaticMemory;
27 namespace ui {
28 enum ScaleFactor : int;
30 class UI_BASE_EXPORT DataPack : public ResourceHandle {
31 public:
32 explicit DataPack(ui::ScaleFactor scale_factor);
33 ~DataPack() override;
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::File file);
41 // Loads a pack file from |region| of |file|, returning false on error.
42 bool LoadFromFileRegion(base::File file,
43 const base::MemoryMappedFile::Region& region);
45 // Writes a pack file containing |resources| to |path|. If there are any
46 // text resources to be written, their encoding must already agree to the
47 // |textEncodingType| specified. If no text resources are present, please
48 // indicate BINARY.
49 static bool WritePack(const base::FilePath& path,
50 const std::map<uint16, base::StringPiece>& resources,
51 TextEncodingType textEncodingType);
53 // ResourceHandle implementation:
54 bool HasResource(uint16 resource_id) const override;
55 bool GetStringPiece(uint16 resource_id,
56 base::StringPiece* data) const override;
57 base::RefCountedStaticMemory* GetStaticMemory(
58 uint16 resource_id) const override;
59 TextEncodingType GetTextEncodingType() const override;
60 ui::ScaleFactor GetScaleFactor() const override;
62 private:
63 // Does the actual loading of a pack file. Called by Load and LoadFromFile.
64 bool LoadImpl();
66 // The memory-mapped data.
67 scoped_ptr<base::MemoryMappedFile> mmap_;
69 // Number of resources in the data.
70 size_t resource_count_;
72 // Type of encoding for text resources.
73 TextEncodingType text_encoding_type_;
75 // The scale of the image in this resource pack relative to images in the 1x
76 // resource pak.
77 ui::ScaleFactor scale_factor_;
79 DISALLOW_COPY_AND_ASSIGN(DataPack);
82 } // namespace ui
84 #endif // UI_BASE_RESOURCE_DATA_PACK_H_