Added getAppData function to the app api

This commit is contained in:
Corne Dorrestijn 2014-08-12 17:01:56 +02:00
parent 66ef1a067d
commit be6599807d
2 changed files with 26 additions and 1 deletions

View file

@ -8,12 +8,19 @@
#include "base/values.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "atom/browser/browser.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "atom/common/node_includes.h"
#if defined(OS_LINUX)
#include "base/nix/xdg_util.h"
#endif
using atom::Browser;
namespace atom {
@ -60,6 +67,22 @@ void App::OnFinishLaunching() {
Emit("ready");
}
std::string App::GetDataPath() {
base::FilePath path;
#if defined(OS_LINUX)
scoped_ptr<base::Environment> env(base::Environment::Create());
path = base::nix::GetXDGDirectory(env.get(),
base::nix::kXdgConfigHomeEnvVar,
base::nix::kDotConfigDir);
#else
CHECK(PathService::Get(base::DIR_APP_DATA, &path));
#endif
base::FilePath dataPath = path.Append(base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName()));
return dataPath.value();
}
mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
Browser* browser = Browser::Get();
@ -75,7 +98,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
.SetMethod("getName", base::Bind(&Browser::GetName,
base::Unretained(browser)))
.SetMethod("setName", base::Bind(&Browser::SetName,
base::Unretained(browser)));
base::Unretained(browser)))
.SetMethod("getDataPath", &App::GetDataPath);
}
// static

View file

@ -34,6 +34,7 @@ class App : public mate::EventEmitter,
virtual void OnActivateWithNoOpenWindows() OVERRIDE;
virtual void OnWillFinishLaunching() OVERRIDE;
virtual void OnFinishLaunching() OVERRIDE;
virtual std::string GetDataPath();
// mate::Wrappable implementations:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(