acf8531b967cf4366b04c42d2d48cf81abf24208
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / application_resource.h
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 XWALK_APPLICATION_COMMON_APPLICATION_RESOURCE_H_
6 #define XWALK_APPLICATION_COMMON_APPLICATION_RESOURCE_H_
7
8 #include <string>
9
10 #include "base/files/file_path.h"
11
12 namespace xwalk {
13 namespace application {
14
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 {
20  public:
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.
23   enum SymlinkPolicy {
24     SYMLINKS_MUST_RESOLVE_WITHIN_ROOT,
25     FOLLOW_SYMLINKS_ANYWHERE,
26   };
27
28   ApplicationResource();
29
30   ApplicationResource(const std::string& application_id,
31                     const base::FilePath& application_root,
32                     const base::FilePath& relative_path);
33
34   ~ApplicationResource();
35
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();
40
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;
45
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.
50   //
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);
57
58   // Getters
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_; }
62
63   bool empty() const { return application_root().empty(); }
64
65   // Unit test helpers.
66   base::FilePath::StringType NormalizeSeperators(
67       const base::FilePath::StringType& path) const;
68   bool ComparePathWithDefault(const base::FilePath& path) const;
69
70  private:
71   // The id of the application that this resource is associated with.
72   std::string application_id_;
73
74   // Application root.
75   base::FilePath application_root_;
76
77   // Relative path to resource.
78   base::FilePath relative_path_;
79
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_;
83
84   // Full path to application resource. Starts empty.
85   mutable base::FilePath full_resource_path_;
86 };
87
88 }  // namespace application
89 }  // namespace xwalk
90
91 #endif  // XWALK_APPLICATION_COMMON_APPLICATION_RESOURCE_H_