1 /*
2  * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3  * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4  * Copyright (C) 2002, 2006, 2008, 2012 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_STYLE_RULE_IMPORT_H_
23 #define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_STYLE_RULE_IMPORT_H_
24 
25 #include "third_party/blink/renderer/core/css/css_origin_clean.h"
26 #include "third_party/blink/renderer/core/css/style_rule.h"
27 #include "third_party/blink/renderer/platform/heap/handle.h"
28 #include "third_party/blink/renderer/platform/loader/fetch/resource_client.h"
29 #include "third_party/blink/renderer/platform/wtf/casting.h"
30 
31 namespace blink {
32 
33 class MediaQuerySet;
34 class StyleSheetContents;
35 
36 class StyleRuleImport : public StyleRuleBase {
37   USING_PRE_FINALIZER(StyleRuleImport, Dispose);
38 
39  public:
40   StyleRuleImport(const String& href,
41                   scoped_refptr<MediaQuerySet>,
42                   OriginClean origin_clean);
43   ~StyleRuleImport();
44 
ParentStyleSheet()45   StyleSheetContents* ParentStyleSheet() const { return parent_style_sheet_; }
SetParentStyleSheet(StyleSheetContents * sheet)46   void SetParentStyleSheet(StyleSheetContents* sheet) {
47     DCHECK(sheet);
48     parent_style_sheet_ = sheet;
49   }
ClearParentStyleSheet()50   void ClearParentStyleSheet() { parent_style_sheet_ = nullptr; }
51 
Href()52   String Href() const { return str_href_; }
GetStyleSheet()53   StyleSheetContents* GetStyleSheet() const { return style_sheet_.Get(); }
54 
55   bool IsLoading() const;
MediaQueries()56   MediaQuerySet* MediaQueries() { return media_queries_.get(); }
57 
58   void RequestStyleSheet();
59 
60   void TraceAfterDispatch(blink::Visitor*) const;
61 
62  private:
63   // FIXME: inherit from ResourceClient directly to eliminate back
64   // pointer, as there are no space savings in this.
65   // NOTE: We put the ResourceClient in a member instead of inheriting
66   // from it to avoid adding a vptr to StyleRuleImport.
67   class ImportedStyleSheetClient final
68       : public GarbageCollected<ImportedStyleSheetClient>,
69         public ResourceClient {
70     USING_GARBAGE_COLLECTED_MIXIN(ImportedStyleSheetClient);
71 
72    public:
ImportedStyleSheetClient(StyleRuleImport * owner_rule)73     ImportedStyleSheetClient(StyleRuleImport* owner_rule)
74         : owner_rule_(owner_rule) {}
75     ~ImportedStyleSheetClient() override = default;
NotifyFinished(Resource * resource)76     void NotifyFinished(Resource* resource) override {
77       owner_rule_->NotifyFinished(resource);
78     }
Dispose()79     void Dispose() { ClearResource(); }
80 
DebugName()81     String DebugName() const override { return "ImportedStyleSheetClient"; }
82 
Trace(Visitor * visitor)83     void Trace(Visitor* visitor) override {
84       visitor->Trace(owner_rule_);
85       ResourceClient::Trace(visitor);
86     }
87 
88    private:
89     Member<StyleRuleImport> owner_rule_;
90   };
91 
92   void NotifyFinished(Resource*);
93 
94   void Dispose();
95 
96   Member<StyleSheetContents> parent_style_sheet_;
97 
98   Member<ImportedStyleSheetClient> style_sheet_client_;
99   String str_href_;
100   scoped_refptr<MediaQuerySet> media_queries_;
101   Member<StyleSheetContents> style_sheet_;
102   bool loading_;
103   // Whether the style sheet that has this import rule is origin-clean:
104   // https://drafts.csswg.org/cssom-1/#concept-css-style-sheet-origin-clean-flag
105   const OriginClean origin_clean_;
106 };
107 
108 template <>
109 struct DowncastTraits<StyleRuleImport> {
110   static bool AllowFrom(const StyleRuleBase& rule) {
111     return rule.IsImportRule();
112   }
113 };
114 
115 }  // namespace blink
116 
117 #endif
118