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