Changeset 162395 in webkit


Ignore:
Timestamp:
Jan 20, 2014 5:51:03 PM (10 years ago)
Author:
andersca@apple.com
Message:

Move user style sheet handling to UserContentController
https://bugs.webkit.org/show_bug.cgi?id=127322
<rdar://problem/15861296>

Reviewed by Andreas Kling.

  • dom/DocumentStyleSheetCollection.cpp:

(WebCore::DocumentStyleSheetCollection::updateInjectedStyleSheetCache):

  • page/PageGroup.cpp:

(WebCore::PageGroup::addUserStyleSheetToWorld):
(WebCore::PageGroup::removeUserStyleSheetFromWorld):
(WebCore::PageGroup::removeUserStyleSheetsFromWorld):
(WebCore::PageGroup::removeAllUserContent):

  • page/PageGroup.h:
  • page/UserContentController.cpp:

(WebCore::UserContentController::addUserStyleSheet):
(WebCore::UserContentController::removeUserStyleSheet):
(WebCore::UserContentController::removeUserStyleSheets):
(WebCore::UserContentController::removeAllUserContent):
(WebCore::UserContentController::invalidateInjectedStyleSheetCacheInAllFrames):

  • page/UserContentController.h:

(WebCore::UserContentController::userStyleSheets):

