1 /* Sessions managment - you'll find things here which you wouldn't expect */
18 #include "bfu/dialog.h"
19 #include "bookmarks/bookmarks.h"
20 #include "cache/cache.h"
21 #include "config/home.h"
22 #include "config/options.h"
23 #include "dialogs/menu.h"
24 #include "dialogs/status.h"
25 #include "document/html/frames.h"
26 #include "document/html/parser.h"
27 #include "document/html/renderer.h"
28 #include "document/refresh.h"
29 #include "document/view.h"
30 #include "globhist/globhist.h"
31 #include "intl/gettext/libintl.h"
32 #include "main/event.h"
33 #include "main/object.h"
34 #include "main/timer.h"
35 #include "network/connection.h"
36 #include "network/state.h"
37 #include "osdep/newwin.h"
38 #include "protocol/protocol.h"
39 #include "protocol/uri.h"
40 #include "session/download.h"
41 #include "session/history.h"
42 #include "session/location.h"
43 #include "session/session.h"
44 #include "session/task.h"
45 #include "terminal/tab.h"
46 #include "terminal/terminal.h"
47 #include "terminal/window.h"
48 #include "util/conv.h"
49 #include "util/error.h"
50 #include "util/memlist.h"
51 #include "util/memory.h"
52 #include "util/string.h"
53 #include "util/time.h"
54 #include "viewer/text/draw.h"
55 #include "viewer/text/form.h"
56 #include "viewer/text/link.h"
57 #include "viewer/text/view.h"
61 LIST_HEAD(struct file_to_load
);
64 unsigned int req_sent
:1;
66 struct cache_entry
*cached
;
67 unsigned char *target_frame
;
69 struct download download
;
72 /* This structure and related functions are used to maintain information
73 * for instances opened in new windows. We store all related session info like
74 * URI and base session to clone from so that when the new instance connects
75 * we can look up this information. In case of failure the session information
78 LIST_HEAD(struct session_info
);
86 enum cache_mode cache_mode
;
89 #define file_to_load_is_active(ftl) ((ftl)->req_sent && is_in_progress_state((ftl)->download.state))
92 INIT_LIST_HEAD(sessions
);
94 enum remote_session_flags remote_session_flags
;
97 static struct file_to_load
*request_additional_file(struct session
*,
101 static window_handler_T tabwin_func
;
104 static INIT_LIST_HEAD(session_info
);
105 static int session_info_id
= 1;
107 static struct session_info
*
108 get_session_info(int id
)
110 struct session_info
*info
;
112 foreach (info
, session_info
) {
115 if (info
->id
!= id
) continue;
117 /* Make sure the info->ses session is still with us. */
118 foreach (ses
, sessions
)
119 if (ses
== info
->ses
)
130 done_session_info(struct session_info
*info
)
133 kill_timer(&info
->timer
);
135 if (info
->uri
) done_uri(info
->uri
);
136 if (info
->referrer
) done_uri(info
->referrer
);
141 done_saved_session_info(void)
143 while (!list_empty(session_info
))
144 done_session_info(session_info
.next
);
148 session_info_timeout(int id
)
150 struct session_info
*info
= get_session_info(id
);
153 info
->timer
= TIMER_ID_UNDEF
;
154 done_session_info(info
);
158 add_session_info(struct session
*ses
, struct uri
*uri
, struct uri
*referrer
,
159 enum cache_mode cache_mode
, enum task_type task
)
161 struct session_info
*info
= mem_calloc(1, sizeof(*info
));
163 if (!info
) return -1;
165 info
->id
= session_info_id
++;
166 /* I don't know what a reasonable start up time for a new instance is
167 * but it won't hurt to have a few seconds atleast. --jonas */
168 install_timer(&info
->timer
, (milliseconds_T
) 10000,
169 (void (*)(void *)) session_info_timeout
,
170 (void *) (long) info
->id
);
174 info
->cache_mode
= cache_mode
;
176 if (uri
) info
->uri
= get_uri_reference(uri
);
177 if (referrer
) info
->referrer
= get_uri_reference(referrer
);
179 add_to_list(session_info
, info
);
184 static struct session
*
185 init_saved_session(struct terminal
*term
, int id
)
187 struct session_info
*info
= get_session_info(id
);
190 if (!info
) return NULL
;
192 ses
= init_session(info
->ses
, term
, info
->uri
, 0);
195 done_session_info(info
);
199 /* Only do the ses_goto()-thing for target=_blank. */
200 if (info
->uri
&& info
->task
!= TASK_NONE
) {
201 /* The init_session() call would have started the download but
202 * not with the requested cache mode etc. so interrupt that
204 abort_loading(ses
, 1);
206 ses
->reloadlevel
= info
->cache_mode
;
207 set_session_referrer(ses
, info
->referrer
);
208 ses_goto(ses
, info
->uri
, NULL
, NULL
, info
->cache_mode
,
212 done_session_info(info
);
217 static struct session
*
218 get_master_session(void)
222 foreach (ses
, sessions
)
223 if (ses
->tab
->term
->master
) {
224 struct window
*current_tab
= get_current_tab(ses
->tab
->term
);
226 return current_tab
? current_tab
->data
: NULL
;
233 get_current_download(struct session
*ses
)
235 struct download
*download
= NULL
;
237 if (!ses
) return NULL
;
240 download
= &ses
->loading
;
241 else if (have_location(ses
))
242 download
= &cur_loc(ses
)->download
;
244 if (download
&& download
->state
== S_OK
) {
245 struct file_to_load
*ftl
;
247 foreach (ftl
, ses
->more_files
)
248 if (file_to_load_is_active(ftl
))
249 return &ftl
->download
;
252 /* Note that @download isn't necessarily NULL here,
253 * if @ses->more_files is empty. -- Miciah */
258 print_error_dialog(struct session
*ses
, enum connection_state state
,
259 struct uri
*uri
, enum connection_priority priority
)
262 unsigned char *uristring
;
264 /* Don't show error dialogs for missing CSS stylesheets */
265 if (priority
== PRI_CSS
266 || !init_string(&msg
))
269 uristring
= uri
? get_uri_string(uri
, URI_PUBLIC
) : NULL
;
272 if (ses
->tab
->term
->utf8
)
273 decode_uri(uristring
);
275 #endif /* CONFIG_UTF8 */
276 decode_uri_for_display(uristring
);
277 add_format_to_string(&msg
,
278 _("Unable to retrieve %s", ses
->tab
->term
),
281 add_to_string(&msg
, ":\n\n");
284 add_to_string(&msg
, get_state_message(state
, ses
->tab
->term
));
286 info_box(ses
->tab
->term
, MSGBOX_FREE_TEXT
,
287 N_("Error"), ALIGN_CENTER
,
294 abort_files_load(struct session
*ses
, int interrupt
)
297 struct file_to_load
*ftl
;
300 foreach (ftl
, ses
->more_files
) {
301 if (!file_to_load_is_active(ftl
))
305 cancel_download(&ftl
->download
, interrupt
);
313 free_files(struct session
*ses
)
315 struct file_to_load
*ftl
;
317 abort_files_load(ses
, 0);
318 foreach (ftl
, ses
->more_files
) {
319 if (ftl
->cached
) object_unlock(ftl
->cached
);
320 if (ftl
->uri
) done_uri(ftl
->uri
);
321 mem_free_if(ftl
->target_frame
);
323 free_list(ses
->more_files
);
329 static void request_frameset(struct session
*, struct frameset_desc
*, int);
332 request_frame(struct session
*ses
, unsigned char *name
,
333 struct uri
*uri
, int depth
)
335 struct location
*loc
= cur_loc(ses
);
338 assertm(have_location(ses
), "request_frame: no location");
339 if_assert_failed
return;
341 foreach (frame
, loc
->frames
) {
342 struct document_view
*doc_view
;
344 if (strcasecmp(frame
->name
, name
))
347 foreach (doc_view
, ses
->scrn_frames
) {
348 if (doc_view
->vs
== &frame
->vs
&& doc_view
->document
->frame_desc
) {
349 request_frameset(ses
, doc_view
->document
->frame_desc
, depth
);
354 request_additional_file(ses
, name
, frame
->vs
.uri
, PRI_FRAME
);
358 frame
= mem_calloc(1, sizeof(*frame
));
361 frame
->name
= stracpy(name
);
367 init_vs(&frame
->vs
, uri
, -1);
369 add_to_list(loc
->frames
, frame
);
371 request_additional_file(ses
, name
, frame
->vs
.uri
, PRI_FRAME
);
375 request_frameset(struct session
*ses
, struct frameset_desc
*frameset_desc
, int depth
)
379 if (depth
> HTML_MAX_FRAME_DEPTH
) return;
381 depth
++; /* Inheritation counter (recursion brake ;) */
383 for (i
= 0; i
< frameset_desc
->n
; i
++) {
384 struct frame_desc
*frame_desc
= &frameset_desc
->frame_desc
[i
];
386 if (frame_desc
->subframe
) {
387 request_frameset(ses
, frame_desc
->subframe
, depth
);
388 } else if (frame_desc
->name
&& frame_desc
->uri
) {
389 request_frame(ses
, frame_desc
->name
,
390 frame_desc
->uri
, depth
);
397 load_css_imports(struct session
*ses
, struct document_view
*doc_view
)
399 struct document
*document
= doc_view
->document
;
403 if (!document
) return;
405 foreach_uri (uri
, index
, &document
->css_imports
) {
406 request_additional_file(ses
, "", uri
, PRI_CSS
);
410 #define load_css_imports(ses, doc_view)
413 #ifdef CONFIG_ECMASCRIPT
415 load_ecmascript_imports(struct session
*ses
, struct document_view
*doc_view
)
417 struct document
*document
= doc_view
->document
;
421 if (!document
) return;
423 foreach_uri (uri
, index
, &document
->ecmascript_imports
) {
424 request_additional_file(ses
, "", uri
, /* XXX */ PRI_CSS
);
428 #define load_ecmascript_imports(ses, doc_view)
432 load_frames(struct session
*ses
, struct document_view
*doc_view
)
434 struct document
*document
= doc_view
->document
;
436 if (!document
|| !document
->frame_desc
) return;
437 request_frameset(ses
, document
->frame_desc
, 0);
439 foreach (doc_view
, ses
->scrn_frames
) {
440 load_css_imports(ses
, doc_view
);
441 load_ecmascript_imports(ses
, doc_view
);
446 display_timer(struct session
*ses
)
448 timeval_T start
, stop
, duration
;
452 draw_formatted(ses
, 3);
454 timeval_sub(&duration
, &start
, &stop
);
456 t
= mult_ms(timeval_to_milliseconds(&duration
), DISPLAY_TIME
);
457 if (t
< DISPLAY_TIME_MIN
) t
= DISPLAY_TIME_MIN
;
458 install_timer(&ses
->display_timer
, t
,
459 (void (*)(void *)) display_timer
,
462 load_frames(ses
, ses
->doc_view
);
463 load_css_imports(ses
, ses
->doc_view
);
464 load_ecmascript_imports(ses
, ses
->doc_view
);
465 process_file_requests(ses
);
469 struct questions_entry
{
470 LIST_HEAD(struct questions_entry
);
472 void (*callback
)(struct session
*, void *);
476 INIT_LIST_HEAD(questions_queue
);
480 check_questions_queue(struct session
*ses
)
482 while (!list_empty(questions_queue
)) {
483 struct questions_entry
*q
= questions_queue
.next
;
485 q
->callback(ses
, q
->data
);
492 add_questions_entry(void (*callback
)(struct session
*, void *), void *data
)
494 struct questions_entry
*q
= mem_alloc(sizeof(*q
));
498 q
->callback
= callback
;
500 add_to_list(questions_queue
, q
);
503 #ifdef CONFIG_SCRIPTING
505 maybe_pre_format_html(struct cache_entry
*cached
, struct session
*ses
)
507 struct fragment
*fragment
;
508 static int pre_format_html_event
= EVENT_NONE
;
510 if (!cached
|| cached
->preformatted
)
513 fragment
= get_cache_fragment(cached
);
514 if (!fragment
) return;
516 /* We cannot do anything if the data are fragmented. */
517 if (!list_is_singleton(cached
->frag
)) return;
519 set_event_id(pre_format_html_event
, "pre-format-html");
520 trigger_event(pre_format_html_event
, ses
, cached
);
522 /* XXX: Keep this after the trigger_event, because hooks might call
523 * normalize_cache_entry()! */
524 cached
->preformatted
= 1;
529 check_incomplete_redirects(struct cache_entry
*cached
)
533 cached
= follow_cached_redirects(cached
);
534 if (cached
&& !cached
->redirect
) {
535 /* XXX: This is not quite true, but does that difference
537 return cached
->incomplete
;
544 session_is_loading(struct session
*ses
)
546 struct download
*download
= get_current_download(ses
);
548 if (!download
) return 0;
550 if (!is_in_result_state(download
->state
))
553 /* The validness of download->cached (especially the download struct in
554 * ses->loading) is hard to maintain so check before using it.
555 * Related to bug 559. */
557 && cache_entry_is_valid(download
->cached
)
558 && check_incomplete_redirects(download
->cached
))
565 doc_loading_callback(struct download
*download
, struct session
*ses
)
569 if (is_in_result_state(download
->state
)) {
570 #ifdef CONFIG_SCRIPTING
571 maybe_pre_format_html(download
->cached
, ses
);
573 kill_timer(&ses
->display_timer
);
575 draw_formatted(ses
, 1);
577 if (get_cmd_opt_bool("auto-submit")) {
578 if (!list_empty(ses
->doc_view
->document
->forms
)) {
579 get_cmd_opt_bool("auto-submit") = 0;
584 load_frames(ses
, ses
->doc_view
);
585 load_css_imports(ses
, ses
->doc_view
);
586 load_ecmascript_imports(ses
, ses
->doc_view
);
587 process_file_requests(ses
);
590 && ses
->doc_view
->document
591 && ses
->doc_view
->document
->refresh
592 && get_opt_bool("document.browse.refresh")) {
593 start_document_refresh(ses
->doc_view
->document
->refresh
,
597 if (download
->state
!= S_OK
) {
598 print_error_dialog(ses
, download
->state
,
599 ses
->doc_view
->document
->uri
,
603 } else if (is_in_transfering_state(download
->state
)
604 && ses
->display_timer
== TIMER_ID_UNDEF
) {
608 check_questions_queue(ses
);
609 print_screen_status(ses
);
611 #ifdef CONFIG_GLOBHIST
612 if (download
->pri
!= PRI_CSS
) {
613 unsigned char *title
= ses
->doc_view
->document
->title
;
617 uri
= download
->conn
->proxied_uri
;
618 else if (download
->cached
)
619 uri
= download
->cached
->uri
;
624 add_global_history_item(struri(uri
), title
, time(NULL
));
628 if (submit
) auto_submit_form(ses
);
632 file_loading_callback(struct download
*download
, struct file_to_load
*ftl
)
634 if (ftl
->download
.cached
&& ftl
->cached
!= ftl
->download
.cached
) {
635 if (ftl
->cached
) object_unlock(ftl
->cached
);
636 ftl
->cached
= ftl
->download
.cached
;
637 object_lock(ftl
->cached
);
640 /* FIXME: We need to do content-type check here! However, we won't
641 * handle properly the "Choose action" dialog now :(. */
642 if (ftl
->cached
&& !ftl
->cached
->redirect_get
&& download
->pri
!= PRI_CSS
) {
643 struct session
*ses
= ftl
->ses
;
644 struct uri
*loading_uri
= ses
->loading_uri
;
645 unsigned char *target_frame
= ses
->task
.target
.frame
;
647 ses
->loading_uri
= ftl
->uri
;
648 ses
->task
.target
.frame
= ftl
->target_frame
;
649 setup_download_handler(ses
, &ftl
->download
, ftl
->cached
, 1);
650 ses
->loading_uri
= loading_uri
;
651 ses
->task
.target
.frame
= target_frame
;
654 doc_loading_callback(download
, ftl
->ses
);
657 static struct file_to_load
*
658 request_additional_file(struct session
*ses
, unsigned char *name
, struct uri
*uri
, int pri
)
660 struct file_to_load
*ftl
;
662 if (uri
->protocol
== PROTOCOL_UNKNOWN
) {
666 /* XXX: We cannot run the external handler here, because
667 * request_additional_file() is called many times for a single URL
668 * (normally the foreach() right below catches them all). Anyway,
669 * having <frame src="mailto:foo"> would be just weird, wouldn't it?
671 if (get_protocol_external_handler(ses
->tab
->term
, uri
)) {
675 foreach (ftl
, ses
->more_files
) {
676 if (compare_uri(ftl
->uri
, uri
, URI_BASE
)) {
677 if (ftl
->pri
> pri
) {
679 move_download(&ftl
->download
, &ftl
->download
, pri
);
685 ftl
= mem_calloc(1, sizeof(*ftl
));
686 if (!ftl
) return NULL
;
688 ftl
->uri
= get_uri_reference(uri
);
689 ftl
->target_frame
= stracpy(name
);
690 ftl
->download
.callback
= (download_callback_T
*) file_loading_callback
;
691 ftl
->download
.data
= ftl
;
695 add_to_list(ses
->more_files
, ftl
);
701 load_additional_file(struct file_to_load
*ftl
, struct document_view
*doc_view
,
702 enum cache_mode cache_mode
)
704 struct uri
*referrer
= doc_view
&& doc_view
->document
705 ? doc_view
->document
->uri
: NULL
;
707 load_uri(ftl
->uri
, referrer
, &ftl
->download
, ftl
->pri
, cache_mode
, -1);
711 process_file_requests(struct session
*ses
)
713 if (ses
->status
.processing_file_requests
) return;
714 ses
->status
.processing_file_requests
= 1;
717 struct file_to_load
*ftl
;
720 foreach (ftl
, ses
->more_files
) {
721 struct document_view
*doc_view
;
728 doc_view
= current_frame(ses
);
729 load_additional_file(ftl
, doc_view
, CACHE_MODE_NORMAL
);
736 ses
->status
.processing_file_requests
= 0;
741 dialog_goto_url_open(void *data
)
743 dialog_goto_url((struct session
*) data
, NULL
);
746 /* Returns 0 if the first session was not properly initialized and
747 * setup_session() should be called on the session as well. */
749 setup_first_session(struct session
*ses
, struct uri
*uri
)
751 /* [gettext_accelerator_context(setup_first_session)] */
752 struct terminal
*term
= ses
->tab
->term
;
754 if (!*get_opt_str("protocol.http.user_agent")) {
756 N_("Warning"), ALIGN_CENTER
,
757 N_("You have empty string in protocol.http.user_agent - "
758 "this was a default value in the past, substituted by "
759 "default ELinks User-Agent string. However, currently "
760 "this means that NO User-Agent HEADER "
761 "WILL BE SENT AT ALL - if this is really what you want, "
762 "set its value to \" \", otherwise please delete line "
763 "with this settings from your configuration file (if you "
764 "have no idea what I'm talking about, just do this), so "
765 "that correct default setting will be used. Apologies for "
766 "any inconvience caused."));
769 if (!get_opt_bool("config.saving_style_w")) {
770 struct option
*opt
= get_opt_rec(config_options
, "config.saving_style_w");
771 opt
->value
.number
= 1;
772 option_changed(ses
, opt
, opt
);
773 if (get_opt_int("config.saving_style") != 3) {
775 N_("Warning"), ALIGN_CENTER
,
776 N_("You have option config.saving_style set to "
777 "a de facto obsolete value. The configuration "
778 "saving algorithms of ELinks were changed from "
779 "the last time you upgraded ELinks. Now, only "
780 "those options which you actually changed are "
781 "saved to the configuration file, instead of "
782 "just all the options. This simplifies our "
783 "situation greatly when we see that some option "
784 "has inappropriate default value or we need to "
785 "change semantic of some option in a subtle way. "
786 "Thus, we recommend you to change the value of "
787 "config.saving_style option to 3 in order to get "
788 "the \"right\" behaviour. Apologies for any "
789 "inconvience caused."));
794 /* Only open the goto URL dialog if no URI was passed on the
796 void *handler
= uri
? NULL
: dialog_goto_url_open
;
800 msg_box(term
, NULL
, 0,
801 N_("Welcome"), ALIGN_CENTER
,
802 N_("Welcome to ELinks!\n\n"
803 "Press ESC for menu. Documentation is available in "
806 N_("~OK"), handler
, B_ENTER
| B_ESC
);
808 /* If there is no URI the goto dialog will pop up so there is
809 * no need to call setup_session(). */
812 #ifdef CONFIG_BOOKMARKS
813 } else if (!uri
&& get_opt_bool("ui.sessions.auto_restore")) {
814 unsigned char *folder
;
816 folder
= get_opt_str("ui.sessions.auto_save_foldername");
817 open_bookmark_folder(ses
, folder
);
822 /* If there's a URI to load we have to call */
826 /* First load the current URI of the base session. In most cases it will just
827 * be fetched from the cache so that the new tab will not appear ``empty' while
828 * loading the real URI or showing the goto URL dialog. */
830 setup_session(struct session
*ses
, struct uri
*uri
, struct session
*base
)
832 if (base
&& have_location(base
)) {
833 goto_uri(ses
, cur_loc(base
)->vs
.uri
);
834 if (ses
->doc_view
&& ses
->doc_view
->vs
835 && base
->doc_view
&& base
->doc_view
->vs
) {
836 struct view_state
*vs
= ses
->doc_view
->vs
;
839 copy_vs(vs
, base
->doc_view
->vs
);
846 } else if (!goto_url_home(ses
)) {
847 if (get_opt_bool("ui.startup_goto_dialog")) {
848 dialog_goto_url_open(ses
);
854 init_session(struct session
*base_session
, struct terminal
*term
,
855 struct uri
*uri
, int in_background
)
857 struct session
*ses
= mem_calloc(1, sizeof(*ses
));
859 if (!ses
) return NULL
;
861 ses
->tab
= init_tab(term
, ses
, tabwin_func
);
867 create_history(&ses
->history
);
868 init_list(ses
->scrn_frames
);
869 init_list(ses
->more_files
);
870 init_list(ses
->type_queries
);
871 ses
->task
.type
= TASK_NONE
;
872 ses
->display_timer
= TIMER_ID_UNDEF
;
875 init_led_panel(&ses
->status
.leds
);
876 ses
->status
.ssl_led
= register_led(ses
, 0);
877 ses
->status
.insert_mode_led
= register_led(ses
, 1);
878 ses
->status
.ecmascript_led
= register_led(ses
, 2);
879 ses
->status
.popup_led
= register_led(ses
, 3);
881 ses
->status
.force_show_status_bar
= -1;
882 ses
->status
.force_show_title_bar
= -1;
884 add_to_list(sessions
, ses
);
886 /* Update the status -- most importantly the info about whether to the
887 * show the title, tab and status bar -- _before_ loading the URI so
888 * the document cache is not filled with useless documents if the
889 * content is already cached.
891 * For big document it also reduces memory usage quite a bit because
892 * (atleast that is my interpretation --jonas) the old document will
893 * have a chance to be released before rendering a new one. A few
894 * numbers when opening a new tab while viewing debians package list
897 * 9307 jonas 15 0 34252 30m 5088 S 0.0 12.2 0:03.63 elinks-old
898 * 9305 jonas 15 0 17060 13m 5088 S 0.0 5.5 0:02.07 elinks-new
902 /* Check if the newly inserted session is the only in the list and do
903 * the special setup for the first session, */
904 if (!list_is_singleton(sessions
) || !setup_first_session(ses
, uri
)) {
905 setup_session(ses
, uri
, base_session
);
909 switch_to_tab(term
, get_tab_number(ses
->tab
), -1);
911 if (!term
->main_menu
) activate_bfu_technology(ses
, -1);
916 init_remote_session(struct session
*ses
, enum remote_session_flags
*remote_ptr
,
919 enum remote_session_flags remote
= *remote_ptr
;
921 if (remote
& SES_REMOTE_CURRENT_TAB
) {
923 /* Mask out the current tab flag */
924 *remote_ptr
= remote
& ~SES_REMOTE_CURRENT_TAB
;
926 /* Remote session was masked out. Open all following URIs in
929 *remote_ptr
= SES_REMOTE_NEW_TAB
;
931 } else if (remote
& SES_REMOTE_NEW_TAB
) {
932 /* FIXME: This is not perfect. Doing multiple -remote
933 * with no URLs will make the goto dialogs
934 * inaccessible. Maybe we should not support this kind
935 * of thing or make the window focus detecting code
936 * more intelligent. --jonas */
937 open_uri_in_new_tab(ses
, uri
, 0, 1);
939 if (remote
& SES_REMOTE_PROMPT_URL
) {
940 dialog_goto_url_open(ses
);
943 } else if (remote
& SES_REMOTE_NEW_WINDOW
) {
944 /* FIXME: It is quite rude because we just take the first
945 * possibility and should maybe make it possible to specify
946 * new-screen etc via -remote "openURL(..., new-*)" --jonas */
947 if (!can_open_in_new(ses
->tab
->term
))
950 open_uri_in_new_window(ses
, uri
, NULL
,
951 ses
->tab
->term
->environment
,
952 CACHE_MODE_NORMAL
, TASK_NONE
);
954 } else if (remote
& SES_REMOTE_ADD_BOOKMARK
) {
955 #ifdef CONFIG_BOOKMARKS
957 add_bookmark(NULL
, 1, struri(uri
), struri(uri
));
960 } else if (remote
& SES_REMOTE_INFO_BOX
) {
965 text
= memacpy(uri
->data
, uri
->datalen
);
968 info_box(ses
->tab
->term
, MSGBOX_FREE_TEXT
,
969 N_("Error"), ALIGN_CENTER
,
972 } else if (remote
& SES_REMOTE_PROMPT_URL
) {
973 dialog_goto_url_open(ses
);
980 encode_session_info(struct string
*info
, struct list_head
*url_list
)
982 struct string_list_item
*url
;
984 if (!init_string(info
)) return NULL
;
986 foreach (url
, *url_list
) {
987 struct string
*str
= &url
->string
;
989 add_bytes_to_string(info
, str
->source
, str
->length
+ 1);
995 /* Older elinks versions (up to and including 0.9.1) sends no magic variable and if
996 * this is detected we fallback to the old session info format. For this format
997 * the magic member of terminal_info hold the length of the URI string. The
998 * old format is handled by the default label in the switch.
1000 * The new session info format supports extraction of multiple URIS from the
1001 * terminal_info data member. The magic variable controls how to interpret
1004 * INTERLINK_NORMAL_MAGIC means use the terminal_info session_info
1005 * variable as an id for a saved session.
1007 * INTERLINK_REMOTE_MAGIC means use the terminal_info session_info
1008 * variable as the remote session flags. */
1011 decode_session_info(struct terminal
*term
, struct terminal_info
*info
)
1013 int len
= info
->length
;
1014 struct session
*base_session
= NULL
;
1015 enum remote_session_flags remote
= 0;
1018 switch (info
->magic
) {
1019 case INTERLINK_NORMAL_MAGIC
:
1020 /* Lookup if there are any saved sessions that should be
1021 * resumed using the session_info as an id. The id is derived
1022 * from the value of -base-session command line option in the
1023 * connecting instance.
1025 * At the moment it is only used when opening instances in new
1026 * window to figure out how to initialize it when the new
1027 * instance connects to the master.
1029 * We don't need to handle it for the old format since new
1030 * instances connecting this way will always be of the same
1031 * origin as the master. */
1032 if (init_saved_session(term
, info
->session_info
))
1036 case INTERLINK_REMOTE_MAGIC
:
1037 /* This could mean some fatal bug but I am unsure if we can
1038 * actually assert it since people could pour all kinds of crap
1039 * down the socket. */
1040 if (!info
->session_info
) {
1041 INTERNAL("Remote magic with no remote flags");
1045 remote
= info
->session_info
;
1047 /* If processing session info from a -remote instance we want
1048 * to hook up with the master so we can handle request for
1049 * stuff in current tab. */
1050 base_session
= get_master_session();
1051 if (!base_session
) return 0;
1056 /* The old format calculates the session_info and magic members
1057 * as part of the data that should be decoded so we have to
1058 * substract it to get the size of the actual URI data. */
1059 len
-= sizeof(info
->session_info
) + sizeof(info
->magic
);
1061 /* Extract URI containing @magic bytes */
1062 if (info
->magic
<= 0 || info
->magic
> len
)
1070 return !!init_session(base_session
, term
, NULL
, 0);
1072 /* Even though there are no URIs we still have to
1073 * handle remote stuff. */
1074 init_remote_session(base_session
, &remote
, NULL
);
1080 /* Extract multiple (possible) NUL terminated URIs */
1082 unsigned char *end
= memchr(str
, 0, len
);
1083 int urilength
= end
? end
- str
: len
;
1084 struct uri
*uri
= NULL
;
1085 unsigned char *uristring
= memacpy(str
, urilength
);
1088 uri
= get_hooked_uri(uristring
, base_session
, term
->cwd
);
1089 mem_free(uristring
);
1092 len
-= urilength
+ 1;
1093 str
+= urilength
+ 1;
1098 init_remote_session(base_session
, &remote
, uri
);
1101 int backgrounded
= !list_empty(term
->windows
);
1103 struct session
*ses
;
1106 uri
= get_uri("about:blank", 0);
1108 ses
= init_session(base_session
, term
, uri
, backgrounded
);
1110 /* End loop if initialization fails */
1112 } else if (bad_url
) {
1113 print_error_dialog(ses
, S_BAD_URL
, NULL
, PRI_MAIN
);
1118 if (uri
) done_uri(uri
);
1121 /* If we only initialized remote sessions or didn't manage to add any
1122 * new tabs return zero so the terminal will be destroyed ASAP. */
1123 return remote
? 0 : !list_empty(term
->windows
);
1128 abort_loading(struct session
*ses
, int interrupt
)
1130 if (have_location(ses
)) {
1131 struct location
*loc
= cur_loc(ses
);
1133 cancel_download(&loc
->download
, interrupt
);
1134 abort_files_load(ses
, interrupt
);
1136 abort_preloading(ses
, interrupt
);
1140 destroy_session(struct session
*ses
)
1142 struct document_view
*doc_view
;
1145 if_assert_failed
return;
1147 destroy_downloads(ses
);
1148 abort_loading(ses
, 0);
1150 if (ses
->doc_view
) {
1151 detach_formatted(ses
->doc_view
);
1152 mem_free(ses
->doc_view
);
1155 foreach (doc_view
, ses
->scrn_frames
)
1156 detach_formatted(doc_view
);
1158 free_list(ses
->scrn_frames
);
1160 destroy_history(&ses
->history
);
1161 set_session_referrer(ses
, NULL
);
1163 if (ses
->loading_uri
) done_uri(ses
->loading_uri
);
1165 kill_timer(&ses
->display_timer
);
1167 while (!list_empty(ses
->type_queries
))
1168 done_type_query(ses
->type_queries
.next
);
1170 if (ses
->download_uri
) done_uri(ses
->download_uri
);
1171 mem_free_if(ses
->search_word
);
1172 mem_free_if(ses
->last_search_word
);
1173 mem_free_if(ses
->status
.last_title
);
1174 #ifdef CONFIG_ECMASCRIPT
1175 mem_free_if(ses
->status
.window_status
);
1181 reload(struct session
*ses
, enum cache_mode cache_mode
)
1183 abort_loading(ses
, 0);
1185 if (cache_mode
== CACHE_MODE_INCREMENT
) {
1186 cache_mode
= CACHE_MODE_NEVER
;
1187 if (ses
->reloadlevel
< CACHE_MODE_NEVER
)
1188 cache_mode
= ++ses
->reloadlevel
;
1190 ses
->reloadlevel
= cache_mode
;
1193 if (have_location(ses
)) {
1194 struct location
*loc
= cur_loc(ses
);
1195 struct file_to_load
*ftl
;
1196 struct document_view
*doc_view
= current_frame(ses
);
1198 #ifdef CONFIG_ECMASCRIPT
1199 loc
->vs
.ecmascript_fragile
= 1;
1202 /* FIXME: When reloading use loading_callback and set up a
1203 * session task so that the reloading will work even when the
1204 * reloaded document contains redirects. This is needed atleast
1205 * when reloading HTTP auth document after the user has entered
1207 loc
->download
.data
= ses
;
1208 loc
->download
.callback
= (download_callback_T
*) doc_loading_callback
;
1210 load_uri(loc
->vs
.uri
, ses
->referrer
, &loc
->download
, PRI_MAIN
, cache_mode
, -1);
1212 foreach (ftl
, ses
->more_files
) {
1213 if (file_to_load_is_active(ftl
))
1216 ftl
->download
.data
= ftl
;
1217 ftl
->download
.callback
= (download_callback_T
*) file_loading_callback
;
1219 load_additional_file(ftl
, doc_view
, cache_mode
);
1226 ses_find_frame(struct session
*ses
, unsigned char *name
)
1228 struct location
*loc
= cur_loc(ses
);
1229 struct frame
*frame
;
1231 assertm(have_location(ses
), "ses_request_frame: no location yet");
1232 if_assert_failed
return NULL
;
1234 foreachback (frame
, loc
->frames
)
1235 if (!strcasecmp(frame
->name
, name
))
1242 set_session_referrer(struct session
*ses
, struct uri
*referrer
)
1244 if (ses
->referrer
) done_uri(ses
->referrer
);
1245 ses
->referrer
= referrer
? get_uri_reference(referrer
) : NULL
;
1249 tabwin_func(struct window
*tab
, struct term_event
*ev
)
1251 struct session
*ses
= tab
->data
;
1255 if (ses
) destroy_session(ses
);
1256 if (!list_empty(sessions
)) update_status();
1263 if (!ses
|| ses
->tab
!= get_current_tab(ses
->tab
->term
))
1266 draw_formatted(ses
, tab
->resize
);
1268 load_frames(ses
, ses
->doc_view
);
1269 process_file_requests(ses
);
1272 print_screen_status(ses
);
1277 /* This check is related to bug 296 */
1278 assert(ses
->tab
== get_current_tab(ses
->tab
->term
));
1279 send_event(ses
, ev
);
1285 * Gets the url being viewed by this session. Writes it into str.
1286 * A maximum of str_size bytes (including null) will be written.
1289 get_current_url(struct session
*ses
, unsigned char *str
, size_t str_size
)
1294 assert(str
&& str_size
> 0);
1296 uri
= have_location(ses
) ? cur_loc(ses
)->vs
.uri
: ses
->loading_uri
;
1298 /* Not looking or loading anything */
1299 if (!uri
) return NULL
;
1301 /* Ensure that the url size is not greater than str_size.
1302 * We can't just happily strncpy(str, here, str_size)
1303 * because we have to stop at POST_CHAR, not only at NULL. */
1304 length
= int_min(get_real_uri_length(uri
), str_size
- 1);
1306 return safe_strncpy(str
, struri(uri
), length
+ 1);
1310 * Gets the title of the page being viewed by this session. Writes it into str.
1311 * A maximum of str_size bytes (including null) will be written.
1314 get_current_title(struct session
*ses
, unsigned char *str
, size_t str_size
)
1316 struct document_view
*doc_view
= current_frame(ses
);
1318 assert(str
&& str_size
> 0);
1320 /* Ensure that the title is defined */
1321 /* TODO: Try globhist --jonas */
1322 if (doc_view
&& doc_view
->document
->title
)
1323 return safe_strncpy(str
, doc_view
->document
->title
, str_size
);
1329 * Gets the url of the link currently selected. Writes it into str.
1330 * A maximum of str_size bytes (including null) will be written.
1333 get_current_link_url(struct session
*ses
, unsigned char *str
, size_t str_size
)
1335 struct link
*link
= get_current_session_link(ses
);
1337 assert(str
&& str_size
> 0);
1339 if (!link
) return NULL
;
1341 return safe_strncpy(str
, link
->where
? link
->where
: link
->where_img
, str_size
);
1344 /* get_current_link_name: returns the name of the current link
1345 * (the text between <A> and </A>), str is a preallocated string,
1346 * str_size includes the null char. */
1348 get_current_link_name(struct session
*ses
, unsigned char *str
, size_t str_size
)
1350 struct link
*link
= get_current_session_link(ses
);
1351 unsigned char *where
, *name
= NULL
;
1353 assert(str
&& str_size
> 0);
1355 if (!link
) return NULL
;
1357 where
= link
->where
? link
->where
: link
->where_img
;
1358 #ifdef CONFIG_GLOBHIST
1360 struct global_history_item
*item
;
1362 item
= get_global_history_item(where
);
1363 if (item
) name
= item
->title
;
1366 if (!name
) name
= get_link_name(link
);
1367 if (!name
) name
= where
;
1369 return safe_strncpy(str
, name
, str_size
);
1373 get_current_link_in_view(struct document_view
*doc_view
)
1375 struct link
*link
= get_current_link(doc_view
);
1377 return link
&& !link_is_form(link
) ? link
: NULL
;
1381 get_current_session_link(struct session
*ses
)
1383 return get_current_link_in_view(current_frame(ses
));
1387 eat_kbd_repeat_count(struct session
*ses
)
1389 int count
= ses
->kbdprefix
.repeat_count
;
1391 ses
->kbdprefix
.repeat_count
= 0;