Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: extensions/common/file_util.cc

Issue 65123002: Move chrome/common/extensions/background_info.h to src/extensions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix ios, browser_tests Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 #include "extensions/common/file_util.h"
6
7 #include <string>
8
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "net/base/escape.h"
12 #include "url/gurl.h"
13
14 namespace extensions {
15 namespace file_util {
16
17 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) {
18 std::string url_path = url.path();
19 if (url_path.empty() || url_path[0] != '/')
20 return base::FilePath();
21
22 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8.
23 std::string file_path = net::UnescapeURLComponent(url_path,
24 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
25 size_t skip = file_path.find_first_not_of("/\\");
26 if (skip != file_path.npos)
27 file_path = file_path.substr(skip);
28
29 base::FilePath path = base::FilePath::FromUTF8Unsafe(file_path);
30
31 // It's still possible for someone to construct an annoying URL whose path
32 // would still wind up not being considered relative at this point.
33 // For example: chrome-extension://id/c:////foo.html
34 if (path.IsAbsolute())
35 return base::FilePath();
36
37 return path;
38 }
39
40 base::FilePath ExtensionResourceURLToFilePath(const GURL& url,
41 const base::FilePath& root) {
42 std::string host = net::UnescapeURLComponent(url.host(),
43 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
44 if (host.empty())
45 return base::FilePath();
46
47 base::FilePath relative_path = ExtensionURLToRelativeFilePath(url);
48 if (relative_path.empty())
49 return base::FilePath();
50
51 base::FilePath path = root.AppendASCII(host).Append(relative_path);
52 if (!base::PathExists(path))
53 return base::FilePath();
54 path = base::MakeAbsoluteFilePath(path);
55 if (path.empty() || !root.IsParent(path))
56 return base::FilePath();
57 return path;
58 }
59
60 } // namespace file_util
61 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698