Launch MicroB if default_browser not set and Tear isn't installed
[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 DEFAULT_BROWSER "/usr/bin/tear"
36 #define LAUNCH_DEFAULT_BROWSER launch_tear
37
38 static void launch_tear(struct swb_context *ctx, char *uri) {
39         int status;
40         static DBusGProxy *tear_proxy = NULL;
41         GError *error = NULL;
42         pid_t pid;
43
44         if (!uri)
45                 uri = "new_window";
46
47         printf("launch_tear with uri '%s'\n", uri);
48
49         status = system("pidof tear > /dev/null");
50         if (WIFEXITED(status) && !WEXITSTATUS(status)) {
51                 if (!tear_proxy)
52                         tear_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
53                                         "com.nokia.tear", "/com/nokia/tear",
54                                         "com.nokia.Tear");
55                 dbus_g_proxy_call(tear_proxy, "OpenAddress", &error,
56                                   G_TYPE_STRING, uri, G_TYPE_INVALID);
57                 if (!ctx->continuous_mode)
58                         exit(0);
59         } else {
60                 if (ctx->continuous_mode) {
61                         if ((pid = fork()) != 0) {
62                                 /* Parent process or error in fork() */
63                                 printf("child: %d\n", (int)pid);
64                                 return;
65                         }
66                         /* Child process */
67                         setsid();
68                 }
69                 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
70         }
71 }
72
73 void launch_microb(struct swb_context *ctx, char *uri) {
74         int kill_browserd = 0;
75         int status;
76         pid_t pid;
77
78         if (!uri)
79                 uri = "new_window";
80
81         status = system("pidof /usr/sbin/browserd > /dev/null");
82         if (WIFEXITED(status) && WEXITSTATUS(status)) {
83                 kill_browserd = 1;
84                 system("/usr/sbin/browserd -d");
85         }
86
87         dbus_release_osso_browser_name(ctx);
88
89         if ((pid = fork()) == -1) {
90                 perror("fork");
91                 exit(1);
92         }
93         if (pid > 0) {
94                 /* Parent process */
95                 waitpid(pid, &status, 0);
96         } else {
97                 /* Child process */
98                 if (!strcmp(uri, "new_window")) {
99                         execl("/usr/bin/maemo-invoker",
100                               "browser", (char *)NULL);
101                 } else {
102                         execl("/usr/bin/maemo-invoker",
103                               "browser", "--url", uri, (char *)NULL);
104                 }
105         }
106
107         if (kill_browserd)
108                 system("kill `pidof /usr/sbin/browserd`");
109
110         if (!ctx || !ctx->continuous_mode) 
111                 exit(0);
112
113         dbus_request_osso_browser_name(ctx);
114 }
115
116 static void launch_other_browser(struct swb_context *ctx, char *uri) {
117         char *command;
118         char *quoted_uri, *quote;
119
120         size_t cmdlen, urilen;
121         size_t quoted_uri_size;
122         size_t offset;
123
124         if (!uri || !strcmp(uri, "new_window"))
125                 uri = "";
126         urilen = strlen(uri);
127         if (urilen > 0) {
128                 /* Quote the URI */
129                 /* urilen+3 = length of URI + 2x \' + \0 */
130                 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
131                         exit(1);
132                 snprintf(quoted_uri, urilen+3, "'%s'", uri);
133
134                 /* If there are any 's in the original URI, URL-escape them
135                    (replace them with %27) */
136                 quoted_uri_size = urilen + 3;
137                 quote = quoted_uri + 1;
138                 while ((quote = strchr(quote, '\'')) &&
139                        (offset = quote-quoted_uri) < strlen(quoted_uri)-1) {
140                         /* Check to make sure we don't shrink the memory area
141                            as a result of integer overflow */
142                         if (quoted_uri_size+2 <= quoted_uri_size)
143                                 exit(1);
144
145                         /* Grow the memory area;
146                            2 = strlen("%27")-strlen("'") */
147                         if (!(quoted_uri = realloc(quoted_uri,
148                                                    quoted_uri_size+2)))
149                                 exit(1);
150                         quoted_uri_size = quoted_uri_size + 2;
151
152                         /* Recalculate the location of the ' character --
153                            realloc() may have moved the string in memory */
154                         quote = quoted_uri + offset;
155
156                         /* Move the string after the ', including the \0,
157                            over two chars */
158                         memmove(quote+3, quote+1, strlen(quote));
159                         memcpy(quote, "%27", 3);
160                         quote = quote + 3;
161                 }
162                 urilen = strlen(quoted_uri);
163         } else
164                 quoted_uri = uri;
165
166         cmdlen = strlen(ctx->other_browser_cmd);
167
168         /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
169            replace "%s"), but is needed in the case other_browser_cmd has no %s
170            and urilen < 2 */
171         if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
172                 exit(1);
173         snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
174         printf("command: '%s'\n", command);
175
176         if (ctx->continuous_mode) {
177                 if (fork() != 0) {
178                         /* Parent process or error in fork() */
179                         if (urilen > 0)
180                                 free(quoted_uri);
181                         free(command);  
182                         return;
183                 }
184                 /* Child process */
185                 setsid();
186         }
187         execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
188 }
189
190 static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
191         size_t len = strlen(cmd);
192
193         free(ctx->other_browser_cmd);
194         ctx->other_browser_cmd = calloc(len+1, sizeof(char));
195         if (!ctx->other_browser_cmd) {
196                 printf("malloc failed!\n");
197                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
198         } else {
199                 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
200                                                  cmd, len+1);
201                 ctx->default_browser_launcher = launch_other_browser;
202         }
203 }
204
205 void update_default_browser(struct swb_context *ctx, char *default_browser) {
206         if (!ctx)
207                 return;
208
209         if (!default_browser) {
210                 /* No default_browser configured -- use DEFAULT_BROWSER if
211                    installed, otherwise launch MicroB */
212                 if (!access(DEFAULT_BROWSER, X_OK))
213                         ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
214                 else
215                         ctx->default_browser_launcher = launch_microb;
216                 return;
217         }
218
219         if (!strcmp(default_browser, "tear"))
220                 ctx->default_browser_launcher = launch_tear;
221         else if (!strcmp(default_browser, "microb"))
222                 ctx->default_browser_launcher = launch_microb;
223         else if (!strcmp(default_browser, "fennec"))
224                 use_other_browser_cmd(ctx, "fennec %s");
225         else if (!strcmp(default_browser, "midori"))
226                 use_other_browser_cmd(ctx, "midori %s");
227         else if (!strcmp(default_browser, "other")) {
228                 if (ctx->other_browser_cmd)
229                         ctx->default_browser_launcher = launch_other_browser;
230                 else {
231                         printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
232                         ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
233                 }
234         } else {
235                 printf("Unknown default_browser %s, using default", default_browser);
236                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
237         }
238 }
239
240 void launch_browser(struct swb_context *ctx, char *uri) {
241         if (ctx && ctx->default_browser_launcher)
242                 ctx->default_browser_launcher(ctx, uri);
243 }