1/*
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef QuickLook_h
27#define QuickLook_h
28
29#if USE(QUICK_LOOK)
30
31#import "QuickLookHandleClient.h"
32#import "ResourceRequest.h"
33#import <objc/objc-runtime.h>
34#import <wtf/PassOwnPtr.h>
35#import <wtf/RefPtr.h>
36
37OBJC_CLASS NSData;
38OBJC_CLASS NSDictionary;
39OBJC_CLASS NSFileHandle;
40OBJC_CLASS NSSet;
41OBJC_CLASS NSString;
42OBJC_CLASS NSURL;
43OBJC_CLASS NSURLConnection;
44OBJC_CLASS NSURLResponse;
45OBJC_CLASS QLPreviewConverter;
46
47#if USE(CFNETWORK)
48typedef struct _CFURLResponse* CFURLResponseRef;
49typedef struct _CFURLConnection* CFURLConnectionRef;
50#endif
51
52namespace WebCore {
53
54class QuickLookHandleClient;
55class ResourceHandle;
56class ResourceLoader;
57class SynchronousResourceHandleCFURLConnectionDelegate;
58
59Class QLPreviewConverterClass();
60NSString *QLTypeCopyBestMimeTypeForFileNameAndMimeType(NSString *fileName, NSString *mimeType);
61NSString *QLTypeCopyBestMimeTypeForURLAndMimeType(NSURL *, NSString *mimeType);
62
63NSSet *QLPreviewGetSupportedMIMETypesSet();
64
65// Used for setting the permissions on the saved QL content
66NSDictionary *QLFileAttributes();
67NSDictionary *QLDirectoryAttributes();
68
69void addQLPreviewConverterWithFileForURL(NSURL *, id converter, NSString *fileName);
70NSString *qlPreviewConverterFileNameForURL(NSURL *);
71NSString *qlPreviewConverterUTIForURL(NSURL *);
72void removeQLPreviewConverterForURL(NSURL *);
73
74PassOwnPtr<ResourceRequest> registerQLPreviewConverterIfNeeded(NSURL *, NSString *mimeType, NSData *);
75
76const URL safeQLURLForDocumentURLAndResourceURL(const URL& documentURL, const String& resourceURL);
77
78const char* QLPreviewProtocol();
79
80NSString *createTemporaryFileForQuickLook(NSString *fileName);
81
82class QuickLookHandle {
83    WTF_MAKE_NONCOPYABLE(QuickLookHandle);
84public:
85    static std::unique_ptr<QuickLookHandle> create(ResourceHandle*, NSURLConnection *, NSURLResponse *, id delegate);
86#if USE(CFNETWORK)
87    static std::unique_ptr<QuickLookHandle> create(ResourceHandle*, SynchronousResourceHandleCFURLConnectionDelegate*, CFURLResponseRef);
88#endif
89    static std::unique_ptr<QuickLookHandle> create(ResourceLoader*, NSURLResponse *);
90    ~QuickLookHandle();
91
92    bool didReceiveDataArray(CFArrayRef);
93    bool didReceiveData(CFDataRef);
94    bool didFinishLoading();
95    void didFail();
96
97    NSURLResponse *nsResponse();
98#if USE(CFNETWORK)
99    CFURLResponseRef cfResponse();
100#endif
101
102    void setClient(PassRefPtr<QuickLookHandleClient> client) { m_client = client; }
103
104    String previewFileName() const;
105    String previewUTI() const;
106    NSURL *firstRequestURL() const { return m_firstRequestURL.get(); }
107    NSURL *previewRequestURL() const;
108    QLPreviewConverter *converter() const { return m_converter.get(); }
109
110private:
111    QuickLookHandle(NSURL *, NSURLConnection *, NSURLResponse *, id delegate);
112
113    RetainPtr<NSURL> m_firstRequestURL;
114    RetainPtr<QLPreviewConverter> m_converter;
115    RetainPtr<id> m_delegate;
116    bool m_finishedLoadingDataIntoConverter;
117    RetainPtr<NSFileHandle *> m_quicklookFileHandle;
118    RetainPtr<NSURLResponse> m_nsResponse;
119    RefPtr<QuickLookHandleClient> m_client;
120};
121
122} // namespace WebCore
123
124#endif // USE(QUICK_LOOK)
125
126#endif // QuickLook_h
127