Skip to content

Commit fd739f4

Browse files
author
Daniel Herzog
committed
Initital commit for DFL-3451: Provide interfaces for network related UIs from one single network-service instance
1 parent 5749beb commit fd739f4

File tree

2 files changed

+79
-47
lines changed

2 files changed

+79
-47
lines changed

src/network/network_service.js

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
cls.NetworkLoggerService = function()
44
{
5-
this.CONTEXT_TYPE_LOGGER = "network-logger";
5+
this.CONTEXT_TYPE_LOGGER = this.MAIN_CONTEXT_TYPE = "network-logger";
66
this.CONTEXT_TYPE_CRAFTER = "request-crafter";
77

88
this._get_matching_context = function(res_id)
@@ -20,7 +20,8 @@ cls.NetworkLoggerService = function()
2020
var ctx = this.contexts[type];
2121
if (!ctx && force)
2222
{
23-
ctx = this.contexts[type] = new cls.RequestContext();
23+
var is_main_context = type === this.MAIN_CONTEXT_TYPE;
24+
ctx = this.contexts[type] = new cls.RequestContext(this, is_main_context);
2425
this.post("context-established", {"context_type": type});
2526
}
2627
return ctx;
@@ -83,12 +84,15 @@ cls.NetworkLoggerService = function()
8384
var window_context = ctx.get_window_context(data.windowID);
8485
if (!window_context)
8586
{
86-
var window_context = new cls.NetworkLoggerService.WindowContext(data.windowID);
87+
var window_context = (
88+
new cls.NetworkLoggerService.WindowContext(data.windowID, this._service, ctx)
89+
);
8790
ctx.window_contexts.push(window_context);
8891
if (!data.parentDocumentID)
8992
{
9093
window_context.saw_main_document = true;
9194
}
95+
ctx.post_("window-context-added", {"window-context": window_context});
9296
}
9397
};
9498
this._on_abouttoloaddocument_bound = this._on_abouttoloaddocument.bind(this, this._on_abouttoloaddocument_bound);
@@ -102,9 +106,10 @@ cls.NetworkLoggerService = function()
102106
var ctx = this._get_matching_context(data.resourceID);
103107
if (!ctx)
104108
{
105-
var context_type = this.CONTEXT_TYPE_LOGGER;
106-
ctx = this.contexts[context_type] = new cls.RequestContext();
107-
this.post("context-established", {"context_type": context_type});
109+
var type = this.CONTEXT_TYPE_LOGGER;
110+
var is_main_context = type === this.MAIN_CONTEXT_TYPE;
111+
ctx = this.contexts[type] = new cls.RequestContext(this, is_main_context);
112+
this.post("context-established", {"context_type": type});
108113
}
109114
ctx.update("urlload", data);
110115
};
@@ -358,35 +363,6 @@ cls.NetworkLoggerService = function()
358363
this._res_service.requestGetResource(tag, [entry.resource_id, [typecode, 1]]);
359364
};
360365

361-
this.get_resource_info = function(resource_id)
362-
{
363-
// Returns a ResourceInfo based on the most recent Entry with that resource_id.
364-
var ctx = this.contexts[this.CONTEXT_TYPE_LOGGER];
365-
if (ctx)
366-
{
367-
var entry = ctx.get_entries_with_res_id(resource_id).last;
368-
if (entry && entry.current_response && entry.current_response.responsebody)
369-
{
370-
return new cls.ResourceInfo(entry);
371-
}
372-
}
373-
return null;
374-
};
375-
376-
this.get_entry = function(resource_id)
377-
{
378-
// Returns the current entry matching a resource id.
379-
// Todo: this only makes sense when it's from a CONTEXT_TYPE_LOGGER context,
380-
// if the same url was requested by the crafter after it was in the page,
381-
// it should still return the one that the page got. That should be in the name,
382-
// or get_entry should just be called on the context instead of the service.
383-
var ctx = this.contexts[this.CONTEXT_TYPE_LOGGER];
384-
if (ctx)
385-
return ctx.get_entries_with_res_id(resource_id).last;
386-
387-
return null;
388-
}
389-
390366
this._handle_get_resource = function(status, data, resource_id)
391367
{
392368
var ctx = this.contexts[this.CONTEXT_TYPE_LOGGER];
@@ -403,21 +379,57 @@ cls.NetworkLoggerService = function()
403379
// Post update message from here. This is only needed when the generic updating per event is paused.
404380
if (this.is_paused)
405381
{
406-
ctx.post("resource-update", {id: resource_id});
382+
ctx.post_("resource-update", {id: event.resourceID});
407383
}
408384
};
409385

410386
this.init();
411387
};
412388

413-
cls.NetworkLoggerService.WindowContext = function(window_id)
389+
cls.NetworkLoggerService.WindowContext = function(window_id, service, context)
414390
{
391+
this._service = service;
392+
this._context = context;
415393
this.id = window_id;
416394
this.saw_main_document = false;
417395
this.entry_ids = [];
418-
}
396+
};
397+
398+
cls.NetworkLoggerService.WindowContextPrototype = function()
399+
{
400+
this.get_resources = function(resource_ids)
401+
{
402+
// resource_ids
403+
// An optional array of resource ids
404+
405+
// Take all entries of the window-context's context, filter by what actually belongs
406+
// to this window context.
407+
var entries = this._context.get_entries().filter(function(entry) {
408+
return this.entry_ids.contains(entry.id) &&
409+
(!resource_ids || resource_ids.contains(entry.id));
410+
}, this);
411+
412+
// Todo: can hopefully be made nicer.
413+
var resource_ids_collected_resources = [];
414+
var resources = [];
415+
entries.forEach(function(entry) {
416+
var index_of_last_with_res_id = resource_ids_collected_resources.indexOf(entry.resource_id);
417+
if (index_of_last_with_res_id != -1)
418+
{
419+
// This is newer, remove the previous entry from resources.
420+
resources.splice(index_of_last_with_res_id, 1);
421+
resource_ids_collected_resources.splice(index_of_last_with_res_id, 1);
422+
}
423+
resources.push(entry);
424+
resource_ids_collected_resources.push(entry.resource_id);
425+
});
426+
return resources.map(function(entry){return new cls.ResourceInfo(entry)});
427+
};
428+
};
429+
430+
cls.NetworkLoggerService.WindowContext.prototype = new cls.NetworkLoggerService.WindowContextPrototype();
419431

