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 EXTENSIONS_COMMON_EXTENSION_RESOURCE_H_
6#define EXTENSIONS_COMMON_EXTENSION_RESOURCE_H_
7
8#include <string>
9
10#include "base/files/file_path.h"
11
12namespace extensions {
13
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.
18class ExtensionResource {
19 public:
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.
22  enum SymlinkPolicy {
23    SYMLINKS_MUST_RESOLVE_WITHIN_ROOT,
24    FOLLOW_SYMLINKS_ANYWHERE,
25  };
26
27  ExtensionResource();
28
29  ExtensionResource(const std::string& extension_id,
30                    const base::FilePath& extension_root,
31                    const base::FilePath& relative_path);
32
33  ~ExtensionResource();
34
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();
39
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;
44
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
48  // ImageLoader.
49  //
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);
56
57  // Getters
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_; }
61
62  bool empty() const { return extension_root().empty(); }
63
64  // Unit test helpers.
65  base::FilePath::StringType NormalizeSeperators(
66      const base::FilePath::StringType& path) const;
67  bool ComparePathWithDefault(const base::FilePath& path) const;
68
69 private:
70  // The id of the extension that this resource is associated with.
71  std::string extension_id_;
72
73  // Extension root.
74  base::FilePath extension_root_;
75
76  // Relative path to resource.
77  base::FilePath relative_path_;
78
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_;
82
83  // Full path to extension resource. Starts empty.
84  mutable base::FilePath full_resource_path_;
85};
86
87}  // namespace extensions
88
89#endif  // EXTENSIONS_COMMON_EXTENSION_RESOURCE_H_
90