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 XWALK_APPLICATION_COMMON_APPLICATION_RESOURCE_H_
6 #define XWALK_APPLICATION_COMMON_APPLICATION_RESOURCE_H_
10 #include "base/files/file_path.h"
13 namespace application {
15 // Represents a resource inside an application. For example, an image, or a
16 // JavaScript file. This is more complicated than just a simple FilePath
17 // because application resources can come from multiple physical file locations
18 // depending on locale.
19 class ApplicationResource {
21 // SymlinkPolicy decides whether we'll allow resources to be a symlink to
22 // anywhere, or whether they must end up within the application root.
24 SYMLINKS_MUST_RESOLVE_WITHIN_ROOT,
25 FOLLOW_SYMLINKS_ANYWHERE,
28 ApplicationResource();
30 ApplicationResource(const std::string& application_id,
31 const base::FilePath& application_root,
32 const base::FilePath& relative_path);
34 ~ApplicationResource();
36 // set_follow_symlinks_anywhere allows the resource to be a symlink to
37 // anywhere in the filesystem. By default, resources have to be within
38 // |application_root| after resolving symlinks.
39 void set_follow_symlinks_anywhere();
41 // Returns actual path to the resource (default or locale specific). In the
42 // browser process, this will DCHECK if not called on the file thread. To
43 // easily load application images on the UI thread, see ImageLoader.
44 const base::FilePath& GetFilePath() const;
46 // Gets the physical file path for the application resource, taking into
47 // account localization. In the browser process, this will DCHECK if not
48 // called on the file thread. To easily load application images on the UI
49 // thread, see ImageLoader.
51 // The relative path must not resolve to a location outside of
52 // |application_root|. Iff |file_can_symlink_outside_root| is true, then the
53 // file can be a symlink that links outside of |application_root|.
54 static base::FilePath GetFilePath(const base::FilePath& application_root,
55 const base::FilePath& relative_path,
56 SymlinkPolicy symlink_policy);
59 const std::string& application_id() const { return application_id_; }
60 const base::FilePath& application_root() const { return application_root_; }
61 const base::FilePath& relative_path() const { return relative_path_; }
63 bool empty() const { return application_root().empty(); }
66 base::FilePath::StringType NormalizeSeperators(
67 const base::FilePath::StringType& path) const;
68 bool ComparePathWithDefault(const base::FilePath& path) const;
71 // The id of the application that this resource is associated with.
72 std::string application_id_;
75 base::FilePath application_root_;
77 // Relative path to resource.
78 base::FilePath relative_path_;
80 // If |follow_symlinks_anywhere_| is true then the resource itself must be
81 // within |application_root|, but it can be a symlink to a file that is not.
82 bool follow_symlinks_anywhere_;
84 // Full path to application resource. Starts empty.
85 mutable base::FilePath full_resource_path_;
88 } // namespace application
91 #endif // XWALK_APPLICATION_COMMON_APPLICATION_RESOURCE_H_