Implementation of leveldb-backed PrefStore.
[chromium-blink-merge.git] / extensions / renderer / file_system_natives.cc
blob94f8d21d1c7036fc7273e4c8a6a8c6e97f04dbfd
1 // Copyright 2014 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 "extensions/renderer/file_system_natives.h"
7 #include <string>
9 #include "extensions/common/constants.h"
10 #include "extensions/renderer/script_context.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/web/WebDOMError.h"
13 #include "third_party/WebKit/public/web/WebDOMFileSystem.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "webkit/common/fileapi/file_system_types.h"
16 #include "webkit/common/fileapi/file_system_util.h"
18 namespace extensions {
20 FileSystemNatives::FileSystemNatives(ScriptContext* context)
21 : ObjectBackedNativeHandler(context) {
22 RouteFunction(
23 "GetFileEntry",
24 base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this)));
25 RouteFunction("GetIsolatedFileSystem",
26 base::Bind(&FileSystemNatives::GetIsolatedFileSystem,
27 base::Unretained(this)));
28 RouteFunction("CrackIsolatedFileSystemName",
29 base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName,
30 base::Unretained(this)));
31 RouteFunction(
32 "GetDOMError",
33 base::Bind(&FileSystemNatives::GetDOMError, base::Unretained(this)));
36 void FileSystemNatives::GetIsolatedFileSystem(
37 const v8::FunctionCallbackInfo<v8::Value>& args) {
38 DCHECK(args.Length() == 1 || args.Length() == 2);
39 DCHECK(args[0]->IsString());
40 std::string file_system_id(*v8::String::Utf8Value(args[0]));
41 blink::WebLocalFrame* webframe =
42 blink::WebLocalFrame::frameForContext(context()->v8_context());
43 DCHECK(webframe);
45 GURL context_url =
46 extensions::ScriptContext::GetDataSourceURLForFrame(webframe);
47 CHECK(context_url.SchemeIs(extensions::kExtensionScheme));
49 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(),
50 file_system_id));
52 // 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.
54 std::string optional_root_name;
55 if (args.Length() == 2) {
56 DCHECK(args[1]->IsString());
57 optional_root_name = *v8::String::Utf8Value(args[1]);
60 GURL root_url(fileapi::GetIsolatedFileSystemRootURIString(
61 context_url.GetOrigin(), file_system_id, optional_root_name));
63 args.GetReturnValue().Set(
64 blink::WebDOMFileSystem::create(webframe,
65 blink::WebFileSystemTypeIsolated,
66 blink::WebString::fromUTF8(name),
67 root_url).toV8Value());
70 void FileSystemNatives::GetFileEntry(
71 const v8::FunctionCallbackInfo<v8::Value>& args) {
72 DCHECK(args.Length() == 5);
73 DCHECK(args[0]->IsString());
74 std::string type_string = *v8::String::Utf8Value(args[0]->ToString());
75 blink::WebFileSystemType type;
76 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type);
77 DCHECK(is_valid_type);
78 if (is_valid_type == false) {
79 return;
82 DCHECK(args[1]->IsString());
83 DCHECK(args[2]->IsString());
84 DCHECK(args[3]->IsString());
85 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString()));
86 GURL file_system_root_url(*v8::String::Utf8Value(args[2]->ToString()));
87 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString()));
88 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string);
89 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value()));
91 DCHECK(args[4]->IsBoolean());
92 blink::WebDOMFileSystem::EntryType entry_type =
93 args[4]->BooleanValue() ? blink::WebDOMFileSystem::EntryTypeDirectory
94 : blink::WebDOMFileSystem::EntryTypeFile;
96 blink::WebLocalFrame* webframe =
97 blink::WebLocalFrame::frameForContext(context()->v8_context());
98 DCHECK(webframe);
99 args.GetReturnValue().Set(
100 blink::WebDOMFileSystem::create(
101 webframe,
102 type,
103 blink::WebString::fromUTF8(file_system_name),
104 file_system_root_url)
105 .createV8Entry(blink::WebString::fromUTF8(file_path_string),
106 entry_type));
109 void FileSystemNatives::CrackIsolatedFileSystemName(
110 const v8::FunctionCallbackInfo<v8::Value>& args) {
111 DCHECK_EQ(args.Length(), 1);
112 DCHECK(args[0]->IsString());
113 std::string filesystem_name = *v8::String::Utf8Value(args[0]->ToString());
114 std::string filesystem_id;
115 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id))
116 return;
118 args.GetReturnValue().Set(v8::String::NewFromUtf8(args.GetIsolate(),
119 filesystem_id.c_str(),
120 v8::String::kNormalString,
121 filesystem_id.size()));
124 void FileSystemNatives::GetDOMError(
125 const v8::FunctionCallbackInfo<v8::Value>& args) {
126 if (args.Length() != 2) {
127 NOTREACHED();
128 return;
130 if (!args[0]->IsString()) {
131 NOTREACHED();
132 return;
134 if (!args[1]->IsString()) {
135 NOTREACHED();
136 return;
139 std::string name(*v8::String::Utf8Value(args[0]));
140 if (name.empty()) {
141 NOTREACHED();
142 return;
144 std::string message(*v8::String::Utf8Value(args[1]));
145 // message is optional hence empty is fine.
147 blink::WebDOMError dom_error = blink::WebDOMError::create(
148 blink::WebString::fromUTF8(name), blink::WebString::fromUTF8(message));
149 args.GetReturnValue().Set(dom_error.toV8Value());
152 } // namespace extensions