Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / renderer / isolated_file_system.cc
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.
4
5 #include <string>
6
7 #include "xwalk/runtime/renderer/isolated_file_system.h"
8
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"
25
26 using content::RenderView;
27 using blink::WebFrame;
28 using blink::WebView;
29
30 namespace {
31
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";
35
36 }  // namespace
37
38 namespace xwalk {
39 namespace extensions {
40
41 void IsolatedFileSystem::GetIsolatedFileSystem(
42     const v8::FunctionCallbackInfo<v8::Value>& info) {
43   CHECK(info.Length() == 1 || info.Length() == 2);
44   CHECK(info[0]->IsString());
45
46   blink::WebLocalFrame* webframe =
47       blink::WebLocalFrame::frameForCurrentContext();
48   CHECK(webframe);
49   std::string file_system_id(*v8::String::Utf8Value(info[0]));
50   blink::WebDataSource* data_source = webframe->provisionalDataSource() ?
51       webframe->provisionalDataSource() : webframe->dataSource();
52   CHECK(data_source);
53   GURL context_url(data_source->request().url());
54
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();
62   }
63   std::string name(storage::GetIsolatedFileSystemName(origin, file_system_id));
64
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 = "";
68
69   blink::WebURL root(GURL(storage::GetIsolatedFileSystemRootURIString(
70       origin,
71       file_system_id,
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));
78 }
79
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);
84   function_data->Set(
85       v8::String::NewFromUtf8(isolate, kIsolatedFileSystemModule),
86       v8::External::New(isolate, this));
87
88   // Register native function templates to object template here.
89   v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New();
90   object_template->Set(
91       isolate,
92       "getIsolatedFileSystem",
93       v8::FunctionTemplate::New(isolate,
94                                 &IsolatedFileSystem::GetIsolatedFileSystem,
95                                 function_data));
96
97   function_data_.Reset(isolate, function_data);
98   object_template_.Reset(isolate, object_template);
99 }
100
101 IsolatedFileSystem::~IsolatedFileSystem() {
102   object_template_.Reset();
103   function_data_.Reset();
104 }
105
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());
112 }
113
114 }  // namespace extensions
115 }  // namespace xwalk