From d666e73cfaadea221cbcf62e11c4e766745b8791 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 21 Feb 2012 15:39:35 -0800 Subject: [PATCH] Add list to $native --- Atom.xcodeproj/project.pbxproj | 34 +++++++++++++++--- Classes/AtomController.mm | 12 ++----- Classes/native_handler.h | 4 ++- Classes/native_handler.mm | 66 ++++++++++++++++++++++------------ src/stdlib/fs.coffee | 20 ++--------- 5 files changed, 81 insertions(+), 55 deletions(-) diff --git a/Atom.xcodeproj/project.pbxproj b/Atom.xcodeproj/project.pbxproj index 62a0fa898..248c7d514 100755 --- a/Atom.xcodeproj/project.pbxproj +++ b/Atom.xcodeproj/project.pbxproj @@ -22,10 +22,18 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + 041C615214F453610018D40C /* AtomController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04F9A6EE14F44F1B005E9C30 /* AtomController.mm */; }; + 041C615314F453630018D40C /* client_handler.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04F9A6F014F44F1B005E9C30 /* client_handler.mm */; }; + 041C615414F453640018D40C /* native_handler.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04F9A6F314F44F1B005E9C30 /* native_handler.mm */; }; + 041C615514F453670018D40C /* Atom.mm in Sources */ = {isa = PBXBuildFile; fileRef = 04F9A6EC14F44F1B005E9C30 /* Atom.mm */; }; + 041C615614F4536A0018D40C /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = 048A10D314F4522200C9E06B /* main.mm */; }; + 041C615914F453A50018D40C /* Atom.icns in Resources */ = {isa = PBXBuildFile; fileRef = 04F9A6F414F44F31005E9C30 /* Atom.icns */; }; + 041C615A14F453A90018D40C /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 041C615714F453970018D40C /* InfoPlist.strings */; }; + 041C615B14F453AC0018D40C /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 04F9A6F814F44F5A005E9C30 /* MainMenu.xib */; }; + 041C615C14F453AD0018D40C /* ClientWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 04F9A6F614F44F46005E9C30 /* ClientWindow.xib */; }; 04F9A6E714F44E80005E9C30 /* libcef.dylib in Copy Required Chrome Frameworks */ = {isa = PBXBuildFile; fileRef = 04F9A6E414F44D6D005E9C30 /* libcef.dylib */; }; 04F9A6E814F44E86005E9C30 /* ffmpegsumo.so in Copy Required Chrome Frameworks */ = {isa = PBXBuildFile; fileRef = 04F9A6E314F44D6D005E9C30 /* ffmpegsumo.so */; }; 04F9A6E914F44EB7005E9C30 /* libcef.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 04F9A6E414F44D6D005E9C30 /* libcef.dylib */; }; - 04F9A6EA14F44EC0005E9C30 /* ffmpegsumo.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 04F9A6E314F44D6D005E9C30 /* ffmpegsumo.so */; }; 05D08528E099C178B00B1260 /* browser_ctocpp.cc in Sources */ = {isa = PBXBuildFile; fileRef = 659F873974A982D7F1F3A259 /* browser_ctocpp.cc */; }; 0FBF704635D05A87B4859B52 /* write_handler_cpptoc.cc in Sources */ = {isa = PBXBuildFile; fileRef = 34CD2C0FE82A67E9EDAFB617 /* write_handler_cpptoc.cc */; }; 1061A7425FD8481F30411B53 /* scheme_handler_cpptoc.cc in Sources */ = {isa = PBXBuildFile; fileRef = B11864C37BCFB8E7EDD0AF23 /* scheme_handler_cpptoc.cc */; }; @@ -141,6 +149,8 @@ 0092CB55903D016B3004BB67 /* response_ctocpp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = response_ctocpp.h; sourceTree = ""; }; 02D02DBAFD451063CD5301BE /* cef_string_list.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cef_string_list.h; sourceTree = ""; }; 02EC892952094A790B013E64 /* cef_types_mac.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cef_types_mac.h; sourceTree = ""; }; + 041C615814F453970018D40C /* English */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = English; path = Classes/mac/English.lproj/InfoPlist.strings; sourceTree = SOURCE_ROOT; }; + 048A10D314F4522200C9E06B /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = main.mm; path = Classes/main.mm; sourceTree = SOURCE_ROOT; }; 04D99B5772F3FB9CDB60BD4F /* download_handler_cpptoc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = download_handler_cpptoc.h; sourceTree = ""; }; 04F9A6E314F44D6D005E9C30 /* ffmpegsumo.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.bundle"; name = ffmpegsumo.so; path = frameworks/ffmpegsumo.so; sourceTree = ""; }; 04F9A6E414F44D6D005E9C30 /* libcef.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libcef.dylib; path = frameworks/libcef.dylib; sourceTree = ""; }; @@ -150,7 +160,6 @@ 04F9A6EE14F44F1B005E9C30 /* AtomController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AtomController.mm; path = Classes/AtomController.mm; sourceTree = ""; }; 04F9A6EF14F44F1B005E9C30 /* client_handler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = client_handler.h; path = Classes/client_handler.h; sourceTree = ""; }; 04F9A6F014F44F1B005E9C30 /* client_handler.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = client_handler.mm; path = Classes/client_handler.mm; sourceTree = ""; }; - 04F9A6F114F44F1B005E9C30 /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = main.mm; path = ../../Classes/main.mm; sourceTree = ""; }; 04F9A6F214F44F1B005E9C30 /* native_handler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = native_handler.h; path = Classes/native_handler.h; sourceTree = ""; }; 04F9A6F314F44F1B005E9C30 /* native_handler.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = native_handler.mm; path = Classes/native_handler.mm; sourceTree = ""; }; 04F9A6F414F44F31005E9C30 /* Atom.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = Atom.icns; path = Classes/mac/Atom.icns; sourceTree = SOURCE_ROOT; }; @@ -317,7 +326,6 @@ files = ( 5343B749CFA415821812AC27 /* libcef_dll_wrapper.a in Frameworks */, CC51D6353D859FAFCBF315BA /* AppKit.framework in Frameworks */, - 04F9A6EA14F44EC0005E9C30 /* ffmpegsumo.so in Frameworks */, 04F9A6E914F44EB7005E9C30 /* libcef.dylib in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -411,11 +419,12 @@ 57338A9AD1F426E475F2A4CA /* Supporting Files */ = { isa = PBXGroup; children = ( + 048A10D314F4522200C9E06B /* main.mm */, 04F9A6F414F44F31005E9C30 /* Atom.icns */, - 04F9A6F114F44F1B005E9C30 /* main.mm */, 04F9A6F514F44F37005E9C30 /* Info.plist */, 04F9A6F614F44F46005E9C30 /* ClientWindow.xib */, 04F9A6F814F44F5A005E9C30 /* MainMenu.xib */, + 041C615714F453970018D40C /* InfoPlist.strings */, ); name = "Supporting Files"; path = cefclient/mac; @@ -658,6 +667,10 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 041C615C14F453AD0018D40C /* ClientWindow.xib in Resources */, + 041C615914F453A50018D40C /* Atom.icns in Resources */, + 041C615A14F453A90018D40C /* InfoPlist.strings in Resources */, + 041C615B14F453AC0018D40C /* MainMenu.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -717,6 +730,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 041C615514F453670018D40C /* Atom.mm in Sources */, + 041C615214F453610018D40C /* AtomController.mm in Sources */, + 041C615314F453630018D40C /* client_handler.mm in Sources */, + 041C615414F453640018D40C /* native_handler.mm in Sources */, + 041C615614F4536A0018D40C /* main.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -804,6 +822,14 @@ /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ + 041C615714F453970018D40C /* InfoPlist.strings */ = { + isa = PBXVariantGroup; + children = ( + 041C615814F453970018D40C /* English */, + ); + name = InfoPlist.strings; + sourceTree = ""; + }; 04F9A6F814F44F5A005E9C30 /* MainMenu.xib */ = { isa = PBXVariantGroup; children = ( diff --git a/Classes/AtomController.mm b/Classes/AtomController.mm index 9b6f74a8b..5a88c882b 100644 --- a/Classes/AtomController.mm +++ b/Classes/AtomController.mm @@ -72,16 +72,8 @@ CefRefPtr g_handler; global->SetValue("$atom", atom, V8_PROPERTY_ATTRIBUTE_NONE); // $native - CefRefPtr native = CefV8Value::CreateObject(NULL); - CefRefPtr nativeHandler = new NativeHandler(); - - CefRefPtr exists = CefV8Value::CreateFunction("exists", nativeHandler); - native->SetValue(exists->GetFunctionName(), exists, V8_PROPERTY_ATTRIBUTE_NONE); - - CefRefPtr read = CefV8Value::CreateFunction("read", nativeHandler); - native->SetValue(read->GetFunctionName(), read, V8_PROPERTY_ATTRIBUTE_NONE); - - global->SetValue("$native", native, V8_PROPERTY_ATTRIBUTE_NONE); + CefRefPtr nativeHandler = new NativeHandler(); + global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE); jsContext->Exit(); } diff --git a/Classes/native_handler.h b/Classes/native_handler.h index 959e3fc18..b94c62aba 100644 --- a/Classes/native_handler.h +++ b/Classes/native_handler.h @@ -5,12 +5,14 @@ class NativeHandler : public CefV8Handler { public: NativeHandler(); + CefRefPtr m_object; + virtual bool Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, CefRefPtr& retval, CefString& exception) OVERRIDE; - + // Provide the reference counting implementation for this class. IMPLEMENT_REFCOUNTING(NativeHandler); }; diff --git a/Classes/native_handler.mm b/Classes/native_handler.mm index 6387a7568..de19a1837 100644 --- a/Classes/native_handler.mm +++ b/Classes/native_handler.mm @@ -1,23 +1,24 @@ #import "native_handler.h" #import "include/cef.h" -@interface NSString (CEF) -+ (NSString *)fromCefV8Value:(const CefRefPtr&)value; -@end - -@implementation NSString (CEF) - -+ (NSString *)fromCefV8Value:(const CefRefPtr&)value { +NSString *stringFromCefV8Value(const CefRefPtr& value) { std::string cc_value = value->GetStringValue().ToString(); return [NSString stringWithUTF8String:cc_value.c_str()]; } -@end - -NativeHandler::NativeHandler() { +NativeHandler::NativeHandler() : CefV8Handler() { + m_object = CefV8Value::CreateObject(NULL); + const char *functionNames[] = {"exists", "read", "absolute", "list"}; + NSUInteger arrayLength = sizeof(functionNames) / sizeof(const char *); + for (NSUInteger i = 0; i < arrayLength; i++) { + const char *functionName = functionNames[i]; + CefRefPtr function = CefV8Value::CreateFunction(functionName, this); + m_object->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE); + } } + bool NativeHandler::Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, @@ -25,14 +26,14 @@ bool NativeHandler::Execute(const CefString& name, CefString& exception) { if (name == "exists") { - NSString *path = [NSString fromCefV8Value:arguments[0]]; + NSString *path = stringFromCefV8Value(arguments[0]); bool exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]; retval = CefV8Value::CreateBool(exists); return true; } else if (name == "read") { - NSString *path = [NSString fromCefV8Value:arguments[0]]; + NSString *path = stringFromCefV8Value(arguments[0]); NSError *error = nil; NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; @@ -47,17 +48,36 @@ bool NativeHandler::Execute(const CefString& name, return true; } else if (name == "absolute") { -// NSString *path = [NSString fromCefV8Value:arguments[0]]; -// -// path = [path stringByStandardizingPath]; -// if ([path characterAtIndex:0] == '/') { -// return path; -// } -// -// NSString *resolvedPath = [[NSFileManager defaultManager] currentDirectoryPath]; -// resolvedPath = [[resolvedPath stringByAppendingPathComponent:path] stringByStandardizingPath]; -// -// return resolvedPath; + NSString *path = stringFromCefV8Value(arguments[0]); + + path = [path stringByStandardizingPath]; + if ([path characterAtIndex:0] == '/') { + retval = CefV8Value::CreateString([path UTF8String]); + } + + return true; + } + else if (name == "list") { + NSString *path = stringFromCefV8Value(arguments[0]); + + NSFileManager *fm = [NSFileManager defaultManager]; + NSArray *relativePaths = [NSArray array]; + NSError *error = nil; + relativePaths = [fm contentsOfDirectoryAtPath:path error:&error]; + + if (error) { + exception = [[error localizedDescription] UTF8String]; + } + else { + retval = CefV8Value::CreateArray(); + for (NSUInteger i = 0; i < relativePaths.count; i++) { + NSString *relativePath = [relativePaths objectAtIndex:i]; + NSString *fullPath = [path stringByAppendingString:relativePath]; + retval->SetValue(i, CefV8Value::CreateString([fullPath UTF8String])); + } + } + + return true; } return false; diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index b5c2b551b..4870ebe14 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -9,7 +9,7 @@ module.exports = # Make the given path absolute by resolving it against the # current working directory. absolute: (path) -> - $atomController.absolute(path).toString() + $native.absolute(path) # Return the basename of the given path. That is the path with # any leading directory components removed. If specified, also @@ -25,7 +25,6 @@ module.exports = # Return the dirname of the given path. That is the path with any trailing # non-directory component removed. directory: (path) -> - absPath = @absolute(path) if @isDirectory(absPath) absPath.replace(/\/?$/, '/') else @@ -55,21 +54,9 @@ module.exports = # Returns an array with all the names of files contained # in the directory path. - list: (path, recursive) -> - path = @absolute path + list: (path) -> fm = OSX.NSFileManager.defaultManager - if recursive - paths = fm.subpathsAtPath path - else - paths = fm.contentsOfDirectoryAtPath_error path, null - _.map paths, (entry) -> "#{path}/#{entry}" - - # Return an array with all directories below (and including) - # the given path, as discovered by depth-first traversal. Entries - # are in lexically sorted order within directories. Symbolic links - # to directories are not traversed into. - listDirectoryTree: (path) -> - @list path, true + $native.list(path, recursive) # Remove a file at the given path. Throws an error if path is not a # file or a symbolic link to a file. @@ -84,7 +71,6 @@ module.exports = # Open, write, flush, and close a file, writing the given content. write: (path, content) -> str = OSX.NSString.stringWithUTF8String content - path = @absolute path enc = OSX.NSUTF8StringEncoding str.writeToFile_atomically_encoding_error path, true, enc, null