Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: gin/test/file.cc

Issue 419673004: gin test - add simple file access for JS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Convert to/from FilePath strings with UTF8Unsafe methods Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/test/file.h ('k') | gin/test/file_runner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 "gin/test/file.h"
6
7 #include <iostream>
8
9 #include "base/bind.h"
10 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_path.h"
13 #include "base/path_service.h"
14 #include "gin/arguments.h"
15 #include "gin/converter.h"
16 #include "gin/object_template_builder.h"
17 #include "gin/per_isolate_data.h"
18 #include "gin/public/wrapper_info.h"
19
20 using v8::ObjectTemplate;
21
22 namespace gin {
23
24 namespace {
25
26 v8::Handle<v8::Value> ReadFileToString(gin::Arguments* args) {
27 std::string filename;
28 if (!args->GetNext(&filename))
29 return v8::Null(args->isolate());
30
31 const base::FilePath& path = base::FilePath::FromUTF8Unsafe(filename);
32 std::string contents;
33 if (!ReadFileToString(path, &contents))
34 return v8::Null(args->isolate());
35
36 return gin::Converter<std::string>::ToV8(args->isolate(), contents);
abarth-chromium 2014/07/27 03:26:47 You should be able to make the return type of this
hansmuller 2014/07/28 16:44:59 I'm returning JS null when no filename is specifie
37 }
38
39 v8::Handle<v8::Value> GetSourceRootDirectory(v8::Isolate* isolate) {
40 base::FilePath path;
41 if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
42 return v8::Null(isolate);
43 return gin::Converter<std::string>::ToV8(isolate, path.AsUTF8Unsafe());
abarth-chromium 2014/07/27 03:26:47 Ditto
44 }
45
46 v8::Handle<v8::Value> GetFilesInDirectory(gin::Arguments* args) {
47 std::string filename;
48 if (!args->GetNext(&filename))
49 return v8::Null(args->isolate());
50
51 const base::FilePath& path = base::FilePath::FromUTF8Unsafe(filename);
52 std::vector<std::string> names;
53 base::FileEnumerator e(path, false, base::FileEnumerator::FILES);
54 for (base::FilePath name = e.Next(); !name.empty(); name = e.Next()) {
55 names.push_back(name.BaseName().AsUTF8Unsafe());
56 }
57
58 return gin::Converter<std::vector<std::string>>::ToV8(args->isolate(), names);
abarth-chromium 2014/07/27 03:26:47 Ditto
59 }
60
61 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
62
63 } // namespace
64
65 const char File::kModuleName[] = "file";
66
67 v8::Local<v8::Value> File::GetModule(v8::Isolate* isolate) {
68 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
69 v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info);
70 if (templ.IsEmpty()) {
71 templ = gin::ObjectTemplateBuilder(isolate)
72 .SetMethod("readFileToString", ReadFileToString)
73 .SetMethod("getFilesInDirectory", GetFilesInDirectory)
74 .SetMethod("getSourceRootDirectory",
75 base::Bind(GetSourceRootDirectory, isolate))
abarth-chromium 2014/07/27 03:26:47 There's no reason to base::Bind here. gin::Argume
hansmuller 2014/07/28 16:44:59 OK.
76 .Build();
77 data->SetObjectTemplate(&g_wrapper_info, templ);
78 }
79 return templ->NewInstance();
80 }
81
82 } // namespace gin
OLDNEW
« no previous file with comments | « gin/test/file.h ('k') | gin/test/file_runner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698