Location:
trunk/Source/WebCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r162394 r162395  
     12014-01-20  Anders Carlsson  <andersca@apple.com>
     2
     3        Move user style sheet handling to UserContentController
     4        https://bugs.webkit.org/show_bug.cgi?id=127322
     5        <rdar://problem/15861296>
     6
     7        Reviewed by Andreas Kling.
     8
     9        * dom/DocumentStyleSheetCollection.cpp:
     10        (WebCore::DocumentStyleSheetCollection::updateInjectedStyleSheetCache):
     11        * page/PageGroup.cpp:
     12        (WebCore::PageGroup::addUserStyleSheetToWorld):
     13        (WebCore::PageGroup::removeUserStyleSheetFromWorld):
     14        (WebCore::PageGroup::removeUserStyleSheetsFromWorld):
     15        (WebCore::PageGroup::removeAllUserContent):
     16        * page/PageGroup.h:
     17        * page/UserContentController.cpp:
     18        (WebCore::UserContentController::addUserStyleSheet):
     19        (WebCore::UserContentController::removeUserStyleSheet):
     20        (WebCore::UserContentController::removeUserStyleSheets):
     21        (WebCore::UserContentController::removeAllUserContent):
     22        (WebCore::UserContentController::invalidateInjectedStyleSheetCacheInAllFrames):
     23        * page/UserContentController.h:
     24        (WebCore::UserContentController::userStyleSheets):
     25
    1262014-01-20  Benjamin Poulain  <benjamin@webkit.org>
    227
  • trunk/Source/WebCore/dom/DocumentStyleSheetCollection.cpp

    r161127 r162395  
    4444#include "StyleSheetContents.h"
    4545#include "StyleSheetList.h"
     46#include "UserContentController.h"
    4647#include "UserContentURLPattern.h"
    4748
     
    141142    if (!owningPage)
    142143        return;
    143        
    144     const PageGroup& pageGroup = owningPage->group();
    145     const UserStyleSheetMap* sheetsMap = pageGroup.userStyleSheets();
    146     if (!sheetsMap)
    147         return;
    148 
    149     UserStyleSheetMap::const_iterator end = sheetsMap->end();
    150     for (UserStyleSheetMap::const_iterator it = sheetsMap->begin(); it != end; ++it) {
    151         const UserStyleSheetVector* sheets = it->value.get();
    152         for (unsigned i = 0; i < sheets->size(); ++i) {
    153             const UserStyleSheet* sheet = sheets->at(i).get();
     144
     145    const auto* userContentController = owningPage->userContentController();
     146    if (!userContentController)
     147        return;
     148
     149    const UserStyleSheetMap* userStyleSheets = userContentController->userStyleSheets();
     150    if (!userStyleSheets)
     151        return;
     152
     153    for (auto& styleSheets : userStyleSheets->values()) {
     154        for (const auto& sheet : *styleSheets) {
    154155            if (sheet->injectedFrames() == InjectInTopFrameOnly && m_document.ownerElement())
    155156                continue;
     157
    156158            if (!UserContentURLPattern::matchesPatterns(m_document.url(), sheet->whitelist(), sheet->blacklist()))
    157159                continue;
     160
    158161            RefPtr<CSSStyleSheet> groupSheet = CSSStyleSheet::createInline(const_cast<Document&>(m_document), sheet->url());
    159162            bool isUserStyleSheet = sheet->level() == UserStyleUserLevel;
     
    162165            else
    163166                m_injectedAuthorStyleSheets.append(groupSheet);
     167
    164168            groupSheet->contents().setIsUserStyleSheet(isUserStyleSheet);
    165169            groupSheet->contents().parseString(sheet->source());
  • trunk/Source/WebCore/page/Frame.cpp

    r162384 r162395  
    705705        return;
    706706
    707     UserContentController* userContentController = m_page->userContentController();
     707    const auto* userContentController = m_page->userContentController();
    708708    if (!userContentController)
    709709        return;
  • trunk/Source/WebCore/page/PageGroup.cpp

    r162384 r162395  
    287287{
    288288    auto userStyleSheet = std::make_unique<UserStyleSheet>(source, url, whitelist, blacklist, injectedFrames, level);
    289     if (!m_userStyleSheets)
    290         m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
    291     std::unique_ptr<UserStyleSheetVector>& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
    292     if (!styleSheetsInWorld)
    293         styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
    294     styleSheetsInWorld->append(std::move(userStyleSheet));
    295 
    296     if (injectionTime == InjectInExistingDocuments)
    297         invalidateInjectedStyleSheetCacheInAllFrames();
     289    m_userContentController->addUserStyleSheet(world, std::move(userStyleSheet), injectionTime);
     290
    298291}
    299292
     
    305298void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
    306299{
    307     if (!m_userStyleSheets)
    308         return;
    309 
    310     auto it = m_userStyleSheets->find(&world);
    311     bool sheetsChanged = false;
    312     if (it == m_userStyleSheets->end())
    313         return;
    314    
    315     auto stylesheets = it->value.get();
    316     for (int i = stylesheets->size() - 1; i >= 0; --i) {
    317         if (stylesheets->at(i)->url() == url) {
    318             stylesheets->remove(i);
    319             sheetsChanged = true;
    320         }
    321     }
    322        
    323     if (!sheetsChanged)
    324         return;
    325 
    326     if (stylesheets->isEmpty())
    327         m_userStyleSheets->remove(it);
    328 
    329     invalidateInjectedStyleSheetCacheInAllFrames();
     300    m_userContentController->removeUserStyleSheet(world, url);
    330301}
    331302
     
    337308void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
    338309{
    339     if (!m_userStyleSheets)
    340         return;
    341 
    342     if (!m_userStyleSheets->remove(&world))
    343         return;
    344 
    345     invalidateInjectedStyleSheetCacheInAllFrames();
     310    m_userContentController->removeUserStyleSheets(world);
    346311}
    347312
     
    349314{
    350315    m_userContentController->removeAllUserContent();
    351 
    352     if (m_userStyleSheets) {
    353         m_userStyleSheets = nullptr;
    354         invalidateInjectedStyleSheetCacheInAllFrames();
    355     }
    356 }
    357 
    358 void PageGroup::invalidateInjectedStyleSheetCacheInAllFrames()
    359 {
    360     // Clear our cached sheets and have them just reparse.
    361     for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it) {
    362         for (Frame* frame = &(*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
    363             frame->document()->styleSheetCollection().invalidateInjectedStyleSheetCache();
    364             frame->document()->styleResolverChanged(DeferRecalcStyle);
    365         }
    366     }
    367316}
    368317
  • trunk/Source/WebCore/page/PageGroup.h

    r162384 r162395  
    101101        void removeAllUserContent();
    102102
    103         const UserStyleSheetMap* userStyleSheets() const { return m_userStyleSheets.get(); }
    104 
    105103        GroupSettings& groupSettings() const { return *m_groupSettings; }
    106104
     
    112110    private:
    113111        void addVisitedLink(LinkHash);
    114         void invalidateInjectedStyleSheetCacheInAllFrames();
    115112
    116113        String m_name;
     
    125122
    126123        RefPtr<UserContentController> m_userContentController;
    127         std::unique_ptr<UserStyleSheetMap> m_userStyleSheets;
    128124
    129125        const std::unique_ptr<GroupSettings> m_groupSettings;
  • trunk/Source/WebCore/page/UserContentController.cpp

    r162384 r162395  
    2828
    2929#include "DOMWrapperWorld.h"
     30#include "Document.h"
     31#include "MainFrame.h"
     32#include "Page.h"
    3033#include "UserScript.h"
     34#include "UserStyleSheet.h"
    3135
    3236namespace WebCore {
     
    9599}
    96100
     101void UserContentController::addUserStyleSheet(DOMWrapperWorld& world, std::unique_ptr<UserStyleSheet> userStyleSheet, UserStyleInjectionTime injectionTime)
     102{
     103    if (!m_userStyleSheets)
     104        m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
     105
     106    auto& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
     107    if (!styleSheetsInWorld)
     108        styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
     109    styleSheetsInWorld->append(std::move(userStyleSheet));
     110
     111    if (injectionTime == InjectInExistingDocuments)
     112        invalidateInjectedStyleSheetCacheInAllFrames();
     113}
     114
     115void UserContentController::removeUserStyleSheet(DOMWrapperWorld& world, const URL& url)
     116{
     117    if (!m_userStyleSheets)
     118        return;
     119
     120    auto it = m_userStyleSheets->find(&world);
     121    if (it == m_userStyleSheets->end())
     122        return;
     123
     124    auto& stylesheets = *it->value;
     125
     126    bool sheetsChanged = false;
     127    for (int i = stylesheets.size() - 1; i >= 0; --i) {
     128        if (stylesheets[i]->url() == url) {
     129            stylesheets.remove(i);
     130            sheetsChanged = true;
     131        }
     132    }
     133
     134    if (!sheetsChanged)
     135        return;
     136
     137    if (stylesheets.isEmpty())
     138        m_userStyleSheets->remove(it);
     139
     140    invalidateInjectedStyleSheetCacheInAllFrames();
     141}
     142
     143void UserContentController::removeUserStyleSheets(DOMWrapperWorld& world)
     144{
     145    if (!m_userStyleSheets)
     146        return;
     147
     148    if (!m_userStyleSheets->remove(&world))
     149        return;
     150
     151    invalidateInjectedStyleSheetCacheInAllFrames();
     152}
     153
    97154void UserContentController::removeAllUserContent()
    98155{
    99156    m_userScripts = nullptr;
     157
     158    if (m_userStyleSheets) {
     159        m_userStyleSheets = nullptr;
     160        invalidateInjectedStyleSheetCacheInAllFrames();
     161    }
     162}
     163
     164void UserContentController::invalidateInjectedStyleSheetCacheInAllFrames()
     165{
     166    for (auto& page : m_pages) {
     167        for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
     168            frame->document()->styleSheetCollection().invalidateInjectedStyleSheetCache();
     169            frame->document()->styleResolverChanged(DeferRecalcStyle);
     170        }
     171    }
    100172}
    101173
  • trunk/Source/WebCore/page/UserContentController.h

    r162384 r162395  
    2828
    2929#include "UserScriptTypes.h"
     30#include "UserStyleSheetTypes.h"
    3031#include <wtf/HashSet.h>
    3132#include <wtf/RefCounted.h>
     
    3839class URL;
    3940class UserScript;
     41class UserStyleSheet;
    4042
    4143class UserContentController : public RefCounted<UserContentController> {
     
    5355    void removeUserScripts(DOMWrapperWorld&);
    5456
     57    const UserStyleSheetMap* userStyleSheets() const { return m_userStyleSheets.get(); }
     58
     59    void addUserStyleSheet(DOMWrapperWorld&, std::unique_ptr<UserStyleSheet>, UserStyleInjectionTime);
     60    void removeUserStyleSheet(DOMWrapperWorld&, const URL&);
     61    void removeUserStyleSheets(DOMWrapperWorld&);
     62
    5563    void removeAllUserContent();
    5664
     
    5866    UserContentController();
    5967
     68    void invalidateInjectedStyleSheetCacheInAllFrames();
     69
    6070    HashSet<Page*> m_pages;
    6171
    6272    std::unique_ptr<UserScriptMap> m_userScripts;
     73    std::unique_ptr<UserStyleSheetMap> m_userStyleSheets;
    6374};
    6475
Note: See TracChangeset for help on using the changeset viewer.