| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/file_system_natives.h" | 5 #include "extensions/renderer/file_system_natives.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 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" | 9 #include "extensions/common/constants.h" |
| 15 #include "extensions/renderer/script_context.h" | 10 #include "extensions/renderer/script_context.h" |
| 16 #include "grit/renderer_resources.h" | |
| 17 #include "third_party/WebKit/public/platform/WebString.h" | 11 #include "third_party/WebKit/public/platform/WebString.h" |
| 18 #include "third_party/WebKit/public/web/WebDOMError.h" | 12 #include "third_party/WebKit/public/web/WebDOMError.h" |
| 19 #include "third_party/WebKit/public/web/WebDOMFileSystem.h" | 13 #include "third_party/WebKit/public/web/WebDOMFileSystem.h" |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 21 #include "webkit/common/fileapi/file_system_types.h" | 15 #include "webkit/common/fileapi/file_system_types.h" |
| 22 #include "webkit/common/fileapi/file_system_util.h" | 16 #include "webkit/common/fileapi/file_system_util.h" |
| 23 | 17 |
| 24 namespace extensions { | 18 namespace extensions { |
| 25 | 19 |
| 26 FileSystemNatives::FileSystemNatives(ChromeV8Context* context) | 20 FileSystemNatives::FileSystemNatives(ScriptContext* context) |
| 27 : ObjectBackedNativeHandler(context) { | 21 : ObjectBackedNativeHandler(context) { |
| 28 RouteFunction("GetFileEntry", | 22 RouteFunction( |
| 23 "GetFileEntry", |
| 29 base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this))); | 24 base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this))); |
| 30 RouteFunction("GetIsolatedFileSystem", | 25 RouteFunction("GetIsolatedFileSystem", |
| 31 base::Bind(&FileSystemNatives::GetIsolatedFileSystem, | 26 base::Bind(&FileSystemNatives::GetIsolatedFileSystem, |
| 32 base::Unretained(this))); | 27 base::Unretained(this))); |
| 33 RouteFunction("CrackIsolatedFileSystemName", | 28 RouteFunction("CrackIsolatedFileSystemName", |
| 34 base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName, | 29 base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName, |
| 35 base::Unretained(this))); | 30 base::Unretained(this))); |
| 36 RouteFunction("GetDOMError", | 31 RouteFunction( |
| 37 base::Bind(&FileSystemNatives::GetDOMError, | 32 "GetDOMError", |
| 38 base::Unretained(this))); | 33 base::Bind(&FileSystemNatives::GetDOMError, base::Unretained(this))); |
| 39 } | 34 } |
| 40 | 35 |
| 41 void FileSystemNatives::GetIsolatedFileSystem( | 36 void FileSystemNatives::GetIsolatedFileSystem( |
| 42 const v8::FunctionCallbackInfo<v8::Value>& args) { | 37 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 43 DCHECK(args.Length() == 1 || args.Length() == 2); | 38 DCHECK(args.Length() == 1 || args.Length() == 2); |
| 44 DCHECK(args[0]->IsString()); | 39 DCHECK(args[0]->IsString()); |
| 45 std::string file_system_id(*v8::String::Utf8Value(args[0])); | 40 std::string file_system_id(*v8::String::Utf8Value(args[0])); |
| 46 blink::WebLocalFrame* webframe = | 41 blink::WebLocalFrame* webframe = |
| 47 blink::WebLocalFrame::frameForContext(context()->v8_context()); | 42 blink::WebLocalFrame::frameForContext(context()->v8_context()); |
| 48 DCHECK(webframe); | 43 DCHECK(webframe); |
| 49 | 44 |
| 50 GURL context_url = | 45 GURL context_url = |
| 51 extensions::ScriptContext::GetDataSourceURLForFrame(webframe); | 46 extensions::ScriptContext::GetDataSourceURLForFrame(webframe); |
| 52 CHECK(context_url.SchemeIs(extensions::kExtensionScheme)); | 47 CHECK(context_url.SchemeIs(extensions::kExtensionScheme)); |
| 53 | 48 |
| 54 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(), | 49 std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(), |
| 55 file_system_id)); | 50 file_system_id)); |
| 56 | 51 |
| 57 // The optional second argument is the subfolder within the isolated file | 52 // 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. | 53 // system at which to root the DOMFileSystem we're returning to the caller. |
| 59 std::string optional_root_name; | 54 std::string optional_root_name; |
| 60 if (args.Length() == 2) { | 55 if (args.Length() == 2) { |
| 61 DCHECK(args[1]->IsString()); | 56 DCHECK(args[1]->IsString()); |
| 62 optional_root_name = *v8::String::Utf8Value(args[1]); | 57 optional_root_name = *v8::String::Utf8Value(args[1]); |
| 63 } | 58 } |
| 64 | 59 |
| 65 GURL root_url(fileapi::GetIsolatedFileSystemRootURIString( | 60 GURL root_url(fileapi::GetIsolatedFileSystemRootURIString( |
| 66 context_url.GetOrigin(), | 61 context_url.GetOrigin(), file_system_id, optional_root_name)); |
| 67 file_system_id, | |
| 68 optional_root_name)); | |
| 69 | 62 |
| 70 args.GetReturnValue().Set( | 63 args.GetReturnValue().Set( |
| 71 blink::WebDOMFileSystem::create( | 64 blink::WebDOMFileSystem::create(webframe, |
| 72 webframe, | 65 blink::WebFileSystemTypeIsolated, |
| 73 blink::WebFileSystemTypeIsolated, | 66 blink::WebString::fromUTF8(name), |
| 74 blink::WebString::fromUTF8(name), | 67 root_url).toV8Value()); |
| 75 root_url).toV8Value()); | |
| 76 } | 68 } |
| 77 | 69 |
| 78 void FileSystemNatives::GetFileEntry( | 70 void FileSystemNatives::GetFileEntry( |
| 79 const v8::FunctionCallbackInfo<v8::Value>& args) { | 71 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 80 DCHECK(args.Length() == 5); | 72 DCHECK(args.Length() == 5); |
| 81 DCHECK(args[0]->IsString()); | 73 DCHECK(args[0]->IsString()); |
| 82 std::string type_string = *v8::String::Utf8Value(args[0]->ToString()); | 74 std::string type_string = *v8::String::Utf8Value(args[0]->ToString()); |
| 83 blink::WebFileSystemType type; | 75 blink::WebFileSystemType type; |
| 84 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type); | 76 bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type); |
| 85 DCHECK(is_valid_type); | 77 DCHECK(is_valid_type); |
| 86 if (is_valid_type == false) { | 78 if (is_valid_type == false) { |
| 87 return; | 79 return; |
| 88 } | 80 } |
| 89 | 81 |
| 90 DCHECK(args[1]->IsString()); | 82 DCHECK(args[1]->IsString()); |
| 91 DCHECK(args[2]->IsString()); | 83 DCHECK(args[2]->IsString()); |
| 92 DCHECK(args[3]->IsString()); | 84 DCHECK(args[3]->IsString()); |
| 93 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString())); | 85 std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString())); |
| 94 GURL file_system_root_url(*v8::String::Utf8Value(args[2]->ToString())); | 86 GURL file_system_root_url(*v8::String::Utf8Value(args[2]->ToString())); |
| 95 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString())); | 87 std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString())); |
| 96 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string); | 88 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string); |
| 97 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value())); | 89 DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value())); |
| 98 | 90 |
| 99 DCHECK(args[4]->IsBoolean()); | 91 DCHECK(args[4]->IsBoolean()); |
| 100 blink::WebDOMFileSystem::EntryType entry_type = args[4]->BooleanValue() | 92 blink::WebDOMFileSystem::EntryType entry_type = |
| 101 ? blink::WebDOMFileSystem::EntryTypeDirectory | 93 args[4]->BooleanValue() ? blink::WebDOMFileSystem::EntryTypeDirectory |
| 102 : blink::WebDOMFileSystem::EntryTypeFile; | 94 : blink::WebDOMFileSystem::EntryTypeFile; |
| 103 | 95 |
| 104 blink::WebLocalFrame* webframe = | 96 blink::WebLocalFrame* webframe = |
| 105 blink::WebLocalFrame::frameForContext(context()->v8_context()); | 97 blink::WebLocalFrame::frameForContext(context()->v8_context()); |
| 106 DCHECK(webframe); | 98 DCHECK(webframe); |
| 107 args.GetReturnValue().Set( | 99 args.GetReturnValue().Set( |
| 108 blink::WebDOMFileSystem::create( | 100 blink::WebDOMFileSystem::create( |
| 109 webframe, type, | 101 webframe, |
| 102 type, |
| 110 blink::WebString::fromUTF8(file_system_name), | 103 blink::WebString::fromUTF8(file_system_name), |
| 111 file_system_root_url).createV8Entry( | 104 file_system_root_url) |
| 112 blink::WebString::fromUTF8(file_path_string), | 105 .createV8Entry(blink::WebString::fromUTF8(file_path_string), |
| 113 entry_type)); | 106 entry_type)); |
| 114 } | 107 } |
| 115 | 108 |
| 116 void FileSystemNatives::CrackIsolatedFileSystemName( | 109 void FileSystemNatives::CrackIsolatedFileSystemName( |
| 117 const v8::FunctionCallbackInfo<v8::Value>& args) { | 110 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 118 DCHECK_EQ(args.Length(), 1); | 111 DCHECK_EQ(args.Length(), 1); |
| 119 DCHECK(args[0]->IsString()); | 112 DCHECK(args[0]->IsString()); |
| 120 std::string filesystem_name = *v8::String::Utf8Value(args[0]->ToString()); | 113 std::string filesystem_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 121 std::string filesystem_id; | 114 std::string filesystem_id; |
| 122 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) | 115 if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) |
| 123 return; | 116 return; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 145 | 138 |
| 146 std::string name(*v8::String::Utf8Value(args[0])); | 139 std::string name(*v8::String::Utf8Value(args[0])); |
| 147 if (name.empty()) { | 140 if (name.empty()) { |
| 148 NOTREACHED(); | 141 NOTREACHED(); |
| 149 return; | 142 return; |
| 150 } | 143 } |
| 151 std::string message(*v8::String::Utf8Value(args[1])); | 144 std::string message(*v8::String::Utf8Value(args[1])); |
| 152 // message is optional hence empty is fine. | 145 // message is optional hence empty is fine. |
| 153 | 146 |
| 154 blink::WebDOMError dom_error = blink::WebDOMError::create( | 147 blink::WebDOMError dom_error = blink::WebDOMError::create( |
| 155 blink::WebString::fromUTF8(name), | 148 blink::WebString::fromUTF8(name), blink::WebString::fromUTF8(message)); |
| 156 blink::WebString::fromUTF8(message)); | |
| 157 args.GetReturnValue().Set(dom_error.toV8Value()); | 149 args.GetReturnValue().Set(dom_error.toV8Value()); |
| 158 } | 150 } |
| 159 | 151 |
| 160 } // namespace extensions | 152 } // namespace extensions |
| OLD | NEW |