c8972998d91652f2865493cc715d82b98671f638
[browser-switch] / launcher.c
1 /*
2  * launcher.c -- functions for launching web browsers for browser-switchboard
3  *
4  * Copyright (C) 2009 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <dbus/dbus-glib.h>
30
31 #include "browser-switchboard.h"
32 #include "launcher.h"
33 #include "dbus-server-bindings.h"
34
35 #define LAUNCH_DEFAULT_BROWSER launch_tear
36
37 static void launch_tear(struct swb_context *ctx, char *uri) {
38         int status;
39         static DBusGProxy *tear_proxy = NULL;
40         GError *error = NULL;
41         pid_t pid;
42
43         if (!uri)
44                 uri = "new_window";
45
46         printf("launch_tear with uri '%s'\n", uri);
47
48         status = system("pidof tear > /dev/null");
49         if (!WIFEXITED(status))
50                 exit(1);
51         if (!WEXITSTATUS(status)) {
52                 if (!tear_proxy)
53                         tear_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
54                                         "com.nokia.tear", "/com/nokia/tear",
55                                         "com.nokia.Tear");
56                 dbus_g_proxy_call(tear_proxy, "OpenAddress",
57                                 &error,
58                                 G_TYPE_STRING, uri,
59                                 G_TYPE_INVALID);
60                 if (!ctx->continuous_mode)
61                         exit(0);
62         } else {
63                 if (ctx->continuous_mode) {
64                         if ((pid = fork()) != 0) {
65                                 /* Parent process or error in fork() */
66                                 printf("child: %d\n", (int)pid);
67                                 return;
68                         }
69                         /* Child process */
70                         setsid();
71                 }
72                 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
73         }
74 }
75
76 void launch_microb(struct swb_context *ctx, char *uri) {
77         int kill_browserd = 0;
78         int status;
79         pid_t pid;
80
81         if (!uri)
82                 uri = "new_window";
83
84         status = system("pidof /usr/sbin/browserd > /dev/null");
85         if (!WIFEXITED(status))
86                 exit(1);
87         if (WEXITSTATUS(status)) {
88                 kill_browserd = 1;
89                 system("/usr/sbin/browserd -d");
90         }
91
92         dbus_release_osso_browser_name(ctx);
93
94         if ((pid = fork()) == -1) {
95                 perror("fork");
96                 exit(1);
97         }
98         if (pid > 0) {
99                 /* Parent process */
100                 waitpid(pid, &status, 0);
101         } else {
102                 /* Child process */
103                 if (!strcmp(uri, "new_window")) {
104                         execl("/usr/bin/maemo-invoker",
105                                        "browser", (char *)NULL);
106                 } else {
107                         execl("/usr/bin/maemo-invoker",
108                                         "browser", "--url", uri, (char *)NULL);
109                 }
110         }
111
112         if (kill_browserd)
113                 system("kill `pidof /usr/sbin/browserd`");
114
115         if (!ctx || !ctx->continuous_mode) 
116                 exit(0);
117
118         dbus_request_osso_browser_name(ctx);
119 }
120
121 static void launch_other_browser(struct swb_context *ctx, char *uri) {
122         char *command;
123         char *quoted_uri, *quote;
124
125         size_t cmdlen, urilen;
126
127         if (!uri || !strcmp(uri, "new_window"))
128                 uri = "";
129         urilen = strlen(uri);
130         if (urilen > 0) {
131                 /* Quote the URI */
132                 /* urilen+3 = length of URI + 2x \' + \0 */
133                 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
134                         exit(1);
135                 strncpy(quoted_uri+1, uri, urilen);
136                 quoted_uri[0] = quoted_uri[urilen+1] = '\'';
137                 /* calloc zeroes the memory, so string is automatically
138                    null terminated */
139
140                 /* If there are any 's in the original URI, URL-escape them
141                    (replace them with %27) */
142                 quote = quoted_uri + 1;
143                 while ((quote = strchr(quote, '\'')) &&
144                                 (quote-quoted_uri) < strlen(quoted_uri)-1) {
145                         /* 3 = strlen("%27")-strlen("'") + \0 */
146                         if (!(quoted_uri = realloc(quoted_uri,
147                                                         strlen(quoted_uri)+3)))
148                                 exit(1);
149                         /* Move the string after the ', including the \0,
150                            over two chars */
151                         memmove(quote+3, quote+1, strlen(quote)+1);
152                         memcpy(quote, "%27", 3);
153                         quote = quote + 3;
154                 }
155                 urilen = strlen(quoted_uri);
156         } else
157                 quoted_uri = uri;
158
159         cmdlen = strlen(ctx->other_browser_cmd);
160
161         /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
162            replace "%s"), but is needed in the case other_browser_cmd has no %s
163            and urilen < 2 */
164         if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
165                 exit(1);
166         snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
167         printf("command: '%s'\n", command);
168
169         if (ctx->continuous_mode) {
170                 if (fork() != 0) {
171                         /* Parent process or error in fork() */
172                         if (urilen > 0)
173                                 free(quoted_uri);
174                         free(command);  
175                         return;
176                 }
177                 /* Child process */
178                 setsid();
179         }
180         execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
181 }
182
183 static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
184         size_t len = strlen(cmd);
185
186         free(ctx->other_browser_cmd);
187         ctx->other_browser_cmd = calloc(len+1, sizeof(char));
188         if (!ctx->other_browser_cmd) {
189                 printf("malloc failed!\n");
190                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
191         } else {
192                 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
193                                 cmd, len);
194                 ctx->other_browser_cmd[len] = '\0';
195                 ctx->default_browser_launcher = launch_other_browser;
196         }
197 }
198
199 void update_default_browser(struct swb_context *ctx, char *default_browser) {
200         if (!ctx)
201                 return;
202
203         if (!default_browser) {
204                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
205                 return;
206         }
207
208         if (!strcmp(default_browser, "tear"))
209                 ctx->default_browser_launcher = launch_tear;
210         else if (!strcmp(default_browser, "microb"))
211                 ctx->default_browser_launcher = launch_microb;
212         else if (!strcmp(default_browser, "fennec"))
213                 use_other_browser_cmd(ctx, "fennec %s");
214         else if (!strcmp(default_browser, "midori"))
215                 use_other_browser_cmd(ctx, "midori %s");
216         else if (!strcmp(default_browser, "other")) {
217                 if (ctx->other_browser_cmd)
218                         ctx->default_browser_launcher = launch_other_browser;
219                 else {
220                         printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
221                         ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
222                 }
223         } else {
224                 printf("Unknown default_browser %s, using default", default_browser);
225                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
226         }
227 }
228
229 void launch_browser(struct swb_context *ctx, char *uri) {
230         if (ctx && ctx->default_browser_launcher)
231                 ctx->default_browser_launcher(ctx, uri);
232 }