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.
5 #include "chrome/renderer/extensions/file_system_natives.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/renderer/extensions/chrome_v8_context.h"
13 #include "chrome/renderer/extensions/user_script_slave.h"
14 #include "extensions/common/constants.h"
15 #include "grit/renderer_resources.h"
16 #include "third_party/WebKit/public/platform/WebFileSystem.h"
17 #include "third_party/WebKit/public/platform/WebFileSystemType.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/web/WebDOMError.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "webkit/common/fileapi/file_system_types.h"
22 #include "webkit/common/fileapi/file_system_util.h"
24 namespace extensions
{
26 FileSystemNatives::FileSystemNatives(ChromeV8Context
* context
)
27 : ObjectBackedNativeHandler(context
) {
28 RouteFunction("GetFileEntry",
29 base::Bind(&FileSystemNatives::GetFileEntry
, base::Unretained(this)));
30 RouteFunction("GetIsolatedFileSystem",
31 base::Bind(&FileSystemNatives::GetIsolatedFileSystem
,
32 base::Unretained(this)));
33 RouteFunction("CrackIsolatedFileSystemName",
34 base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName
,
35 base::Unretained(this)));
36 RouteFunction("GetDOMError",
37 base::Bind(&FileSystemNatives::GetDOMError
,
38 base::Unretained(this)));
41 void FileSystemNatives::GetIsolatedFileSystem(
42 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
43 DCHECK(args
.Length() == 1 || args
.Length() == 2);
44 DCHECK(args
[0]->IsString());
45 std::string
file_system_id(*v8::String::Utf8Value(args
[0]));
46 blink::WebFrame
* webframe
=
47 blink::WebFrame::frameForContext(context()->v8_context());
51 extensions::UserScriptSlave::GetDataSourceURLForFrame(webframe
);
52 CHECK(context_url
.SchemeIs(extensions::kExtensionScheme
));
54 std::string
name(fileapi::GetIsolatedFileSystemName(context_url
.GetOrigin(),
57 // The optional second argument is the subfolder within the isolated file
58 // system at which to root the DOMFileSystem we're returning to the caller.
59 std::string optional_root_name
;
60 if (args
.Length() == 2) {
61 DCHECK(args
[1]->IsString());
62 optional_root_name
= *v8::String::Utf8Value(args
[1]);
65 std::string
root(fileapi::GetIsolatedFileSystemRootURIString(
66 context_url
.GetOrigin(),
70 args
.GetReturnValue().Set(webframe
->createFileSystem(
71 blink::WebFileSystemTypeIsolated
,
72 blink::WebString::fromUTF8(name
),
73 blink::WebString::fromUTF8(root
)));
76 void FileSystemNatives::GetFileEntry(
77 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
78 DCHECK(args
.Length() == 5);
79 DCHECK(args
[0]->IsString());
80 std::string type_string
= *v8::String::Utf8Value(args
[0]->ToString());
81 blink::WebFileSystemType type
;
82 bool is_valid_type
= fileapi::GetFileSystemPublicType(type_string
, &type
);
83 DCHECK(is_valid_type
);
84 if (is_valid_type
== false) {
88 DCHECK(args
[1]->IsString());
89 DCHECK(args
[2]->IsString());
90 DCHECK(args
[3]->IsString());
91 std::string
file_system_name(*v8::String::Utf8Value(args
[1]->ToString()));
92 std::string
file_system_root_url(*v8::String::Utf8Value(args
[2]->ToString()));
93 std::string
file_path_string(*v8::String::Utf8Value(args
[3]->ToString()));
94 base::FilePath file_path
= base::FilePath::FromUTF8Unsafe(file_path_string
);
95 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path
.value()));
97 DCHECK(args
[4]->IsBoolean());
98 bool is_directory
= args
[4]->BooleanValue();
100 blink::WebFrame
* webframe
=
101 blink::WebFrame::frameForContext(context()->v8_context());
103 args
.GetReturnValue().Set(webframe
->createFileEntry(
105 blink::WebString::fromUTF8(file_system_name
),
106 blink::WebString::fromUTF8(file_system_root_url
),
107 blink::WebString::fromUTF8(file_path_string
),
111 void FileSystemNatives::CrackIsolatedFileSystemName(
112 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
113 DCHECK_EQ(args
.Length(), 1);
114 DCHECK(args
[0]->IsString());
115 std::string filesystem_name
= *v8::String::Utf8Value(args
[0]->ToString());
116 std::string filesystem_id
;
117 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name
, &filesystem_id
))
120 args
.GetReturnValue().Set(v8::String::NewFromUtf8(args
.GetIsolate(),
121 filesystem_id
.c_str(),
122 v8::String::kNormalString
,
123 filesystem_id
.size()));
126 void FileSystemNatives::GetDOMError(
127 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
128 if (args
.Length() != 2) {
132 if (!args
[0]->IsString()) {
136 if (!args
[1]->IsString()) {
141 std::string
name(*v8::String::Utf8Value(args
[0]));
146 std::string
message(*v8::String::Utf8Value(args
[1]));
147 // message is optional hence empty is fine.
149 blink::WebDOMError dom_error
= blink::WebDOMError::create(
150 blink::WebString::fromUTF8(name
),
151 blink::WebString::fromUTF8(message
));
152 args
.GetReturnValue().Set(dom_error
.toV8Value());
155 } // namespace extensions