17026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant// Copyright (c) 2012 The Chromium Authors. All rights reserved.
27026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant// Use of this source code is governed by a BSD-style license that can be
37026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant// found in the LICENSE file.
47026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant#include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h"
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant
77026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include <string>
87026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant
97026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "base/basictypes.h"
107026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "base/logging.h"
117026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "extensions/renderer/script_context.h"
127026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "third_party/WebKit/public/platform/WebString.h"
137026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "third_party/WebKit/public/web/WebDOMFileSystem.h"
147026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "third_party/WebKit/public/web/WebLocalFrame.h"
157026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant#include "third_party/WebKit/public/web/WebScriptBindings.h"
167026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant
177026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnantnamespace extensions {
187026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant
197026a17a482a9f7fdd9fc41aead6a167167efb1bHoward HinnantFileBrowserHandlerCustomBindings::FileBrowserHandlerCustomBindings(
207026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant    ScriptContext* context)
217026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant    : ObjectBackedNativeHandler(context) {
227026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant  RouteFunction(
237026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant      "GetExternalFileEntry",
247026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant      base::Bind(&FileBrowserHandlerCustomBindings::GetExternalFileEntry,
257026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant                 base::Unretained(this)));
267026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant  RouteFunction("GetEntryURL",
277026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant                base::Bind(&FileBrowserHandlerCustomBindings::GetEntryURL,
287026a17a482a9f7fdd9fc41aead6a167167efb1bHoward Hinnant                           base::Unretained(this)));
29}
30
31void FileBrowserHandlerCustomBindings::GetExternalFileEntry(
32    const v8::FunctionCallbackInfo<v8::Value>& args) {
33  // TODO(zelidrag): Make this magic work on other platforms when file browser
34  // matures enough on ChromeOS.
35#if defined(OS_CHROMEOS)
36    CHECK(args.Length() == 1);
37    CHECK(args[0]->IsObject());
38    v8::Local<v8::Object> file_def = args[0]->ToObject();
39    std::string file_system_name(
40        *v8::String::Utf8Value(file_def->Get(
41            v8::String::NewFromUtf8(args.GetIsolate(), "fileSystemName"))));
42    GURL file_system_root(
43        *v8::String::Utf8Value(file_def->Get(
44            v8::String::NewFromUtf8(args.GetIsolate(), "fileSystemRoot"))));
45    std::string file_full_path(
46        *v8::String::Utf8Value(file_def->Get(
47            v8::String::NewFromUtf8(args.GetIsolate(), "fileFullPath"))));
48    bool is_directory = file_def->Get(v8::String::NewFromUtf8(
49        args.GetIsolate(), "fileIsDirectory"))->ToBoolean()->Value();
50    blink::WebDOMFileSystem::EntryType entry_type =
51        is_directory ? blink::WebDOMFileSystem::EntryTypeDirectory
52                     : blink::WebDOMFileSystem::EntryTypeFile;
53    blink::WebLocalFrame* webframe =
54        blink::WebLocalFrame::frameForContext(context()->v8_context());
55    args.GetReturnValue().Set(
56        blink::WebDOMFileSystem::create(
57            webframe,
58            blink::WebFileSystemTypeExternal,
59            blink::WebString::fromUTF8(file_system_name),
60            file_system_root)
61            .createV8Entry(blink::WebString::fromUTF8(file_full_path),
62                           entry_type,
63                           args.Holder(),
64                           args.GetIsolate()));
65#endif
66}
67
68void FileBrowserHandlerCustomBindings::GetEntryURL(
69    const v8::FunctionCallbackInfo<v8::Value>& args) {
70  CHECK(args.Length() == 1);
71  CHECK(args[0]->IsObject());
72  const blink::WebURL& url =
73      blink::WebDOMFileSystem::createFileSystemURL(args[0]);
74  args.GetReturnValue().Set(
75      blink::WebScriptBindings::toV8String(url.string(), args.GetIsolate()));
76}
77
78}  // namespace extensions
79