Home | History | Annotate | Download | only in web_navigation
      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 RenderFrameHost;
     16 class RenderViewHost;
     17 }
     18 
     19 namespace extensions {
     20 
     21 // Tracks the navigation state of all frame hosts in a given tab currently known
     22 // to the webNavigation API. It is mainly used to track in which frames an error
     23 // occurred so no further events for this frame are being sent.
     24 class FrameNavigationState {
     25  public:
     26   typedef std::set<content::RenderFrameHost*>::const_iterator const_iterator;
     27 
     28   FrameNavigationState();
     29   ~FrameNavigationState();
     30 
     31   // Use these to iterate over all frame hosts known by this object.
     32   const_iterator begin() const { return frame_hosts_.begin(); }
     33   const_iterator end() const { return frame_hosts_.end(); }
     34 
     35   // True if navigation events for the given frame can be sent.
     36   bool CanSendEvents(content::RenderFrameHost* frame_host) const;
     37 
     38   // TODO(dcheng): This should be static.
     39   // True if in general webNavigation events may be sent for the given URL.
     40   bool IsValidUrl(const GURL& url) const;
     41 
     42   // Starts to track a |frame_host| showing the URL |url|.
     43   void TrackFrame(content::RenderFrameHost* frame_host,
     44                   const GURL& url,
     45                   bool is_error_page,
     46                   bool is_iframe_srcdoc);
     47 
     48   // Marks |frame_host| as detached and stops tracking it.
     49   void FrameDetached(content::RenderFrameHost* frame_host);
     50 
     51   // Stops tracking all frame hosts but |frame_host_to_skip| in
     52   // |render_view_host|.
     53   void StopTrackingFramesInRVH(content::RenderViewHost* render_view_host,
     54                                content::RenderFrameHost* frame_host_to_skip);
     55 
     56   // Update the URL associated with |frame_host|.
     57   void UpdateFrame(content::RenderFrameHost* frame_host, const GURL& url);
     58 
     59   // Returns true if |frame_host| is a known frame host.
     60   bool IsValidFrame(content::RenderFrameHost* frame_host) const;
     61 
     62   // Returns the URL corresponding to a tracked |frame_host|.
     63   // TODO(dcheng): Why is this needed? Can't this information be extracted from
     64   // RenderFrameHost?
     65   GURL GetUrl(content::RenderFrameHost* frame_host) const;
     66 
     67   // Returns a pointer to the last comitted main frame host.
     68   content::RenderFrameHost* GetLastCommittedMainFrameHost() const;
     69 
     70   // Marks |frame_host| as in an error state, i.e. the onErrorOccurred event was
     71   // fired for it, and no further events should be sent for it.
     72   void SetErrorOccurredInFrame(content::RenderFrameHost* frame_host);
     73 
     74   // True if |frame_host| is marked as being in an error state.
     75   bool GetErrorOccurredInFrame(content::RenderFrameHost* frame_host) const;
     76 
     77   // Marks |frame_host| as having finished its last navigation, i.e. the
     78   // onCompleted event was fired for this frame.
     79   void SetNavigationCompleted(content::RenderFrameHost* frame_host);
     80 
     81   // True if |frame_host| is currently not navigating.
     82   bool GetNavigationCompleted(content::RenderFrameHost* frame_host) const;
     83 
     84   // Marks |frame_host| as having finished parsing.
     85   void SetParsingFinished(content::RenderFrameHost* frame_host);
     86 
     87   // True if |frame_host| has finished parsing.
     88   bool GetParsingFinished(content::RenderFrameHost* frame_host) const;
     89 
     90   // Marks |frame_host| as having committed its navigation, i.e. the onCommitted
     91   // event was fired for this frame.
     92   void SetNavigationCommitted(content::RenderFrameHost* frame_host);
     93 
     94   // True if |frame_host| has committed its navigation.
     95   bool GetNavigationCommitted(content::RenderFrameHost* frame_host) const;
     96 
     97   // Marks |frame_host| as redirected by the server.
     98   void SetIsServerRedirected(content::RenderFrameHost* frame_host);
     99 
    100   // True if |frame_host| was redirected by the server.
    101   bool GetIsServerRedirected(content::RenderFrameHost* frame_host) const;
    102 
    103 #ifdef UNIT_TEST
    104   static void set_allow_extension_scheme(bool allow_extension_scheme) {
    105     allow_extension_scheme_ = allow_extension_scheme;
    106   }
    107 #endif
    108 
    109  private:
    110   struct FrameState {
    111     FrameState();
    112 
    113     bool error_occurred;  // True if an error has occurred in this frame.
    114     bool is_iframe_srcdoc;  // True if the frame is displaying its srcdoc.
    115     bool is_navigating;  // True if there is a navigation going on.
    116     bool is_committed;  // True if the navigation is already committed.
    117     bool is_server_redirected;  // True if a server redirect happened.
    118     bool is_parsing;  // True if the frame is still parsing.
    119     GURL url;  // URL of this frame.
    120   };
    121   typedef std::map<content::RenderFrameHost*, FrameState> FrameHostToStateMap;
    122 
    123   // Tracks the state of known frame hosts.
    124   FrameHostToStateMap frame_host_state_map_;
    125 
    126   // Set of all known frame hosts.
    127   std::set<content::RenderFrameHost*> frame_hosts_;
    128 
    129   // The last comitted main frame.
    130   content::RenderFrameHost* main_frame_host_;
    131 
    132   // If true, also allow events from chrome-extension:// URLs.
    133   static bool allow_extension_scheme_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(FrameNavigationState);
    136 };
    137 
    138 }  // namespace extensions
    139 
    140 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_FRAME_NAVIGATION_STATE_H_
    141