1/*
2 * Copyright (C) 2008 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#import "WebOpenPanelResultListener.h"
27
28#import <WebCore/FileChooser.h>
29#import <wtf/PassRefPtr.h>
30
31#if PLATFORM(IOS)
32#import <WebCore/Icon.h>
33#endif
34
35using namespace WebCore;
36
37@implementation WebOpenPanelResultListener
38
39- (id)initWithChooser:(PassRefPtr<FileChooser>)chooser
40{
41    self = [super init];
42    if (!self)
43        return nil;
44    _chooser = chooser.leakRef();
45    return self;
46}
47
48#ifndef NDEBUG
49- (void)dealloc
50{
51    ASSERT(!_chooser);
52    [super dealloc];
53}
54
55- (void)finalize
56{
57    ASSERT(!_chooser);
58    [super finalize];
59}
60#endif
61
62- (void)cancel
63{
64    ASSERT(_chooser);
65    if (!_chooser)
66        return;
67    _chooser->deref();
68    _chooser = 0;
69}
70
71- (void)chooseFilename:(NSString *)filename
72{
73    ASSERT(_chooser);
74    if (!_chooser)
75        return;
76    _chooser->chooseFile(filename);
77    _chooser->deref();
78    _chooser = 0;
79}
80
81- (void)chooseFilenames:(NSArray *)filenames
82{
83    ASSERT(_chooser);
84    if (!_chooser)
85        return;
86    int count = [filenames count];
87    Vector<String> names(count);
88    for (int i = 0; i < count; i++)
89        names[i] = [filenames objectAtIndex:i];
90    _chooser->chooseFiles(names);
91    _chooser->deref();
92    _chooser = 0;
93}
94
95#if PLATFORM(IOS)
96- (void)chooseFilename:(NSString *)filename displayString:(NSString *)displayString iconImage:(CGImageRef)imageRef
97{
98    [self chooseFilenames:[NSArray arrayWithObject:filename] displayString:displayString iconImage:imageRef];
99}
100
101- (void)chooseFilenames:(NSArray *)filenames displayString:(NSString *)displayString iconImage:(CGImageRef)imageRef
102{
103    ASSERT(_chooser);
104    if (!_chooser)
105        return;
106
107    RefPtr<Icon> icon = Icon::createIconForImage(imageRef);
108
109    NSUInteger count = [filenames count];
110    Vector<String> names(count);
111    for (NSUInteger i = 0; i < count; ++i)
112        names[i] = [filenames objectAtIndex:i];
113    _chooser->chooseMediaFiles(names, displayString, icon.get());
114
115    // FIXME: we shouldn't be manually deref()'ing here.
116    _chooser->deref();
117    _chooser = nullptr;
118}
119#endif
120
121@end
122