Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
add file api for writeImportant
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Apr 13, 2017
1 parent 5ad6919 commit 2da2701
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 9 deletions.
14 changes: 8 additions & 6 deletions atom/browser/javascript_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/threading/thread_task_runner_handle.h"
#include "brave/browser/extensions/file_bindings.h"
#include "brave/browser/extensions/path_bindings.h"
#include "brave/common/extensions/shared_memory_bindings.h"
#include "content/public/common/content_switches.h"
Expand Down Expand Up @@ -149,10 +150,6 @@ JavascriptEnvironment::JavascriptEnvironment()
script_context_->module_system()->RegisterNativeHandler(
"path", std::unique_ptr<extensions::NativeHandler>(
new brave::PathBindings(script_context_.get(), &source_map_)));
script_context_->module_system()->RegisterNativeHandler(
"shared_memory", std::unique_ptr<extensions::NativeHandler>(
new extensions::SharedMemoryBindings(
script_context_.get())));
}

ModuleRegistry* registry = ModuleRegistry::From(context());
Expand All @@ -164,8 +161,13 @@ JavascriptEnvironment::JavascriptEnvironment()
v8::Local<v8::Object> muon = v8::Object::New(isolate_);
global->Set(v8::String::NewFromUtf8(isolate_, "muon"), muon);

v8::Local<v8::Object> api = extensions::SharedMemoryBindings::API(script_context_.get());
muon->Set(v8::String::NewFromUtf8(isolate_, "shared_memory"), api);
v8::Local<v8::Object> shared_memory =
extensions::SharedMemoryBindings::API(script_context_.get());
muon->Set(v8::String::NewFromUtf8(isolate_, "shared_memory"), shared_memory);

v8::Local<v8::Object> file =
extensions::FileBindings::API(script_context_.get());
muon->Set(v8::String::NewFromUtf8(isolate_, "file"), file);
}

