Chromium Code Reviews| Index: mojo/bindings/js/file_io.cc |
| diff --git a/mojo/bindings/js/file_io.cc b/mojo/bindings/js/file_io.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9439c44929cc787fd316256f8810529dc07cd779 |
| --- /dev/null |
| +++ b/mojo/bindings/js/file_io.cc |
| @@ -0,0 +1,71 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/bindings/js/file_io.h" |
| + |
| +#include <iostream> |
| + |
| +#include "base/strings/string_util.h" |
| +#include "gin/arguments.h" |
| +#include "gin/converter.h" |
| +#include "gin/object_template_builder.h" |
| +#include "gin/per_isolate_data.h" |
| +#include "gin/public/wrapper_info.h" |
| +#include "mojo/public/cpp/test_support/test_support.h" |
|
abarth-chromium
2014/07/22 03:51:00
This module shouldn't need anything from Mojo. I
hansmuller
2014/07/22 15:49:06
I'm loading mojo data files from a source relative
|
| + |
| +using v8::ObjectTemplate; |
| + |
| +namespace mojo { |
| +namespace js { |
| + |
| +namespace { |
| + |
| +v8::Handle<v8::Value> ReadFileAsString(gin::Arguments* args) { |
| + std::string filename; |
| + if (!args->GetNext(&filename)) |
| + return v8::Null(args->isolate()); |
| + |
| + FILE* fp = test::OpenSourceRootRelativeFile(filename.c_str()); |
| + if (!fp) |
| + return v8::Null(args->isolate()); |
| + |
| + fseek(fp, 0, SEEK_END); |
| + size_t size = static_cast<size_t>(ftell(fp)); |
| + if (size == 0) { |
| + fclose(fp); |
| + return v8::Null(args->isolate()); |
| + } |
| + |
| + fseek(fp, 0, SEEK_SET); |
| + std::string result; |
| + result.resize(size); |
| + size_t size_read = fread(&result.at(0), 1, size, fp); |
|
abarth-chromium
2014/07/22 03:51:00
I thought there was a function in base that did al
hansmuller
2014/07/22 15:49:06
You are correct: base::ReadFileToString(). I'll us
|
| + fclose(fp); |
| + if (size != size_read) |
| + return v8::Null(args->isolate()); |
| + |
| + return gin::Converter<std::string>::ToV8(args->isolate(), result); |
| +} |
| + |
| + gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; |
|
abarth-chromium
2014/07/22 03:51:00
Bad indent
hansmuller
2014/07/22 15:49:06
Will fix.
|
| + |
| +} // namespace |
| + |
| +const char FileIO::kModuleName[] = "mojo/public/js/bindings/fileio"; |
|
abarth-chromium
2014/07/22 03:51:00
This path should match the location of the C++ cod
hansmuller
2014/07/22 15:49:06
OK. And if I'm reading you correctly, the FileIO m
|
| + |
| +v8::Local<v8::Value> FileIO::GetModule(v8::Isolate* isolate) { |
| + gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
| + v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info); |
| + if (templ.IsEmpty()) { |
| + templ = gin::ObjectTemplateBuilder(isolate) |
| + .SetMethod("readSourceRelativeFileAsString", ReadFileAsString) |
| + .Build(); |
| + data->SetObjectTemplate(&g_wrapper_info, templ); |
| + } |
| + return templ->NewInstance(); |
| +} |
| + |
| +} // namespace js |
| +} // namespace mojo |
| + |