1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_DOCUMENT_H_
32 #define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_DOCUMENT_H_
33 
34 #include "net/cookies/site_for_cookies.h"
35 #include "services/metrics/public/cpp/ukm_source_id.h"
36 #include "services/network/public/mojom/referrer_policy.mojom-shared.h"
37 #include "third_party/blink/public/platform/web_security_origin.h"
38 #include "third_party/blink/public/platform/web_vector.h"
39 #include "third_party/blink/public/web/web_draggable_region.h"
40 #include "third_party/blink/public/web/web_frame.h"
41 #include "third_party/blink/public/web/web_node.h"
42 #include "third_party/skia/include/core/SkColor.h"
43 
44 namespace blink {
45 
46 class Document;
47 class WebElement;
48 class WebFormElement;
49 class WebElementCollection;
50 class WebString;
51 class WebURL;
52 struct WebDistillabilityFeatures;
53 
54 using WebStyleSheetKey = WebString;
55 
56 // Provides readonly access to some properties of a DOM document.
57 class WebDocument : public WebNode {
58  public:
59   enum CSSOrigin { kAuthorOrigin, kUserOrigin };
60 
61   WebDocument() = default;
62   WebDocument(const WebDocument& e) = default;
63 
64   WebDocument& operator=(const WebDocument& e) {
65     WebNode::Assign(e);
66     return *this;
67   }
Assign(const WebDocument & e)68   void Assign(const WebDocument& e) { WebNode::Assign(e); }
69 
70   BLINK_EXPORT WebURL Url() const;
71   // Note: Security checks should use the getSecurityOrigin(), not url().
72   BLINK_EXPORT WebSecurityOrigin GetSecurityOrigin() const;
73   BLINK_EXPORT bool IsSecureContext() const;
74 
75   BLINK_EXPORT WebString Encoding() const;
76   BLINK_EXPORT WebString ContentLanguage() const;
77   BLINK_EXPORT WebString GetReferrer() const;
78   BLINK_EXPORT base::Optional<SkColor> ThemeColor() const;
79   // The url of the OpenSearch Desription Document (if any).
80   BLINK_EXPORT WebURL OpenSearchDescriptionURL() const;
81 
82   // Returns the frame the document belongs to or 0 if the document is
83   // frameless.
84   BLINK_EXPORT WebLocalFrame* GetFrame() const;
85   BLINK_EXPORT bool IsHTMLDocument() const;
86   BLINK_EXPORT bool IsXHTMLDocument() const;
87   BLINK_EXPORT bool IsPluginDocument() const;
88   BLINK_EXPORT WebURL BaseURL() const;
89   BLINK_EXPORT ukm::SourceId GetUkmSourceId() const;
90 
91   // The SiteForCookies is used to compute whether this document
92   // appears in a "third-party" context for the purpose of third-party
93   // cookie blocking.
94   BLINK_EXPORT net::SiteForCookies SiteForCookies() const;
95 
96   BLINK_EXPORT WebSecurityOrigin TopFrameOrigin() const;
97   BLINK_EXPORT WebElement DocumentElement() const;
98   BLINK_EXPORT WebElement Body() const;
99   BLINK_EXPORT WebElement Head();
100   BLINK_EXPORT WebString Title() const;
101   BLINK_EXPORT WebString ContentAsTextForTesting() const;
102   BLINK_EXPORT WebElementCollection All();
103   BLINK_EXPORT WebVector<WebFormElement> Forms() const;
104   BLINK_EXPORT WebURL CompleteURL(const WebString&) const;
105   BLINK_EXPORT WebElement GetElementById(const WebString&) const;
106   BLINK_EXPORT WebElement FocusedElement() const;
107 
108   // Inserts the given CSS source code as a style sheet in the document.
109   BLINK_EXPORT WebStyleSheetKey InsertStyleSheet(
110       const WebString& source_code,
111       const WebStyleSheetKey* = nullptr,
112       CSSOrigin = kAuthorOrigin);
113 
114   // Removes the CSS which was previously inserted by a call to
115   // InsertStyleSheet().
116   BLINK_EXPORT void RemoveInsertedStyleSheet(const WebStyleSheetKey&,
117                                              CSSOrigin = kAuthorOrigin);
118 
119   // Arranges to call WebLocalFrameClient::didMatchCSS(frame(), ...) when one of
120   // the selectors matches or stops matching an element in this document.
121   // Each call to this method overrides any previous calls.
122   BLINK_EXPORT void WatchCSSSelectors(const WebVector<WebString>& selectors);
123 
124   BLINK_EXPORT WebVector<WebDraggableRegion> DraggableRegions() const;
125 
126   BLINK_EXPORT WebURL CanonicalUrlForSharing() const;
127 
128   BLINK_EXPORT WebDistillabilityFeatures DistillabilityFeatures();
129 
130   BLINK_EXPORT void SetShowBeforeUnloadDialog(bool show_dialog);
131 
132   // See cc/paint/element_id.h for the definition of these id.
133   BLINK_EXPORT uint64_t GetVisualViewportScrollingElementIdForTesting();
134 
135   BLINK_EXPORT bool IsLoaded();
136 
137 #if INSIDE_BLINK
138   BLINK_EXPORT WebDocument(Document*);
139   BLINK_EXPORT WebDocument& operator=(Document*);
140   BLINK_EXPORT operator Document*() const;
141 #endif
142 };
143 
144 DECLARE_WEB_NODE_TYPE_CASTS(WebDocument);
145 
146 }  // namespace blink
147 
148 #endif
149