UI and stages
Cross hosts one stage at a time. An application can replace that
stage as it moves between logical parts. Business logic belongs in a
core::Stage-derived class. A stage normally loads its UI
from Attach:
bridge::LoadView(Index(), "document");
Platform adapters resolve that name to
assets/document.htm and initialize the bridge in the loaded
document.
UI-to-core communication
The UI sends a message to the current stage by calling
CallHandler(id, command, info). All three arguments are
strings; use an empty string for info when the message has
no payload. For portability across the platform adapters,
id and command must be non-empty and contain
no whitespace.
Derived stages register message callbacks in the protected
handlers_ map. A map key corresponds to the message
id supplied by the UI:
using HANDLER = std::function<void(const char* command, const char* info)>;
std::map<std::string, HANDLER> handlers_;
Binary content
Images, audio, downloads, and other HTML resources can be supplied as
binary data by overriding Stage::FeedUri:
virtual void FeedUri(const char* uri, std::function<void(
const std::vector<unsigned char>&)>&& consume) = 0;
Cross validates cross://<receiver>/<path>
requests for the active stage and passes only <path>
to FeedUri. The override completes the request by invoking
consume with the resource bytes.
Save data
Cross delegates persistence to the platform adapter. The adapter
transforms its native storage into streams and passes those streams to
the application:
void application::Restore(std::istream& input, Completion completion);
void application::Checkpoint(std::ostream& output);
An empty input stream means that there is no saved snapshot. The
adapter keeps the restore stream alive until the application invokes
completion. The application must not retain the checkpoint
output after Checkpoint returns; the adapter commits it to
native storage afterward. If the application sets failbit
on the output, the adapter discards the attempted checkpoint and
preserves the existing native storage.