1 // Copyright (c) 2014 Intel Corporation. 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.
7 #include "xwalk/runtime/renderer/isolated_file_system.h"
9 #include "base/logging.h"
10 #include "content/public/renderer/render_view.h"
11 #include "content/public/renderer/v8_value_converter.h"
12 #include "storage/common/fileapi/file_system_types.h"
13 #include "storage/common/fileapi/file_system_util.h"
14 #include "third_party/WebKit/public/platform/WebFileSystem.h"
15 #include "third_party/WebKit/public/platform/WebFileSystemType.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/web/WebDataSource.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/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebLocalFrame.h"
22 #include "third_party/WebKit/public/web/WebView.h"
23 #include "v8/include/v8.h"
24 #include "xwalk/extensions/renderer/xwalk_module_system.h"
26 using content::RenderView;
27 using blink::WebFrame;
32 // This is the key used in the data object passed to our callbacks to store a
33 // pointer back to IsolatedFileSystem.
34 const char* kIsolatedFileSystemModule = "kIsolatedFileSystemModule";
39 namespace extensions {
41 void IsolatedFileSystem::GetIsolatedFileSystem(
42 const v8::FunctionCallbackInfo<v8::Value>& info) {
43 CHECK(info.Length() == 1 || info.Length() == 2);
44 CHECK(info[0]->IsString());
46 blink::WebLocalFrame* webframe =
47 blink::WebLocalFrame::frameForCurrentContext();
49 std::string file_system_id(*v8::String::Utf8Value(info[0]));
50 blink::WebDataSource* data_source = webframe->provisionalDataSource() ?
51 webframe->provisionalDataSource() : webframe->dataSource();
53 GURL context_url(data_source->request().url());
55 // In instrument test, context_url.GetOrigin() returns emtpy string.
56 // That causes app crash. So assign "file:///" as default value to
57 // origin to avoid this.
58 GURL origin("file:///");
59 if (!context_url.SchemeIs(url::kDataScheme) &&
60 !context_url.GetOrigin().is_empty()) {
61 origin = context_url.GetOrigin();
63 std::string name(storage::GetIsolatedFileSystemName(origin, file_system_id));
65 // The optional second argument is the subfolder within the isolated file
66 // system at which to root the DOMFileSystem we're returning to the caller.
67 std::string optional_root_name = "";
69 blink::WebURL root(GURL(storage::GetIsolatedFileSystemRootURIString(
72 optional_root_name)));
73 v8::Isolate* isolate = v8::Isolate::GetCurrent();
74 info.GetReturnValue().Set(blink::WebDOMFileSystem::create(webframe,
75 blink::WebFileSystemTypeIsolated,
76 blink::WebString::fromUTF8(name),
77 root).toV8Value(isolate->GetCurrentContext()->Global(), isolate));
80 IsolatedFileSystem::IsolatedFileSystem() {
81 v8::Isolate* isolate = v8::Isolate::GetCurrent();
82 v8::HandleScope handle_scope(isolate);
83 v8::Handle<v8::Object> function_data = v8::Object::New(isolate);
85 v8::String::NewFromUtf8(isolate, kIsolatedFileSystemModule),
86 v8::External::New(isolate, this));
88 // Register native function templates to object template here.
89 v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New();
92 "getIsolatedFileSystem",
93 v8::FunctionTemplate::New(isolate,
94 &IsolatedFileSystem::GetIsolatedFileSystem,
97 function_data_.Reset(isolate, function_data);
98 object_template_.Reset(isolate, object_template);
101 IsolatedFileSystem::~IsolatedFileSystem() {
102 object_template_.Reset();
103 function_data_.Reset();
106 v8::Handle<v8::Object> IsolatedFileSystem::NewInstance() {
107 v8::Isolate* isolate = v8::Isolate::GetCurrent();
108 v8::EscapableHandleScope handle_scope(isolate);
109 v8::Handle<v8::ObjectTemplate> object_template =
110 v8::Local<v8::ObjectTemplate>::New(isolate, object_template_);
111 return handle_scope.Escape(object_template->NewInstance());
114 } // namespace extensions