Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / web_navigation / web_navigation_api.h
blob74c43aa0ce8f10f7b65d9cadc36528b2dcfdf5da
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,
64 bool was_ignored_by_handler) override;
65 void DocumentLoadedInFrame(
66 content::RenderFrameHost* render_frame_host) override;
67 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
68 const GURL& validated_url) override;
69 void DidFailLoad(content::RenderFrameHost* render_frame_host,
70 const GURL& validated_url,
71 int error_code,
72 const base::string16& error_description,
73 bool was_ignored_by_handler) override;
74 void DidGetRedirectForResourceRequest(
75 content::RenderFrameHost* render_frame_host,
76 const content::ResourceRedirectDetails& details) override;
77 void DidOpenRequestedURL(content::WebContents* new_contents,
78 content::RenderFrameHost* source_render_frame_host,
79 const GURL& url,
80 const content::Referrer& referrer,
81 WindowOpenDisposition disposition,
82 ui::PageTransition transition) override;
83 void WebContentsDestroyed() override;
85 private:
86 explicit WebNavigationTabObserver(content::WebContents* web_contents);
87 friend class content::WebContentsUserData<WebNavigationTabObserver>;
89 // True if the transition and target url correspond to a reference fragment
90 // navigation.
91 bool IsReferenceFragmentNavigation(content::RenderFrameHost* frame_host,
92 const GURL& url);
94 // Creates and sends onErrorOccurred events for all on-going navigations. If
95 // |render_view_host| is non-NULL, only generates events for frames in this
96 // render view host. If |frame_host_to_skip| is given, no events are sent for
97 // that
98 // frame.
99 void SendErrorEvents(content::WebContents* web_contents,
100 content::RenderViewHost* render_view_host,
101 content::RenderFrameHost* frame_host_to_skip);
103 // Tracks the state of the frames we are sending events for.
104 FrameNavigationState navigation_state_;
106 // Used for tracking registrations to redirect notifications.
107 content::NotificationRegistrar registrar_;
109 DISALLOW_COPY_AND_ASSIGN(WebNavigationTabObserver);
112 // Observes navigation notifications and routes them as events to the extension
113 // system.
114 class WebNavigationEventRouter : public TabStripModelObserver,
115 public chrome::BrowserListObserver,
116 public content::NotificationObserver {
117 public:
118 explicit WebNavigationEventRouter(Profile* profile);
119 ~WebNavigationEventRouter() override;
121 private:
122 // Used to cache the information about newly created WebContents objects.
123 struct PendingWebContents{
124 PendingWebContents();
125 PendingWebContents(content::WebContents* source_web_contents,
126 content::RenderFrameHost* source_frame_host,
127 content::WebContents* target_web_contents,
128 const GURL& target_url);
129 ~PendingWebContents();
131 content::WebContents* source_web_contents;
132 content::RenderFrameHost* source_frame_host;
133 content::WebContents* target_web_contents;
134 GURL target_url;
137 // TabStripModelObserver implementation.
138 void TabReplacedAt(TabStripModel* tab_strip_model,
139 content::WebContents* old_contents,
140 content::WebContents* new_contents,
141 int index) override;
143 // chrome::BrowserListObserver implementation.
144 void OnBrowserAdded(Browser* browser) override;
145 void OnBrowserRemoved(Browser* browser) override;
147 // content::NotificationObserver implementation.
148 void Observe(int type,
149 const content::NotificationSource& source,
150 const content::NotificationDetails& details) override;
152 // Handler for the NOTIFICATION_RETARGETING event. The method takes the
153 // details of such an event and stores them for the later
154 // NOTIFICATION_TAB_ADDED event.
155 void Retargeting(const RetargetingDetails* details);
157 // Handler for the NOTIFICATION_TAB_ADDED event. The method takes the details
158 // of such an event and creates a JSON formated extension event from it.
159 void TabAdded(content::WebContents* tab);
161 // Handler for NOTIFICATION_WEB_CONTENTS_DESTROYED. If |tab| is in
162 // |pending_web_contents_|, it is removed.
163 void TabDestroyed(content::WebContents* tab);
165 // Mapping pointers to WebContents objects to information about how they got
166 // created.
167 std::map<content::WebContents*, PendingWebContents> pending_web_contents_;
169 // Used for tracking registrations to navigation notifications.
170 content::NotificationRegistrar registrar_;
172 // The profile that owns us via ExtensionService.
173 Profile* profile_;
175 DISALLOW_COPY_AND_ASSIGN(WebNavigationEventRouter);
178 // API function that returns the state of a given frame.
179 class WebNavigationGetFrameFunction : public ChromeSyncExtensionFunction {
180 ~WebNavigationGetFrameFunction() override {}
181 bool RunSync() override;
182 DECLARE_EXTENSION_FUNCTION("webNavigation.getFrame", WEBNAVIGATION_GETFRAME)
185 // API function that returns the states of all frames in a given tab.
186 class WebNavigationGetAllFramesFunction : public ChromeSyncExtensionFunction {
187 ~WebNavigationGetAllFramesFunction() override {}
188 bool RunSync() override;
189 DECLARE_EXTENSION_FUNCTION("webNavigation.getAllFrames",
190 WEBNAVIGATION_GETALLFRAMES)
193 class WebNavigationAPI : public BrowserContextKeyedAPI,
194 public extensions::EventRouter::Observer {
195 public:
196 explicit WebNavigationAPI(content::BrowserContext* context);
197 ~WebNavigationAPI() override;
199 // KeyedService implementation.
200 void Shutdown() override;
202 // BrowserContextKeyedAPI implementation.
203 static BrowserContextKeyedAPIFactory<WebNavigationAPI>* GetFactoryInstance();
205 // EventRouter::Observer implementation.
206 void OnListenerAdded(const extensions::EventListenerInfo& details) override;
208 private:
209 friend class BrowserContextKeyedAPIFactory<WebNavigationAPI>;
211 content::BrowserContext* browser_context_;
213 // BrowserContextKeyedAPI implementation.
214 static const char* service_name() {
215 return "WebNavigationAPI";
217 static const bool kServiceIsNULLWhileTesting = true;
219 // Created lazily upon OnListenerAdded.
220 scoped_ptr<WebNavigationEventRouter> web_navigation_event_router_;
222 DISALLOW_COPY_AND_ASSIGN(WebNavigationAPI);
225 } // namespace extensions
227 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_