1 /** Sessions managment - you'll find things here which you wouldn't expect
14 #include "bfu/dialog.h"
15 #include "bookmarks/bookmarks.h"
16 #include "cache/cache.h"
17 #include "config/home.h"
18 #include "config/options.h"
19 #include "dialogs/menu.h"
20 #include "dialogs/status.h"
21 #include "document/document.h"
22 #include "document/html/frames.h"
23 #include "document/refresh.h"
24 #include "document/renderer.h"
25 #include "document/view.h"
26 #include "globhist/globhist.h"
27 #include "intl/gettext/libintl.h"
28 #include "main/event.h"
29 #include "main/object.h"
30 #include "main/timer.h"
31 #include "network/connection.h"
32 #include "network/state.h"
33 #include "osdep/newwin.h"
34 #include "protocol/protocol.h"
35 #include "protocol/uri.h"
36 #ifdef CONFIG_SCRIPTING_SPIDERMONKEY
37 # include "scripting/smjs/smjs.h"
39 #include "session/download.h"
40 #include "session/history.h"
41 #include "session/location.h"
42 #include "session/session.h"
43 #include "session/task.h"
44 #include "terminal/tab.h"
45 #include "terminal/terminal.h"
46 #include "terminal/window.h"
47 #include "util/conv.h"
48 #include "util/error.h"
49 #include "util/memlist.h"
50 #include "util/memory.h"
51 #include "util/string.h"
52 #include "util/time.h"
53 #include "viewer/text/draw.h"
54 #include "viewer/text/form.h"
55 #include "viewer/text/link.h"
56 #include "viewer/text/view.h"
60 LIST_HEAD(struct file_to_load
);
63 unsigned int req_sent
:1;
65 struct cache_entry
*cached
;
66 unsigned char *target_frame
;
68 struct download download
;
71 /** This structure and related functions are used to maintain information
72 * for instances opened in new windows. We store all related session info like
73 * URI and base session to clone from so that when the new instance connects
74 * we can look up this information. In case of failure the session information
77 LIST_HEAD(struct session_info
);
85 enum cache_mode cache_mode
;
88 #define file_to_load_is_active(ftl) ((ftl)->req_sent && is_in_progress_state((ftl)->download.state))
91 INIT_LIST_OF(struct session
, sessions
);
93 enum remote_session_flags remote_session_flags
;
96 static struct file_to_load
*request_additional_file(struct session
*,
100 static window_handler_T tabwin_func
;
103 static INIT_LIST_OF(struct session_info
, session_info
);
104 static int session_info_id
= 1;
106 static struct session_info
*
107 get_session_info(int id
)
109 struct session_info
*info
;
111 foreach (info
, session_info
) {
114 if (info
->id
!= id
) continue;
116 /* Make sure the info->ses session is still with us. */
117 foreach (ses
, sessions
)
118 if (ses
== info
->ses
)
129 done_session_info(struct session_info
*info
)
132 kill_timer(&info
->timer
);
134 if (info
->uri
) done_uri(info
->uri
);
135 if (info
->referrer
) done_uri(info
->referrer
);
140 done_saved_session_info(void)
142 while (!list_empty(session_info
))
143 done_session_info(session_info
.next
);
146 /** Timer callback for session_info.timer. As explained in install_timer(),
147 * this function must erase the expired timer ID from all variables. */
149 session_info_timeout(int id
)
151 struct session_info
*info
= get_session_info(id
);
154 info
->timer
= TIMER_ID_UNDEF
;
155 /* The expired timer ID has now been erased. */
156 done_session_info(info
);
160 add_session_info(struct session
*ses
, struct uri
*uri
, struct uri
*referrer
,
161 enum cache_mode cache_mode
, enum task_type task
)
163 struct session_info
*info
= mem_calloc(1, sizeof(*info
));
165 if (!info
) return -1;
167 info
->id
= session_info_id
++;
168 /* I don't know what a reasonable start up time for a new instance is
169 * but it won't hurt to have a few seconds atleast. --jonas */
170 install_timer(&info
->timer
, (milliseconds_T
) 10000,
171 (void (*)(void *)) session_info_timeout
,
172 (void *) (long) info
->id
);
176 info
->cache_mode
= cache_mode
;
178 if (uri
) info
->uri
= get_uri_reference(uri
);
179 if (referrer
) info
->referrer
= get_uri_reference(referrer
);
181 add_to_list(session_info
, info
);
186 static struct session
*
187 init_saved_session(struct terminal
*term
, int id
)
189 struct session_info
*info
= get_session_info(id
);
192 if (!info
) return NULL
;
194 ses
= init_session(info
->ses
, term
, info
->uri
, 0);
197 done_session_info(info
);
201 /* Only do the ses_goto()-thing for target=_blank. */
202 if (info
->uri
&& info
->task
!= TASK_NONE
) {
203 /* The init_session() call would have started the download but
204 * not with the requested cache mode etc. so interrupt that
206 abort_loading(ses
, 1);
208 ses
->reloadlevel
= info
->cache_mode
;
209 set_session_referrer(ses
, info
->referrer
);
210 ses_goto(ses
, info
->uri
, NULL
, NULL
, info
->cache_mode
,
214 done_session_info(info
);
219 static struct session
*
220 get_master_session(void)
224 foreach (ses
, sessions
)
225 if (ses
->tab
->term
->master
) {
226 struct window
*current_tab
= get_current_tab(ses
->tab
->term
);
228 return current_tab
? current_tab
->data
: NULL
;
234 /** @relates session */
236 get_current_download(struct session
*ses
)
238 struct download
*download
= NULL
;
240 if (!ses
) return NULL
;
243 download
= &ses
->loading
;
244 else if (have_location(ses
))
245 download
= &cur_loc(ses
)->download
;
247 if (download
&& is_in_state(download
->state
, S_OK
)) {
248 struct file_to_load
*ftl
;
250 foreach (ftl
, ses
->more_files
)
251 if (file_to_load_is_active(ftl
))
252 return &ftl
->download
;
255 /* Note that @download isn't necessarily NULL here,
256 * if @ses->more_files is empty. -- Miciah */
261 retry_connection_without_verification(void *data
)
263 struct delayed_open
*deo
= (struct delayed_open
*)data
;
266 deo
->ses
->verify
= 0;
267 goto_uri(deo
->ses
, deo
->uri
);
274 print_error_dialog(struct session
*ses
, struct connection_state state
,
275 struct uri
*uri
, enum connection_priority priority
)
278 unsigned char *uristring
;
280 /* Don't show error dialogs for missing CSS stylesheets */
281 if (priority
== PRI_CSS
282 || !init_string(&msg
))
285 uristring
= uri
? get_uri_string(uri
, URI_PUBLIC
) : NULL
;
288 if (ses
->tab
->term
->utf8_cp
)
289 decode_uri(uristring
);
291 #endif /* CONFIG_UTF8 */
292 decode_uri_for_display(uristring
);
293 add_format_to_string(&msg
,
294 _("Unable to retrieve %s", ses
->tab
->term
),
297 add_to_string(&msg
, ":\n\n");
300 add_to_string(&msg
, get_state_message(state
, ses
->tab
->term
));
302 if (!ses
->verify
|| !uri
) {
303 info_box(ses
->tab
->term
, MSGBOX_FREE_TEXT
,
304 N_("Error"), ALIGN_CENTER
,
307 struct delayed_open
*deo
= mem_calloc(1, sizeof(*deo
));
311 add_to_string(&msg
, "\n\n");
312 add_to_string(&msg
, N_("Retry without verification?"));
314 deo
->uri
= get_uri_reference(uri
);
316 msg_box(ses
->tab
->term
, NULL
, MSGBOX_FREE_TEXT
,
317 N_("Error"), ALIGN_CENTER
,
320 MSG_BOX_BUTTON(N_("~Yes"), retry_connection_without_verification
, B_ENTER
),
321 MSG_BOX_BUTTON(N_("~No"), NULL
, B_ESC
));
328 abort_files_load(struct session
*ses
, int interrupt
)
331 struct file_to_load
*ftl
;
334 foreach (ftl
, ses
->more_files
) {
335 if (!file_to_load_is_active(ftl
))
339 cancel_download(&ftl
->download
, interrupt
);
347 free_files(struct session
*ses
)
349 struct file_to_load
*ftl
;
351 abort_files_load(ses
, 0);
352 foreach (ftl
, ses
->more_files
) {
353 if (ftl
->cached
) object_unlock(ftl
->cached
);
354 if (ftl
->uri
) done_uri(ftl
->uri
);
355 mem_free_if(ftl
->target_frame
);
357 free_list(ses
->more_files
);
363 static void request_frameset(struct session
*, struct frameset_desc
*, int);
366 request_frame(struct session
*ses
, unsigned char *name
,
367 struct uri
*uri
, int depth
)
369 struct location
*loc
= cur_loc(ses
);
372 assertm(have_location(ses
), "request_frame: no location");
373 if_assert_failed
return;
375 foreach (frame
, loc
->frames
) {
376 struct document_view
*doc_view
;
378 if (c_strcasecmp(frame
->name
, name
))
381 foreach (doc_view
, ses
->scrn_frames
) {
382 if (doc_view
->vs
== &frame
->vs
&& doc_view
->document
->frame_desc
) {
383 request_frameset(ses
, doc_view
->document
->frame_desc
, depth
);
388 request_additional_file(ses
, name
, frame
->vs
.uri
, PRI_FRAME
);
392 frame
= mem_calloc(1, sizeof(*frame
));
395 frame
->name
= stracpy(name
);
401 init_vs(&frame
->vs
, uri
, -1);
403 add_to_list(loc
->frames
, frame
);
405 request_additional_file(ses
, name
, frame
->vs
.uri
, PRI_FRAME
);
409 request_frameset(struct session
*ses
, struct frameset_desc
*frameset_desc
, int depth
)
413 if (depth
> HTML_MAX_FRAME_DEPTH
) return;
415 depth
++; /* Inheritation counter (recursion brake ;) */
417 for (i
= 0; i
< frameset_desc
->n
; i
++) {
418 struct frame_desc
*frame_desc
= &frameset_desc
->frame_desc
[i
];
420 if (frame_desc
->subframe
) {
421 request_frameset(ses
, frame_desc
->subframe
, depth
);
422 } else if (frame_desc
->name
&& frame_desc
->uri
) {
423 request_frame(ses
, frame_desc
->name
,
424 frame_desc
->uri
, depth
);
431 load_css_imports(struct session
*ses
, struct document_view
*doc_view
)
433 struct document
*document
= doc_view
->document
;
437 if (!document
) return;
439 foreach_uri (uri
, index
, &document
->css_imports
) {
440 request_additional_file(ses
, "", uri
, PRI_CSS
);
444 #define load_css_imports(ses, doc_view)
447 #ifdef CONFIG_ECMASCRIPT
449 load_ecmascript_imports(struct session
*ses
, struct document_view
*doc_view
)
451 struct document
*document
= doc_view
->document
;
455 if (!document
) return;
457 foreach_uri (uri
, index
, &document
->ecmascript_imports
) {
458 request_additional_file(ses
, "", uri
, /* XXX */ PRI_CSS
);
462 #define load_ecmascript_imports(ses, doc_view)
465 NONSTATIC_INLINE
void
466 load_frames(struct session
*ses
, struct document_view
*doc_view
)
468 struct document
*document
= doc_view
->document
;
470 if (!document
|| !document
->frame_desc
) return;
471 request_frameset(ses
, document
->frame_desc
, 0);
473 foreach (doc_view
, ses
->scrn_frames
) {
474 load_css_imports(ses
, doc_view
);
475 load_ecmascript_imports(ses
, doc_view
);
479 /** Timer callback for session.display_timer. As explained in install_timer(),
480 * this function must erase the expired timer ID from all variables. */
482 display_timer(struct session
*ses
)
484 timeval_T start
, stop
, duration
;
488 draw_formatted(ses
, 3);
490 timeval_sub(&duration
, &start
, &stop
);
492 t
= mult_ms(timeval_to_milliseconds(&duration
), DISPLAY_TIME
);
493 if (t
< DISPLAY_TIME_MIN
) t
= DISPLAY_TIME_MIN
;
494 install_timer(&ses
->display_timer
, t
,
495 (void (*)(void *)) display_timer
,
497 /* The expired timer ID has now been erased. */
499 load_frames(ses
, ses
->doc_view
);
500 load_css_imports(ses
, ses
->doc_view
);
501 load_ecmascript_imports(ses
, ses
->doc_view
);
502 process_file_requests(ses
);
506 struct questions_entry
{
507 LIST_HEAD(struct questions_entry
);
509 void (*callback
)(struct session
*, void *);
513 INIT_LIST_OF(struct questions_entry
, questions_queue
);
517 check_questions_queue(struct session
*ses
)
519 while (!list_empty(questions_queue
)) {
520 struct questions_entry
*q
= questions_queue
.next
;
522 q
->callback(ses
, q
->data
);
529 add_questions_entry(void (*callback
)(struct session
*, void *), void *data
)
531 struct questions_entry
*q
= mem_alloc(sizeof(*q
));
535 q
->callback
= callback
;
537 add_to_list(questions_queue
, q
);
540 #ifdef CONFIG_SCRIPTING
542 maybe_pre_format_html(struct cache_entry
*cached
, struct session
*ses
)
544 struct fragment
*fragment
;
545 static int pre_format_html_event
= EVENT_NONE
;
547 if (!cached
|| cached
->preformatted
)
550 /* The script called from here may indirectly call
551 * garbage_collection(). If the refcount of the cache entry
552 * were 0, it could then be freed, and the
553 * cached->preformatted assignment at the end of this function
554 * would crash. Normally, the document has a reference to the
555 * cache entry, and that suffices. However, if the cache
556 * entry was loaded to satisfy e.g. USEMAP="imgmap.html#map",
557 * then cached->object.refcount == 0 here, and must be
560 * cached->object.refcount == 0 is safe while the cache entry
561 * is being loaded, because garbage_collection() calls
562 * is_entry_used(), which checks whether any connection is
563 * using the cache entry. But loading has ended before this
567 fragment
= get_cache_fragment(cached
);
568 if (!fragment
) goto unlock_and_return
;
570 /* We cannot do anything if the data are fragmented. */
571 if (!list_is_singleton(cached
->frag
)) goto unlock_and_return
;
573 set_event_id(pre_format_html_event
, "pre-format-html");
574 trigger_event(pre_format_html_event
, ses
, cached
);
576 /* XXX: Keep this after the trigger_event, because hooks might call
577 * normalize_cache_entry()! */
578 cached
->preformatted
= 1;
581 object_unlock(cached
);
586 check_incomplete_redirects(struct cache_entry
*cached
)
590 cached
= follow_cached_redirects(cached
);
591 if (cached
&& !cached
->redirect
) {
592 /* XXX: This is not quite true, but does that difference
594 return cached
->incomplete
;
601 session_is_loading(struct session
*ses
)
603 struct download
*download
= get_current_download(ses
);
605 if (!download
) return 0;
607 if (!is_in_result_state(download
->state
))
610 /* The validness of download->cached (especially the download struct in
611 * ses->loading) is hard to maintain so check before using it.
612 * Related to bug 559. */
614 && cache_entry_is_valid(download
->cached
)
615 && check_incomplete_redirects(download
->cached
))
622 doc_loading_callback(struct download
*download
, struct session
*ses
)
626 if (is_in_result_state(download
->state
)) {
627 #ifdef CONFIG_SCRIPTING
628 maybe_pre_format_html(download
->cached
, ses
);
630 kill_timer(&ses
->display_timer
);
632 draw_formatted(ses
, 1);
634 if (get_cmd_opt_bool("auto-submit")) {
635 if (!list_empty(ses
->doc_view
->document
->forms
)) {
636 get_cmd_opt_bool("auto-submit") = 0;
641 load_frames(ses
, ses
->doc_view
);
642 load_css_imports(ses
, ses
->doc_view
);
643 load_ecmascript_imports(ses
, ses
->doc_view
);
644 process_file_requests(ses
);
646 start_document_refreshes(ses
);
648 if (!is_in_state(download
->state
, S_OK
)) {
649 print_error_dialog(ses
, download
->state
,
650 ses
->doc_view
->document
->uri
,
654 } else if (is_in_transfering_state(download
->state
)
655 && ses
->display_timer
== TIMER_ID_UNDEF
) {
659 check_questions_queue(ses
);
660 print_screen_status(ses
);
662 #ifdef CONFIG_GLOBHIST
663 if (download
->pri
!= PRI_CSS
) {
664 unsigned char *title
= ses
->doc_view
->document
->title
;
668 uri
= download
->conn
->proxied_uri
;
669 else if (download
->cached
)
670 uri
= download
->cached
->uri
;
675 add_global_history_item(struri(uri
), title
, time(NULL
));
679 if (submit
) auto_submit_form(ses
);
683 file_loading_callback(struct download
*download
, struct file_to_load
*ftl
)
685 if (ftl
->download
.cached
&& ftl
->cached
!= ftl
->download
.cached
) {
686 if (ftl
->cached
) object_unlock(ftl
->cached
);
687 ftl
->cached
= ftl
->download
.cached
;
688 object_lock(ftl
->cached
);
691 /* FIXME: We need to do content-type check here! However, we won't
692 * handle properly the "Choose action" dialog now :(. */
693 if (ftl
->cached
&& !ftl
->cached
->redirect_get
&& download
->pri
!= PRI_CSS
) {
694 struct session
*ses
= ftl
->ses
;
695 struct uri
*loading_uri
= ses
->loading_uri
;
696 unsigned char *target_frame
= null_or_stracpy(ses
->task
.target
.frame
);
698 ses
->loading_uri
= ftl
->uri
;
699 mem_free_set(&ses
->task
.target
.frame
, null_or_stracpy(ftl
->target_frame
));
700 setup_download_handler(ses
, &ftl
->download
, ftl
->cached
, 1);
701 ses
->loading_uri
= loading_uri
;
702 mem_free_set(&ses
->task
.target
.frame
, target_frame
);
705 doc_loading_callback(download
, ftl
->ses
);
708 static struct file_to_load
*
709 request_additional_file(struct session
*ses
, unsigned char *name
, struct uri
*uri
, int pri
)
711 struct file_to_load
*ftl
;
713 if (uri
->protocol
== PROTOCOL_UNKNOWN
) {
717 /* XXX: We cannot run the external handler here, because
718 * request_additional_file() is called many times for a single URL
719 * (normally the foreach() right below catches them all). Anyway,
720 * having <frame src="mailto:foo"> would be just weird, wouldn't it?
722 if (get_protocol_external_handler(ses
->tab
->term
, uri
)) {
726 foreach (ftl
, ses
->more_files
) {
727 if (compare_uri(ftl
->uri
, uri
, URI_BASE
)) {
728 if (ftl
->pri
> pri
) {
730 move_download(&ftl
->download
, &ftl
->download
, pri
);
736 ftl
= mem_calloc(1, sizeof(*ftl
));
737 if (!ftl
) return NULL
;
739 ftl
->uri
= get_uri_reference(uri
);
740 ftl
->target_frame
= stracpy(name
);
741 ftl
->download
.callback
= (download_callback_T
*) file_loading_callback
;
742 ftl
->download
.data
= ftl
;
746 add_to_list(ses
->more_files
, ftl
);
752 load_additional_file(struct file_to_load
*ftl
, enum cache_mode cache_mode
)
754 struct document_view
*doc_view
= current_frame(ftl
->ses
);
755 struct uri
*referrer
= doc_view
&& doc_view
->document
756 ? doc_view
->document
->uri
: NULL
;
758 load_uri(ftl
->uri
, referrer
, &ftl
->download
, ftl
->pri
, cache_mode
, -1);
762 process_file_requests(struct session
*ses
)
764 if (ses
->status
.processing_file_requests
) return;
765 ses
->status
.processing_file_requests
= 1;
768 struct file_to_load
*ftl
;
771 foreach (ftl
, ses
->more_files
) {
777 load_additional_file(ftl
, CACHE_MODE_NORMAL
);
784 ses
->status
.processing_file_requests
= 0;
789 dialog_goto_url_open(void *data
)
791 dialog_goto_url((struct session
*) data
, NULL
);
794 /** @returns 0 if the first session was not properly initialized and
795 * setup_session() should be called on the session as well. */
797 setup_first_session(struct session
*ses
, struct uri
*uri
)
799 /* [gettext_accelerator_context(setup_first_session)] */
800 struct terminal
*term
= ses
->tab
->term
;
802 if (!*get_opt_str("protocol.http.user_agent", NULL
)) {
804 N_("Warning"), ALIGN_CENTER
,
805 N_("You have an empty string in protocol.http.user_agent - "
806 "this was a default value in the past, substituted by "
807 "default ELinks User-Agent string. However, currently "
808 "this means that NO User-Agent HEADER "
809 "WILL BE SENT AT ALL - if this is really what you want, "
810 "set its value to \" \", otherwise please delete the line "
811 "with this setting from your configuration file (if you "
812 "have no idea what I'm talking about, just do this), so "
813 "that the correct default setting will be used. Apologies for "
814 "any inconvience caused."));
817 if (!get_opt_bool("config.saving_style_w", NULL
)) {
818 struct option
*opt
= get_opt_rec(config_options
, "config.saving_style_w");
819 opt
->value
.number
= 1;
820 option_changed(ses
, opt
);
821 if (get_opt_int("config.saving_style", NULL
) != 3) {
823 N_("Warning"), ALIGN_CENTER
,
824 N_("You have option config.saving_style set to "
825 "a de facto obsolete value. The configuration "
826 "saving algorithms of ELinks were changed from "
827 "the last time you upgraded ELinks. Now, only "
828 "those options which you actually changed are "
829 "saved to the configuration file, instead of "
830 "all the options. This simplifies our "
831 "situation greatly when we see that some option "
832 "has an inappropriate default value or we need to "
833 "change the semantics of some option in a subtle way. "
834 "Thus, we recommend you change the value of "
835 "config.saving_style option to 3 in order to get "
836 "the \"right\" behaviour. Apologies for any "
837 "inconvience caused."));
842 /* Only open the goto URL dialog if no URI was passed on the
844 void *handler
= uri
? NULL
: dialog_goto_url_open
;
848 msg_box(term
, NULL
, 0,
849 N_("Welcome"), ALIGN_CENTER
,
850 N_("Welcome to ELinks!\n\n"
851 "Press ESC for menu. Documentation is available in "
854 MSG_BOX_BUTTON(N_("~OK"), handler
, B_ENTER
| B_ESC
));
856 /* If there is no URI the goto dialog will pop up so there is
857 * no need to call setup_session(). */
860 #ifdef CONFIG_BOOKMARKS
861 } else if (!uri
&& get_opt_bool("ui.sessions.auto_restore", NULL
)) {
862 unsigned char *folder
; /* UTF-8 */
864 folder
= get_auto_save_bookmark_foldername_utf8();
866 open_bookmark_folder(ses
, folder
);
873 /* If there's a URI to load we have to call */
877 /** First load the current URI of the base session. In most cases it will just
878 * be fetched from the cache so that the new tab will not appear ``empty' while
879 * loading the real URI or showing the goto URL dialog. */
881 setup_session(struct session
*ses
, struct uri
*uri
, struct session
*base
)
883 if (base
&& have_location(base
)) {
884 ses_load(ses
, get_uri_reference(cur_loc(base
)->vs
.uri
),
885 NULL
, NULL
, CACHE_MODE_ALWAYS
, TASK_FORWARD
);
886 if (ses
->doc_view
&& ses
->doc_view
->vs
887 && base
->doc_view
&& base
->doc_view
->vs
) {
888 struct view_state
*vs
= ses
->doc_view
->vs
;
891 copy_vs(vs
, base
->doc_view
->vs
);
893 ses
->doc_view
->vs
= vs
;
900 } else if (!goto_url_home(ses
)) {
901 if (get_opt_bool("ui.startup_goto_dialog", NULL
)) {
902 dialog_goto_url_open(ses
);
907 /** @relates session */
909 init_session(struct session
*base_session
, struct terminal
*term
,
910 struct uri
*uri
, int in_background
)
912 struct session
*ses
= mem_calloc(1, sizeof(*ses
));
914 if (!ses
) return NULL
;
916 ses
->tab
= init_tab(term
, ses
, tabwin_func
);
924 ses
->option
= copy_option(config_options
,
925 CO_SHALLOW
| CO_NO_LISTBOX_ITEM
);
926 create_history(&ses
->history
);
927 init_list(ses
->scrn_frames
);
928 init_list(ses
->more_files
);
929 init_list(ses
->type_queries
);
930 ses
->task
.type
= TASK_NONE
;
931 ses
->display_timer
= TIMER_ID_UNDEF
;
934 init_led_panel(&ses
->status
.leds
);
935 ses
->status
.ssl_led
= register_led(ses
, 0);
936 ses
->status
.insert_mode_led
= register_led(ses
, 1);
937 ses
->status
.ecmascript_led
= register_led(ses
, 2);
938 ses
->status
.popup_led
= register_led(ses
, 3);
940 ses
->status
.download_led
= register_led(ses
, 5);
942 ses
->status
.force_show_status_bar
= -1;
943 ses
->status
.force_show_title_bar
= -1;
945 add_to_list(sessions
, ses
);
947 /* Update the status -- most importantly the info about whether to the
948 * show the title, tab and status bar -- _before_ loading the URI so
949 * the document cache is not filled with useless documents if the
950 * content is already cached.
952 * For big document it also reduces memory usage quite a bit because
953 * (atleast that is my interpretation --jonas) the old document will
954 * have a chance to be released before rendering a new one. A few
955 * numbers when opening a new tab while viewing debians package list
958 * 9307 jonas 15 0 34252 30m 5088 S 0.0 12.2 0:03.63 elinks-old
959 * 9305 jonas 15 0 17060 13m 5088 S 0.0 5.5 0:02.07 elinks-new
963 /* Check if the newly inserted session is the only in the list and do
964 * the special setup for the first session, */
965 if (!list_is_singleton(sessions
) || !setup_first_session(ses
, uri
)) {
966 setup_session(ses
, uri
, base_session
);
970 switch_to_tab(term
, get_tab_number(ses
->tab
), -1);
972 if (!term
->main_menu
) activate_bfu_technology(ses
, -1);
977 init_remote_session(struct session
*ses
, enum remote_session_flags
*remote_ptr
,
980 enum remote_session_flags remote
= *remote_ptr
;
982 if (remote
& SES_REMOTE_CURRENT_TAB
) {
984 /* Mask out the current tab flag */
985 *remote_ptr
= remote
& ~SES_REMOTE_CURRENT_TAB
;
987 /* Remote session was masked out. Open all following URIs in
990 *remote_ptr
= SES_REMOTE_NEW_TAB
;
992 } else if (remote
& SES_REMOTE_NEW_TAB
) {
993 /* FIXME: This is not perfect. Doing multiple -remote
994 * with no URLs will make the goto dialogs
995 * inaccessible. Maybe we should not support this kind
996 * of thing or make the window focus detecting code
997 * more intelligent. --jonas */
998 open_uri_in_new_tab(ses
, uri
, 0, 0);
1000 if (remote
& SES_REMOTE_PROMPT_URL
) {
1001 dialog_goto_url_open(ses
);
1004 } else if (remote
& SES_REMOTE_NEW_WINDOW
) {
1005 /* FIXME: It is quite rude because we just take the first
1006 * possibility and should maybe make it possible to specify
1007 * new-screen etc via -remote "openURL(..., new-*)" --jonas */
1008 if (!can_open_in_new(ses
->tab
->term
))
1011 open_uri_in_new_window(ses
, uri
, NULL
,
1012 ses
->tab
->term
->environment
,
1013 CACHE_MODE_NORMAL
, TASK_NONE
);
1015 } else if (remote
& SES_REMOTE_ADD_BOOKMARK
) {
1016 #ifdef CONFIG_BOOKMARKS
1020 /** @todo Bug 1066: What is the encoding of struri()?
1021 * This code currently assumes the system charset.
1022 * It might be best to keep URIs in plain ASCII and
1023 * then have a function that reversibly converts them
1024 * to IRIs for display in a given encoding. */
1025 uri_cp
= get_cp_index("System");
1026 add_bookmark_cp(NULL
, 1, uri_cp
, struri(uri
), struri(uri
));
1029 } else if (remote
& SES_REMOTE_INFO_BOX
) {
1030 unsigned char *text
;
1034 text
= memacpy(uri
->data
, uri
->datalen
);
1037 info_box(ses
->tab
->term
, MSGBOX_FREE_TEXT
,
1038 N_("Error"), ALIGN_CENTER
,
1041 } else if (remote
& SES_REMOTE_PROMPT_URL
) {
1042 dialog_goto_url_open(ses
);
1044 } else if (remote
& SES_REMOTE_RELOAD
) {
1045 reload(ses
, CACHE_MODE_FORCE_RELOAD
);
1052 encode_session_info(struct string
*info
,
1053 LIST_OF(struct string_list_item
) *url_list
)
1055 struct string_list_item
*url
;
1057 if (!init_string(info
)) return NULL
;
1059 foreach (url
, *url_list
) {
1060 struct string
*str
= &url
->string
;
1062 add_bytes_to_string(info
, str
->source
, str
->length
+ 1);
1068 /** Older elinks versions (up to and including 0.9.1) sends no magic variable and if
1069 * this is detected we fallback to the old session info format. For this format
1070 * the magic member of terminal_info hold the length of the URI string. The
1071 * old format is handled by the default label in the switch.
1073 * The new session info format supports extraction of multiple URIS from the
1074 * terminal_info data member. The magic variable controls how to interpret
1077 * - INTERLINK_NORMAL_MAGIC means use the terminal_info session_info
1078 * variable as an id for a saved session.
1080 * - INTERLINK_REMOTE_MAGIC means use the terminal_info session_info
1081 * variable as the remote session flags. */
1083 decode_session_info(struct terminal
*term
, struct terminal_info
*info
)
1085 int len
= info
->length
;
1086 struct session
*base_session
= NULL
;
1087 enum remote_session_flags remote
= 0;
1090 switch (info
->magic
) {
1091 case INTERLINK_NORMAL_MAGIC
:
1092 /* Lookup if there are any saved sessions that should be
1093 * resumed using the session_info as an id. The id is derived
1094 * from the value of -base-session command line option in the
1095 * connecting instance.
1097 * At the moment it is only used when opening instances in new
1098 * window to figure out how to initialize it when the new
1099 * instance connects to the master.
1101 * We don't need to handle it for the old format since new
1102 * instances connecting this way will always be of the same
1103 * origin as the master. */
1104 if (init_saved_session(term
, info
->session_info
))
1108 case INTERLINK_REMOTE_MAGIC
:
1109 /* This could mean some fatal bug but I am unsure if we can
1110 * actually assert it since people could pour all kinds of crap
1111 * down the socket. */
1112 if (!info
->session_info
) {
1113 INTERNAL("Remote magic with no remote flags");
1117 remote
= info
->session_info
;
1119 /* If processing session info from a -remote instance we want
1120 * to hook up with the master so we can handle request for
1121 * stuff in current tab. */
1122 base_session
= get_master_session();
1123 if (!base_session
) return 0;
1128 /* The old format calculates the session_info and magic members
1129 * as part of the data that should be decoded so we have to
1130 * substract it to get the size of the actual URI data. */
1131 len
-= sizeof(info
->session_info
) + sizeof(info
->magic
);
1133 /* Extract URI containing @magic bytes */
1134 if (info
->magic
<= 0 || info
->magic
> len
)
1142 return !!init_session(base_session
, term
, NULL
, 0);
1144 /* Even though there are no URIs we still have to
1145 * handle remote stuff. */
1146 init_remote_session(base_session
, &remote
, NULL
);
1152 /* Extract multiple (possible) NUL terminated URIs */
1154 unsigned char *end
= memchr(str
, 0, len
);
1155 int urilength
= end
? end
- str
: len
;
1156 struct uri
*uri
= NULL
;
1157 unsigned char *uristring
= memacpy(str
, urilength
);
1160 uri
= get_hooked_uri(uristring
, base_session
, term
->cwd
);
1161 mem_free(uristring
);
1164 len
-= urilength
+ 1;
1165 str
+= urilength
+ 1;
1170 init_remote_session(base_session
, &remote
, uri
);
1173 int backgrounded
= !list_empty(term
->windows
);
1175 struct session
*ses
;
1178 uri
= get_uri("about:blank", 0);
1180 ses
= init_session(base_session
, term
, uri
, backgrounded
);
1182 /* End loop if initialization fails */
1184 } else if (bad_url
) {
1185 print_error_dialog(ses
, connection_state(S_BAD_URL
),
1191 if (uri
) done_uri(uri
);
1194 /* If we only initialized remote sessions or didn't manage to add any
1195 * new tabs return zero so the terminal will be destroyed ASAP. */
1196 return remote
? 0 : !list_empty(term
->windows
);
1201 abort_loading(struct session
*ses
, int interrupt
)
1203 if (have_location(ses
)) {
1204 struct location
*loc
= cur_loc(ses
);
1206 cancel_download(&loc
->download
, interrupt
);
1207 abort_files_load(ses
, interrupt
);
1209 abort_preloading(ses
, interrupt
);
1213 destroy_session(struct session
*ses
)
1215 struct document_view
*doc_view
;
1218 if_assert_failed
return;
1220 #ifdef CONFIG_SCRIPTING_SPIDERMONKEY
1221 smjs_detach_session_object(ses
);
1223 destroy_downloads(ses
);
1224 abort_loading(ses
, 0);
1226 if (ses
->doc_view
) {
1227 detach_formatted(ses
->doc_view
);
1228 mem_free(ses
->doc_view
);
1231 foreach (doc_view
, ses
->scrn_frames
)
1232 detach_formatted(doc_view
);
1234 free_list(ses
->scrn_frames
);
1236 destroy_history(&ses
->history
);
1237 set_session_referrer(ses
, NULL
);
1239 if (ses
->loading_uri
) done_uri(ses
->loading_uri
);
1241 kill_timer(&ses
->display_timer
);
1243 while (!list_empty(ses
->type_queries
))
1244 done_type_query(ses
->type_queries
.next
);
1246 if (ses
->download_uri
) done_uri(ses
->download_uri
);
1247 mem_free_if(ses
->search_word
);
1248 mem_free_if(ses
->last_search_word
);
1249 mem_free_if(ses
->status
.last_title
);
1250 #ifdef CONFIG_ECMASCRIPT
1251 mem_free_if(ses
->status
.window_status
);
1254 delete_option(ses
->option
);
1261 reload(struct session
*ses
, enum cache_mode cache_mode
)
1263 reload_frame(ses
, NULL
, cache_mode
);
1267 reload_frame(struct session
*ses
, unsigned char *name
,
1268 enum cache_mode cache_mode
)
1270 abort_loading(ses
, 0);
1272 if (cache_mode
== CACHE_MODE_INCREMENT
) {
1273 cache_mode
= CACHE_MODE_NEVER
;
1274 if (ses
->reloadlevel
< CACHE_MODE_NEVER
)
1275 cache_mode
= ++ses
->reloadlevel
;
1277 ses
->reloadlevel
= cache_mode
;
1280 if (have_location(ses
)) {
1281 struct location
*loc
= cur_loc(ses
);
1282 struct file_to_load
*ftl
;
1284 #ifdef CONFIG_ECMASCRIPT
1285 loc
->vs
.ecmascript_fragile
= 1;
1288 /* FIXME: When reloading use loading_callback and set up a
1289 * session task so that the reloading will work even when the
1290 * reloaded document contains redirects. This is needed atleast
1291 * when reloading HTTP auth document after the user has entered
1293 loc
->download
.data
= ses
;
1294 loc
->download
.callback
= (download_callback_T
*) doc_loading_callback
;
1296 mem_free_set(&ses
->task
.target
.frame
, null_or_stracpy(name
));
1298 load_uri(loc
->vs
.uri
, ses
->referrer
, &loc
->download
, PRI_MAIN
, cache_mode
, -1);
1300 foreach (ftl
, ses
->more_files
) {
1301 if (file_to_load_is_active(ftl
))
1304 ftl
->download
.data
= ftl
;
1305 ftl
->download
.callback
= (download_callback_T
*) file_loading_callback
;
1307 load_additional_file(ftl
, cache_mode
);
1314 ses_find_frame(struct session
*ses
, unsigned char *name
)
1316 struct location
*loc
= cur_loc(ses
);
1317 struct frame
*frame
;
1319 assertm(have_location(ses
), "ses_request_frame: no location yet");
1320 if_assert_failed
return NULL
;
1322 foreachback (frame
, loc
->frames
)
1323 if (!c_strcasecmp(frame
->name
, name
))
1330 set_session_referrer(struct session
*ses
, struct uri
*referrer
)
1332 if (ses
->referrer
) done_uri(ses
->referrer
);
1333 ses
->referrer
= referrer
? get_uri_reference(referrer
) : NULL
;
1337 tabwin_func(struct window
*tab
, struct term_event
*ev
)
1339 struct session
*ses
= tab
->data
;
1343 if (ses
) destroy_session(ses
);
1344 if (!list_empty(sessions
)) update_status();
1351 if (!ses
|| ses
->tab
!= get_current_tab(ses
->tab
->term
))
1354 draw_formatted(ses
, tab
->resize
);
1356 load_frames(ses
, ses
->doc_view
);
1357 process_file_requests(ses
);
1360 print_screen_status(ses
);
1365 /* This check is related to bug 296 */
1366 assert(ses
->tab
== get_current_tab(ses
->tab
->term
));
1367 send_event(ses
, ev
);
1373 * Gets the url being viewed by this session. Writes it into @a str.
1374 * A maximum of @a str_size bytes (including null) will be written.
1378 get_current_url(struct session
*ses
, unsigned char *str
, size_t str_size
)
1383 assert(str
&& str_size
> 0);
1385 uri
= have_location(ses
) ? cur_loc(ses
)->vs
.uri
: ses
->loading_uri
;
1387 /* Not looking or loading anything */
1388 if (!uri
) return NULL
;
1390 /* Ensure that the url size is not greater than str_size.
1391 * We can't just happily strncpy(str, here, str_size)
1392 * because we have to stop at POST_CHAR, not only at NULL. */
1393 length
= int_min(get_real_uri_length(uri
), str_size
- 1);
1395 return safe_strncpy(str
, struri(uri
), length
+ 1);
1399 * Gets the title of the page being viewed by this session. Writes it into
1400 * @a str. A maximum of @a str_size bytes (including null) will be written.
1404 get_current_title(struct session
*ses
, unsigned char *str
, size_t str_size
)
1406 struct document_view
*doc_view
= current_frame(ses
);
1408 assert(str
&& str_size
> 0);
1410 /* Ensure that the title is defined */
1411 /* TODO: Try globhist --jonas */
1412 if (doc_view
&& doc_view
->document
->title
)
1413 return safe_strncpy(str
, doc_view
->document
->title
, str_size
);
1419 * Gets the url of the link currently selected. Writes it into @a str.
1420 * A maximum of @a str_size bytes (including null) will be written.
1424 get_current_link_url(struct session
*ses
, unsigned char *str
, size_t str_size
)
1426 struct link
*link
= get_current_session_link(ses
);
1428 assert(str
&& str_size
> 0);
1430 if (!link
) return NULL
;
1432 return safe_strncpy(str
, link
->where
? link
->where
: link
->where_img
, str_size
);
1435 /** get_current_link_name: returns the name of the current link
1436 * (the text between @<A> and @</A>), @a str is a preallocated string,
1437 * @a str_size includes the null char.
1438 * @relates session */
1440 get_current_link_name(struct session
*ses
, unsigned char *str
, size_t str_size
)
1442 struct link
*link
= get_current_session_link(ses
);
1443 unsigned char *where
, *name
= NULL
;
1445 assert(str
&& str_size
> 0);
1447 if (!link
) return NULL
;
1449 where
= link
->where
? link
->where
: link
->where_img
;
1450 #ifdef CONFIG_GLOBHIST
1452 struct global_history_item
*item
;
1454 item
= get_global_history_item(where
);
1455 if (item
) name
= item
->title
;
1458 if (!name
) name
= get_link_name(link
);
1459 if (!name
) name
= where
;
1461 return safe_strncpy(str
, name
, str_size
);
1465 get_current_link_in_view(struct document_view
*doc_view
)
1467 struct link
*link
= get_current_link(doc_view
);
1469 return link
&& !link_is_form(link
) ? link
: NULL
;
1472 /** @relates session */
1474 get_current_session_link(struct session
*ses
)
1476 return get_current_link_in_view(current_frame(ses
));
1479 /** @relates session */
1481 eat_kbd_repeat_count(struct session
*ses
)
1483 int count
= ses
->kbdprefix
.repeat_count
;
1485 set_kbd_repeat_count(ses
, 0);
1487 /* Clear status bar when prefix is eaten (bug 930) */
1488 print_screen_status(ses
);
1492 /** @relates session */
1494 set_kbd_repeat_count(struct session
*ses
, int new_count
)
1496 int old_count
= ses
->kbdprefix
.repeat_count
;
1498 if (new_count
== old_count
)
1501 ses
->kbdprefix
.repeat_count
= new_count
;
1503 /* Update the status bar. */
1504 print_screen_status(ses
);
1506 /* Clear the old link highlighting. */
1507 draw_formatted(ses
, 0);
1509 if (new_count
!= 0) {
1510 struct document_view
*doc_view
= current_frame(ses
);
1512 highlight_links_with_prefixes_that_start_with_n(ses
->tab
->term
,