3131#include  < stdio.h> 
3232#include  < stdlib.h> 
3333#include  < sys/stat.h> 
34+ #include  < unistd.h> 
3435#include  < X11/Xlib.h> 
3536
3637GtkWidget* _menuWidget;
@@ -40,48 +41,16 @@ int ConvertGnomeErrorCode(GError* gerror, bool isReading = true);
4041
4142extern  bool  isReallyClosing;
4243
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" 
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, " :" 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- }
7844
7945int32 OpenLiveBrowser (ExtensionString argURL, bool  enableRemoteDebugging)
8046{    
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 ("  " 
8554
8655    if (enableRemoteDebugging)
8756        arg2.assign (" --remote-debugging-port=9222" 
@@ -95,10 +64,16 @@ int32 OpenLiveBrowser(ExtensionString argURL, bool enableRemoteDebugging)
9564        case  -1 :    // # Something went wrong
9665                return  ConvertLinuxErrorCode (errno);
9766        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+                 }
10074                error=errno;
10175                _exit (0 );
76+ 
10277        default :
10378                if (error!=0 )
10479                {
0 commit comments