- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / extensions / file_system_natives.cc
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 "grit/renderer_resources.h"
16 #include "third_party/WebKit/public/platform/WebFileSystem.h"
17 #include "third_party/WebKit/public/platform/WebFileSystemType.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/web/WebFrame.h"
20 #include "webkit/common/fileapi/file_system_types.h"
21 #include "webkit/common/fileapi/file_system_util.h"
22
23 namespace extensions {
24
25 FileSystemNatives::FileSystemNatives(ChromeV8Context* context)
26     : ObjectBackedNativeHandler(context) {
27   RouteFunction("GetFileEntry",
28       base::Bind(&FileSystemNatives::GetFileEntry, base::Unretained(this)));
29   RouteFunction("GetIsolatedFileSystem",
30       base::Bind(&FileSystemNatives::GetIsolatedFileSystem,
31                  base::Unretained(this)));
32   RouteFunction("CrackIsolatedFileSystemName",
33       base::Bind(&FileSystemNatives::CrackIsolatedFileSystemName,
34                  base::Unretained(this)));
35 }
36
37 void FileSystemNatives::GetIsolatedFileSystem(
38     const v8::FunctionCallbackInfo<v8::Value>& args) {
39   DCHECK(args.Length() == 1 || args.Length() == 2);
40   DCHECK(args[0]->IsString());
41   std::string file_system_id(*v8::String::Utf8Value(args[0]));
42   WebKit::WebFrame* webframe =
43       WebKit::WebFrame::frameForContext(context()->v8_context());
44   DCHECK(webframe);
45
46   GURL context_url =
47       extensions::UserScriptSlave::GetDataSourceURLForFrame(webframe);
48   CHECK(context_url.SchemeIs(extensions::kExtensionScheme));
49
50   std::string name(fileapi::GetIsolatedFileSystemName(context_url.GetOrigin(),
51                                                       file_system_id));
52
53   // The optional second argument is the subfolder within the isolated file
54   // system at which to root the DOMFileSystem we're returning to the caller.
55   std::string optional_root_name;
56   if (args.Length() == 2) {
57     DCHECK(args[1]->IsString());
58     optional_root_name = *v8::String::Utf8Value(args[1]);
59   }
60
61   std::string root(fileapi::GetIsolatedFileSystemRootURIString(
62       context_url.GetOrigin(),
63       file_system_id,
64       optional_root_name));
65
66   args.GetReturnValue().Set(webframe->createFileSystem(
67       WebKit::WebFileSystemTypeIsolated,
68       WebKit::WebString::fromUTF8(name),
69       WebKit::WebString::fromUTF8(root)));
70 }
71
72 void FileSystemNatives::GetFileEntry(
73     const v8::FunctionCallbackInfo<v8::Value>& args) {
74   DCHECK(args.Length() == 5);
75   DCHECK(args[0]->IsString());
76   std::string type_string = *v8::String::Utf8Value(args[0]->ToString());
77   WebKit::WebFileSystemType type;
78   bool is_valid_type = fileapi::GetFileSystemPublicType(type_string, &type);
79   DCHECK(is_valid_type);
80   if (is_valid_type == false) {
81     return;
82   }
83
84   DCHECK(args[1]->IsString());
85   DCHECK(args[2]->IsString());
86   DCHECK(args[3]->IsString());
87   std::string file_system_name(*v8::String::Utf8Value(args[1]->ToString()));
88   std::string file_system_root_url(*v8::String::Utf8Value(args[2]->ToString()));
89   std::string file_path_string(*v8::String::Utf8Value(args[3]->ToString()));
90   base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string);
91   DCHECK(fileapi::VirtualPath::IsAbsolute(file_path.value()));
92
93   DCHECK(args[4]->IsBoolean());
94   bool is_directory = args[4]->BooleanValue();
95
96   WebKit::WebFrame* webframe =
97       WebKit::WebFrame::frameForContext(context()->v8_context());
98   DCHECK(webframe);
99   args.GetReturnValue().Set(webframe->createFileEntry(
100       type,
101       WebKit::WebString::fromUTF8(file_system_name),
102       WebKit::WebString::fromUTF8(file_system_root_url),
103       WebKit::WebString::fromUTF8(file_path_string),
104       is_directory));
105 }
106
107 void FileSystemNatives::CrackIsolatedFileSystemName(
108     const v8::FunctionCallbackInfo<v8::Value>& args) {
109   DCHECK_EQ(args.Length(), 1);
110   DCHECK(args[0]->IsString());
111   std::string filesystem_name = *v8::String::Utf8Value(args[0]->ToString());
112   std::string filesystem_id;
113   if (!fileapi::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id))
114     return;
115
116   args.GetReturnValue().Set(
117       v8::String::New(filesystem_id.c_str(), filesystem_id.size()));
118 }
119
120 }  // namespace extensions