Update changelog, bump version number
[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_microb
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         /* We should be able to just call the D-Bus service to open Tear ...
49            but if Tear's not open, that cuases D-Bus to start Tear and then
50            pass it the OpenAddress call, which results in two browser windows.
51            Properly fixing this probably requires Tear to provide a D-Bus
52            method that opens an address in an existing window, but for now work
53            around by just invoking Tear with exec() if it's not running. */
54         status = system("pidof tear > /dev/null");
55         if (WIFEXITED(status) && !WEXITSTATUS(status)) {
56                 if (!tear_proxy)
57                         tear_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
58                                         "com.nokia.tear", "/com/nokia/tear",
59                                         "com.nokia.Tear");
60                 dbus_g_proxy_call(tear_proxy, "OpenAddress", &error,
61                                   G_TYPE_STRING, uri, G_TYPE_INVALID);
62                 if (!ctx->continuous_mode)
63                         exit(0);
64         } else {
65                 if (ctx->continuous_mode) {
66                         if ((pid = fork()) != 0) {
67                                 /* Parent process or error in fork() */
68                                 printf("child: %d\n", (int)pid);
69                                 return;
70                         }
71                         /* Child process */
72                         setsid();
73                 }
74                 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
75         }
76 }
77
78 void launch_microb(struct swb_context *ctx, char *uri) {
79         int kill_browserd = 0;
80         int status;
81         pid_t pid;
82
83         if (!uri)
84                 uri = "new_window";
85
86         printf("launch_microb with uri '%s'\n", uri);
87
88         /* Launch browserd if it's not running */
89         status = system("pidof /usr/sbin/browserd > /dev/null");
90         if (WIFEXITED(status) && WEXITSTATUS(status)) {
91                 kill_browserd = 1;
92                 system("/usr/sbin/browserd -d");
93         }
94
95         /* Release the osso_browser D-Bus name so that MicroB can take it */
96         dbus_release_osso_browser_name(ctx);
97
98         if ((pid = fork()) == -1) {
99                 perror("fork");
100                 exit(1);
101         }
102         if (pid > 0) {
103                 /* Parent process */
104                 waitpid(pid, &status, 0);
105         } else {
106                 /* Child process */
107                 /* exec maemo-invoker directly instead of relying on the
108                    /usr/bin/browser symlink, since /usr/bin/browser may have
109                    been replaced with a shell script calling us via D-Bus */
110                 if (!strcmp(uri, "new_window")) {
111                         execl("/usr/bin/maemo-invoker",
112                               "browser", (char *)NULL);
113                 } else {
114                         execl("/usr/bin/maemo-invoker",
115                               "browser", "--url", uri, (char *)NULL);
116                 }
117         }
118
119         /* Kill off browserd if we started it */
120         if (kill_browserd)
121                 system("kill `pidof /usr/sbin/browserd`");
122
123         if (!ctx || !ctx->continuous_mode) 
124                 exit(0);
125
126         dbus_request_osso_browser_name(ctx);
127 }
128
129 static void launch_other_browser(struct swb_context *ctx, char *uri) {
130         char *command;
131         char *quoted_uri, *quote;
132
133         size_t cmdlen, urilen;
134         size_t quoted_uri_size;
135         size_t offset;
136
137         if (!uri || !strcmp(uri, "new_window"))
138                 uri = "";
139
140         printf("launch_other_browser with uri '%s'\n", uri);
141
142         if ((urilen = strlen(uri)) > 0) {
143                 /* Quote the URI to prevent the shell from interpreting it */
144                 /* urilen+3 = length of URI + 2x \' + \0 */
145                 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
146                         exit(1);
147                 snprintf(quoted_uri, urilen+3, "'%s'", uri);
148
149                 /* If there are any 's in the original URI, URL-escape them
150                    (replace them with %27) */
151                 quoted_uri_size = urilen + 3;
152                 quote = quoted_uri + 1;
153                 while ((quote = strchr(quote, '\'')) &&
154                        (offset = quote-quoted_uri) < strlen(quoted_uri)-1) {
155                         /* Check to make sure we don't shrink the memory area
156                            as a result of integer overflow */
157                         if (quoted_uri_size+2 <= quoted_uri_size)
158                                 exit(1);
159
160                         /* Grow the memory area;
161                            2 = strlen("%27")-strlen("'") */
162                         if (!(quoted_uri = realloc(quoted_uri,
163                                                    quoted_uri_size+2)))
164                                 exit(1);
165                         quoted_uri_size = quoted_uri_size + 2;
166
167                         /* Recalculate the location of the ' character --
168                            realloc() may have moved the string in memory */
169                         quote = quoted_uri + offset;
170
171                         /* Move the string after the ', including the \0,
172                            over two chars */
173                         memmove(quote+3, quote+1, strlen(quote));
174                         memcpy(quote, "%27", 3);
175                         quote = quote + 3;
176                 }
177                 urilen = strlen(quoted_uri);
178         } else
179                 quoted_uri = uri;
180
181         cmdlen = strlen(ctx->other_browser_cmd);
182
183         /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
184            replace "%s"), but is needed in the case other_browser_cmd has no %s
185            and urilen < 2 */
186         if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
187                 exit(1);
188         snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
189         printf("command: '%s'\n", command);
190
191         if (ctx->continuous_mode) {
192                 if (fork() != 0) {
193                         /* Parent process or error in fork() */
194                         if (urilen > 0)
195                                 free(quoted_uri);
196                         free(command);  
197                         return;
198                 }
199                 /* Child process */
200                 setsid();
201         }
202         execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
203 }
204
205 /* Use launch_other_browser as the default browser launcher, with the string
206    passed in as the other_browser_cmd
207    Resulting other_browser_cmd is always safe to free(), even if a pointer
208    to a string constant is passed in */
209 static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
210         size_t len = strlen(cmd);
211
212         free(ctx->other_browser_cmd);
213         ctx->other_browser_cmd = calloc(len+1, sizeof(char));
214         if (!ctx->other_browser_cmd) {
215                 printf("malloc failed!\n");
216                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
217         } else {
218                 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
219                                                  cmd, len+1);
220                 ctx->default_browser_launcher = launch_other_browser;
221         }
222 }
223
224 void update_default_browser(struct swb_context *ctx, char *default_browser) {
225         if (!ctx)
226                 return;
227
228         if (!default_browser) {
229                 /* No default_browser configured -- use built-in default */
230                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
231                 return;
232         }
233
234         if (!strcmp(default_browser, "tear"))
235                 ctx->default_browser_launcher = launch_tear;
236         else if (!strcmp(default_browser, "microb"))
237                 ctx->default_browser_launcher = launch_microb;
238         else if (!strcmp(default_browser, "fennec"))
239                 /* Cheat and reuse launch_other_browser, since we don't appear
240                    to need to do anything special */
241                 use_other_browser_cmd(ctx, "fennec %s");
242         else if (!strcmp(default_browser, "midori"))
243                 use_other_browser_cmd(ctx, "midori %s");
244         else if (!strcmp(default_browser, "other")) {
245                 if (ctx->other_browser_cmd)
246                         ctx->default_browser_launcher = launch_other_browser;
247                 else {
248                         printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
249                         ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
250                 }
251         } else {
252                 printf("Unknown default_browser %s, using default", default_browser);
253                 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
254         }
255 }
256
257 void launch_browser(struct swb_context *ctx, char *uri) {
258         if (ctx && ctx->default_browser_launcher)
259                 ctx->default_browser_launcher(ctx, uri);
260 }