1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Simon Hausmann <hausmann@kde.org>
5 * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2010, 2014 Apple Inc. All rights reserved.
6 *           (C) 2006 Graham Dennis (graham.dennis@gmail.com)
7 * Copyright (C) 2011 Google Inc. All rights reserved.
8 * Copyright (C) 2012 Motorola Mobility Inc.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB.  If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26#include "config.h"
27#include "DOMURL.h"
28
29#include "SecurityOrigin.h"
30#include "ActiveDOMObject.h"
31#include "Blob.h"
32#include "BlobURL.h"
33#include "MemoryCache.h"
34#include "PublicURLManager.h"
35#include "ResourceRequest.h"
36#include "ScriptExecutionContext.h"
37#include <wtf/MainThread.h>
38
39namespace WebCore {
40
41PassRefPtr<DOMURL> DOMURL::create(const String& url, const String& base, ExceptionCode& ec)
42{
43    return adoptRef(new DOMURL(url, base, ec));
44}
45
46PassRefPtr<DOMURL> DOMURL::create(const String& url, const DOMURL* base, ExceptionCode& ec)
47{
48    ASSERT(base);
49    return adoptRef(new DOMURL(url, *base, ec));
50}
51
52PassRefPtr<DOMURL> DOMURL::create(const String& url, ExceptionCode& ec)
53{
54    return adoptRef(new DOMURL(url, ec));
55}
56
57inline DOMURL::DOMURL(const String& url, const String& base, ExceptionCode& ec)
58    : m_baseURL(URL(), base)
59    , m_url(m_baseURL, url)
60{
61    if (!m_baseURL.isValid() || !m_url.isValid())
62        ec = TypeError;
63}
64
65inline DOMURL::DOMURL(const String& url, const DOMURL& base, ExceptionCode& ec)
66    : m_baseURL(base.href())
67    , m_url(m_baseURL, url)
68{
69    if (!m_baseURL.isValid() || !m_url.isValid())
70        ec = TypeError;
71}
72
73inline DOMURL::DOMURL(const String& url, ExceptionCode& ec)
74    : m_baseURL(blankURL())
75    , m_url(m_baseURL, url)
76{
77    if (!m_url.isValid())
78        ec = TypeError;
79}
80
81void DOMURL::setHref(const String& url)
82{
83    m_url = URL(m_baseURL, url);
84}
85
86void DOMURL::setHref(const String& url, ExceptionCode& ec)
87{
88    setHref(url);
89    if (!m_url.isValid())
90        ec = TypeError;
91}
92
93String DOMURL::createObjectURL(ScriptExecutionContext* scriptExecutionContext, Blob* blob)
94{
95    if (!scriptExecutionContext || !blob)
96        return String();
97    return createPublicURL(scriptExecutionContext, blob);
98}
99
100String DOMURL::createPublicURL(ScriptExecutionContext* scriptExecutionContext, URLRegistrable* registrable)
101{
102    URL publicURL = BlobURL::createPublicURL(scriptExecutionContext->securityOrigin());
103    if (publicURL.isEmpty())
104        return String();
105
106    scriptExecutionContext->publicURLManager().registerURL(scriptExecutionContext->securityOrigin(), publicURL, registrable);
107
108    return publicURL.string();
109}
110
111void DOMURL::revokeObjectURL(ScriptExecutionContext* scriptExecutionContext, const String& urlString)
112{
113    if (!scriptExecutionContext)
114        return;
115
116    URL url(URL(), urlString);
117    ResourceRequest request(url);
118#if ENABLE(CACHE_PARTITIONING)
119    request.setCachePartition(scriptExecutionContext->topOrigin()->cachePartition());
120#endif
121    MemoryCache::removeRequestFromSessionCaches(scriptExecutionContext, request);
122
123    scriptExecutionContext->publicURLManager().revoke(url);
124}
125
126} // namespace WebCore
127