// Copyright 1998-2000 Omni Development, Inc.  All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.

#import <Foundation/Foundation.h>
#import <OmniBase/OmniBase.h>
#import <OmniFoundation/OmniFoundation.h>

#import "OWContentInfo-Scripting.h"

#import "OHDocumentProtocol.h"
#import "OHDocumentFrameProtocol.h"
#import "OHDocumentViewProtocol.h"
#import "OWPipeline-ScriptExtensions.h"
#import "OWScriptDocumentProxy.h"

RCS_ID("$Header: /Network/Source/CVS/OmniGroup/Frameworks/OmniHTML/Scripting.subproj/OWContentInfo-Scripting.m,v 1.3 2000/01/20 00:27:56 kc Exp $")

@implementation OWContentInfo (Scripting)

// This is a kind of odd place to put the script context (which contains the script interpreter(s)). However, the lifetime of a script context is one appearance of an HTML document. If we make rendered HTML documents non-cacheable, then this corresponds to the lifetime of an OHHTMLPageView and its ContentInfo. Sheesh.

- (OWScriptContext *)scriptContext;
{
    id <OHDocumentFrame> anOpportunisticFrameView;

    [lock lock];
    NS_DURING;
    
    if (!scriptContext) {

        /* Only documents have script contexts, not other kinds of content */
        if (![nonretainedContent conformsToProtocol:@protocol(OHDocument)]) {
            [lock unlock];
            NS_VALUERETURN(nil, OWScriptContext *);
        }

        scriptContext = [[OWScriptContext allocWithZone:[self zone]] initWithOwner:self];
    }

    anOpportunisticFrameView = [[(id <OHDocument>)nonretainedContent omniDocumentView] frameView];
    if (anOpportunisticFrameView)
        [scriptContext setFrame:[anOpportunisticFrameView scriptProxy] hint:NO];

    NS_HANDLER {
        [lock unlock];
        [localException raise];
    } NS_ENDHANDLER;
    [lock unlock];

    return scriptContext;
}

- (OWScriptContext *)existingScriptContext;
{
    return scriptContext;
}

- (OWScriptDocumentProxy *)documentProxy
{
    OWPipeline *docPipeline = nil;

    [lock lock];
    NS_DURING;
    
    if ([tasks count]) {
        /* As far as I know this is always true, but if it isn't, we'll need to choose a particular pipeline somehow */
        OBASSERT([tasks count] == 1);
        docPipeline = [[tasks objectAtIndex:0] retain];
    } else {
        /* This happens if the pipeline has already been deactivated, in which case OnUnload will already have been called, although the scriptContext may not have been torn down yet */
        docPipeline = nil;
    }

    NS_HANDLER {
        [docPipeline release];
        [lock unlock];
        [localException raise];
    } NS_ENDHANDLER;
    [lock unlock];

    if (docPipeline) {
        OWScriptDocumentProxy *docProxy = [docPipeline scriptProxy];
        [[docProxy retain] autorelease];
        [docPipeline release];
        return docProxy;
    } else
        return nil;
}

@end
