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. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_ 7 8 #include <map> 9 #include <set> 10 11 #include "base/compiler_specific.h" 12 #include "url/gurl.h" 13 14 namespace content { 15 class RenderViewHost; 16 } 17 18 namespace extensions { 19 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 { 24 public: 25 // A frame is uniquely identified by its frame ID and the RVH it's in. 26 struct FrameID { 27 FrameID(); 28 FrameID(int64 frame_num, content::RenderViewHost* render_view_host); 29 30 bool operator<(const FrameID& other) const; 31 bool operator==(const FrameID& other) const; 32 bool operator!=(const FrameID& other) const; 33 34 int64 frame_num; 35 content::RenderViewHost* render_view_host; 36 }; 37 typedef std::set<FrameID>::const_iterator const_iterator; 38 39 FrameNavigationState(); 40 ~FrameNavigationState(); 41 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(); } 45 46 // True if navigation events for the given frame can be sent. 47 bool CanSendEvents(FrameID frame_id) const; 48 49 // True if in general webNavigation events may be sent for the given URL. 50 bool IsValidUrl(const GURL& url) const; 51 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, 55 const GURL& url, 56 bool is_main_frame, 57 bool is_error_page, 58 bool is_iframe_srcdoc); 59 60 // Marks the frame as detached and stops tracking it. 61 void FrameDetached(FrameID frame_id); 62 63 // Stops tracking all frames but the frame with |id_to_skip| for a given 64 // RenderViewHost. 65 void StopTrackingFramesInRVH(content::RenderViewHost* render_view_host, 66 FrameID id_to_skip); 67 68 // Update the URL associated with a given frame. 69 void UpdateFrame(FrameID frame_id, const GURL& url); 70 71 // Returns true if |frame_id| is a known frame. 72 bool IsValidFrame(FrameID frame_id) const; 73 74 // Returns the URL corresponding to a tracked frame given by its |frame_id|. 75 GURL GetUrl(FrameID frame_id) const; 76 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; 80 81 // Returns the frame ID of the last comitted main frame, or -1 if the frame 82 // ID is not known. 83 FrameID GetMainFrameID() const; 84 85 // Get the parent frame ID (or an invalid ID, if |frame_id| is a main frame). 86 FrameID GetParentFrameID(FrameID frame_id) const; 87 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); 91 92 // True if the frame is marked as being in an error state. 93 bool GetErrorOccurredInFrame(FrameID frame_id) const; 94 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); 98 99 // True if the frame is currently not navigating. 100 bool GetNavigationCompleted(FrameID frame_id) const; 101 102 // Marks a frame as having finished parsing. 103 void SetParsingFinished(FrameID frame_id); 104 105 // True if the frame has finished parsing. 106 bool GetParsingFinished(FrameID frame_id) const; 107 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); 111 112 // True if the frame has committed its navigation. 113 bool GetNavigationCommitted(FrameID frame_id) const; 114 115 // Marks a frame as redirected by the server. 116 void SetIsServerRedirected(FrameID frame_id); 117 118 // True if the frame was redirected by the server. 119 bool GetIsServerRedirected(FrameID frame_id) const; 120 121 #ifdef UNIT_TEST 122 static void set_allow_extension_scheme(bool allow_extension_scheme) { 123 allow_extension_scheme_ = allow_extension_scheme; 124 } 125 #endif 126 127 private: 128 struct FrameState { 129 FrameState(); 130 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. 140 }; 141 typedef std::map<FrameID, FrameState> FrameIdToStateMap; 142 143 // Tracks the state of known frames. 144 FrameIdToStateMap frame_state_map_; 145 146 // Set of all known frames. 147 std::set<FrameID> frame_ids_; 148 149 // The id of the last comitted main frame. 150 FrameID main_frame_id_; 151 152 // If true, also allow events from chrome-extension:// URLs. 153 static bool allow_extension_scheme_; 154 155 DISALLOW_COPY_AND_ASSIGN(FrameNavigationState); 156 }; 157 158 } // namespace extensions 159 160 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_ 161