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 "ActiveDOMObject.h"
30#include "Blob.h"
31#include "BlobURL.h"
32#include "MemoryCache.h"
33#include "PublicURLManager.h"
34#include "ResourceRequest.h"
35#include "ScriptExecutionContext.h"
36#include "SecurityOrigin.h"
37#include "URLSearchParams.h"
38#include <wtf/MainThread.h>
39
40namespace WebCore {
41
42inline DOMURL::DOMURL(URL&& completeURL, URL&& baseURL)
43 : m_baseURL(WTFMove(baseURL))
44 , m_url(WTFMove(completeURL))
45{
46}
47
48ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const String& base)
49{
50 URL baseURL { URL { }, base };
51 if (!baseURL.isValid())
52 return Exception { TypeError };
53 URL completeURL { baseURL, url };
54 if (!completeURL.isValid())
55 return Exception { TypeError };
56 return adoptRef(*new DOMURL(WTFMove(completeURL), WTFMove(baseURL)));
57}
58
59ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const DOMURL& base)
60{
61 return create(url, base.href());
62}
63
64ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url)
65{
66 URL baseURL { WTF::blankURL() };
67 URL completeURL { baseURL, url };
68 if (!completeURL.isValid())
69 return Exception { TypeError };
70 return adoptRef(*new DOMURL(WTFMove(completeURL), WTFMove(baseURL)));
71}
72
73DOMURL::~DOMURL()
74{
75 if (m_searchParams)
76 m_searchParams->associatedURLDestroyed();
77}
78
79ExceptionOr<void> DOMURL::setHref(const String& url)
80{
81 URL completeURL { m_baseURL, url };
82 if (!completeURL.isValid())
83 return Exception { TypeError };
84 m_url = WTFMove(completeURL);
85 if (m_searchParams)
86 m_searchParams->updateFromAssociatedURL();
87 return { };
88}
89
90void DOMURL::setQuery(const String& query)
91{
92 m_url.setQuery(query);
93}
94
95String DOMURL::createObjectURL(ScriptExecutionContext& scriptExecutionContext, Blob& blob)
96{
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
111URLSearchParams& DOMURL::searchParams()
112{
113 if (!m_searchParams)
114 m_searchParams = URLSearchParams::create(search(), this);
115 return *m_searchParams;
116}
117
118void DOMURL::revokeObjectURL(ScriptExecutionContext& scriptExecutionContext, const String& urlString)
119{
120 URL url(URL(), urlString);
121 ResourceRequest request(url);
122 request.setDomainForCachePartition(scriptExecutionContext.domainForCachePartition());
123
124 MemoryCache::removeRequestFromSessionCaches(scriptExecutionContext, request);
125
126 scriptExecutionContext.publicURLManager().revoke(url);
127}
128
129} // namespace WebCore
130