JavascriptEnvironment::~JavascriptEnvironment() {
Expand Down
2 changes: 2 additions & 0 deletions brave/browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ static_library("extensions") {
]

sources = [
"file_bindings.cc",
"file_bindings.h",
"path_bindings.cc",
"path_bindings.h",
"api/guest_view/tab_view/tab_view_internal_api.cc",
Expand Down
133 changes: 133 additions & 0 deletions brave/browser/extensions/file_bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) 2017 The Brave 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 <utility>

#include "brave/browser/extensions/file_bindings.h"

#include "base/files/important_file_writer.h"
#include "base/memory/ptr_util.h"
#include "base/sequenced_task_runner.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "brave/common/converters/string16_converter.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/v8_helpers.h"
#include "v8/include/v8.h"

using content::BrowserThread;

namespace extensions {

namespace {

void PostWriteCallback(
const base::Callback<void(bool success)>& callback,
scoped_refptr<base::SequencedTaskRunner> reply_task_runner,
bool write_success) {
// We can't run |callback| on the current thread. Bounce back to
// the |reply_task_runner| which is the correct sequenced thread.
reply_task_runner->PostTask(FROM_HERE,
base::Bind(callback, write_success));
}

} // namespace

FileBindings::FileBindings(extensions::ScriptContext* context)
: extensions::ObjectBackedNativeHandler(context) {
RouteFunction("WriteImportantFile",
base::Bind(&FileBindings::WriteImportantFile, base::Unretained(this)));
}

FileBindings::~FileBindings() {
}

// static
v8::Local<v8::Object> FileBindings::API(
extensions::ScriptContext* context) {
context->module_system()->RegisterNativeHandler(
"muon_file", std::unique_ptr<extensions::NativeHandler>(
new FileBindings(context)));

v8::Local<v8::Object> file_api = v8::Object::New(context->isolate());
context->module_system()->SetNativeLazyField(
file_api, "writeImportant", "muon_file", "WriteImportantFile");

return file_api;
}

void FileBindings::WriteImportantFile(
const v8::FunctionCallbackInfo<v8::Value>& args) {
auto isolate = args.GetIsolate();

if (args.Length() < 2) {
isolate->ThrowException(v8::String::NewFromUtf8(
isolate,
"Wrong number of arguments: expected 2 and received " + args.Length()));
return;
}

base::FilePath::StringType path_name;
if (!args[0]->IsString() ||
!gin::Converter<base::FilePath::StringType>::FromV8(
isolate, args[0], &path_name)) {
isolate->ThrowException(v8::String::NewFromUtf8(
isolate, "`path` must be a string"));
return;
}
base::FilePath path(path_name);

if (!args[1]->IsString()) {
isolate->ThrowException(v8::String::NewFromUtf8(
isolate, "`data` must be a string"));
return;
}
std::string data = *v8::String::Utf8Value(args[1]);

std::unique_ptr<v8::Global<v8::Function>> callback;
if (args.Length() > 2 && args[2]->IsFunction()) {
callback.reset(
new v8::Global<v8::Function>(isolate, args[2].As<v8::Function>()));
}

auto task_runner = GetTaskRunnerForFile(path,
BrowserThread::GetBlockingPool());
base::ImportantFileWriter writer(path, task_runner);

writer.RegisterOnNextWriteCallbacks(
base::Closure(),
base::Bind(
&PostWriteCallback,
base::Bind(&FileBindings::RunCallback, AsWeakPtr(),
base::Passed(&callback)),
base::SequencedTaskRunnerHandle::Get()));

writer.WriteNow(base::MakeUnique<std::string>(data));
}

scoped_refptr<base::SequencedTaskRunner> FileBindings::GetTaskRunnerForFile(
const base::FilePath& filename,
base::SequencedWorkerPool* worker_pool) {
std::string token("muon-file-");
token.append(filename.AsUTF8Unsafe());
return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
worker_pool->GetNamedSequenceToken(token),
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
}

void FileBindings::RunCallback(
std::unique_ptr<v8::Global<v8::Function>> callback, bool success) {
if (!context()->is_valid() || !callback.get() || callback->IsEmpty())
return;

auto isolate = context()->isolate();
v8::HandleScope handle_scope(isolate);

v8::Local<v8::Value> callback_args[] = {
v8::Boolean::New(isolate, success) };
context()->SafeCallFunction(
v8::Local<v8::Function>::New(isolate, *callback), 1, callback_args);
}

} // namespace extensions
46 changes: 46 additions & 0 deletions brave/browser/extensions/file_bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2017 The Brave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BRAVE_BROWSER_EXTENSIONS_FILE_BINDINGS_H_
#define BRAVE_BROWSER_EXTENSIONS_FILE_BINDINGS_H_

#include <memory>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "extensions/renderer/object_backed_native_handler.h"
#include "v8/include/v8.h"

namespace base {
class FilePath;
class SequencedTaskRunner;
class SequencedWorkerPool;
}

namespace extensions {

class FileBindings : public extensions::ObjectBackedNativeHandler,
public base::SupportsWeakPtr<FileBindings> {
public:
explicit FileBindings(extensions::ScriptContext* context);
~FileBindings() override;

static v8::Local<v8::Object> API(extensions::ScriptContext* context);

private:
void WriteImportantFile(const v8::FunctionCallbackInfo<v8::Value>& args);
void RunCallback(
std::unique_ptr<v8::Global<v8::Function>> holder, bool success);

static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile(
const base::FilePath& filename,
base::SequencedWorkerPool* worker_pool);

DISALLOW_COPY_AND_ASSIGN(FileBindings);
};

} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_FILE_BINDINGS_H_
5 changes: 3 additions & 2 deletions brave/common/converters/string16_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ template<>
struct Converter<base::string16> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::string16& val) {
return MATE_STRING_NEW_FROM_UTF16(
isolate, reinterpret_cast<const uint16_t*>(val.data()), val.size());
return v8::String::NewFromTwoByte(
isolate, reinterpret_cast<const uint16_t*>(val.data()),
v8::String::kNormalString, val.size());
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
Expand Down
6 changes: 5 additions & 1 deletion brave/common/extensions/shared_memory_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,13 @@ SharedMemoryBindings::~SharedMemoryBindings() {
// static
v8::Local<v8::Object> SharedMemoryBindings::API(
extensions::ScriptContext* context) {
context->module_system()->RegisterNativeHandler(
"muon_shared_memory", std::unique_ptr<extensions::NativeHandler>(
new SharedMemoryBindings(context)));

v8::Local<v8::Object> shared_memory_api = v8::Object::New(context->isolate());
context->module_system()->SetNativeLazyField(
shared_memory_api, "create", "shared_memory", "Create");
shared_memory_api, "create", "muon_shared_memory", "Create");
return shared_memory_api;
}

Expand Down

1 comment on commit 2da2701

@bsclifton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Please # to comment.