]> khamidou — GitHub mirrors - electron.git/commitdiff
Add app.setPath and app.getPath APIs
authorCheng Zhao <zcbenz@gmail.com>
Mon, 19 Jan 2015 01:52:15 +0000 (17:52 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 19 Jan 2015 05:43:19 +0000 (21:43 -0800)
They can be used to set/get any path defined in PathService

atom/browser/api/atom_api_app.cc
atom/browser/api/atom_api_app.h
atom/browser/api/lib/app.coffee
vendor/brightray

index 9a9ce028fdff0bc12806491e6f43b9771d4e3f0a..d5c68835298073a3e7830e6201a392c2673b3e32 100644 (file)
@@ -61,6 +61,16 @@ namespace api {
 
 namespace {
 
+// Return the path constant from string.
+int GetPathConstant(const std::string& name) {
+  if (name == "appData")
+    return brightray::DIR_APP_DATA;
+  else if (name == "userData")
+    return brightray::DIR_USER_DATA;
+  else
+    return -1;
+}
+
 class ResolveProxyHelper {
  public:
   ResolveProxyHelper(const GURL& url, App::ResolveProxyCallback callback)
@@ -139,16 +149,20 @@ void App::OnFinishLaunching() {
   Emit("ready");
 }
 
-void App::SetDataPath(const base::FilePath& path) {
-  PathService::Override(brightray::DIR_USER_DATA, path);
-}
-
-base::FilePath App::GetDataPath() {
+base::FilePath App::GetPath(const std::string& name) {
   base::FilePath path;
-  PathService::Get(brightray::DIR_USER_DATA, &path);
+  int key = GetPathConstant(name);
+  if (key >= 0)
+    PathService::Get(key, &path);
   return path;
 }
 
+void App::SetPath(const std::string& name, const base::FilePath& path) {
+  int key = GetPathConstant(name);
+  if (key >= 0)
+    PathService::Override(key, path);
+}
+
 void App::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
   new ResolveProxyHelper(url, callback);
 }
@@ -179,8 +193,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
       .SetMethod("setUserTasks",
                  base::Bind(&Browser::SetUserTasks, browser))
 #endif
-      .SetMethod("setDataPath", &App::SetDataPath)
-      .SetMethod("getDataPath", &App::GetDataPath)
+      .SetMethod("setPath", &App::SetPath)
+      .SetMethod("getPath", &App::GetPath)
       .SetMethod("resolveProxy", &App::ResolveProxy)
       .SetMethod("setDesktopName", &App::SetDesktopName);
 }
index 61d061569daecc2313012dd6cfb30d5b1adc062d..9b67cb5b717356570462b128c0f92e3557614125 100644 (file)
@@ -48,6 +48,10 @@ class App : public mate::EventEmitter,
       v8::Isolate* isolate) override;
 
  private:
+  // Get/Set the pre-defined path in PathService.
+  base::FilePath GetPath(const std::string& name);
+  void SetPath(const std::string& name, const base::FilePath& path);
+
   void SetDataPath(const base::FilePath& path);
   base::FilePath GetDataPath();
   void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
index 51fd66098ac012cb79c7c64c8588ae9ee6f3f47f..5ac0d719f4c9a45adbdddbbf5fe77bc153651847 100644 (file)
@@ -8,6 +8,12 @@ app.__proto__ = EventEmitter.prototype
 app.getHomeDir = ->
   process.env[if process.platform is 'win32' then 'USERPROFILE' else 'HOME']
 
+app.getDataPath = ->
+  app.getPath 'userData'
+
+app.setDataPath = (path) ->
+  app.setPath 'userData', path
+
 app.setApplicationMenu = (menu) ->
   require('menu').setApplicationMenu menu
 
index 395b3359cb5e8209fa740b9c678fd60709789106..8eb714dac77e3d3fd43fba76c835d29b838cc8be 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 395b3359cb5e8209fa740b9c678fd60709789106
+Subproject commit 8eb714dac77e3d3fd43fba76c835d29b838cc8be