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