1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright 2014 The Chromium Authors. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "core/page/InjectedStyleSheets.h"
23
24#include "core/dom/Document.h"
25#include "core/dom/StyleEngine.h"
26#include "core/frame/LocalFrame.h"
27#include "core/page/Page.h"
28#include "wtf/HashSet.h"
29
30namespace blink {
31
32// static
33InjectedStyleSheets& InjectedStyleSheets::instance()
34{
35    DEFINE_STATIC_LOCAL(InjectedStyleSheets, instance, ());
36    return instance;
37}
38
39void InjectedStyleSheets::add(const String& source, const Vector<String>& whitelist, StyleInjectionTarget injectIn)
40{
41    m_entries.append(adoptPtr(new InjectedStyleSheetEntry(source, whitelist, injectIn)));
42    invalidateInjectedStyleSheetCacheInAllFrames();
43}
44
45void InjectedStyleSheets::removeAll()
46{
47    m_entries.clear();
48    invalidateInjectedStyleSheetCacheInAllFrames();
49}
50
51void InjectedStyleSheets::invalidateInjectedStyleSheetCacheInAllFrames()
52{
53    // Clear our cached sheets and have them just reparse.
54    const HashSet<Page*>& pages = Page::ordinaryPages();
55
56    HashSet<Page*>::const_iterator end = pages.end();
57    for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
58        for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
59            if (frame->isLocalFrame())
60                toLocalFrame(frame)->document()->styleEngine()->invalidateInjectedStyleSheetCache();
61        }
62    }
63}
64
65} // namespace blink
66