420-
cls.RequestContext = function()
432+
cls.RequestContext = function(service, is_main_context)
421433
{
422434
this.FILTER_ALLOW_ALL = {
423435
type_list: [],
@@ -426,8 +438,11 @@ cls.RequestContext = function()
426438
this.allocated_res_ids = [];
427439
this.window_contexts = [];
428440
this.is_paused = false;
441+
this.is_waiting_for_create_request = false;
429442
this._logger_entries = [];
430443
this._filters = [this.FILTER_ALLOW_ALL];
444+
this._is_main_context = is_main_context;
445+
this._service = service;
431446
this._init();
432447
};
433448

@@ -436,13 +451,11 @@ cls.RequestContextPrototype = function()
436451
this._init = function()
437452
{
438453
// When a new context is initiated, it's not paused by default. Reset the setting.
439-
// Todo: The context for the request crafter shouldn't do that.
440454
// Todo: Ideally, when paused, the new context should be created in a different
441455
// place, so the old one can be kept while we're on pause.
442-
if (settings.network_logger.get("pause") != false)
456+
if (this._is_main_context && settings.network_logger.get("pause") != false)
443457
settings.network_logger.set("pause", false);
444458

445-
this.is_waiting_for_create_request = false;
446459
this._filter_function_bound = this._filter_function.bind(this);
447460
window.cls.MessageMixin.apply(this);
448461
};
@@ -566,7 +579,9 @@ cls.RequestContextPrototype = function()
566579
var matching_window_context = this.get_window_context(event.windowID);
567580
if (!matching_window_context)
568581
{
569-
this.window_contexts.push(new cls.NetworkLoggerService.WindowContext(event.windowID));
582+
var window_context = new cls.NetworkLoggerService.WindowContext(event.windowID, this._service, this);
583+
this.window_contexts.push(window_context);
584+
this.post_("window-context-added", {"window-context": window_context});
570585
}
571586
}
572587

@@ -629,10 +644,21 @@ cls.RequestContextPrototype = function()
629644

630645
if (!this.is_paused)
631646
{
632-
this.post("resource-update", {id: event.resourceID});
647+
this.post_("resource-update", {id: event.resourceID});
633648
}
634649
};
635650

651+
this.post_ = function(name, body) // post_on_context_or_service?
652+
{
653+
// Find out where to post the update message.
654+
// Messages of main_contexts are posted on the service, not the context.
655+
var posting_object = this;
656+
if (this._is_main_context)
657+
posting_object = this._service;
658+
659+
posting_object.post(name, body);
660+
};
661+
636662
this.remove_window_context = function(window_id)
637663
{
638664
var window_context = this.get_window_context(window_id);
@@ -652,6 +678,7 @@ cls.RequestContextPrototype = function()
652678
return window_id != context.id;
653679
}
654680
);
681+
this.post_("window-context-removed", {"window-id": window_id});
655682
};
656683

657684
this.get_entry_from_filtered = function(id)
@@ -1323,6 +1350,8 @@ cls.NetworkLoggerResponse.prototype = new cls.NetworkLoggerResponsePrototype();
13231350
cls.ResourceInfo = function(entry)
13241351
{
13251352
this.url = entry.url;
1326-
this.responseheaders = entry.current_response.response_headers;
1327-
this.responsebody = entry.current_response.responsebody;
1353+
this.id = entry.id;
1354+
this.document_id = entry.document_id;
1355+
this.type = entry.type;
1356+
this._entry = entry; // dbg
13281357
};

src/network/network_view.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ cls.NetworkLogView = function(id, name, container_class, html, default_handler,
6161
var ctx = this._service.get_request_context(this._service.CONTEXT_TYPE_LOGGER);
6262
if (ctx)
6363
{
64+
if (this._type_filters)
65+
ctx.set_filters(this._type_filters);
66+
6467
this._render_main_view(this._container);
6568
this.text_search.update_search();
6669
}
@@ -657,7 +660,6 @@ cls.NetworkLogView = function(id, name, container_class, html, default_handler,
657660
if (this._type_filters)
658661
ctx.set_filters(this._type_filters);
659662

660-
ctx.addListener("resource-update", this.update.bind(this));
661663
}
662664
}.bind(this);
663665

@@ -688,6 +690,7 @@ cls.NetworkLogView = function(id, name, container_class, html, default_handler,
688690

689691
this._service.addListener("context-established", this._on_context_established_bound);
690692
this._service.addListener("context-cleared", this.update.bind(this));
693+
this._service.addListener("resource-update", this.update.bind(this));
691694

692695
ActionHandlerInterface.apply(this);
693696
this._handlers = {

0 commit comments

Comments
 (0)