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 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_
11 #include "base/compiler_specific.h"
18 namespace extensions
{
20 // Tracks the navigation state of all frames in a given tab currently known to
21 // the webNavigation API. It is mainly used to track in which frames an error
22 // occurred so no further events for this frame are being sent.
23 class FrameNavigationState
{
25 // A frame is uniquely identified by its frame ID and the RVH it's in.
28 FrameID(int64 frame_num
, content::RenderViewHost
* render_view_host
);
30 bool operator<(const FrameID
& other
) const;
31 bool operator==(const FrameID
& other
) const;
32 bool operator!=(const FrameID
& other
) const;
35 content::RenderViewHost
* render_view_host
;
37 typedef std::set
<FrameID
>::const_iterator const_iterator
;
39 FrameNavigationState();
40 ~FrameNavigationState();
42 // Use these to iterate over all frame IDs known by this object.
43 const_iterator
begin() const { return frame_ids_
.begin(); }
44 const_iterator
end() const { return frame_ids_
.end(); }
46 // True if navigation events for the given frame can be sent.
47 bool CanSendEvents(FrameID frame_id
) const;
49 // True if in general webNavigation events may be sent for the given URL.
50 bool IsValidUrl(const GURL
& url
) const;
52 // Starts to track a frame identified by its |frame_id| showing the URL |url|.
53 void TrackFrame(FrameID frame_id
,
54 FrameID parent_frame_id
,
58 bool is_iframe_srcdoc
);
60 // Marks the frame as detached and stops tracking it.
61 void FrameDetached(FrameID frame_id
);
63 // Stops tracking all frames but the frame with |id_to_skip| for a given
65 void StopTrackingFramesInRVH(content::RenderViewHost
* render_view_host
,
68 // Update the URL associated with a given frame.
69 void UpdateFrame(FrameID frame_id
, const GURL
& url
);
71 // Returns true if |frame_id| is a known frame.
72 bool IsValidFrame(FrameID frame_id
) const;
74 // Returns the URL corresponding to a tracked frame given by its |frame_id|.
75 GURL
GetUrl(FrameID frame_id
) const;
77 // True if the frame given by its |frame_id| is a main frame of its tab.
78 // There might be multiple uncomitted main frames.
79 bool IsMainFrame(FrameID frame_id
) const;
81 // Returns the frame ID of the last comitted main frame, or -1 if the frame
83 FrameID
GetMainFrameID() const;
85 // Get the parent frame ID (or an invalid ID, if |frame_id| is a main frame).
86 FrameID
GetParentFrameID(FrameID frame_id
) const;
88 // Marks a frame as in an error state, i.e. the onErrorOccurred event was
89 // fired for this frame, and no further events should be sent for it.
90 void SetErrorOccurredInFrame(FrameID frame_id
);
92 // True if the frame is marked as being in an error state.
93 bool GetErrorOccurredInFrame(FrameID frame_id
) const;
95 // Marks a frame as having finished its last navigation, i.e. the onCompleted
96 // event was fired for this frame.
97 void SetNavigationCompleted(FrameID frame_id
);
99 // True if the frame is currently not navigating.
100 bool GetNavigationCompleted(FrameID frame_id
) const;
102 // Marks a frame as having finished parsing.
103 void SetParsingFinished(FrameID frame_id
);
105 // True if the frame has finished parsing.
106 bool GetParsingFinished(FrameID frame_id
) const;
108 // Marks a frame as having committed its navigation, i.e. the onCommitted
109 // event was fired for this frame.
110 void SetNavigationCommitted(FrameID frame_id
);
112 // True if the frame has committed its navigation.
113 bool GetNavigationCommitted(FrameID frame_id
) const;
115 // Marks a frame as redirected by the server.
116 void SetIsServerRedirected(FrameID frame_id
);
118 // True if the frame was redirected by the server.
119 bool GetIsServerRedirected(FrameID frame_id
) const;
122 static void set_allow_extension_scheme(bool allow_extension_scheme
) {
123 allow_extension_scheme_
= allow_extension_scheme
;
131 bool error_occurred
; // True if an error has occurred in this frame.
132 bool is_main_frame
; // True if this is a main frame.
133 bool is_iframe_srcdoc
; // True if the frame is displaying its srcdoc.
134 bool is_navigating
; // True if there is a navigation going on.
135 bool is_committed
; // True if the navigation is already committed.
136 bool is_server_redirected
; // True if a server redirect happened.
137 bool is_parsing
; // True if the frame is still parsing.
138 int64 parent_frame_num
;
139 GURL url
; // URL of this frame.
141 typedef std::map
<FrameID
, FrameState
> FrameIdToStateMap
;
143 // Tracks the state of known frames.
144 FrameIdToStateMap frame_state_map_
;
146 // Set of all known frames.
147 std::set
<FrameID
> frame_ids_
;
149 // The id of the last comitted main frame.
150 FrameID main_frame_id_
;
152 // If true, also allow events from chrome-extension:// URLs.
153 static bool allow_extension_scheme_
;
155 DISALLOW_COPY_AND_ASSIGN(FrameNavigationState
);
158 } // namespace extensions
160 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_