Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 "mojo/bindings/js/file_io.h" | |
| 6 | |
| 7 #include <iostream> | |
| 8 | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "gin/arguments.h" | |
| 11 #include "gin/converter.h" | |
| 12 #include "gin/object_template_builder.h" | |
| 13 #include "gin/per_isolate_data.h" | |
| 14 #include "gin/public/wrapper_info.h" | |
| 15 #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
| |
| 16 | |
| 17 using v8::ObjectTemplate; | |
| 18 | |
| 19 namespace mojo { | |
| 20 namespace js { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 v8::Handle<v8::Value> ReadFileAsString(gin::Arguments* args) { | |
| 25 std::string filename; | |
| 26 if (!args->GetNext(&filename)) | |
| 27 return v8::Null(args->isolate()); | |
| 28 | |
| 29 FILE* fp = test::OpenSourceRootRelativeFile(filename.c_str()); | |
| 30 if (!fp) | |
| 31 return v8::Null(args->isolate()); | |
| 32 | |
| 33 fseek(fp, 0, SEEK_END); | |
| 34 size_t size = static_cast<size_t>(ftell(fp)); | |
| 35 if (size == 0) { | |
| 36 fclose(fp); | |
| 37 return v8::Null(args->isolate()); | |
| 38 } | |
| 39 | |
| 40 fseek(fp, 0, SEEK_SET); | |
| 41 std::string result; | |
| 42 result.resize(size); | |
| 43 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
| |
| 44 fclose(fp); | |
| 45 if (size != size_read) | |
| 46 return v8::Null(args->isolate()); | |
| 47 | |
| 48 return gin::Converter<std::string>::ToV8(args->isolate(), result); | |
| 49 } | |
| 50 | |
| 51 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.
| |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 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
| |
| 56 | |
| 57 v8::Local<v8::Value> FileIO::GetModule(v8::Isolate* isolate) { | |
| 58 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | |
| 59 v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info); | |
| 60 if (templ.IsEmpty()) { | |
| 61 templ = gin::ObjectTemplateBuilder(isolate) | |
| 62 .SetMethod("readSourceRelativeFileAsString", ReadFileAsString) | |
| 63 .Build(); | |
| 64 data->SetObjectTemplate(&g_wrapper_info, templ); | |
| 65 } | |
| 66 return templ->NewInstance(); | |
| 67 } | |
| 68 | |
| 69 } // namespace js | |
| 70 } // namespace mojo | |
| 71 | |
| OLD | NEW |