1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Motorola Mobility Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28
29#if ENABLE(BLOB)
30
31#include "DOMURL.h"
32
33#include "ActiveDOMObject.h"
34#include "Blob.h"
35#include "BlobURL.h"
36#include "KURL.h"
37#include "MemoryCache.h"
38#include "PublicURLManager.h"
39#include "ResourceRequest.h"
40#include "ScriptExecutionContext.h"
41#include "SecurityOrigin.h"
42#include "ThreadableBlobRegistry.h"
43#include <wtf/PassOwnPtr.h>
44#include <wtf/MainThread.h>
45
46#if ENABLE(MEDIA_SOURCE)
47#include "MediaSource.h"
48#include "MediaSourceRegistry.h"
49#endif
50
51#if ENABLE(MEDIA_STREAM)
52#include "MediaStream.h"
53#include "MediaStreamRegistry.h"
54#endif
55
56namespace WebCore {
57
58#if ENABLE(MEDIA_SOURCE)
59String DOMURL::createObjectURL(ScriptExecutionContext* scriptExecutionContext, MediaSource* source)
60{
61    // Since WebWorkers cannot obtain MediaSource objects, we should be on the main thread.
62    ASSERT(isMainThread());
63
64    if (!scriptExecutionContext || !source)
65        return String();
66
67    KURL publicURL = BlobURL::createPublicURL(scriptExecutionContext->securityOrigin());
68    if (publicURL.isEmpty())
69        return String();
70
71    MediaSourceRegistry::registry().registerMediaSourceURL(publicURL, source);
72    scriptExecutionContext->publicURLManager().sourceURLs().add(publicURL.string());
73
74    return publicURL.string();
75}
76#endif
77
78#if ENABLE(MEDIA_STREAM)
79String DOMURL::createObjectURL(ScriptExecutionContext* scriptExecutionContext, MediaStream* stream)
80{
81    if (!scriptExecutionContext || !stream)
82        return String();
83
84    KURL publicURL = BlobURL::createPublicURL(scriptExecutionContext->securityOrigin());
85    if (publicURL.isEmpty())
86        return String();
87
88    // Since WebWorkers cannot obtain Stream objects, we should be on the main thread.
89    ASSERT(isMainThread());
90
91    MediaStreamRegistry::registry().registerMediaStreamURL(publicURL, stream);
92    scriptExecutionContext->publicURLManager().streamURLs().add(publicURL.string());
93
94    return publicURL.string();
95}
96#endif
97
98String DOMURL::createObjectURL(ScriptExecutionContext* scriptExecutionContext, Blob* blob)
99{
100    if (!scriptExecutionContext || !blob)
101        return String();
102
103    KURL publicURL = BlobURL::createPublicURL(scriptExecutionContext->securityOrigin());
104    if (publicURL.isEmpty())
105        return String();
106
107    ThreadableBlobRegistry::registerBlobURL(scriptExecutionContext->securityOrigin(), publicURL, blob->url());
108    scriptExecutionContext->publicURLManager().blobURLs().add(publicURL.string());
109
110    return publicURL.string();
111}
112
113void DOMURL::revokeObjectURL(ScriptExecutionContext* scriptExecutionContext, const String& urlString)
114{
115    if (!scriptExecutionContext)
116        return;
117
118    KURL url(KURL(), urlString);
119    ResourceRequest request(url);
120#if ENABLE(CACHE_PARTITIONING)
121    request.setCachePartition(scriptExecutionContext->topOrigin()->cachePartition());
122#endif
123    MemoryCache::removeRequestFromCache(scriptExecutionContext, request);
124
125    HashSet<String>& blobURLs = scriptExecutionContext->publicURLManager().blobURLs();
126    if (blobURLs.contains(url.string())) {
127        ThreadableBlobRegistry::unregisterBlobURL(url);
128        blobURLs.remove(url.string());
129    }
130
131#if ENABLE(MEDIA_SOURCE)
132    HashSet<String>& sourceURLs = scriptExecutionContext->publicURLManager().sourceURLs();
133    if (sourceURLs.contains(url.string())) {
134        MediaSourceRegistry::registry().unregisterMediaSourceURL(url);
135        sourceURLs.remove(url.string());
136    }
137#endif
138#if ENABLE(MEDIA_STREAM)
139    HashSet<String>& streamURLs = scriptExecutionContext->publicURLManager().streamURLs();
140    if (streamURLs.contains(url.string())) {
141        // FIXME: make sure of this assertion below. Raise a spec question if required.
142        // Since WebWorkers cannot obtain Stream objects, we should be on the main thread.
143        ASSERT(isMainThread());
144        MediaStreamRegistry::registry().unregisterMediaStreamURL(url);
145        streamURLs.remove(url.string());
146    }
147#endif
148}
149
150} // namespace WebCore
151
152#endif // ENABLE(BLOB)
153