31
31
#include < stdio.h>
32
32
#include < stdlib.h>
33
33
#include < sys/stat.h>
34
+ #include < unistd.h>
34
35
#include < X11/Xlib.h>
35
36
36
37
GtkWidget* _menuWidget;
@@ -40,48 +41,16 @@ int ConvertGnomeErrorCode(GError* gerror, bool isReading = true);
40
41
41
42
extern bool isReallyClosing;
42
43
43
- static const char * GetPathToLiveBrowser ()
44
- {
45
- // #TODO Use execlp and be done with it! No need to reinvent the wheel; so badly that too!
46
- char *envPath = getenv ( " PATH" ), *path, *dir, *currentPath;
47
-
48
- // # copy PATH and not modify the original
49
- path=(char *)malloc (strlen (envPath)+1 );
50
- strcpy (path, envPath);
51
-
52
- // Prepend a forward-slash. For convenience
53
- const char * executable=" /google-chrome" ;
54
- struct stat buf;
55
- int len;
56
-
57
- for ( dir = strtok ( path, " :" ); dir; dir = strtok ( NULL , " :" ) )
58
- {
59
- len=strlen (dir)+strlen (executable);
60
- // if((strrchr(dir,'/')-dir)==strlen(dir))
61
- // {
62
- // currentPath = (char*)malloc(len);
63
- // strcpy(currentPath,dir);
64
- // } else
65
- // {
66
- // stat handles consecutive forward slashes automatically. No need for above
67
- currentPath = (char *)malloc (len+1 );
68
- strncpy (currentPath,dir,len);
69
- // }
70
- strcat (currentPath,executable);
71
-
72
- if (stat (currentPath,&buf)==0 && S_ISREG (buf.st_mode ))
73
- return currentPath;
74
- }
75
-
76
- return " " ;
77
- }
78
44
79
45
int32 OpenLiveBrowser (ExtensionString argURL, bool enableRemoteDebugging)
80
46
{
81
- // # COnsider using execlp and avoid all this path mess!
82
- const char *appPath = GetPathToLiveBrowser (),
83
- *arg1 = " --allow-file-access-from-files" ;
84
- std::string arg2 (" " );
47
+ // Supported browsers (order matters):
48
+ // - google-chorme
49
+ // - chromium-browser - chromium executable name (in ubuntu)
50
+ // - chromium - other chromium executable name (in arch linux)
51
+ std::string browsers[3 ] = {" google-chrome" , " chromium-browser" , " chromium" },
52
+ arg1 (" --allow-file-access-from-files" ),
53
+ arg2 (" " );
85
54
86
55
if (enableRemoteDebugging)
87
56
arg2.assign (" --remote-debugging-port=9222" );
@@ -95,10 +64,16 @@ int32 OpenLiveBrowser(ExtensionString argURL, bool enableRemoteDebugging)
95
64
case -1 : // # Something went wrong
96
65
return ConvertLinuxErrorCode (errno);
97
66
case 0 : // # I'm the child. When I successfully exec, parent is resumed. Or when I _exec()
98
- execl (appPath, arg1, argURL.c_str (), arg2.c_str (),(char *)0 );
99
-
67
+ // check for supported browsers (in PATH directories)
68
+ for (size_t i = 0 ; i < sizeof (browsers) / sizeof (browsers[0 ]); i++) {
69
+ if (execlp (browsers[i].c_str (), browsers[i].c_str (), arg1.c_str (), argURL.c_str (), arg2.c_str (), NULL ) != -1 ) {
70
+ // browser is found in os; stop iterating
71
+ break ;
72
+ }
73
+ }
100
74
error=errno;
101
75
_exit (0 );
76
+
102
77
default :
103
78
if (error!=0 )
104
79
{
0 commit comments