This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auditors @bbondy @bsclifton
- Loading branch information
Showing
6 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2da2701
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++