Permalink
Browse files

add fs api

  • Loading branch information...
1 parent b3ca906 commit 1da3af1f22bee19038170f5c80052a993fef87a2 yong z committed Feb 1, 2012
View
@@ -2,3 +2,5 @@
*.suo
*.opensdf
*.vcxproj.user
+*.log
+*.pdb
@@ -16,6 +16,7 @@
#include <shellapi.h>
#include "ext/os.h"
+#include "ext/fs.h"
#define MAX_LOADSTRING 100
#define MAX_URL_LENGTH 255
@@ -302,6 +303,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
info.SetAsChild(hWnd, rect);
OS::Initialize();
+ FS::Initialize();
std::string working_dir(szWorkingDir);
std::replace(working_dir.begin(), working_dir.end(), '\\', '/' );
@@ -22,15 +22,6 @@ bool ClientHandler::OnBeforePopup(CefRefPtr<CefBrowser> parentBrowser,
{
REQUIRE_UI_THREAD();
-#ifdef TEST_REDIRECT_POPUP_URLS
- std::string urlStr = url;
- if(urlStr.find("chrome-devtools:") == std::string::npos) {
- // Show all popup windows excluding DevTools in the current window.
- windowInfo.m_dwStyle &= ~WS_VISIBLE;
- client = new ClientPopupHandler(m_Browser);
- }
-#endif // TEST_REDIRECT_POPUP_URLS
-
return false;
}
View
@@ -0,0 +1,133 @@
+#include "fs.h"
+#include "include/cef.h"
+#include "include/cef_runnable.h"
+
+#include <io.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sstream>
+
+CefRefPtr<CefV8Value> fs_open(const CefV8ValueList& arguments)
+{
+ if (arguments.size() != 2)
+ return CefV8Value::CreateNull();
+
+ const CefString& filename = arguments[0]->GetStringValue();
+ int mode = _O_RDWR;
+
+ int fd = _wopen(filename.c_str(), mode);
+
+ return CefV8Value::CreateInt(fd);
+}
+
+CefRefPtr<CefV8Value> fs_close(const CefV8ValueList& arguments)
+{
+ if (arguments.size() != 1)
+ return CefV8Value::CreateNull();
+
+ int fd = arguments[0]->GetIntValue();
+
+ int e = _close(fd);
+
+ return CefV8Value::CreateInt(e);
+}
+
+CefRefPtr<CefV8Value> fs_readFile(const CefV8ValueList& arguments)
+{
+ if (arguments.size() != 2)
+ return CefV8Value::CreateNull();
+
+ if (!(arguments[0]->IsString())) {
+ return CefV8Value::CreateNull();
+ }
+
+ if (!(arguments[1]->IsFunction())) {
+ return CefV8Value::CreateNull();
+ }
+
+ CefString path = arguments[0]->GetStringValue();
+ const wchar_t* path_s = path.c_str();
+ FILE *f = _wfopen(path_s, L"r" );
+ if (NULL == f) {
+ return CefV8Value::CreateNull();
+ }
+
+ std::stringstream str;
+ char buff[1024] = {0};
+ int n;
+ while ((n = fread(buff, 1, sizeof(buff) - 1, f)) > 0) {
+ buff[n] = '\0';
+ str << buff;
+ }
+ fclose(f);
+
+ CefRefPtr<CefV8Value> callback_func = arguments[1];
+ CefRefPtr<CefV8Value> retval;
+ CefRefPtr<CefV8Exception> exception;
+ CefV8ValueList args;
+ args.push_back(CefV8Value::CreateNull());
+ args.push_back(CefV8Value::CreateString(str.str()));
+ callback_func->ExecuteFunctionWithContext(CefV8Context::GetCurrentContext(), NULL, args, retval, exception, true);
+
+ return CefV8Value::CreateNull();
+}
+
+class MyV8Handler : public CefV8Handler
+{
+public:
+ MyV8Handler() {}
+
+ virtual bool Execute(const CefString& name,
+ CefRefPtr<CefV8Value> object,
+ const CefV8ValueList& arguments,
+ CefRefPtr<CefV8Value>& retval,
+ CefString& exception) OVERRIDE
+ {
+ if (name == "__fs_open") {
+ retval = fs_open(arguments);
+ return true;
+ } else if (name == "__fs_close") {
+ retval = fs_close(arguments);
+ return true;
+ } else if (name == "__fs_readFile") {
+ retval = fs_readFile(arguments);
+ return true;
+ }
+
+ // Function does not exist.
+ return false;
+ }
+
+ // Provide the reference counting implementation for this class.
+ IMPLEMENT_REFCOUNTING(MyV8Handler);
+};
+
+void FS::Initialize()
+{
+ // Define the extension contents.
+ std::string extensionCode =
+ "var fs;"
+ "if (!fs)"
+ " fs = {};"
+ "(function() {"
+ " fs.open = function(filename, mode) {"
+ " native function __fs_open(filename, mode);"
+ " return __fs_open(filename, mode);"
+ " };"
+ " fs.close = function(fd) {"
+ " native function __fs_close(fd);"
+ " return __fs_close(fd);"
+ " };"
+ " fs.readFile = function(filename, callback) {"
+ " native function __fs_readFile(filename, callback);"
+ " return __fs_readFile(filename, callback);"
+ " };"
+ "})();";
+
+ // Create an instance of my CefV8Handler object.
+ CefRefPtr<CefV8Handler> handler = new MyV8Handler();
+
+ // Register the extension.
+ CefRegisterExtension("v8/fs", extensionCode, handler);
+}
View
@@ -0,0 +1,9 @@
+#ifndef _H54D_FS_H
+#define _H54D_FS_H
+
+class FS {
+public:
+ static void Initialize ();
+};
+
+#endif
View
@@ -1,49 +1,56 @@
-#include "os.h"
+#include "os.h"
#include "include/cef.h"
-#include "include/cef_runnable.h"
-
-class MyV8Handler : public CefV8Handler
-{
-public:
- MyV8Handler() {}
-
- virtual bool Execute(const CefString& name,
- CefRefPtr<CefV8Value> object,
- const CefV8ValueList& arguments,
- CefRefPtr<CefV8Value>& retval,
- CefString& exception) OVERRIDE
- {
- if (name == "myfunc") {
- // Return my string value.
- retval = CefV8Value::CreateString("My Value!");
- return true;
- }
-
- // Function does not exist.
- return false;
- }
-
- // Provide the reference counting implementation for this class.
- IMPLEMENT_REFCOUNTING(MyV8Handler);
-};
-
-void OS::Initialize()
-{
- // Define the extension contents.
- std::string extensionCode =
- "var test;"
- "if (!test)"
- " test = {};"
- "(function() {"
- " test.myfunc = function() {"
- " native function myfunc();"
- " return myfunc();"
- " };"
- "})();";
-
- // Create an instance of my CefV8Handler object.
- CefRefPtr<CefV8Handler> handler = new MyV8Handler();
-
- // Register the extension.
- CefRegisterExtension("v8/test", extensionCode, handler);
+#include "include/cef_runnable.h"
+
+extern CefRefPtr<CefV8Value> getNetworkInterface();
+
+class MyV8Handler : public CefV8Handler
+{
+public:
+ MyV8Handler() {}
+
+ virtual bool Execute(const CefString& name,
+ CefRefPtr<CefV8Value> object,
+ const CefV8ValueList& arguments,
+ CefRefPtr<CefV8Value>& retval,
+ CefString& exception) OVERRIDE
+ {
+ if (name == "networkInterfaces") {
+ // Return my string value.
+ retval = getNetworkInterface();
+ return true;
+ }
+
+ // Function does not exist.
+ return false;
+ }
+
+ // Provide the reference counting implementation for this class.
+ IMPLEMENT_REFCOUNTING(MyV8Handler);
+};
+
+void OS::Initialize()
+{
+ // Define the extension contents.
+ std::string extensionCode =
+ "var os;"
+ "if (!os)"
+ " os = {};"
+ "(function() {"
+#ifdef WIN32
+ " os.type = function() {return 'windows'};"
+#elif __APPLE__
+ " os.type = function() {return 'mac'};"
+#endif
+ " os.networkInterfaces = function() {"
+ " native function networkInterfaces();"
+ " return networkInterfaces();"
+ " };"
+ "})();";
+
+ // Create an instance of my CefV8Handler object.
+ CefRefPtr<CefV8Handler> handler = new MyV8Handler();
+
+ // Register the extension.
+ CefRegisterExtension("v8/os", extensionCode, handler);
}
Oops, something went wrong.

0 comments on commit 1da3af1

Please sign in to comment.