| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/renderer/extensions/file_system_natives.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 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 "extensions/renderer/script_context.h" | |
| 16 #include "grit/renderer_resources.h" | |
| 17 #include "third_party/WebKit/public/platform/WebString.h" | |
| 18 #include "third_party/WebKit/public/web/WebDOMError.h" | |
| 19 #include "third_party/WebKit/public/web/WebDOMFileSystem.h" | |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 21 #include "webkit/common/fileapi/file_system_types.h" | |
| 22 #include "webkit/common/fileapi/file_system_util.h" | |
| 23 | |
| 24 namespace extensions { | |
| 25 | |
| 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))); | |
| 39 } | |
| 40 | |
| 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::WebLocalFrame* webframe = | |
| 47 blink::WebLocalFrame::frameForContext(context()->v8_context()); | |
| 48 DCHECK(webframe); | |
| 49 | |
| 50 GURL context_url = | |
| 51 extensions::ScriptContext::GetDataSourceURLForFrame(webframe); | |
| 52 CHECK(context_url.SchemeIs(extensions::kExtensionScheme)); | |
| 53 | |
| 54 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(), | |
| 55 file_system_id)); | |
| 56 | |
| 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]); | |
| 63 } | |
| 64 | |
| 65 GURL root_url(fileapi::GetIsolatedFileSystemRootURIString( | |
| 66 context_url.GetOrigin(), | |
| 67 file_system_id, | |
| 68 optional_root_name)); | |
| 69 | |
| 70 args.GetReturnValue().Set( | |
| 71 blink::WebDOMFileSystem::create( | |
| 72 webframe, | |
| 73 blink::WebFileSystemTypeIsolated, | |
| 74 blink::WebString::fromUTF8(name), | |
| 75 root_url).toV8Value()); | |
| 76 } | |
| 77 | |
| 78 void FileSystemNatives::GetFileEntry( | |
| 79 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 80 DCHECK(args.Length() == 5); | |
| 81 DCHECK(args[0]->IsString()); | |
| 82 std::string type_string = *v8::String::Utf8Value(args[0]->ToString()); | |
| 83 blink::WebFileSystemType type; | |
| 84 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type); | |
| 85 DCHECK(is_valid_type); | |
| 86 if (is_valid_type == false) { | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 DCHECK(args[1]->IsString()); | |
| 91 DCHECK(args[2]->IsString()); | |
| 92 DCHECK(args[3]->IsString()); | |
| 93 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString())); | |
| 94 GURL file_system_root_url(*v8::String::Utf8Value(args[2]->ToString())); | |
| 95 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString())); | |
| 96 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string); | |
| 97 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value())); | |
| 98 | |
| 99 DCHECK(args[4]->IsBoolean()); | |
| 100 blink::WebDOMFileSystem::EntryType entry_type = args[4]->BooleanValue() | |
| 101 ? blink::WebDOMFileSystem::EntryTypeDirectory | |
| 102 : blink::WebDOMFileSystem::EntryTypeFile; | |
| 103 | |
| 104 blink::WebLocalFrame* webframe = | |
| 105 blink::WebLocalFrame::frameForContext(context()->v8_context()); | |
| 106 DCHECK(webframe); | |
| 107 args.GetReturnValue().Set( | |
| 108 blink::WebDOMFileSystem::create( | |
| 109 webframe, type, | |
| 110 blink::WebString::fromUTF8(file_system_name), | |
| 111 file_system_root_url).createV8Entry( | |
| 112 blink::WebString::fromUTF8(file_path_string), | |
| 113 entry_type)); | |
| 114 } | |
| 115 | |
| 116 void FileSystemNatives::CrackIsolatedFileSystemName( | |
| 117 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 118 DCHECK_EQ(args.Length(), 1); | |
| 119 DCHECK(args[0]->IsString()); | |
| 120 std::string filesystem_name = *v8::String::Utf8Value(args[0]->ToString()); | |
| 121 std::string filesystem_id; | |
| 122 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) | |
| 123 return; | |
| 124 | |
| 125 args.GetReturnValue().Set(v8::String::NewFromUtf8(args.GetIsolate(), | |
| 126 filesystem_id.c_str(), | |
| 127 v8::String::kNormalString, | |
| 128 filesystem_id.size())); | |
| 129 } | |
| 130 | |
| 131 void FileSystemNatives::GetDOMError( | |
| 132 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 133 if (args.Length() != 2) { | |
| 134 NOTREACHED(); | |
| 135 return; | |
| 136 } | |
| 137 if (!args[0]->IsString()) { | |
| 138 NOTREACHED(); | |
| 139 return; | |
| 140 } | |
| 141 if (!args[1]->IsString()) { | |
| 142 NOTREACHED(); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 std::string name(*v8::String::Utf8Value(args[0])); | |
| 147 if (name.empty()) { | |
| 148 NOTREACHED(); | |
| 149 return; | |
| 150 } | |
| 151 std::string message(*v8::String::Utf8Value(args[1])); | |
| 152 // message is optional hence empty is fine. | |
| 153 | |
| 154 blink::WebDOMError dom_error = blink::WebDOMError::create( | |
| 155 blink::WebString::fromUTF8(name), | |
| 156 blink::WebString::fromUTF8(message)); | |
| 157 args.GetReturnValue().Set(dom_error.toV8Value()); | |
| 158 } | |
| 159 | |
| 160 } // namespace extensions | |
| OLD | NEW |