Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / api / web_navigation / web_navigation_api.h
blobb2ab0c25b91e5004d7374dad684c8b551b934589
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_
11 #include <map>
12 #include <set>
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"
26 #include "url/gurl.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::WebContentsObserver,
35 public content::WebContentsUserData<WebNavigationTabObserver> {
36 public:
37 ~WebNavigationTabObserver() override;
39 // Returns the object for the given |web_contents|.
40 static WebNavigationTabObserver* Get(content::WebContents* web_contents);
42 const FrameNavigationState& frame_navigation_state() const {
43 return navigation_state_;
46 // content::WebContentsObserver implementation.
47 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
48 void FrameDeleted(content::RenderFrameHost* render_frame_host) override;
49 void RenderFrameHostChanged(content::RenderFrameHost* old_host,
50 content::RenderFrameHost* new_host) override;
51 void DidStartProvisionalLoadForFrame(
52 content::RenderFrameHost* render_frame_host,
53 const GURL& validated_url,
54 bool is_error_page,
55 bool is_iframe_srcdoc) override;
56 void DidCommitProvisionalLoadForFrame(
57 content::RenderFrameHost* render_frame_host,
58 const GURL& url,
59 ui::PageTransition transition_type) override;
60 void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
61 const GURL& validated_url,
62 int error_code,
63 const base::string16& error_description) override;
64 void DocumentLoadedInFrame(
65 content::RenderFrameHost* render_frame_host) override;
66 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
67 const GURL& validated_url) override;
68 void DidFailLoad(content::RenderFrameHost* render_frame_host,
69 const GURL& validated_url,
70 int error_code,
71 const base::string16& error_description) override;
72 void DidGetRedirectForResourceRequest(
73 content::RenderFrameHost* render_frame_host,
74 const content::ResourceRedirectDetails& details) override;
75 void DidOpenRequestedURL(content::WebContents* new_contents,
76 content::RenderFrameHost* source_render_frame_host,
77 const GURL& url,
78 const content::Referrer& referrer,
79 WindowOpenDisposition disposition,
80 ui::PageTransition transition) override;
81 void WebContentsDestroyed() override;
83 private:
84 explicit WebNavigationTabObserver(content::WebContents* web_contents);
85 friend class content::WebContentsUserData<WebNavigationTabObserver>;
87 // True if the transition and target url correspond to a reference fragment
88 // navigation.
89 bool IsReferenceFragmentNavigation(content::RenderFrameHost* frame_host,
90 const GURL& url);
92 // Creates and sends onErrorOccurred events for all on-going navigations. If
93 // |render_view_host| is non-NULL, only generates events for frames in this
94 // render view host. If |frame_host_to_skip| is given, no events are sent for
95 // that
96 // frame.
97 void SendErrorEvents(content::WebContents* web_contents,
98 content::RenderViewHost* render_view_host,
99 content::RenderFrameHost* frame_host_to_skip);
101 // Tracks the state of the frames we are sending events for.
102 FrameNavigationState navigation_state_;
104 // Used for tracking registrations to redirect notifications.
105 content::NotificationRegistrar registrar_;
107 DISALLOW_COPY_AND_ASSIGN(WebNavigationTabObserver);
110 // Observes navigation notifications and routes them as events to the extension
111 // system.
112 class WebNavigationEventRouter : public TabStripModelObserver,
113 public chrome::BrowserListObserver,
114 public content::NotificationObserver {
115 public:
116 explicit WebNavigationEventRouter(Profile* profile);
117 ~WebNavigationEventRouter() override;
119 private:
120 // Used to cache the information about newly created WebContents objects.
121 struct PendingWebContents{
122 PendingWebContents();
123 PendingWebContents(content::WebContents* source_web_contents,
124 content::RenderFrameHost* source_frame_host,
125 content::WebContents* target_web_contents,
126 const GURL& target_url);
127 ~PendingWebContents();
129 content::WebContents* source_web_contents;
130 content::RenderFrameHost* source_frame_host;
131 content::WebContents* target_web_contents;
132 GURL target_url;
135 // TabStripModelObserver implementation.
136 void TabReplacedAt(TabStripModel* tab_strip_model,
137 content::WebContents* old_contents,
138 content::WebContents* new_contents,
139 int index) override;
141 // chrome::BrowserListObserver implementation.
142 void OnBrowserAdded(Browser* browser) override;
143 void OnBrowserRemoved(Browser* browser) override;
145 // content::NotificationObserver implementation.
146 void Observe(int type,
147 const content::NotificationSource& source,
148 const content::NotificationDetails& details) override;
150 // Handler for the NOTIFICATION_RETARGETING event. The method takes the
151 // details of such an event and stores them for the later
152 // NOTIFICATION_TAB_ADDED event.
153 void Retargeting(const RetargetingDetails* details);
155 // Handler for the NOTIFICATION_TAB_ADDED event. The method takes the details
156 // of such an event and creates a JSON formated extension event from it.
157 void TabAdded(content::WebContents* tab);
159 // Handler for NOTIFICATION_WEB_CONTENTS_DESTROYED. If |tab| is in
160 // |pending_web_contents_|, it is removed.
161 void TabDestroyed(content::WebContents* tab);
163 // Mapping pointers to WebContents objects to information about how they got
164 // created.
165 std::map<content::WebContents*, PendingWebContents> pending_web_contents_;
167 // Used for tracking registrations to navigation notifications.
168 content::NotificationRegistrar registrar_;
170 // The profile that owns us via ExtensionService.
171 Profile* profile_;
173 DISALLOW_COPY_AND_ASSIGN(WebNavigationEventRouter);
176 // API function that returns the state of a given frame.
177 class WebNavigationGetFrameFunction : public ChromeSyncExtensionFunction {
178 ~WebNavigationGetFrameFunction() override {}
179 bool RunSync() override;
180 DECLARE_EXTENSION_FUNCTION("webNavigation.getFrame", WEBNAVIGATION_GETFRAME)
183 // API function that returns the states of all frames in a given tab.
184 class WebNavigationGetAllFramesFunction : public ChromeSyncExtensionFunction {
185 ~WebNavigationGetAllFramesFunction() override {}
186 bool RunSync() override;
187 DECLARE_EXTENSION_FUNCTION("webNavigation.getAllFrames",
188 WEBNAVIGATION_GETALLFRAMES)
191 class WebNavigationAPI : public BrowserContextKeyedAPI,
192 public extensions::EventRouter::Observer {
193 public:
194 explicit WebNavigationAPI(content::BrowserContext* context);
195 ~WebNavigationAPI() override;
197 // KeyedService implementation.
198 void Shutdown() override;
200 // BrowserContextKeyedAPI implementation.
201 static BrowserContextKeyedAPIFactory<WebNavigationAPI>* GetFactoryInstance();
203 // EventRouter::Observer implementation.
204 void OnListenerAdded(const extensions::EventListenerInfo& details) override;
206 private:
207 friend class BrowserContextKeyedAPIFactory<WebNavigationAPI>;
209 content::BrowserContext* browser_context_;
211 // BrowserContextKeyedAPI implementation.
212 static const char* service_name() {
213 return "WebNavigationAPI";
215 static const bool kServiceIsNULLWhileTesting = true;
217 // Created lazily upon OnListenerAdded.
218 scoped_ptr<WebNavigationEventRouter> web_navigation_event_router_;
220 DISALLOW_COPY_AND_ASSIGN(WebNavigationAPI);
223 } // namespace extensions
225 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_