1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/web_navigation/frame_navigation_state.h"
7 #include "base/logging.h"
8 #include "chrome/common/url_constants.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "extensions/common/constants.h"
12 namespace extensions
{
16 // URL schemes for which we'll send events.
17 const char* const kValidSchemes
[] = {
18 content::kChromeUIScheme
,
23 url::kJavaScriptScheme
,
25 url::kFileSystemScheme
,
30 FrameNavigationState::FrameState::FrameState() {}
33 bool FrameNavigationState::allow_extension_scheme_
= false;
35 FrameNavigationState::FrameNavigationState() {
38 FrameNavigationState::~FrameNavigationState() {}
41 bool FrameNavigationState::IsValidUrl(const GURL
& url
) {
42 for (unsigned i
= 0; i
< arraysize(kValidSchemes
); ++i
) {
43 if (url
.scheme() == kValidSchemes
[i
])
46 // Allow about:blank and about:srcdoc.
47 if (url
.spec() == url::kAboutBlankURL
||
48 url
.spec() == content::kAboutSrcDocURL
) {
51 return allow_extension_scheme_
&& url
.scheme() == kExtensionScheme
;
54 bool FrameNavigationState::CanSendEvents(
55 content::RenderFrameHost
* frame_host
) const {
56 FrameHostToStateMap::const_iterator it
=
57 frame_host_state_map_
.find(frame_host
);
58 if (it
== frame_host_state_map_
.end() || it
->second
.error_occurred
) {
61 return IsValidUrl(it
->second
.url
);
64 void FrameNavigationState::StartTrackingNavigation(
65 content::RenderFrameHost
* frame_host
,
68 bool is_iframe_srcdoc
) {
69 FrameState
& frame_state
= frame_host_state_map_
[frame_host
];
70 frame_state
.error_occurred
= is_error_page
;
71 frame_state
.url
= url
;
72 frame_state
.is_iframe_srcdoc
= is_iframe_srcdoc
;
73 DCHECK(!is_iframe_srcdoc
|| url
== GURL(url::kAboutBlankURL
));
74 frame_state
.is_navigating
= true;
75 frame_state
.is_committed
= false;
76 frame_state
.is_server_redirected
= false;
77 frame_state
.is_parsing
= true;
80 void FrameNavigationState::FrameHostCreated(
81 content::RenderFrameHost
* frame_host
) {
82 frame_hosts_
.insert(frame_host
);
85 void FrameNavigationState::FrameHostDeleted(
86 content::RenderFrameHost
* frame_host
) {
87 frame_host_state_map_
.erase(frame_host
);
88 frame_hosts_
.erase(frame_host
);
91 void FrameNavigationState::UpdateFrame(content::RenderFrameHost
* frame_host
,
93 FrameHostToStateMap::iterator it
= frame_host_state_map_
.find(frame_host
);
94 if (it
== frame_host_state_map_
.end()) {
101 bool FrameNavigationState::IsValidFrame(
102 content::RenderFrameHost
* frame_host
) const {
103 return frame_host_state_map_
.find(frame_host
) != frame_host_state_map_
.end();
106 GURL
FrameNavigationState::GetUrl(content::RenderFrameHost
* frame_host
) const {
107 FrameHostToStateMap::const_iterator it
=
108 frame_host_state_map_
.find(frame_host
);
109 if (it
== frame_host_state_map_
.end()) {
113 if (it
->second
.is_iframe_srcdoc
)
114 return GURL(content::kAboutSrcDocURL
);
115 return it
->second
.url
;
118 void FrameNavigationState::SetErrorOccurredInFrame(
119 content::RenderFrameHost
* frame_host
) {
120 FrameHostToStateMap::iterator it
= frame_host_state_map_
.find(frame_host
);
121 if (it
== frame_host_state_map_
.end()) {
125 it
->second
.error_occurred
= true;
128 bool FrameNavigationState::GetErrorOccurredInFrame(
129 content::RenderFrameHost
* frame_host
) const {
130 FrameHostToStateMap::const_iterator it
=
131 frame_host_state_map_
.find(frame_host
);
132 DCHECK(it
!= frame_host_state_map_
.end());
133 return it
== frame_host_state_map_
.end() || it
->second
.error_occurred
;
136 void FrameNavigationState::SetNavigationCompleted(
137 content::RenderFrameHost
* frame_host
) {
138 FrameHostToStateMap::iterator it
= frame_host_state_map_
.find(frame_host
);
139 if (it
== frame_host_state_map_
.end()) {
143 it
->second
.is_navigating
= false;
146 bool FrameNavigationState::GetNavigationCompleted(
147 content::RenderFrameHost
* frame_host
) const {
148 FrameHostToStateMap::const_iterator it
=
149 frame_host_state_map_
.find(frame_host
);
150 DCHECK(it
!= frame_host_state_map_
.end());
151 return it
== frame_host_state_map_
.end() || !it
->second
.is_navigating
;
154 void FrameNavigationState::SetParsingFinished(
155 content::RenderFrameHost
* frame_host
) {
156 FrameHostToStateMap::iterator it
= frame_host_state_map_
.find(frame_host
);
157 if (it
== frame_host_state_map_
.end()) {
161 it
->second
.is_parsing
= false;
164 bool FrameNavigationState::GetParsingFinished(
165 content::RenderFrameHost
* frame_host
) const {
166 FrameHostToStateMap::const_iterator it
=
167 frame_host_state_map_
.find(frame_host
);
168 DCHECK(it
!= frame_host_state_map_
.end());
169 return it
== frame_host_state_map_
.end() || !it
->second
.is_parsing
;
172 void FrameNavigationState::SetNavigationCommitted(
173 content::RenderFrameHost
* frame_host
) {
174 FrameHostToStateMap::iterator it
= frame_host_state_map_
.find(frame_host
);
175 if (it
== frame_host_state_map_
.end()) {
179 it
->second
.is_committed
= true;
182 bool FrameNavigationState::GetNavigationCommitted(
183 content::RenderFrameHost
* frame_host
) const {
184 FrameHostToStateMap::const_iterator it
=
185 frame_host_state_map_
.find(frame_host
);
186 DCHECK(it
!= frame_host_state_map_
.end());
187 return it
!= frame_host_state_map_
.end() && it
->second
.is_committed
;
190 void FrameNavigationState::SetIsServerRedirected(
191 content::RenderFrameHost
* frame_host
) {
192 FrameHostToStateMap::iterator it
= frame_host_state_map_
.find(frame_host
);
193 if (it
== frame_host_state_map_
.end()) {
197 it
->second
.is_server_redirected
= true;
200 bool FrameNavigationState::GetIsServerRedirected(
201 content::RenderFrameHost
* frame_host
) const {
202 FrameHostToStateMap::const_iterator it
=
203 frame_host_state_map_
.find(frame_host
);
204 DCHECK(it
!= frame_host_state_map_
.end());
205 return it
!= frame_host_state_map_
.end() && it
->second
.is_server_redirected
;
208 } // namespace extensions