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