Add performInBackgroundContext

Also refactored a lot of current/main context code to work with both
document-based and shoebox-type applications.