Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / web_navigation / web_navigation_api.h
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 // Defines the Chrome Extensions WebNavigation API functions for observing and
6 // intercepting navigation events, as specified in the extension JSON API.
7
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_
10
11 #include <map>
12 #include <set>
13
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"
27
28 struct RetargetingDetails;
29
30 namespace extensions {
31
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> {
37  public:
38   ~WebNavigationTabObserver() override;
39
40   // Returns the object for the given |web_contents|.
41   static WebNavigationTabObserver* Get(content::WebContents* web_contents);
42
43   const FrameNavigationState& frame_navigation_state() const {
44     return navigation_state_;
45   }
46
47   content::RenderViewHost* GetRenderViewHostInProcess(int process_id) const;
48
49   // content::NotificationObserver implementation.
50   void Observe(int type,
51                const content::NotificationSource& source,
52                const content::NotificationDetails& details) override;
53
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,
62       bool is_error_page,
63       bool is_iframe_srcdoc) override;
64   void DidCommitProvisionalLoadForFrame(
65       content::RenderFrameHost* render_frame_host,
66       const GURL& url,
67       ui::PageTransition transition_type) override;
68   void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
69                               const GURL& validated_url,
70                               int error_code,
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,
78                    int error_code,
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,
84                            const GURL& url,
85                            const content::Referrer& referrer,
86                            WindowOpenDisposition disposition,
87                            ui::PageTransition transition,
88                            int64 source_frame_num) override;
89   void WebContentsDestroyed() override;
90
91  private:
92   explicit WebNavigationTabObserver(content::WebContents* web_contents);
93   friend class content::WebContentsUserData<WebNavigationTabObserver>;
94
95   // True if the transition and target url correspond to a reference fragment
96   // navigation.
97   bool IsReferenceFragmentNavigation(content::RenderFrameHost* frame_host,
98                                      const GURL& url);
99
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
103   // that
104   // frame.
105   void SendErrorEvents(content::WebContents* web_contents,
106                        content::RenderViewHost* render_view_host,
107                        content::RenderFrameHost* frame_host_to_skip);
108
109   // Tracks the state of the frames we are sending events for.
110   FrameNavigationState navigation_state_;
111
112   // Used for tracking registrations to redirect notifications.
113   content::NotificationRegistrar registrar_;
114
115   // The current RenderViewHost of the observed WebContents.
116   content::RenderViewHost* render_view_host_;
117
118   // During a cross site navigation, the WebContents has a second, pending
119   // RenderViewHost.
120   content::RenderViewHost* pending_render_view_host_;
121
122   DISALLOW_COPY_AND_ASSIGN(WebNavigationTabObserver);
123 };
124
125 // Observes navigation notifications and routes them as events to the extension
126 // system.
127 class WebNavigationEventRouter : public TabStripModelObserver,
128                                  public chrome::BrowserListObserver,
129                                  public content::NotificationObserver {
130  public:
131   explicit WebNavigationEventRouter(Profile* profile);
132   ~WebNavigationEventRouter() override;
133
134  private:
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();
143
144     content::WebContents* source_web_contents;
145     content::RenderFrameHost* source_frame_host;
146     content::WebContents* target_web_contents;
147     GURL target_url;
148   };
149
150   // TabStripModelObserver implementation.
151   void TabReplacedAt(TabStripModel* tab_strip_model,
152                      content::WebContents* old_contents,
153                      content::WebContents* new_contents,
154                      int index) override;
155
156   // chrome::BrowserListObserver implementation.
157   void OnBrowserAdded(Browser* browser) override;
158   void OnBrowserRemoved(Browser* browser) override;
159
160   // content::NotificationObserver implementation.
161   void Observe(int type,
162                const content::NotificationSource& source,
163                const content::NotificationDetails& details) override;
164
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);
169
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);
173
174   // Handler for NOTIFICATION_WEB_CONTENTS_DESTROYED. If |tab| is in
175   // |pending_web_contents_|, it is removed.
176   void TabDestroyed(content::WebContents* tab);
177
178   // Mapping pointers to WebContents objects to information about how they got
179   // created.
180   std::map<content::WebContents*, PendingWebContents> pending_web_contents_;
181
182   // Used for tracking registrations to navigation notifications.
183   content::NotificationRegistrar registrar_;
184
185   // The profile that owns us via ExtensionService.
186   Profile* profile_;
187
188   DISALLOW_COPY_AND_ASSIGN(WebNavigationEventRouter);
189 };
190
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)
196 };
197
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)
204 };
205
206 class WebNavigationAPI : public BrowserContextKeyedAPI,
207                          public extensions::EventRouter::Observer {
208  public:
209   explicit WebNavigationAPI(content::BrowserContext* context);
210   ~WebNavigationAPI() override;
211
212   // KeyedService implementation.
213   void Shutdown() override;
214
215   // BrowserContextKeyedAPI implementation.
216   static BrowserContextKeyedAPIFactory<WebNavigationAPI>* GetFactoryInstance();
217
218   // EventRouter::Observer implementation.
219   void OnListenerAdded(const extensions::EventListenerInfo& details) override;
220
221  private:
222   friend class BrowserContextKeyedAPIFactory<WebNavigationAPI>;
223
224   content::BrowserContext* browser_context_;
225
226   // BrowserContextKeyedAPI implementation.
227   static const char* service_name() {
228     return "WebNavigationAPI";
229   }
230   static const bool kServiceIsNULLWhileTesting = true;
231
232   // Created lazily upon OnListenerAdded.
233   scoped_ptr<WebNavigationEventRouter> web_navigation_event_router_;
234
235   DISALLOW_COPY_AND_ASSIGN(WebNavigationAPI);
236 };
237
238 }  // namespace extensions
239
240 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_