Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add fs api

  • Loading branch information...
commit 1da3af1f22bee19038170f5c80052a993fef87a2 1 parent b3ca906
yong z authored
View
2  .gitignore
@@ -2,3 +2,5 @@
*.suo
*.opensdf
*.vcxproj.user
+*.log
+*.pdb
View
2  cefclient/cefclient_win.cpp
@@ -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(), '\\', '/' );
View
9 cefclient/client_handler_win.cpp
@@ -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
133 cefclient/ext/fs.cpp
@@ -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
9 cefclient/ext/fs.h
@@ -0,0 +1,9 @@
+#ifndef _H54D_FS_H
+#define _H54D_FS_H
+
+class FS {
+public:
+ static void Initialize ();
+};
+
+#endif
View
101 cefclient/ext/os.cpp
@@ -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);
}
View
146 cefclient/ext/win.cpp
@@ -0,0 +1,146 @@
+#include "include/cef.h"
+#include "include/cef_runnable.h"
+
+#ifdef WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+
+CefRefPtr<CefV8Value> getNetworkInterface()
+{
+ return CefV8Value::CreateNull();
+}
+
+/*
+CefRefPtr<CefV8Value> getNetworkInterface()
+{
+ SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
+ if (sd == SOCKET_ERROR) {
+ return CefV8Value::CreateNull();
+ }
+
+ INTERFACE_INFO InterfaceList[20];
+ unsigned long nBytesReturned;
+ if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
+ sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) {
+ closesocket(sd);
+ return CefV8Value::CreateNull();
+ }
+
+ CefRefPtr<CefV8Value> arr = CefV8Value::CreateArray();
+
+ int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
+ for (int i = 0; i < nNumInterfaces; ++i) {
+ CefRefPtr<CefV8Value> v = CefV8Value::CreateObject(NULL);
+ arr->SetValue(i, v);
+
+ v->SetValue("name", CefV8Value::CreateString(pCurrAddresses->FriendlyName), V8_PROPERTY_ATTRIBUTE_NONE);
+
+ sockaddr_in *pAddress;
+ pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress);
+ v->SetValue("ip", CefV8Value::CreateString(inet_ntoa(pAddress->sin_addr)), V8_PROPERTY_ATTRIBUTE_NONE);
+
+ pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
+ v->SetValue("broadcast", CefV8Value::CreateString(inet_ntoa(pAddress->sin_addr)), V8_PROPERTY_ATTRIBUTE_NONE);
+
+ pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask);
+ v->SetValue("netmask", CefV8Value::CreateString(inet_ntoa(pAddress->sin_addr)), V8_PROPERTY_ATTRIBUTE_NONE);
+
+
+ cout << " Iface is ";
+ u_long nFlags = InterfaceList[i].iiFlags;
+ if (nFlags & IFF_UP) cout << "up";
+ else cout << "down";
+ if (nFlags & IFF_POINTTOPOINT) cout << ", is point-to-point";
+ if (nFlags & IFF_LOOPBACK) cout << ", is a loopback iface";
+ cout << ", and can do: ";
+ if (nFlags & IFF_BROADCAST) cout << "bcast ";
+ if (nFlags & IFF_MULTICAST) cout << "multicast ";
+ cout << endl;
+ }
+
+ return arr;
+}
+
+
+CefRefPtr<CefV8Value> getNetworkInterface()
+{
+ PIP_ADAPTER_ADDRESSES pAddresses = NULL;
+ DWORD dwRetVal = 0;
+ ULONG Iterations = 0;
+ ULONG outBufLen = WORKING_BUFFER_SIZE; // Allocate a 15 KB buffer to start with.
+ do {
+ pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
+ if (pAddresses == NULL) {
+ return CefV8Value::CreateNull();
+ }
+
+ dwRetVal = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
+ if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
+ FREE(pAddresses);
+ pAddresses = NULL;
+ } else {
+ break;
+ }
+
+ Iterations++;
+ } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES));
+
+ if (dwRetVal != NO_ERROR) {
+ if (pAddresses)
+ FREE(pAddresses);
+ return CefV8Value::CreateNull();
+ }
+
+ CefRefPtr<CefV8Value> arr = CefV8Value::CreateArray();
+ int index = 0;
+ PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses;
+ while (pCurrAddresses) {
+ CefRefPtr<CefV8Value> v = CefV8Value::CreateObject(NULL);
+
+ v->SetValue("name", CefV8Value::CreateString(pCurrAddresses->FriendlyName), V8_PROPERTY_ATTRIBUTE_NONE);
+
+ PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pCurrAddresses->FirstUnicastAddress;
+ if (pUnicast) {
+ char buff[100] = {0};
+ if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) {
+ sockaddr_in *sa_in = (sockaddr_in *)pUnicast->Address.lpSockaddr;
+ v->SetValue("ip", CefV8Value::CreateString(inet_ntop(AF_INET,&(sa_in->sin_addr),buff,sizeof(buff) - 1)), V8_PROPERTY_ATTRIBUTE_NONE);
+ }
+ else if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6)
+ {
+ sockaddr_in6 *sa_in6 = (sockaddr_in6 *)pUnicast->Address.lpSockaddr;
+ v->SetValue("ip", CefV8Value::CreateString(inet_ntop(AF_INET6,&(sa_in6->sin6_addr),buff,sizeof(buff) - 1)), V8_PROPERTY_ATTRIBUTE_NONE);
+ }
+ v->SetValue("netmask", CefV8Value::CreateString("up"), V8_PROPERTY_ATTRIBUTE_NONE);
+ }
+
+ PIP_ADAPTER_GATEWAY_ADDRESS_LH pGateway = pCurrAddresses->FirstGatewayAddress;
+ if (pGateway) {
+ v->SetValue("broadcast", CefV8Value::CreateString(inet_ntoa(*(struct in_addr*)pGateway->Address.lpSockaddr)), V8_PROPERTY_ATTRIBUTE_NONE);
+ }
+
+ switch (pCurrAddresses->OperStatus) {
+ case IfOperStatusUp:
+ v->SetValue("status", CefV8Value::CreateString("up"), V8_PROPERTY_ATTRIBUTE_NONE);
+ break;
+ case IfOperStatusDown:
+ v->SetValue("status", CefV8Value::CreateString("down"), V8_PROPERTY_ATTRIBUTE_NONE);
+ break;
+ default:
+ v->SetValue("status", CefV8Value::CreateString("other"), V8_PROPERTY_ATTRIBUTE_NONE);
+ }
+
+ pCurrAddresses = pCurrAddresses->Next;
+ arr->SetValue(index++, v);
+ }
+
+ if (pAddresses) {
+ FREE(pAddresses);
+ }
+
+ return arr;
+}
+
+*/
+
+#endif //win32
View
0  dist/debug.log
No changes.
View
20 h54d.sln
@@ -1,5 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 11.00
-# Visual Studio 2010
+# Visual C++ Express 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "h54d", "h54d.vcxproj", "{6617FED9-C5D4-4907-BF55-A90062A6683F}"
ProjectSection(ProjectDependencies) = postProject
{A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9} = {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}
@@ -9,28 +9,28 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcef_dll_wrapper", "libce
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Release|x64 = Release|x64
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
+ Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|x64.ActiveCfg = Release|x64
- {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|x64.Build.0 = Release|x64
{6617FED9-C5D4-4907-BF55-A90062A6683F}.Debug|Win32.ActiveCfg = Debug|Win32
{6617FED9-C5D4-4907-BF55-A90062A6683F}.Debug|Win32.Build.0 = Debug|Win32
{6617FED9-C5D4-4907-BF55-A90062A6683F}.Debug|x64.ActiveCfg = Debug|x64
{6617FED9-C5D4-4907-BF55-A90062A6683F}.Debug|x64.Build.0 = Debug|x64
- {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|Win32.ActiveCfg = Release|Win32
- {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|Win32.Build.0 = Release|Win32
- {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|x64.ActiveCfg = Release|x64
- {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|x64.Build.0 = Release|x64
+ {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|Win32.ActiveCfg = Debug|Win32
+ {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|Win32.Build.0 = Debug|Win32
+ {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|x64.ActiveCfg = Release|x64
+ {6617FED9-C5D4-4907-BF55-A90062A6683F}.Release|x64.Build.0 = Release|x64
{A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Debug|Win32.ActiveCfg = Debug|Win32
{A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Debug|Win32.Build.0 = Debug|Win32
{A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Debug|x64.ActiveCfg = Debug|x64
{A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Debug|x64.Build.0 = Debug|x64
- {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|Win32.ActiveCfg = Release|Win32
- {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|Win32.Build.0 = Release|Win32
+ {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|Win32.ActiveCfg = Debug|Win32
+ {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|Win32.Build.0 = Debug|Win32
+ {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|x64.ActiveCfg = Release|x64
+ {A9D6DC71-C0DC-4549-AEA0-3B15B44E86A9}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
5 h54d.vcxproj
@@ -42,7 +42,7 @@
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
- <OutDir>$(Configuration)\</OutDir>
+ <OutDir>win\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -273,6 +273,7 @@
<None Include="cefclient\res\uiplugin.html" />
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="cefclient\ext\fs.h" />
<ClInclude Include="cefclient\ext\os.h" />
<ClInclude Include="include\cef_nplugin_capi.h" />
<ClInclude Include="include\cef_nplugin.h" />
@@ -309,7 +310,9 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="cefclient\cefclient.cpp" />
+ <ClCompile Include="cefclient\ext\fs.cpp" />
<ClCompile Include="cefclient\ext\os.cpp" />
+ <ClCompile Include="cefclient\ext\win.cpp" />
<ClCompile Include="cefclient\string_util.cpp" />
<ClCompile Include="cefclient\cefclient_switches.cpp" />
<ClCompile Include="cefclient\download_handler.cpp" />
View
9 h54d.vcxproj.filters
@@ -186,10 +186,19 @@
<ClInclude Include="cefclient\ext\os.h">
<Filter>h54d\ext</Filter>
</ClInclude>
+ <ClInclude Include="cefclient\ext\fs.h">
+ <Filter>h54d\ext</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="cefclient\ext\os.cpp">
<Filter>h54d\ext</Filter>
</ClCompile>
+ <ClCompile Include="cefclient\ext\fs.cpp">
+ <Filter>h54d\ext</Filter>
+ </ClCompile>
+ <ClCompile Include="cefclient\ext\win.cpp">
+ <Filter>h54d\ext</Filter>
+ </ClCompile>
</ItemGroup>
</Project>
View
16 index.htm
@@ -1,16 +0,0 @@
-
-<html>
- <head>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- $("a").click(function() {
- alert(test.myfunc());
- });
- });
- </script>
- </head>
- <body>
- <a href="">Link</a>
- </body>
- </html>
View
0  dist/chrome.pak → win/chrome.pak
File renamed without changes
View
BIN  win/h54d.exe
Binary file not shown
View
0  dist/icudt.dll → win/icudt.dll
File renamed without changes
View
24 win/index.htm
@@ -0,0 +1,24 @@
+<html>
+ <head>
+ <title>your app</title>
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script type="text/javascript">
+ $(document).ready(function() {
+ $("#message").html("Your operating system is: " + os.type());
+
+ $("#readhost").click(function() {
+ var filepath = (os.type() != 'windows') ? '/etc/hosts' : 'C:\\Windows\\System32\\drivers\\etc\\hosts';
+ var callback = function (err, data) {
+ if (err) throw err;
+ alert(data);
+ };
+ fs.readFile(filepath, callback);
+ });
+ });
+ </script>
+ </head>
+ <body>
+ <p id="message"></p>
+ <a href="#" id="readhost">Click to read /etc/hosts file</a>
+ </body>
+ </html>
View
0  dist/libcef.dll → win/libcef.dll
File renamed without changes
View
0  dist/locales/en-US.pak → win/locales/en-US.pak
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.