-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
BrowserApp.cpp
532 lines (458 loc) · 13.7 KB
/
BrowserApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* Copyright (C) 2007 Ryan Leavengood <leavengood@gmail.com>
* Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BrowserApp.h"
#include <AboutWindow.h>
#include <Alert.h>
#include <Autolock.h>
#include <Catalog.h>
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <Locale.h>
#include <Path.h>
#include <Screen.h>
#include <UrlContext.h>
#include <debugger.h>
#include <stdio.h>
#include "BrowserWindow.h"
#include "BrowsingHistory.h"
#include "DownloadWindow.h"
#include "SettingsMessage.h"
#include "SettingsWindow.h"
#include "ConsoleWindow.h"
#include "CookieWindow.h"
#include "NetworkCookieJar.h"
#include "WebKitInfo.h"
#include "WebPage.h"
#include "WebSettings.h"
#include "WebView.h"
#include "WebViewConstants.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "WebPositive"
const char* kApplicationSignature = "application/x-vnd.Haiku-WebPositive";
const char* kApplicationName = B_TRANSLATE_SYSTEM_NAME("WebPositive");
static const uint32 PRELOAD_BROWSING_HISTORY = 'plbh';
#define ENABLE_NATIVE_COOKIES 1
BrowserApp::BrowserApp()
:
BApplication(kApplicationSignature),
fWindowCount(0),
fLastWindowFrame(50, 50, 950, 750),
fLaunchRefsMessage(0),
fInitialized(false),
fSettings(NULL),
fCookies(NULL),
fContext(NULL),
fDownloadWindow(NULL),
fSettingsWindow(NULL),
fConsoleWindow(NULL),
fCookieWindow(NULL)
{
#if ENABLE_NATIVE_COOKIES
BString cookieStorePath = kApplicationName;
cookieStorePath << "/Cookies";
fCookies = new SettingsMessage(B_USER_SETTINGS_DIRECTORY,
cookieStorePath.String());
BMessage cookieArchive = fCookies->GetValue("cookies", cookieArchive);
fContext = new BUrlContext();
fContext->SetCookieJar(BNetworkCookieJar(&cookieArchive));
#endif
}
BrowserApp::~BrowserApp()
{
delete fLaunchRefsMessage;
delete fSettings;
delete fCookies;
delete fContext;
}
void
BrowserApp::AboutRequested()
{
BAboutWindow* window = new BAboutWindow(kApplicationName,
kApplicationSignature);
// create the about window
const char* authors[] = {
"Andrea Anzani",
"Stephan Aßmus",
"Alexandre Deckner",
"Rene Gollent",
"Ryan Leavengood",
"Michael Lotz",
"Maxime Simon",
NULL
};
BString aboutText("");
aboutText << "HaikuWebKit " << WebKitInfo::HaikuWebKitVersion();
aboutText << "\nWebKit " << WebKitInfo::WebKitVersion();
window->AddCopyright(2007, "Haiku, Inc.");
window->AddAuthors(authors);
window->AddExtraInfo(aboutText.String());
window->Show();
}
void
BrowserApp::ArgvReceived(int32 argc, char** argv)
{
BMessage message(B_REFS_RECEIVED);
for (int i = 1; i < argc; i++) {
if (strcmp("-f", argv[i]) == 0
|| strcmp("--fullscreen", argv[i]) == 0) {
message.AddBool("fullscreen", true);
continue;
}
const char* url = argv[i];
BEntry entry(argv[i], true);
BPath path;
if (entry.Exists() && entry.GetPath(&path) == B_OK)
url = path.Path();
message.AddString("url", url);
}
// Upon program launch, it will buffer a copy of the message, since
// ArgReceived() is called before ReadyToRun().
RefsReceived(&message);
}
void
BrowserApp::ReadyToRun()
{
// Since we will essentially run the GUI...
set_thread_priority(Thread(), B_DISPLAY_PRIORITY);
BWebPage::InitializeOnce();
BWebPage::SetCacheModel(B_WEBKIT_CACHE_MODEL_WEB_BROWSER);
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK
&& path.Append(kApplicationName) == B_OK
&& create_directory(path.Path(), 0777) == B_OK) {
BWebSettings::SetPersistentStoragePath(path.Path());
}
BString mainSettingsPath(kApplicationName);
mainSettingsPath << "/Application";
fSettings = new SettingsMessage(B_USER_SETTINGS_DIRECTORY,
mainSettingsPath.String());
fLastWindowFrame = fSettings->GetValue("window frame", fLastWindowFrame);
BRect defaultDownloadWindowFrame(-10, -10, 365, 265);
BRect downloadWindowFrame = fSettings->GetValue("downloads window frame",
defaultDownloadWindowFrame);
BRect settingsWindowFrame = fSettings->GetValue("settings window frame",
BRect());
BRect consoleWindowFrame = fSettings->GetValue("console window frame",
BRect(50, 50, 400, 300));
BRect cookieWindowFrame = fSettings->GetValue("cookie window frame",
BRect(50, 50, 400, 300));
bool showDownloads = fSettings->GetValue("show downloads", false);
fDownloadWindow = new DownloadWindow(downloadWindowFrame, showDownloads,
fSettings);
if (downloadWindowFrame == defaultDownloadWindowFrame) {
// Initially put download window in lower right of screen.
BRect screenFrame = BScreen().Frame();
BMessage decoratorSettings;
fDownloadWindow->GetDecoratorSettings(&decoratorSettings);
float borderWidth = 0;
if (decoratorSettings.FindFloat("border width", &borderWidth) != B_OK)
borderWidth = 5;
fDownloadWindow->MoveTo(screenFrame.Width()
- fDownloadWindow->Frame().Width() - borderWidth,
screenFrame.Height() - fDownloadWindow->Frame().Height()
- borderWidth);
}
fSettingsWindow = new SettingsWindow(settingsWindowFrame, fSettings);
BWebPage::SetDownloadListener(BMessenger(fDownloadWindow));
fConsoleWindow = new ConsoleWindow(consoleWindowFrame);
fCookieWindow = new CookieWindow(cookieWindowFrame, fContext->GetCookieJar());
fInitialized = true;
int32 pagesCreated = 0;
bool fullscreen = false;
if (fLaunchRefsMessage) {
_RefsReceived(fLaunchRefsMessage, &pagesCreated, &fullscreen);
delete fLaunchRefsMessage;
fLaunchRefsMessage = NULL;
}
if (pagesCreated == 0)
_CreateNewWindow("", fullscreen);
PostMessage(PRELOAD_BROWSING_HISTORY);
}
void
BrowserApp::MessageReceived(BMessage* message)
{
switch (message->what) {
case PRELOAD_BROWSING_HISTORY:
// Accessing the default instance will load the history from disk.
BrowsingHistory::DefaultInstance();
break;
case B_SILENT_RELAUNCH:
_CreateNewPage("");
break;
case NEW_WINDOW: {
BString url;
if (message->FindString("url", &url) != B_OK)
break;
_CreateNewWindow(url);
break;
}
case NEW_TAB: {
BrowserWindow* window;
if (message->FindPointer("window",
reinterpret_cast<void**>(&window)) != B_OK)
break;
BString url;
message->FindString("url", &url);
bool select = false;
message->FindBool("select", &select);
_CreateNewTab(window, url, select);
break;
}
case WINDOW_OPENED:
fWindowCount++;
fDownloadWindow->SetMinimizeOnClose(false);
break;
case WINDOW_CLOSED:
fWindowCount--;
message->FindRect("window frame", &fLastWindowFrame);
if (fWindowCount <= 0)
PostMessage(B_QUIT_REQUESTED);
break;
case SHOW_DOWNLOAD_WINDOW:
_ShowWindow(message, fDownloadWindow);
break;
case SHOW_SETTINGS_WINDOW:
_ShowWindow(message, fSettingsWindow);
break;
case SHOW_CONSOLE_WINDOW:
_ShowWindow(message, fConsoleWindow);
break;
case SHOW_COOKIE_WINDOW:
_ShowWindow(message, fCookieWindow);
break;
case ADD_CONSOLE_MESSAGE:
fConsoleWindow->PostMessage(message);
break;
default:
BApplication::MessageReceived(message);
break;
}
}
void
BrowserApp::RefsReceived(BMessage* message)
{
if (!fInitialized) {
delete fLaunchRefsMessage;
fLaunchRefsMessage = new BMessage(*message);
return;
}
_RefsReceived(message);
}
bool
BrowserApp::QuitRequested()
{
if (fDownloadWindow->DownloadsInProgress()) {
BAlert* alert = new BAlert(B_TRANSLATE("Downloads in progress"),
B_TRANSLATE("There are still downloads in progress, do you really "
"want to quit WebPositive now?"), B_TRANSLATE("Quit"),
B_TRANSLATE("Continue downloads"));
int32 choice = alert->Go();
if (choice == 1) {
if (fWindowCount == 0) {
if (fDownloadWindow->Lock()) {
fDownloadWindow->SetWorkspaces(1 << current_workspace());
if (fDownloadWindow->IsHidden())
fDownloadWindow->Show();
else
fDownloadWindow->Activate();
fDownloadWindow->SetMinimizeOnClose(true);
fDownloadWindow->Unlock();
return false;
}
} else
return false;
}
}
for (int i = 0; BWindow* window = WindowAt(i); i++) {
BrowserWindow* webWindow = dynamic_cast<BrowserWindow*>(window);
if (!webWindow)
continue;
if (!webWindow->Lock())
continue;
if (webWindow->QuitRequested()) {
fLastWindowFrame = webWindow->WindowFrame();
webWindow->Quit();
i--;
} else {
webWindow->Unlock();
return false;
}
}
BWebPage::ShutdownOnce();
fSettings->SetValue("window frame", fLastWindowFrame);
if (fDownloadWindow->Lock()) {
fSettings->SetValue("downloads window frame", fDownloadWindow->Frame());
fSettings->SetValue("show downloads", !fDownloadWindow->IsHidden());
fDownloadWindow->Unlock();
}
if (fSettingsWindow->Lock()) {
fSettings->SetValue("settings window frame", fSettingsWindow->Frame());
fSettingsWindow->Unlock();
}
if (fConsoleWindow->Lock()) {
fSettings->SetValue("console window frame", fConsoleWindow->Frame());
fConsoleWindow->Unlock();
}
if (fCookieWindow->Lock()) {
fSettings->SetValue("cookie window frame", fCookieWindow->Frame());
fCookieWindow->Unlock();
}
BMessage cookieArchive;
BNetworkCookieJar& cookieJar = fContext->GetCookieJar();
cookieJar.PurgeForExit();
if (cookieJar.Archive(&cookieArchive) == B_OK)
fCookies->SetValue("cookies", cookieArchive);
return true;
}
void
BrowserApp::_RefsReceived(BMessage* message, int32* _pagesCreated,
bool* _fullscreen)
{
int32 pagesCreated = 0;
BrowserWindow* window = NULL;
if (message->FindPointer("window", (void**)&window) != B_OK)
window = NULL;
bool fullscreen;
if (message->FindBool("fullscreen", &fullscreen) != B_OK)
fullscreen = false;
entry_ref ref;
for (int32 i = 0; message->FindRef("refs", i, &ref) == B_OK; i++) {
BEntry entry(&ref, true);
if (!entry.Exists())
continue;
BPath path;
if (entry.GetPath(&path) != B_OK)
continue;
BUrl url(path);
window = _CreateNewPage(url.UrlString(), window, fullscreen,
pagesCreated == 0);
pagesCreated++;
}
BString url;
for (int32 i = 0; message->FindString("url", i, &url) == B_OK; i++) {
window = _CreateNewPage(url, window, fullscreen, pagesCreated == 0);
pagesCreated++;
}
if (_pagesCreated != NULL)
*_pagesCreated = pagesCreated;
if (_fullscreen != NULL)
*_fullscreen = fullscreen;
}
BrowserWindow*
BrowserApp::_CreateNewPage(const BString& url, BrowserWindow* webWindow,
bool fullscreen, bool useBlankTab)
{
// Let's first see if we must target a specific window...
if (webWindow && webWindow->Lock()) {
if (useBlankTab && webWindow->IsBlankTab()) {
if (url.Length() != 0)
webWindow->CurrentWebView()->LoadURL(url);
} else
webWindow->CreateNewTab(url, true);
webWindow->Activate();
webWindow->CurrentWebView()->MakeFocus(true);
webWindow->Unlock();
return webWindow;
}
// Otherwise, try to find one in the current workspace
uint32 workspace = 1 << current_workspace();
bool loadedInWindowOnCurrentWorkspace = false;
for (int i = 0; BWindow* window = WindowAt(i); i++) {
webWindow = dynamic_cast<BrowserWindow*>(window);
if (!webWindow)
continue;
if (webWindow->Lock()) {
if (webWindow->Workspaces() & workspace) {
if (useBlankTab && webWindow->IsBlankTab()) {
if (url.Length() != 0)
webWindow->CurrentWebView()->LoadURL(url);
} else
webWindow->CreateNewTab(url, true);
webWindow->Activate();
webWindow->CurrentWebView()->MakeFocus(true);
loadedInWindowOnCurrentWorkspace = true;
}
webWindow->Unlock();
}
if (loadedInWindowOnCurrentWorkspace)
return webWindow;
}
// Finally, if no window is available, let's create one.
return _CreateNewWindow(url, fullscreen);
}
BrowserWindow*
BrowserApp::_CreateNewWindow(const BString& url, bool fullscreen)
{
// Offset the window frame unless this is the first window created in the
// session.
if (fWindowCount > 0)
fLastWindowFrame.OffsetBy(20, 20);
if (!BScreen().Frame().Contains(fLastWindowFrame))
fLastWindowFrame.OffsetTo(50, 50);
BrowserWindow* window = new BrowserWindow(fLastWindowFrame, fSettings,
url, fContext);
if (fullscreen)
window->ToggleFullscreen();
window->Show();
return window;
}
void
BrowserApp::_CreateNewTab(BrowserWindow* window, const BString& url,
bool select)
{
if (!window->Lock())
return;
window->CreateNewTab(url, select);
window->Unlock();
}
void
BrowserApp::_ShowWindow(const BMessage* message, BWindow* window)
{
BAutolock _(window);
uint32 workspaces;
if (message->FindUInt32("workspaces", &workspaces) == B_OK)
window->SetWorkspaces(workspaces);
if (window->IsHidden())
window->Show();
else
window->Activate();
}
// #pragma mark -
int
main(int, char**)
{
try {
new BrowserApp();
be_app->Run();
delete be_app;
} catch (...) {
debugger("Exception caught.");
}
return 0;
}