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 #include "session/download.h"
37 #include "session/history.h"
38 #include "session/location.h"
39 #include "session/session.h"
40 #include "session/task.h"
41 #include "terminal/tab.h"
42 #include "terminal/terminal.h"
43 #include "terminal/window.h"
44 #include "util/conv.h"
45 #include "util/error.h"
46 #include "util/memlist.h"
47 #include "util/memory.h"
48 #include "util/string.h"
49 #include "util/time.h"
50 #include "viewer/text/draw.h"
51 #include "viewer/text/form.h"
52 #include "viewer/text/link.h"
53 #include "viewer/text/view.h"
57 LIST_HEAD(struct file_to_load
);
60 unsigned int req_sent
:1;
62 struct cache_entry
*cached
;
63 unsigned char *target_frame
;
65 struct download download
;
68 /** This structure and related functions are used to maintain information
69 * for instances opened in new windows. We store all related session info like
70 * URI and base session to clone from so that when the new instance connects
71 * we can look up this information. In case of failure the session information
74 LIST_HEAD(struct session_info
);
82 enum cache_mode cache_mode
;
85 #define file_to_load_is_active(ftl) ((ftl)->req_sent && is_in_progress_state((ftl)->download.state))
88 INIT_LIST_OF(struct session
, sessions
);
90 enum remote_session_flags remote_session_flags
;
93 static struct file_to_load
*request_additional_file(struct session
*,
97 static window_handler_T tabwin_func
;
100 static INIT_LIST_OF(struct session_info
, session_info
);
101 static int session_info_id
= 1;
103 static struct session_info
*
104 get_session_info(int id
)
106 struct session_info
*info
;
108 foreach (info
, session_info
) {
111 if (info
->id
!= id
) continue;
113 /* Make sure the info->ses session is still with us. */
114 foreach (ses
, sessions
)
115 if (ses
== info
->ses
)
126 done_session_info(struct session_info
*info
)
129 kill_timer(&info
->timer
);
131 if (info
->uri
) done_uri(info
->uri
);
132 if (info
->referrer
) done_uri(info
->referrer
);
137 done_saved_session_info(void)
139 while (!list_empty(session_info
))
140 done_session_info(session_info
.next
);
143 /** Timer callback for session_info.timer. As explained in install_timer(),
144 * this function must erase the expired timer ID from all variables. */
146 session_info_timeout(int id
)
148 struct session_info
*info
= get_session_info(id
);
151 info
->timer
= TIMER_ID_UNDEF
;
152 /* The expired timer ID has now been erased. */
153 done_session_info(info
);
157 add_session_info(struct session
*ses
, struct uri
*uri
, struct uri
*referrer
,
158 enum cache_mode cache_mode
, enum task_type task
)
160 struct session_info
*info
= mem_calloc(1, sizeof(*info
));
162 if (!info
) return -1;
164 info
->id
= session_info_id
++;
165 /* I don't know what a reasonable start up time for a new instance is
166 * but it won't hurt to have a few seconds atleast. --jonas */
167 install_timer(&info
->timer
, (milliseconds_T
) 10000,
168 (void (*)(void *)) session_info_timeout
,
169 (void *) (long) info
->id
);
173 info
->cache_mode
= cache_mode
;
175 if (uri
) info
->uri
= get_uri_reference(uri
);
176 if (referrer
) info
->referrer
= get_uri_reference(referrer
);
178 add_to_list(session_info
, info
);
183 static struct session
*
184 init_saved_session(struct terminal
*term
, int id
)
186 struct session_info
*info
= get_session_info(id
);
189 if (!info
) return NULL
;
191 ses
= init_session(info
->ses
, term
, info
->uri
, 0);
194 done_session_info(info
);
198 /* Only do the ses_goto()-thing for target=_blank. */
199 if (info
->uri
&& info
->task
!= TASK_NONE
) {
200 /* The init_session() call would have started the download but
201 * not with the requested cache mode etc. so interrupt that
203 abort_loading(ses
, 1);
205 ses
->reloadlevel
= info
->cache_mode
;
206 set_session_referrer(ses
, info
->referrer
);
207 ses_goto(ses
, info
->uri
, NULL
, NULL
, info
->cache_mode
,
211 done_session_info(info
);
216 static struct session
*
217 get_master_session(void)
221 foreach (ses
, sessions
)
222 if (ses
->tab
->term
->master
) {
223 struct window
*current_tab
= get_current_tab(ses
->tab
->term
);
225 return current_tab
? current_tab
->data
: NULL
;
231 /** @relates session */
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
&& is_in_state(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
, struct 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_cp
)
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 (c_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)
433 load_image_imports(struct session
*ses
, struct document_view
*doc_view
)
435 struct document
*document
= doc_view
->document
;
439 if (!document
) return;
441 foreach_uri (uri
, index
, &document
->image_imports
) {
442 request_additional_file(ses
, "", uri
, /* XXX */ PRI_CSS
);
446 #define load_image_imports(ses, doc_view)
449 NONSTATIC_INLINE
void
450 load_frames(struct session
*ses
, struct document_view
*doc_view
)
452 struct document
*document
= doc_view
->document
;
454 if (!document
|| !document
->frame_desc
) return;
455 request_frameset(ses
, document
->frame_desc
, 0);
457 foreach (doc_view
, ses
->scrn_frames
) {
458 load_css_imports(ses
, doc_view
);
459 load_ecmascript_imports(ses
, doc_view
);
460 load_image_imports(ses
, doc_view
);
464 /** Timer callback for session.display_timer. As explained in install_timer(),
465 * this function must erase the expired timer ID from all variables. */
467 display_timer(struct session
*ses
)
469 timeval_T start
, stop
, duration
;
473 draw_formatted(ses
, 3);
475 timeval_sub(&duration
, &start
, &stop
);
477 t
= mult_ms(timeval_to_milliseconds(&duration
), DISPLAY_TIME
);
478 if (t
< DISPLAY_TIME_MIN
) t
= DISPLAY_TIME_MIN
;
479 install_timer(&ses
->display_timer
, t
,
480 (void (*)(void *)) display_timer
,
482 /* The expired timer ID has now been erased. */
484 load_frames(ses
, ses
->doc_view
);
485 load_css_imports(ses
, ses
->doc_view
);
486 load_ecmascript_imports(ses
, ses
->doc_view
);
487 load_image_imports(ses
, ses
->doc_view
);
488 process_file_requests(ses
);
492 struct questions_entry
{
493 LIST_HEAD(struct questions_entry
);
495 void (*callback
)(struct session
*, void *);
499 INIT_LIST_OF(struct questions_entry
, questions_queue
);
503 check_questions_queue(struct session
*ses
)
505 while (!list_empty(questions_queue
)) {
506 struct questions_entry
*q
= questions_queue
.next
;
508 q
->callback(ses
, q
->data
);
515 add_questions_entry(void (*callback
)(struct session
*, void *), void *data
)
517 struct questions_entry
*q
= mem_alloc(sizeof(*q
));
521 q
->callback
= callback
;
523 add_to_list(questions_queue
, q
);
526 #ifdef CONFIG_SCRIPTING
528 maybe_pre_format_html(struct cache_entry
*cached
, struct session
*ses
)
530 struct fragment
*fragment
;
531 static int pre_format_html_event
= EVENT_NONE
;
533 if (!cached
|| cached
->preformatted
)
536 /* The script called from here may indirectly call
537 * garbage_collection(). If the refcount of the cache entry
538 * were 0, it could then be freed, and the
539 * cached->preformatted assignment at the end of this function
540 * would crash. Normally, the document has a reference to the
541 * cache entry, and that suffices. However, if the cache
542 * entry was loaded to satisfy e.g. USEMAP="imgmap.html#map",
543 * then cached->object.refcount == 0 here, and must be
546 * cached->object.refcount == 0 is safe while the cache entry
547 * is being loaded, because garbage_collection() calls
548 * is_entry_used(), which checks whether any connection is
549 * using the cache entry. But loading has ended before this
553 fragment
= get_cache_fragment(cached
);
554 if (!fragment
) goto unlock_and_return
;
556 /* We cannot do anything if the data are fragmented. */
557 if (!list_is_singleton(cached
->frag
)) goto unlock_and_return
;
559 set_event_id(pre_format_html_event
, "pre-format-html");
560 trigger_event(pre_format_html_event
, ses
, cached
);
562 /* XXX: Keep this after the trigger_event, because hooks might call
563 * normalize_cache_entry()! */
564 cached
->preformatted
= 1;
567 object_unlock(cached
);
572 check_incomplete_redirects(struct cache_entry
*cached
)
576 cached
= follow_cached_redirects(cached
);
577 if (cached
&& !cached
->redirect
) {
578 /* XXX: This is not quite true, but does that difference
580 return cached
->incomplete
;
587 session_is_loading(struct session
*ses
)
589 struct download
*download
= get_current_download(ses
);
591 if (!download
) return 0;
593 if (!is_in_result_state(download
->state
))
596 /* The validness of download->cached (especially the download struct in
597 * ses->loading) is hard to maintain so check before using it.
598 * Related to bug 559. */
600 && cache_entry_is_valid(download
->cached
)
601 && check_incomplete_redirects(download
->cached
))
608 doc_loading_callback(struct download
*download
, struct session
*ses
)
612 if (is_in_result_state(download
->state
)) {
613 #ifdef CONFIG_SCRIPTING
614 maybe_pre_format_html(download
->cached
, ses
);
616 kill_timer(&ses
->display_timer
);
618 draw_formatted(ses
, 1);
620 if (get_cmd_opt_bool("auto-submit")) {
621 if (!list_empty(ses
->doc_view
->document
->forms
)) {
622 get_cmd_opt_bool("auto-submit") = 0;
627 load_frames(ses
, ses
->doc_view
);
628 load_css_imports(ses
, ses
->doc_view
);
629 load_ecmascript_imports(ses
, ses
->doc_view
);
630 load_image_imports(ses
, ses
->doc_view
);
631 process_file_requests(ses
);
633 start_document_refreshes(ses
);
635 if (!is_in_state(download
->state
, S_OK
)) {
636 print_error_dialog(ses
, download
->state
,
637 ses
->doc_view
->document
->uri
,
641 } else if (is_in_transfering_state(download
->state
)
642 && ses
->display_timer
== TIMER_ID_UNDEF
) {
646 check_questions_queue(ses
);
647 print_screen_status(ses
);
649 #ifdef CONFIG_GLOBHIST
650 if (download
->pri
!= PRI_CSS
) {
651 unsigned char *title
= ses
->doc_view
->document
->title
;
655 uri
= download
->conn
->proxied_uri
;
656 else if (download
->cached
)
657 uri
= download
->cached
->uri
;
662 add_global_history_item(struri(uri
), title
, time(NULL
));
666 if (submit
) auto_submit_form(ses
);
670 file_loading_callback(struct download
*download
, struct file_to_load
*ftl
)
672 if (ftl
->download
.cached
&& ftl
->cached
!= ftl
->download
.cached
) {
673 if (ftl
->cached
) object_unlock(ftl
->cached
);
674 ftl
->cached
= ftl
->download
.cached
;
675 object_lock(ftl
->cached
);
678 /* FIXME: We need to do content-type check here! However, we won't
679 * handle properly the "Choose action" dialog now :(. */
680 if (ftl
->cached
&& !ftl
->cached
->redirect_get
&& download
->pri
!= PRI_CSS
) {
681 struct session
*ses
= ftl
->ses
;
682 struct uri
*loading_uri
= ses
->loading_uri
;
683 unsigned char *target_frame
= null_or_stracpy(ses
->task
.target
.frame
);
685 ses
->loading_uri
= ftl
->uri
;
686 mem_free_set(&ses
->task
.target
.frame
, null_or_stracpy(ftl
->target_frame
));
687 setup_download_handler(ses
, &ftl
->download
, ftl
->cached
, 1);
688 ses
->loading_uri
= loading_uri
;
689 mem_free_set(&ses
->task
.target
.frame
, target_frame
);
692 doc_loading_callback(download
, ftl
->ses
);
695 static struct file_to_load
*
696 request_additional_file(struct session
*ses
, unsigned char *name
, struct uri
*uri
, int pri
)
698 struct file_to_load
*ftl
;
700 if (uri
->protocol
== PROTOCOL_UNKNOWN
) {
704 /* XXX: We cannot run the external handler here, because
705 * request_additional_file() is called many times for a single URL
706 * (normally the foreach() right below catches them all). Anyway,
707 * having <frame src="mailto:foo"> would be just weird, wouldn't it?
709 if (get_protocol_external_handler(ses
->tab
->term
, uri
)) {
713 foreach (ftl
, ses
->more_files
) {
714 if (compare_uri(ftl
->uri
, uri
, URI_BASE
)) {
715 if (ftl
->pri
> pri
) {
717 move_download(&ftl
->download
, &ftl
->download
, pri
);
723 ftl
= mem_calloc(1, sizeof(*ftl
));
724 if (!ftl
) return NULL
;
726 ftl
->uri
= get_uri_reference(uri
);
727 ftl
->target_frame
= stracpy(name
);
728 ftl
->download
.callback
= (download_callback_T
*) file_loading_callback
;
729 ftl
->download
.data
= ftl
;
733 add_to_list(ses
->more_files
, ftl
);
739 load_additional_file(struct file_to_load
*ftl
, enum cache_mode cache_mode
)
741 struct document_view
*doc_view
= current_frame(ftl
->ses
);
742 struct uri
*referrer
= doc_view
&& doc_view
->document
743 ? doc_view
->document
->uri
: NULL
;
745 load_uri(ftl
->uri
, referrer
, &ftl
->download
, ftl
->pri
, cache_mode
, -1);
749 process_file_requests(struct session
*ses
)
751 if (ses
->status
.processing_file_requests
) return;
752 ses
->status
.processing_file_requests
= 1;
755 struct file_to_load
*ftl
;
758 foreach (ftl
, ses
->more_files
) {
764 load_additional_file(ftl
, CACHE_MODE_NORMAL
);
771 ses
->status
.processing_file_requests
= 0;
776 dialog_goto_url_open(void *data
)
778 dialog_goto_url((struct session
*) data
, NULL
);
781 /** @returns 0 if the first session was not properly initialized and
782 * setup_session() should be called on the session as well. */
784 setup_first_session(struct session
*ses
, struct uri
*uri
)
786 /* [gettext_accelerator_context(setup_first_session)] */
787 struct terminal
*term
= ses
->tab
->term
;
789 if (!*get_opt_str("protocol.http.user_agent", NULL
)) {
791 N_("Warning"), ALIGN_CENTER
,
792 N_("You have an empty string in protocol.http.user_agent - "
793 "this was a default value in the past, substituted by "
794 "default ELinks User-Agent string. However, currently "
795 "this means that NO User-Agent HEADER "
796 "WILL BE SENT AT ALL - if this is really what you want, "
797 "set its value to \" \", otherwise please delete the line "
798 "with this setting from your configuration file (if you "
799 "have no idea what I'm talking about, just do this), so "
800 "that the correct default setting will be used. Apologies for "
801 "any inconvience caused."));
804 if (!get_opt_bool("config.saving_style_w", NULL
)) {
805 struct option
*opt
= get_opt_rec(config_options
, "config.saving_style_w");
806 opt
->value
.number
= 1;
807 option_changed(ses
, opt
);
808 if (get_opt_int("config.saving_style", NULL
) != 3) {
810 N_("Warning"), ALIGN_CENTER
,
811 N_("You have option config.saving_style set to "
812 "a de facto obsolete value. The configuration "
813 "saving algorithms of ELinks were changed from "
814 "the last time you upgraded ELinks. Now, only "
815 "those options which you actually changed are "
816 "saved to the configuration file, instead of "
817 "all the options. This simplifies our "
818 "situation greatly when we see that some option "
819 "has an inappropriate default value or we need to "
820 "change the semantics of some option in a subtle way. "
821 "Thus, we recommend you change the value of "
822 "config.saving_style option to 3 in order to get "
823 "the \"right\" behaviour. Apologies for any "
824 "inconvience caused."));
829 /* Only open the goto URL dialog if no URI was passed on the
831 void *handler
= uri
? NULL
: dialog_goto_url_open
;
835 msg_box(term
, NULL
, 0,
836 N_("Welcome"), ALIGN_CENTER
,
837 N_("Welcome to ELinks!\n\n"
838 "Press ESC for menu. Documentation is available in "
841 MSG_BOX_BUTTON(N_("~OK"), handler
, B_ENTER
| B_ESC
));
843 /* If there is no URI the goto dialog will pop up so there is
844 * no need to call setup_session(). */
847 #ifdef CONFIG_BOOKMARKS
848 } else if (!uri
&& get_opt_bool("ui.sessions.auto_restore", NULL
)) {
849 unsigned char *folder
; /* UTF-8 */
851 folder
= get_auto_save_bookmark_foldername_utf8();
853 open_bookmark_folder(ses
, folder
);
860 /* If there's a URI to load we have to call */
864 /** First load the current URI of the base session. In most cases it will just
865 * be fetched from the cache so that the new tab will not appear ``empty' while
866 * loading the real URI or showing the goto URL dialog. */
868 setup_session(struct session
*ses
, struct uri
*uri
, struct session
*base
)
870 if (base
&& have_location(base
)) {
871 ses_load(ses
, get_uri_reference(cur_loc(base
)->vs
.uri
),
872 NULL
, NULL
, CACHE_MODE_ALWAYS
, TASK_FORWARD
);
873 if (ses
->doc_view
&& ses
->doc_view
->vs
874 && base
->doc_view
&& base
->doc_view
->vs
) {
875 struct view_state
*vs
= ses
->doc_view
->vs
;
878 copy_vs(vs
, base
->doc_view
->vs
);
880 ses
->doc_view
->vs
= vs
;
887 } else if (!goto_url_home(ses
)) {
888 if (get_opt_bool("ui.startup_goto_dialog", NULL
)) {
889 dialog_goto_url_open(ses
);
894 /** @relates session */
896 init_session(struct session
*base_session
, struct terminal
*term
,
897 struct uri
*uri
, int in_background
)
899 struct session
*ses
= mem_calloc(1, sizeof(*ses
));
901 if (!ses
) return NULL
;
903 ses
->tab
= init_tab(term
, ses
, tabwin_func
);
909 ses
->option
= copy_option(config_options
,
910 CO_SHALLOW
| CO_NO_LISTBOX_ITEM
);
911 create_history(&ses
->history
);
912 init_list(ses
->scrn_frames
);
913 init_list(ses
->more_files
);
914 init_list(ses
->type_queries
);
915 ses
->task
.type
= TASK_NONE
;
916 ses
->display_timer
= TIMER_ID_UNDEF
;
919 init_led_panel(&ses
->status
.leds
);
920 ses
->status
.ssl_led
= register_led(ses
, 0);
921 ses
->status
.insert_mode_led
= register_led(ses
, 1);
922 ses
->status
.ecmascript_led
= register_led(ses
, 2);
923 ses
->status
.popup_led
= register_led(ses
, 3);
925 ses
->status
.download_led
= register_led(ses
, 5);
927 ses
->status
.force_show_status_bar
= -1;
928 ses
->status
.force_show_title_bar
= -1;
930 add_to_list(sessions
, ses
);
932 /* Update the status -- most importantly the info about whether to the
933 * show the title, tab and status bar -- _before_ loading the URI so
934 * the document cache is not filled with useless documents if the
935 * content is already cached.
937 * For big document it also reduces memory usage quite a bit because
938 * (atleast that is my interpretation --jonas) the old document will
939 * have a chance to be released before rendering a new one. A few
940 * numbers when opening a new tab while viewing debians package list
943 * 9307 jonas 15 0 34252 30m 5088 S 0.0 12.2 0:03.63 elinks-old
944 * 9305 jonas 15 0 17060 13m 5088 S 0.0 5.5 0:02.07 elinks-new
948 /* Check if the newly inserted session is the only in the list and do
949 * the special setup for the first session, */
950 if (!list_is_singleton(sessions
) || !setup_first_session(ses
, uri
)) {
951 setup_session(ses
, uri
, base_session
);
955 switch_to_tab(term
, get_tab_number(ses
->tab
), -1);
957 if (!term
->main_menu
) activate_bfu_technology(ses
, -1);
962 init_remote_session(struct session
*ses
, enum remote_session_flags
*remote_ptr
,
965 enum remote_session_flags remote
= *remote_ptr
;
967 if (remote
& SES_REMOTE_CURRENT_TAB
) {
969 /* Mask out the current tab flag */
970 *remote_ptr
= remote
& ~SES_REMOTE_CURRENT_TAB
;
972 /* Remote session was masked out. Open all following URIs in
975 *remote_ptr
= SES_REMOTE_NEW_TAB
;
977 } else if (remote
& SES_REMOTE_NEW_TAB
) {
978 /* FIXME: This is not perfect. Doing multiple -remote
979 * with no URLs will make the goto dialogs
980 * inaccessible. Maybe we should not support this kind
981 * of thing or make the window focus detecting code
982 * more intelligent. --jonas */
983 open_uri_in_new_tab(ses
, uri
, 0, 0);
985 if (remote
& SES_REMOTE_PROMPT_URL
) {
986 dialog_goto_url_open(ses
);
989 } else if (remote
& SES_REMOTE_NEW_WINDOW
) {
990 /* FIXME: It is quite rude because we just take the first
991 * possibility and should maybe make it possible to specify
992 * new-screen etc via -remote "openURL(..., new-*)" --jonas */
993 if (!can_open_in_new(ses
->tab
->term
))
996 open_uri_in_new_window(ses
, uri
, NULL
,
997 ses
->tab
->term
->environment
,
998 CACHE_MODE_NORMAL
, TASK_NONE
);
1000 } else if (remote
& SES_REMOTE_ADD_BOOKMARK
) {
1001 #ifdef CONFIG_BOOKMARKS
1005 /** @todo Bug 1066: What is the encoding of struri()?
1006 * This code currently assumes the system charset.
1007 * It might be best to keep URIs in plain ASCII and
1008 * then have a function that reversibly converts them
1009 * to IRIs for display in a given encoding. */
1010 uri_cp
= get_cp_index("System");
1011 add_bookmark_cp(NULL
, 1, uri_cp
, struri(uri
), struri(uri
));
1014 } else if (remote
& SES_REMOTE_INFO_BOX
) {
1015 unsigned char *text
;
1019 text
= memacpy(uri
->data
, uri
->datalen
);
1022 info_box(ses
->tab
->term
, MSGBOX_FREE_TEXT
,
1023 N_("Error"), ALIGN_CENTER
,
1026 } else if (remote
& SES_REMOTE_PROMPT_URL
) {
1027 dialog_goto_url_open(ses
);
1034 encode_session_info(struct string
*info
,
1035 LIST_OF(struct string_list_item
) *url_list
)
1037 struct string_list_item
*url
;
1039 if (!init_string(info
)) return NULL
;
1041 foreach (url
, *url_list
) {
1042 struct string
*str
= &url
->string
;
1044 add_bytes_to_string(info
, str
->source
, str
->length
+ 1);
1050 /** Older elinks versions (up to and including 0.9.1) sends no magic variable and if
1051 * this is detected we fallback to the old session info format. For this format
1052 * the magic member of terminal_info hold the length of the URI string. The
1053 * old format is handled by the default label in the switch.
1055 * The new session info format supports extraction of multiple URIS from the
1056 * terminal_info data member. The magic variable controls how to interpret
1059 * - INTERLINK_NORMAL_MAGIC means use the terminal_info session_info
1060 * variable as an id for a saved session.
1062 * - INTERLINK_REMOTE_MAGIC means use the terminal_info session_info
1063 * variable as the remote session flags. */
1065 decode_session_info(struct terminal
*term
, struct terminal_info
*info
)
1067 int len
= info
->length
;
1068 struct session
*base_session
= NULL
;
1069 enum remote_session_flags remote
= 0;
1072 switch (info
->magic
) {
1073 case INTERLINK_NORMAL_MAGIC
:
1074 /* Lookup if there are any saved sessions that should be
1075 * resumed using the session_info as an id. The id is derived
1076 * from the value of -base-session command line option in the
1077 * connecting instance.
1079 * At the moment it is only used when opening instances in new
1080 * window to figure out how to initialize it when the new
1081 * instance connects to the master.
1083 * We don't need to handle it for the old format since new
1084 * instances connecting this way will always be of the same
1085 * origin as the master. */
1086 if (init_saved_session(term
, info
->session_info
))
1090 case INTERLINK_REMOTE_MAGIC
:
1091 /* This could mean some fatal bug but I am unsure if we can
1092 * actually assert it since people could pour all kinds of crap
1093 * down the socket. */
1094 if (!info
->session_info
) {
1095 INTERNAL("Remote magic with no remote flags");
1099 remote
= info
->session_info
;
1101 /* If processing session info from a -remote instance we want
1102 * to hook up with the master so we can handle request for
1103 * stuff in current tab. */
1104 base_session
= get_master_session();
1105 if (!base_session
) return 0;
1110 /* The old format calculates the session_info and magic members
1111 * as part of the data that should be decoded so we have to
1112 * substract it to get the size of the actual URI data. */
1113 len
-= sizeof(info
->session_info
) + sizeof(info
->magic
);
1115 /* Extract URI containing @magic bytes */
1116 if (info
->magic
<= 0 || info
->magic
> len
)
1124 return !!init_session(base_session
, term
, NULL
, 0);
1126 /* Even though there are no URIs we still have to
1127 * handle remote stuff. */
1128 init_remote_session(base_session
, &remote
, NULL
);
1134 /* Extract multiple (possible) NUL terminated URIs */
1136 unsigned char *end
= memchr(str
, 0, len
);
1137 int urilength
= end
? end
- str
: len
;
1138 struct uri
*uri
= NULL
;
1139 unsigned char *uristring
= memacpy(str
, urilength
);
1142 uri
= get_hooked_uri(uristring
, base_session
, term
->cwd
);
1143 mem_free(uristring
);
1146 len
-= urilength
+ 1;
1147 str
+= urilength
+ 1;
1152 init_remote_session(base_session
, &remote
, uri
);
1155 int backgrounded
= !list_empty(term
->windows
);
1157 struct session
*ses
;
1160 uri
= get_uri("about:blank", 0);
1162 ses
= init_session(base_session
, term
, uri
, backgrounded
);
1164 /* End loop if initialization fails */
1166 } else if (bad_url
) {
1167 print_error_dialog(ses
, connection_state(S_BAD_URL
),
1173 if (uri
) done_uri(uri
);
1176 /* If we only initialized remote sessions or didn't manage to add any
1177 * new tabs return zero so the terminal will be destroyed ASAP. */
1178 return remote
? 0 : !list_empty(term
->windows
);
1183 abort_loading(struct session
*ses
, int interrupt
)
1185 if (have_location(ses
)) {
1186 struct location
*loc
= cur_loc(ses
);
1188 cancel_download(&loc
->download
, interrupt
);
1189 abort_files_load(ses
, interrupt
);
1191 abort_preloading(ses
, interrupt
);
1195 destroy_session(struct session
*ses
)
1197 struct document_view
*doc_view
;
1200 if_assert_failed
return;
1202 destroy_downloads(ses
);
1203 abort_loading(ses
, 0);
1205 if (ses
->doc_view
) {
1206 detach_formatted(ses
->doc_view
);
1207 mem_free(ses
->doc_view
);
1210 foreach (doc_view
, ses
->scrn_frames
)
1211 detach_formatted(doc_view
);
1213 free_list(ses
->scrn_frames
);
1215 destroy_history(&ses
->history
);
1216 set_session_referrer(ses
, NULL
);
1218 if (ses
->loading_uri
) done_uri(ses
->loading_uri
);
1220 kill_timer(&ses
->display_timer
);
1222 while (!list_empty(ses
->type_queries
))
1223 done_type_query(ses
->type_queries
.next
);
1225 if (ses
->download_uri
) done_uri(ses
->download_uri
);
1226 mem_free_if(ses
->search_word
);
1227 mem_free_if(ses
->last_search_word
);
1228 mem_free_if(ses
->status
.last_title
);
1229 #ifdef CONFIG_ECMASCRIPT
1230 mem_free_if(ses
->status
.window_status
);
1233 delete_option(ses
->option
);
1240 reload(struct session
*ses
, enum cache_mode cache_mode
)
1242 reload_frame(ses
, NULL
, cache_mode
);
1246 reload_frame(struct session
*ses
, unsigned char *name
,
1247 enum cache_mode cache_mode
)
1249 abort_loading(ses
, 0);
1251 if (cache_mode
== CACHE_MODE_INCREMENT
) {
1252 cache_mode
= CACHE_MODE_NEVER
;
1253 if (ses
->reloadlevel
< CACHE_MODE_NEVER
)
1254 cache_mode
= ++ses
->reloadlevel
;
1256 ses
->reloadlevel
= cache_mode
;
1259 if (have_location(ses
)) {
1260 struct location
*loc
= cur_loc(ses
);
1261 struct file_to_load
*ftl
;
1263 #ifdef CONFIG_ECMASCRIPT
1264 loc
->vs
.ecmascript_fragile
= 1;
1267 /* FIXME: When reloading use loading_callback and set up a
1268 * session task so that the reloading will work even when the
1269 * reloaded document contains redirects. This is needed atleast
1270 * when reloading HTTP auth document after the user has entered
1272 loc
->download
.data
= ses
;
1273 loc
->download
.callback
= (download_callback_T
*) doc_loading_callback
;
1275 mem_free_set(&ses
->task
.target
.frame
, null_or_stracpy(name
));
1277 load_uri(loc
->vs
.uri
, ses
->referrer
, &loc
->download
, PRI_MAIN
, cache_mode
, -1);
1279 foreach (ftl
, ses
->more_files
) {
1280 if (file_to_load_is_active(ftl
))
1283 ftl
->download
.data
= ftl
;
1284 ftl
->download
.callback
= (download_callback_T
*) file_loading_callback
;
1286 load_additional_file(ftl
, cache_mode
);
1293 ses_find_frame(struct session
*ses
, unsigned char *name
)
1295 struct location
*loc
= cur_loc(ses
);
1296 struct frame
*frame
;
1298 assertm(have_location(ses
), "ses_request_frame: no location yet");
1299 if_assert_failed
return NULL
;
1301 foreachback (frame
, loc
->frames
)
1302 if (!c_strcasecmp(frame
->name
, name
))
1309 set_session_referrer(struct session
*ses
, struct uri
*referrer
)
1311 if (ses
->referrer
) done_uri(ses
->referrer
);
1312 ses
->referrer
= referrer
? get_uri_reference(referrer
) : NULL
;
1316 tabwin_func(struct window
*tab
, struct term_event
*ev
)
1318 struct session
*ses
= tab
->data
;
1322 if (ses
) destroy_session(ses
);
1323 if (!list_empty(sessions
)) update_status();
1330 if (!ses
|| ses
->tab
!= get_current_tab(ses
->tab
->term
))
1333 draw_formatted(ses
, tab
->resize
);
1335 load_frames(ses
, ses
->doc_view
);
1336 process_file_requests(ses
);
1339 print_screen_status(ses
);
1344 /* This check is related to bug 296 */
1345 assert(ses
->tab
== get_current_tab(ses
->tab
->term
));
1346 send_event(ses
, ev
);
1352 * Gets the url being viewed by this session. Writes it into @a str.
1353 * A maximum of @a str_size bytes (including null) will be written.
1357 get_current_url(struct session
*ses
, unsigned char *str
, size_t str_size
)
1362 assert(str
&& str_size
> 0);
1364 uri
= have_location(ses
) ? cur_loc(ses
)->vs
.uri
: ses
->loading_uri
;
1366 /* Not looking or loading anything */
1367 if (!uri
) return NULL
;
1369 /* Ensure that the url size is not greater than str_size.
1370 * We can't just happily strncpy(str, here, str_size)
1371 * because we have to stop at POST_CHAR, not only at NULL. */
1372 length
= int_min(get_real_uri_length(uri
), str_size
- 1);
1374 return safe_strncpy(str
, struri(uri
), length
+ 1);
1378 * Gets the title of the page being viewed by this session. Writes it into
1379 * @a str. A maximum of @a str_size bytes (including null) will be written.
1383 get_current_title(struct session
*ses
, unsigned char *str
, size_t str_size
)
1385 struct document_view
*doc_view
= current_frame(ses
);
1387 assert(str
&& str_size
> 0);
1389 /* Ensure that the title is defined */
1390 /* TODO: Try globhist --jonas */
1391 if (doc_view
&& doc_view
->document
->title
)
1392 return safe_strncpy(str
, doc_view
->document
->title
, str_size
);
1398 * Gets the url of the link currently selected. Writes it into @a str.
1399 * A maximum of @a str_size bytes (including null) will be written.
1403 get_current_link_url(struct session
*ses
, unsigned char *str
, size_t str_size
)
1405 struct link
*link
= get_current_session_link(ses
);
1407 assert(str
&& str_size
> 0);
1409 if (!link
) return NULL
;
1411 return safe_strncpy(str
, link
->where
? link
->where
: link
->where_img
, str_size
);
1414 /** get_current_link_name: returns the name of the current link
1415 * (the text between @<A> and @</A>), @a str is a preallocated string,
1416 * @a str_size includes the null char.
1417 * @relates session */
1419 get_current_link_name(struct session
*ses
, unsigned char *str
, size_t str_size
)
1421 struct link
*link
= get_current_session_link(ses
);
1422 unsigned char *where
, *name
= NULL
;
1424 assert(str
&& str_size
> 0);
1426 if (!link
) return NULL
;
1428 where
= link
->where
? link
->where
: link
->where_img
;
1429 #ifdef CONFIG_GLOBHIST
1431 struct global_history_item
*item
;
1433 item
= get_global_history_item(where
);
1434 if (item
) name
= item
->title
;
1437 if (!name
) name
= get_link_name(link
);
1438 if (!name
) name
= where
;
1440 return safe_strncpy(str
, name
, str_size
);
1444 get_current_link_in_view(struct document_view
*doc_view
)
1446 struct link
*link
= get_current_link(doc_view
);
1448 return link
&& !link_is_form(link
) ? link
: NULL
;
1451 /** @relates session */
1453 get_current_session_link(struct session
*ses
)
1455 return get_current_link_in_view(current_frame(ses
));
1458 /** @relates session */
1460 eat_kbd_repeat_count(struct session
*ses
)
1462 int count
= ses
->kbdprefix
.repeat_count
;
1464 set_kbd_repeat_count(ses
, 0);
1466 /* Clear status bar when prefix is eaten (bug 930) */
1467 print_screen_status(ses
);
1471 /** @relates session */
1473 set_kbd_repeat_count(struct session
*ses
, int new_count
)
1475 int old_count
= ses
->kbdprefix
.repeat_count
;
1477 if (new_count
== old_count
)
1480 ses
->kbdprefix
.repeat_count
= new_count
;
1482 /* Update the status bar. */
1483 print_screen_status(ses
);
1485 /* Clear the old link highlighting. */
1486 draw_formatted(ses
, 0);
1488 if (new_count
!= 0) {
1489 struct document_view
*doc_view
= current_frame(ses
);
1491 highlight_links_with_prefixes_that_start_with_n(ses
->tab
->term
,