78a4e276571ab8aefa9b937c6578ff284ffba87c
[platform/framework/web/crosswalk.git] / src / chrome / renderer / extensions / media_galleries_custom_bindings.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/media_galleries_custom_bindings.h"
6
7 #include <string>
8
9 #include "chrome/common/extensions/extension_constants.h"
10 #include "third_party/WebKit/public/web/WebDOMFileSystem.h"
11 #include "third_party/WebKit/public/web/WebDocument.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "v8/include/v8.h"
14 #include "webkit/common/fileapi/file_system_util.h"
15
16 namespace extensions {
17
18 namespace {
19
20 // FileSystemObject GetMediaFileSystem(string file_system_url): construct
21 // a file system object from a file system url.
22 void GetMediaFileSystemObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
23   CHECK_EQ(1, args.Length());
24   CHECK(args[0]->IsString());
25
26   std::string fs_mount(*v8::String::Utf8Value(args[0]));
27   CHECK(!fs_mount.empty());
28
29   blink::WebFrame* webframe = blink::WebFrame::frameForCurrentContext();
30   const GURL origin = GURL(webframe->document().securityOrigin().toString());
31   std::string fs_name =
32       fileapi::GetFileSystemName(origin, fileapi::kFileSystemTypeExternal);
33   fs_name.append("_");
34   fs_name.append(fs_mount);
35   const GURL root_url(
36       fileapi::GetExternalFileSystemRootURIString(origin, fs_mount));
37   args.GetReturnValue().Set(
38       blink::WebDOMFileSystem::create(webframe,
39                                       blink::WebFileSystemTypeExternal,
40                                       blink::WebString::fromUTF8(fs_name),
41                                       root_url).toV8Value());
42 }
43
44 }  // namespace
45
46 MediaGalleriesCustomBindings::MediaGalleriesCustomBindings(
47     Dispatcher* dispatcher, ChromeV8Context* context)
48     : ChromeV8Extension(dispatcher, context) {
49   RouteFunction("GetMediaFileSystemObject",
50                 base::Bind(&GetMediaFileSystemObject));
51 }
52
53 }  // namespace extensions