[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 5 | #include "extensions/renderer/file_system_natives.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 9 | #include "extensions/common/constants.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 10 | #include "extensions/renderer/script_context.h" |
[email protected] | e48cf4f | 2013-05-30 12:40:12 | [diff] [blame] | 11 | #include "third_party/WebKit/public/platform/WebString.h" |
[email protected] | ada24fd | 2013-11-08 06:15:08 | [diff] [blame] | 12 | #include "third_party/WebKit/public/web/WebDOMError.h" |
[email protected] | 0e2dd10f | 2014-02-26 17:27:58 | [diff] [blame] | 13 | #include "third_party/WebKit/public/web/WebDOMFileSystem.h" |
[email protected] | d357694 | 2014-04-10 18:45:37 | [diff] [blame] | 14 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 61d271f3 | 2013-05-28 04:59:23 | [diff] [blame] | 15 | #include "webkit/common/fileapi/file_system_types.h" |
| 16 | #include "webkit/common/fileapi/file_system_util.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 17 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 18 | namespace extensions { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 19 | |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 20 | FileSystemNatives::FileSystemNatives(ScriptContext* context) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 21 | : ObjectBackedNativeHandler(context) { |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 22 | RouteFunction( |
| 23 | "GetFileEntry", |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 24 | base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this))); |
| 25 | RouteFunction("GetIsolatedFileSystem", |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 26 | base::Bind(&FileSystemNatives::GetIsolatedFileSystem, |
| 27 | base::Unretained(this))); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 28 | RouteFunction("CrackIsolatedFileSystemName", |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 29 | base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName, |
| 30 | base::Unretained(this))); |
| 31 | RouteFunction( |
| 32 | "GetDOMError", |
| 33 | base::Bind(&FileSystemNatives::GetDOMError, base::Unretained(this))); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 36 | void FileSystemNatives::GetIsolatedFileSystem( |
| 37 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | cd8741d7 | 2012-08-30 10:19:34 | [diff] [blame] | 38 | DCHECK(args.Length() == 1 || args.Length() == 2); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 39 | DCHECK(args[0]->IsString()); |
| 40 | std::string file_system_id(*v8::String::Utf8Value(args[0])); |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 41 | blink::WebLocalFrame* webframe = |
| 42 | blink::WebLocalFrame::frameForContext(context()->v8_context()); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 43 | DCHECK(webframe); |
| 44 | |
[email protected] | 20f97c9 | 2012-07-13 23:12:37 | [diff] [blame] | 45 | GURL context_url = |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 46 | extensions::ScriptContext::GetDataSourceURLForFrame(webframe); |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 47 | CHECK(context_url.SchemeIs(extensions::kExtensionScheme)); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 48 | |
[email protected] | ffc7b4d | 2012-06-08 00:05:32 | [diff] [blame] | 49 | std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(), |
| 50 | file_system_id)); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 51 | |
[email protected] | cd8741d7 | 2012-08-30 10:19:34 | [diff] [blame] | 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. |
[email protected] | 1dd8625 | 2013-01-10 06:00:20 | [diff] [blame] | 54 | std::string optional_root_name; |
[email protected] | cd8741d7 | 2012-08-30 10:19:34 | [diff] [blame] | 55 | if (args.Length() == 2) { |
| 56 | DCHECK(args[1]->IsString()); |
[email protected] | 1dd8625 | 2013-01-10 06:00:20 | [diff] [blame] | 57 | optional_root_name = *v8::String::Utf8Value(args[1]); |
[email protected] | cd8741d7 | 2012-08-30 10:19:34 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 0e2dd10f | 2014-02-26 17:27:58 | [diff] [blame] | 60 | GURL root_url(fileapi::GetIsolatedFileSystemRootURIString( |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 61 | context_url.GetOrigin(), file_system_id, optional_root_name)); |
[email protected] | 1dd8625 | 2013-01-10 06:00:20 | [diff] [blame] | 62 | |
[email protected] | 0e2dd10f | 2014-02-26 17:27:58 | [diff] [blame] | 63 | args.GetReturnValue().Set( |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 64 | blink::WebDOMFileSystem::create(webframe, |
| 65 | blink::WebFileSystemTypeIsolated, |
| 66 | blink::WebString::fromUTF8(name), |
[email protected] | fdb0b8e | 2014-06-06 01:26:11 | [diff] [blame] | 67 | root_url) |
| 68 | .toV8Value(args.Holder(), args.GetIsolate())); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 71 | void FileSystemNatives::GetFileEntry( |
| 72 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 73 | DCHECK(args.Length() == 5); |
| 74 | DCHECK(args[0]->IsString()); |
| 75 | std::string type_string = *v8::String::Utf8Value(args[0]->ToString()); |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 76 | blink::WebFileSystemType type; |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 77 | bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type); |
| 78 | DCHECK(is_valid_type); |
| 79 | if (is_valid_type == false) { |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 80 | return; |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 81 | } |
| 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] | 0e2dd10f | 2014-02-26 17:27:58 | [diff] [blame] | 87 | GURL file_system_root_url(*v8::String::Utf8Value(args[2]->ToString())); |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 88 | std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString())); |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 89 | base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string); |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 90 | DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value())); |
| 91 | |
| 92 | DCHECK(args[4]->IsBoolean()); |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 93 | blink::WebDOMFileSystem::EntryType entry_type = |
| 94 | args[4]->BooleanValue() ? blink::WebDOMFileSystem::EntryTypeDirectory |
| 95 | : blink::WebDOMFileSystem::EntryTypeFile; |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 96 | |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 97 | blink::WebLocalFrame* webframe = |
| 98 | blink::WebLocalFrame::frameForContext(context()->v8_context()); |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 99 | DCHECK(webframe); |
[email protected] | 0e2dd10f | 2014-02-26 17:27:58 | [diff] [blame] | 100 | args.GetReturnValue().Set( |
| 101 | blink::WebDOMFileSystem::create( |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 102 | webframe, |
| 103 | type, |
[email protected] | 0e2dd10f | 2014-02-26 17:27:58 | [diff] [blame] | 104 | blink::WebString::fromUTF8(file_system_name), |
[email protected] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 105 | file_system_root_url) |
| 106 | .createV8Entry(blink::WebString::fromUTF8(file_path_string), |
[email protected] | fdb0b8e | 2014-06-06 01:26:11 | [diff] [blame] | 107 | entry_type, |
| 108 | args.Holder(), |
| 109 | args.GetIsolate())); |
[email protected] | 9a70a6a3 | 2013-02-06 16:14:53 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 112 | void FileSystemNatives::CrackIsolatedFileSystemName( |
| 113 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 114 | 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] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 119 | return; |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 120 | |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 121 | args.GetReturnValue().Set(v8::String::NewFromUtf8(args.GetIsolate(), |
| 122 | filesystem_id.c_str(), |
| 123 | v8::String::kNormalString, |
| 124 | filesystem_id.size())); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | ada24fd | 2013-11-08 06:15:08 | [diff] [blame] | 127 | void 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] | 701a94e | 2014-04-17 04:37:37 | [diff] [blame] | 151 | blink::WebString::fromUTF8(name), blink::WebString::fromUTF8(message)); |
[email protected] | ac0514f | 2014-06-06 11:06:00 | [diff] [blame] | 152 | args.GetReturnValue().Set( |
| 153 | dom_error.toV8Value(args.Holder(), args.GetIsolate())); |
[email protected] | ada24fd | 2013-11-08 06:15:08 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 156 | } // namespace extensions |