Home | History | Annotate | Download | only in extensions
      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 "base/files/file_path.h"
     10 #include "base/strings/stringprintf.h"
     11 #include "chrome/common/extensions/extension_constants.h"
     12 #include "third_party/WebKit/public/web/WebDocument.h"
     13 #include "third_party/WebKit/public/web/WebFrame.h"
     14 #include "v8/include/v8.h"
     15 #include "webkit/common/fileapi/file_system_util.h"
     16 
     17 namespace extensions {
     18 
     19 MediaGalleriesCustomBindings::MediaGalleriesCustomBindings(
     20     Dispatcher* dispatcher, ChromeV8Context* context)
     21     : ChromeV8Extension(dispatcher, context) {
     22   RouteFunction(
     23       "GetMediaFileSystemObject",
     24       base::Bind(&MediaGalleriesCustomBindings::GetMediaFileSystemObject,
     25                  base::Unretained(this)));
     26 }
     27 
     28 void MediaGalleriesCustomBindings::GetMediaFileSystemObject(
     29     const v8::FunctionCallbackInfo<v8::Value>& args) {
     30   if (args.Length() != 1) {
     31     NOTREACHED();
     32     return;
     33   }
     34   if (!args[0]->IsString()) {
     35     NOTREACHED();
     36     return;
     37   }
     38 
     39   std::string fsid(*v8::String::Utf8Value(args[0]));
     40   if (fsid.empty()) {
     41     NOTREACHED();
     42     return;
     43   }
     44 
     45   blink::WebFrame* webframe = blink::WebFrame::frameForCurrentContext();
     46   const GURL origin = GURL(webframe->document().securityOrigin().toString());
     47   const std::string fs_name = fileapi::GetIsolatedFileSystemName(origin, fsid);
     48   const std::string root_url =
     49       fileapi::GetIsolatedFileSystemRootURIString(
     50           origin, fsid, extension_misc::kMediaFileSystemPathPart);
     51   args.GetReturnValue().Set(
     52       webframe->createFileSystem(blink::WebFileSystemTypeIsolated,
     53                                  blink::WebString::fromUTF8(fs_name),
     54                                  blink::WebString::fromUTF8(root_url)));
     55 }
     56 
     57 }  // namespace extensions
     58