OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/extensions/file_system_natives.h" | 5 #include "chrome/renderer/extensions/file_system_natives.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
12 #include "chrome/renderer/extensions/user_script_slave.h" | 12 #include "chrome/renderer/extensions/user_script_slave.h" |
13 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
14 #include "grit/renderer_resources.h" | 14 #include "grit/renderer_resources.h" |
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebFileSystem.h" | 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebFileSystem.h" |
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
18 #include "webkit/fileapi/file_system_types.h" | 18 #include "webkit/fileapi/file_system_types.h" |
19 #include "webkit/fileapi/file_system_util.h" | 19 #include "webkit/fileapi/file_system_util.h" |
20 | 20 |
21 namespace { | 21 namespace extensions { |
22 | 22 |
23 static v8::Handle<v8::Value> GetIsolatedFileSystem( | 23 FileSystemNatives::FileSystemNatives(v8::Handle<v8::Context> context) |
| 24 : ObjectBackedNativeHandler(context) { |
| 25 RouteFunction("GetFileEntry", |
| 26 base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this))); |
| 27 RouteFunction("GetIsolatedFileSystem", |
| 28 base::Bind(&FileSystemNatives::GetIsolatedFileSystem, |
| 29 base::Unretained(this))); |
| 30 } |
| 31 |
| 32 v8::Handle<v8::Value> FileSystemNatives::GetIsolatedFileSystem( |
24 const v8::Arguments& args) { | 33 const v8::Arguments& args) { |
25 DCHECK(args.Length() == 1 || args.Length() == 2); | 34 DCHECK(args.Length() == 1 || args.Length() == 2); |
26 DCHECK(args[0]->IsString()); | 35 DCHECK(args[0]->IsString()); |
27 std::string file_system_id(*v8::String::Utf8Value(args[0])); | 36 std::string file_system_id(*v8::String::Utf8Value(args[0])); |
28 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); | 37 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForContext(v8_context()); |
29 DCHECK(webframe); | 38 DCHECK(webframe); |
30 | 39 |
31 GURL context_url = | 40 GURL context_url = |
32 extensions::UserScriptSlave::GetDataSourceURLForFrame(webframe); | 41 extensions::UserScriptSlave::GetDataSourceURLForFrame(webframe); |
33 CHECK(context_url.SchemeIs(extensions::kExtensionScheme)); | 42 CHECK(context_url.SchemeIs(extensions::kExtensionScheme)); |
34 | 43 |
35 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(), | 44 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(), |
36 file_system_id)); | 45 file_system_id)); |
37 | 46 |
38 // The optional second argument is the subfolder within the isolated file | 47 // The optional second argument is the subfolder within the isolated file |
39 // system at which to root the DOMFileSystem we're returning to the caller. | 48 // system at which to root the DOMFileSystem we're returning to the caller. |
40 std::string optional_root_name; | 49 std::string optional_root_name; |
41 if (args.Length() == 2) { | 50 if (args.Length() == 2) { |
42 DCHECK(args[1]->IsString()); | 51 DCHECK(args[1]->IsString()); |
43 optional_root_name = *v8::String::Utf8Value(args[1]); | 52 optional_root_name = *v8::String::Utf8Value(args[1]); |
44 } | 53 } |
45 | 54 |
46 std::string root(fileapi::GetIsolatedFileSystemRootURIString( | 55 std::string root(fileapi::GetIsolatedFileSystemRootURIString( |
47 context_url.GetOrigin(), | 56 context_url.GetOrigin(), |
48 file_system_id, | 57 file_system_id, |
49 optional_root_name)); | 58 optional_root_name)); |
50 | 59 |
51 return webframe->createFileSystem( | 60 return webframe->createFileSystem( |
52 WebKit::WebFileSystem::TypeIsolated, | 61 WebKit::WebFileSystem::TypeIsolated, |
53 WebKit::WebString::fromUTF8(name), | 62 WebKit::WebString::fromUTF8(name), |
54 WebKit::WebString::fromUTF8(root)); | 63 WebKit::WebString::fromUTF8(root)); |
55 } | 64 } |
56 | 65 |
57 static v8::Handle<v8::Value> GetFileEntry(const v8::Arguments& args) { | 66 v8::Handle<v8::Value> FileSystemNatives::GetFileEntry( |
| 67 const v8::Arguments& args) { |
58 DCHECK(args.Length() == 5); | 68 DCHECK(args.Length() == 5); |
59 DCHECK(args[0]->IsString()); | 69 DCHECK(args[0]->IsString()); |
60 std::string type_string = *v8::String::Utf8Value(args[0]->ToString()); | 70 std::string type_string = *v8::String::Utf8Value(args[0]->ToString()); |
61 WebKit::WebFileSystem::Type type; | 71 WebKit::WebFileSystem::Type type; |
62 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type); | 72 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type); |
63 DCHECK(is_valid_type); | 73 DCHECK(is_valid_type); |
64 if (is_valid_type == false) { | 74 if (is_valid_type == false) { |
65 return v8::Undefined(); | 75 return v8::Undefined(); |
66 } | 76 } |
67 | 77 |
68 DCHECK(args[1]->IsString()); | 78 DCHECK(args[1]->IsString()); |
69 DCHECK(args[2]->IsString()); | 79 DCHECK(args[2]->IsString()); |
70 DCHECK(args[3]->IsString()); | 80 DCHECK(args[3]->IsString()); |
71 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString())); | 81 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString())); |
72 std::string file_system_root_url(*v8::String::Utf8Value(args[2]->ToString())); | 82 std::string file_system_root_url(*v8::String::Utf8Value(args[2]->ToString())); |
73 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString())); | 83 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString())); |
74 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string); | 84 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string); |
75 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value())); | 85 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value())); |
76 | 86 |
77 DCHECK(args[4]->IsBoolean()); | 87 DCHECK(args[4]->IsBoolean()); |
78 bool is_directory = args[4]->BooleanValue(); | 88 bool is_directory = args[4]->BooleanValue(); |
79 | 89 |
80 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); | 90 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForContext(v8_context()); |
81 DCHECK(webframe); | 91 DCHECK(webframe); |
82 return webframe->createFileEntry( | 92 return webframe->createFileEntry( |
83 type, | 93 type, |
84 WebKit::WebString::fromUTF8(file_system_name), | 94 WebKit::WebString::fromUTF8(file_system_name), |
85 WebKit::WebString::fromUTF8(file_system_root_url), | 95 WebKit::WebString::fromUTF8(file_system_root_url), |
86 WebKit::WebString::fromUTF8(file_path_string), | 96 WebKit::WebString::fromUTF8(file_path_string), |
87 is_directory); | 97 is_directory); |
88 } | 98 } |
89 | 99 |
90 } // namespace | |
91 | |
92 namespace extensions { | |
93 | |
94 FileSystemNatives::FileSystemNatives() | |
95 : ChromeV8Extension(NULL) { | |
96 RouteStaticFunction("GetFileEntry", &GetFileEntry); | |
97 RouteStaticFunction("GetIsolatedFileSystem", &GetIsolatedFileSystem); | |
98 } | |
99 | |
100 } // namespace extensions | 100 } // namespace extensions |
OLD | NEW |