summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/extension_resource.cc
blob: be22ec9aff4335881b2640b974d165d033e45c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/extensions/extension_resource.h"

#include "base/file_util.h"
#include "base/logging.h"
#include "base/thread_restrictions.h"

PlatformThreadId ExtensionResource::file_thread_id_ = 0;

bool ExtensionResource::check_for_file_thread_ = false;

ExtensionResource::ExtensionResource() {
}

ExtensionResource::ExtensionResource(const std::string& extension_id,
                                     const FilePath& extension_root,
                                     const FilePath& relative_path)
    : extension_id_(extension_id),
      extension_root_(extension_root),
      relative_path_(relative_path) {
}

const FilePath& ExtensionResource::GetFilePathOnAnyThreadHack() const {
  if (extension_root_.empty() || relative_path_.empty()) {
    DCHECK(full_resource_path_.empty());
    return full_resource_path_;
  }

  // We've already checked, just return last value.
  if (!full_resource_path_.empty())
    return full_resource_path_;

  full_resource_path_ =
      GetFilePathOnAnyThreadHack(extension_root_, relative_path_);
  return full_resource_path_;
}

const FilePath& ExtensionResource::GetFilePath() const {
  ExtensionResource::CheckFileAccessFromFileThread();
  return GetFilePathOnAnyThreadHack();
}

// static
FilePath ExtensionResource::GetFilePathOnAnyThreadHack(
    const FilePath& extension_root, const FilePath& relative_path) {
  // This function is a hack, and causes us to block the IO thread.
  // Fixing
  //   http://code.google.com/p/chromium/issues/detail?id=59849
  // would also fix this.  Suppress the error for now.
  base::ThreadRestrictions::ScopedAllowIO allow_io;

  // We need to resolve the parent references in the extension_root
  // path on its own because IsParent doesn't like parent references.
  FilePath clean_extension_root(extension_root);
  if (!file_util::AbsolutePath(&clean_extension_root))
    return FilePath();

  FilePath full_path = clean_extension_root.Append(relative_path);

  // We must resolve the absolute path of the combined path when
  // the relative path contains references to a parent folder (i.e., '..').
  // We also check if the path exists because the posix version of AbsolutePath
  // will fail if the path doesn't exist, and we want the same behavior on
  // Windows... So until the posix and Windows version of AbsolutePath are
  // unified, we need an extra call to PathExists, unfortunately.
  // TODO(mad): Fix this once AbsolutePath is unified.
  if (file_util::AbsolutePath(&full_path) &&
      file_util::PathExists(full_path) &&
      clean_extension_root.IsParent(full_path)) {
    return full_path;
  }

  return FilePath();
}

// static
FilePath ExtensionResource::GetFilePath(
    const FilePath& extension_root, const FilePath& relative_path) {
  CheckFileAccessFromFileThread();
  return GetFilePathOnAnyThreadHack(extension_root, relative_path);
}

// static
void ExtensionResource::CheckFileAccessFromFileThread() {
  DCHECK(!check_for_file_thread_ ||
         file_thread_id_ == PlatformThread::CurrentId());
}

// Unit-testing helpers.
FilePath::StringType ExtensionResource::NormalizeSeperators(
    FilePath::StringType path) const {
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
  FilePath::StringType ret_val;
  for (size_t i = 0; i < path.length(); i++) {
    if (FilePath::IsSeparator(path[i]))
      path[i] = FilePath::kSeparators[0];
  }
#endif  // FILE_PATH_USES_WIN_SEPARATORS
  return path;
}

bool ExtensionResource::ComparePathWithDefault(const FilePath& path) const {
  // Make sure we have a cached value to test against...
  if (full_resource_path_.empty())
    GetFilePath();
  if (NormalizeSeperators(path.value()) ==
    NormalizeSeperators(full_resource_path_.value())) {
    return true;
  } else {
    return false;
  }
}