blob: 847e7a8656fd819b097e022c899fa73c50246303 [file] [log] [blame]
[email protected]701a94e2014-04-17 04:37:371// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]12e540452012-05-26 07:09:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]701a94e2014-04-17 04:37:375#include "extensions/renderer/file_system_natives.h"
[email protected]12e540452012-05-26 07:09:366
7#include <string>
8
[email protected]885c0e92012-11-13 20:27:429#include "extensions/common/constants.h"
[email protected]f55c90ee62014-04-12 00:50:0310#include "extensions/renderer/script_context.h"
[email protected]e48cf4f2013-05-30 12:40:1211#include "third_party/WebKit/public/platform/WebString.h"
[email protected]ada24fd2013-11-08 06:15:0812#include "third_party/WebKit/public/web/WebDOMError.h"
[email protected]0e2dd10f2014-02-26 17:27:5813#include "third_party/WebKit/public/web/WebDOMFileSystem.h"
[email protected]d3576942014-04-10 18:45:3714#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]61d271f32013-05-28 04:59:2315#include "webkit/common/fileapi/file_system_types.h"
16#include "webkit/common/fileapi/file_system_util.h"
[email protected]12e540452012-05-26 07:09:3617
[email protected]4f1633f2013-03-09 14:26:2418namespace extensions {
[email protected]12e540452012-05-26 07:09:3619
[email protected]701a94e2014-04-17 04:37:3720FileSystemNatives::FileSystemNatives(ScriptContext* context)
[email protected]4f1633f2013-03-09 14:26:2421 : ObjectBackedNativeHandler(context) {
[email protected]701a94e2014-04-17 04:37:3722 RouteFunction(
23 "GetFileEntry",
[email protected]4f1633f2013-03-09 14:26:2424 base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this)));
25 RouteFunction("GetIsolatedFileSystem",
[email protected]701a94e2014-04-17 04:37:3726 base::Bind(&FileSystemNatives::GetIsolatedFileSystem,
27 base::Unretained(this)));
[email protected]961745f2013-05-25 14:09:2428 RouteFunction("CrackIsolatedFileSystemName",
[email protected]701a94e2014-04-17 04:37:3729 base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName,
30 base::Unretained(this)));
31 RouteFunction(
32 "GetDOMError",
33 base::Bind(&FileSystemNatives::GetDOMError, base::Unretained(this)));
[email protected]4f1633f2013-03-09 14:26:2434}
35
[email protected]d8c5fbb2013-06-14 11:35:2536void FileSystemNatives::GetIsolatedFileSystem(
37 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]cd8741d72012-08-30 10:19:3438 DCHECK(args.Length() == 1 || args.Length() == 2);
[email protected]12e540452012-05-26 07:09:3639 DCHECK(args[0]->IsString());
40 std::string file_system_id(*v8::String::Utf8Value(args[0]));
[email protected]c5041c322014-04-08 05:06:4741 blink::WebLocalFrame* webframe =
42 blink::WebLocalFrame::frameForContext(context()->v8_context());
[email protected]12e540452012-05-26 07:09:3643 DCHECK(webframe);
44
[email protected]20f97c92012-07-13 23:12:3745 GURL context_url =
[email protected]f55c90ee62014-04-12 00:50:0346 extensions::ScriptContext::GetDataSourceURLForFrame(webframe);
[email protected]885c0e92012-11-13 20:27:4247 CHECK(context_url.SchemeIs(extensions::kExtensionScheme));
[email protected]12e540452012-05-26 07:09:3648
[email protected]ffc7b4d2012-06-08 00:05:3249 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(),
50 file_system_id));
[email protected]12e540452012-05-26 07:09:3651
[email protected]cd8741d72012-08-30 10:19:3452 // The optional second argument is the subfolder within the isolated file
53 // system at which to root the DOMFileSystem we're returning to the caller.
[email protected]1dd86252013-01-10 06:00:2054 std::string optional_root_name;
[email protected]cd8741d72012-08-30 10:19:3455 if (args.Length() == 2) {
56 DCHECK(args[1]->IsString());
[email protected]1dd86252013-01-10 06:00:2057 optional_root_name = *v8::String::Utf8Value(args[1]);
[email protected]cd8741d72012-08-30 10:19:3458 }
59
[email protected]0e2dd10f2014-02-26 17:27:5860 GURL root_url(fileapi::GetIsolatedFileSystemRootURIString(
[email protected]701a94e2014-04-17 04:37:3761 context_url.GetOrigin(), file_system_id, optional_root_name));
[email protected]1dd86252013-01-10 06:00:2062
[email protected]0e2dd10f2014-02-26 17:27:5863 args.GetReturnValue().Set(
[email protected]701a94e2014-04-17 04:37:3764 blink::WebDOMFileSystem::create(webframe,
65 blink::WebFileSystemTypeIsolated,
66 blink::WebString::fromUTF8(name),
[email protected]fdb0b8e2014-06-06 01:26:1167 root_url)
68 .toV8Value(args.Holder(), args.GetIsolate()));
[email protected]12e540452012-05-26 07:09:3669}
70
[email protected]d8c5fbb2013-06-14 11:35:2571void FileSystemNatives::GetFileEntry(
72 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a70a6a32013-02-06 16:14:5373 DCHECK(args.Length() == 5);
74 DCHECK(args[0]->IsString());
75 std::string type_string = *v8::String::Utf8Value(args[0]->ToString());
[email protected]a1221aea2013-11-07 01:31:3076 blink::WebFileSystemType type;
[email protected]9a70a6a32013-02-06 16:14:5377 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type);
78 DCHECK(is_valid_type);
79 if (is_valid_type == false) {
[email protected]d8c5fbb2013-06-14 11:35:2580 return;
[email protected]9a70a6a32013-02-06 16:14:5381 }
82
83 DCHECK(args[1]->IsString());
84 DCHECK(args[2]->IsString());
85 DCHECK(args[3]->IsString());
86 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString()));
[email protected]0e2dd10f2014-02-26 17:27:5887 GURL file_system_root_url(*v8::String::Utf8Value(args[2]->ToString()));
[email protected]9a70a6a32013-02-06 16:14:5388 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString()));
[email protected]3ea1b182013-02-08 22:38:4189 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string);
[email protected]9a70a6a32013-02-06 16:14:5390 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value()));
91
92 DCHECK(args[4]->IsBoolean());
[email protected]701a94e2014-04-17 04:37:3793 blink::WebDOMFileSystem::EntryType entry_type =
94 args[4]->BooleanValue() ? blink::WebDOMFileSystem::EntryTypeDirectory
95 : blink::WebDOMFileSystem::EntryTypeFile;
[email protected]9a70a6a32013-02-06 16:14:5396
[email protected]c5041c322014-04-08 05:06:4797 blink::WebLocalFrame* webframe =
98 blink::WebLocalFrame::frameForContext(context()->v8_context());
[email protected]9a70a6a32013-02-06 16:14:5399 DCHECK(webframe);
[email protected]0e2dd10f2014-02-26 17:27:58100 args.GetReturnValue().Set(
101 blink::WebDOMFileSystem::create(
[email protected]701a94e2014-04-17 04:37:37102 webframe,
103 type,
[email protected]0e2dd10f2014-02-26 17:27:58104 blink::WebString::fromUTF8(file_system_name),
[email protected]701a94e2014-04-17 04:37:37105 file_system_root_url)
106 .createV8Entry(blink::WebString::fromUTF8(file_path_string),
[email protected]fdb0b8e2014-06-06 01:26:11107 entry_type,
108 args.Holder(),
109 args.GetIsolate()));
[email protected]9a70a6a32013-02-06 16:14:53110}
111
[email protected]d8c5fbb2013-06-14 11:35:25112void FileSystemNatives::CrackIsolatedFileSystemName(
113 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]961745f2013-05-25 14:09:24114 DCHECK_EQ(args.Length(), 1);
115 DCHECK(args[0]->IsString());
116 std::string filesystem_name = *v8::String::Utf8Value(args[0]->ToString());
117 std::string filesystem_id;
118 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id))
[email protected]d8c5fbb2013-06-14 11:35:25119 return;
[email protected]961745f2013-05-25 14:09:24120
[email protected]9c47471e2013-11-28 14:41:21121 args.GetReturnValue().Set(v8::String::NewFromUtf8(args.GetIsolate(),
122 filesystem_id.c_str(),
123 v8::String::kNormalString,
124 filesystem_id.size()));
[email protected]961745f2013-05-25 14:09:24125}
126
[email protected]ada24fd2013-11-08 06:15:08127void FileSystemNatives::GetDOMError(
128 const v8::FunctionCallbackInfo<v8::Value>& args) {
129 if (args.Length() != 2) {
130 NOTREACHED();
131 return;
132 }
133 if (!args[0]->IsString()) {
134 NOTREACHED();
135 return;
136 }
137 if (!args[1]->IsString()) {
138 NOTREACHED();
139 return;
140 }
141
142 std::string name(*v8::String::Utf8Value(args[0]));
143 if (name.empty()) {
144 NOTREACHED();
145 return;
146 }
147 std::string message(*v8::String::Utf8Value(args[1]));
148 // message is optional hence empty is fine.
149
150 blink::WebDOMError dom_error = blink::WebDOMError::create(
[email protected]701a94e2014-04-17 04:37:37151 blink::WebString::fromUTF8(name), blink::WebString::fromUTF8(message));
[email protected]ac0514f2014-06-06 11:06:00152 args.GetReturnValue().Set(
153 dom_error.toV8Value(args.Holder(), args.GetIsolate()));
[email protected]ada24fd2013-11-08 06:15:08154}
155
[email protected]12e540452012-05-26 07:09:36156} // namespace extensions