OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_RESOURCE_H_ | |
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_RESOURCE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/files/file_path.h" | |
11 | |
12 // Represents a resource inside an extension. For example, an image, or a | |
13 // JavaScript file. This is more complicated than just a simple FilePath | |
14 // because extension resources can come from multiple physical file locations | |
15 // depending on locale. | |
16 class ExtensionResource { | |
17 public: | |
18 // SymlinkPolicy decides whether we'll allow resources to be a symlink to | |
19 // anywhere, or whether they must end up within the extension root. | |
20 enum SymlinkPolicy { | |
21 SYMLINKS_MUST_RESOLVE_WITHIN_ROOT, | |
22 FOLLOW_SYMLINKS_ANYWHERE, | |
23 }; | |
24 | |
25 ExtensionResource(); | |
26 | |
27 ExtensionResource(const std::string& extension_id, | |
28 const base::FilePath& extension_root, | |
29 const base::FilePath& relative_path); | |
30 | |
31 ~ExtensionResource(); | |
32 | |
33 // set_follow_symlinks_anywhere allows the resource to be a symlink to | |
34 // anywhere in the filesystem. By default, resources have to be within | |
35 // |extension_root| after resolving symlinks. | |
36 void set_follow_symlinks_anywhere(); | |
37 | |
38 // Returns actual path to the resource (default or locale specific). In the | |
39 // browser process, this will DCHECK if not called on the file thread. To | |
40 // easily load extension images on the UI thread, see ImageLoader. | |
41 const base::FilePath& GetFilePath() const; | |
42 | |
43 // Gets the physical file path for the extension resource, taking into account | |
44 // localization. In the browser process, this will DCHECK if not called on the | |
45 // file thread. To easily load extension images on the UI thread, see | |
46 // ImageLoader. | |
47 // | |
48 // The relative path must not resolve to a location outside of | |
49 // |extension_root|. Iff |file_can_symlink_outside_root| is true, then the | |
50 // file can be a symlink that links outside of |extension_root|. | |
51 static base::FilePath GetFilePath(const base::FilePath& extension_root, | |
52 const base::FilePath& relative_path, | |
53 SymlinkPolicy symlink_policy); | |
54 | |
55 // Getters | |
56 const std::string& extension_id() const { return extension_id_; } | |
57 const base::FilePath& extension_root() const { return extension_root_; } | |
58 const base::FilePath& relative_path() const { return relative_path_; } | |
59 | |
60 bool empty() const { return extension_root().empty(); } | |
61 | |
62 // Unit test helpers. | |
63 base::FilePath::StringType NormalizeSeperators( | |
64 const base::FilePath::StringType& path) const; | |
65 bool ComparePathWithDefault(const base::FilePath& path) const; | |
66 | |
67 private: | |
68 // The id of the extension that this resource is associated with. | |
69 std::string extension_id_; | |
70 | |
71 // Extension root. | |
72 base::FilePath extension_root_; | |
73 | |
74 // Relative path to resource. | |
75 base::FilePath relative_path_; | |
76 | |
77 // If |follow_symlinks_anywhere_| is true then the resource itself must be | |
78 // within |extension_root|, but it can be a symlink to a file that is not. | |
79 bool follow_symlinks_anywhere_; | |
80 | |
81 // Full path to extension resource. Starts empty. | |
82 mutable base::FilePath full_resource_path_; | |
83 }; | |
84 | |
85 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_RESOURCE_H_ | |
OLD | NEW |