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 // Defines the Chrome Extensions WebNavigation API functions for observing and
6 // intercepting navigation events, as specified in the extension JSON API.
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_
14 #include "base/compiler_specific.h"
15 #include "chrome/browser/extensions/api/web_navigation/frame_navigation_state.h"
16 #include "chrome/browser/extensions/chrome_extension_function.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser_list_observer.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/browser/web_contents_user_data.h"
24 #include "extensions/browser/browser_context_keyed_api_factory.h"
25 #include "extensions/browser/event_router.h"
28 struct RetargetingDetails;
30 namespace extensions {
32 // Tab contents observer that forwards navigation events to the event router.
33 class WebNavigationTabObserver
34 : public content::NotificationObserver,
35 public content::WebContentsObserver,
36 public content::WebContentsUserData<WebNavigationTabObserver> {
38 ~WebNavigationTabObserver() override;
40 // Returns the object for the given |web_contents|.
41 static WebNavigationTabObserver* Get(content::WebContents* web_contents);
43 const FrameNavigationState& frame_navigation_state() const {
44 return navigation_state_;
47 content::RenderViewHost* GetRenderViewHostInProcess(int process_id) const;
49 // content::NotificationObserver implementation.
50 void Observe(int type,
51 const content::NotificationSource& source,
52 const content::NotificationDetails& details) override;
54 // content::WebContentsObserver implementation.
55 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
56 void RenderViewDeleted(content::RenderViewHost* render_view_host) override;
57 void AboutToNavigateRenderView(
58 content::RenderViewHost* render_view_host) override;
59 void DidStartProvisionalLoadForFrame(
60 content::RenderFrameHost* render_frame_host,
61 const GURL& validated_url,
63 bool is_iframe_srcdoc) override;
64 void DidCommitProvisionalLoadForFrame(
65 content::RenderFrameHost* render_frame_host,
67 ui::PageTransition transition_type) override;
68 void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
69 const GURL& validated_url,
71 const base::string16& error_description) override;
72 void DocumentLoadedInFrame(
73 content::RenderFrameHost* render_frame_host) override;
74 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
75 const GURL& validated_url) override;
76 void DidFailLoad(content::RenderFrameHost* render_frame_host,
77 const GURL& validated_url,
79 const base::string16& error_description) override;
80 void DidGetRedirectForResourceRequest(
81 content::RenderViewHost* render_view_host,
82 const content::ResourceRedirectDetails& details) override;
83 void DidOpenRequestedURL(content::WebContents* new_contents,
85 const content::Referrer& referrer,
86 WindowOpenDisposition disposition,
87 ui::PageTransition transition,
88 int64 source_frame_num) override;
89 void WebContentsDestroyed() override;
92 explicit WebNavigationTabObserver(content::WebContents* web_contents);
93 friend class content::WebContentsUserData<WebNavigationTabObserver>;
95 // True if the transition and target url correspond to a reference fragment
97 bool IsReferenceFragmentNavigation(content::RenderFrameHost* frame_host,
100 // Creates and sends onErrorOccurred events for all on-going navigations. If
101 // |render_view_host| is non-NULL, only generates events for frames in this
102 // render view host. If |frame_host_to_skip| is given, no events are sent for
105 void SendErrorEvents(content::WebContents* web_contents,
106 content::RenderViewHost* render_view_host,
107 content::RenderFrameHost* frame_host_to_skip);
109 // Tracks the state of the frames we are sending events for.
110 FrameNavigationState navigation_state_;
112 // Used for tracking registrations to redirect notifications.
113 content::NotificationRegistrar registrar_;
115 // The current RenderViewHost of the observed WebContents.
116 content::RenderViewHost* render_view_host_;
118 // During a cross site navigation, the WebContents has a second, pending
120 content::RenderViewHost* pending_render_view_host_;
122 DISALLOW_COPY_AND_ASSIGN(WebNavigationTabObserver);
125 // Observes navigation notifications and routes them as events to the extension
127 class WebNavigationEventRouter : public TabStripModelObserver,
128 public chrome::BrowserListObserver,
129 public content::NotificationObserver {
131 explicit WebNavigationEventRouter(Profile* profile);
132 ~WebNavigationEventRouter() override;
135 // Used to cache the information about newly created WebContents objects.
136 struct PendingWebContents{
137 PendingWebContents();
138 PendingWebContents(content::WebContents* source_web_contents,
139 content::RenderFrameHost* source_frame_host,
140 content::WebContents* target_web_contents,
141 const GURL& target_url);
142 ~PendingWebContents();
144 content::WebContents* source_web_contents;
145 content::RenderFrameHost* source_frame_host;
146 content::WebContents* target_web_contents;
150 // TabStripModelObserver implementation.
151 void TabReplacedAt(TabStripModel* tab_strip_model,
152 content::WebContents* old_contents,
153 content::WebContents* new_contents,
156 // chrome::BrowserListObserver implementation.
157 void OnBrowserAdded(Browser* browser) override;
158 void OnBrowserRemoved(Browser* browser) override;
160 // content::NotificationObserver implementation.
161 void Observe(int type,
162 const content::NotificationSource& source,
163 const content::NotificationDetails& details) override;
165 // Handler for the NOTIFICATION_RETARGETING event. The method takes the
166 // details of such an event and stores them for the later
167 // NOTIFICATION_TAB_ADDED event.
168 void Retargeting(const RetargetingDetails* details);
170 // Handler for the NOTIFICATION_TAB_ADDED event. The method takes the details
171 // of such an event and creates a JSON formated extension event from it.
172 void TabAdded(content::WebContents* tab);
174 // Handler for NOTIFICATION_WEB_CONTENTS_DESTROYED. If |tab| is in
175 // |pending_web_contents_|, it is removed.
176 void TabDestroyed(content::WebContents* tab);
178 // Mapping pointers to WebContents objects to information about how they got
180 std::map<content::WebContents*, PendingWebContents> pending_web_contents_;
182 // Used for tracking registrations to navigation notifications.
183 content::NotificationRegistrar registrar_;
185 // The profile that owns us via ExtensionService.
188 DISALLOW_COPY_AND_ASSIGN(WebNavigationEventRouter);
191 // API function that returns the state of a given frame.
192 class WebNavigationGetFrameFunction : public ChromeSyncExtensionFunction {
193 ~WebNavigationGetFrameFunction() override {}
194 bool RunSync() override;
195 DECLARE_EXTENSION_FUNCTION("webNavigation.getFrame", WEBNAVIGATION_GETFRAME)
198 // API function that returns the states of all frames in a given tab.
199 class WebNavigationGetAllFramesFunction : public ChromeSyncExtensionFunction {
200 ~WebNavigationGetAllFramesFunction() override {}
201 bool RunSync() override;
202 DECLARE_EXTENSION_FUNCTION("webNavigation.getAllFrames",
203 WEBNAVIGATION_GETALLFRAMES)
206 class WebNavigationAPI : public BrowserContextKeyedAPI,
207 public extensions::EventRouter::Observer {
209 explicit WebNavigationAPI(content::BrowserContext* context);
210 ~WebNavigationAPI() override;
212 // KeyedService implementation.
213 void Shutdown() override;
215 // BrowserContextKeyedAPI implementation.
216 static BrowserContextKeyedAPIFactory<WebNavigationAPI>* GetFactoryInstance();
218 // EventRouter::Observer implementation.
219 void OnListenerAdded(const extensions::EventListenerInfo& details) override;
222 friend class BrowserContextKeyedAPIFactory<WebNavigationAPI>;
224 content::BrowserContext* browser_context_;
226 // BrowserContextKeyedAPI implementation.
227 static const char* service_name() {
228 return "WebNavigationAPI";
230 static const bool kServiceIsNULLWhileTesting = true;
232 // Created lazily upon OnListenerAdded.
233 scoped_ptr<WebNavigationEventRouter> web_navigation_event_router_;
235 DISALLOW_COPY_AND_ASSIGN(WebNavigationAPI);
238 } // namespace extensions
240 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_