- add sources.
[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/profile_keyed_api_factory.h"
16 #include "chrome/browser/extensions/api/web_navigation/frame_navigation_state.h"
17 #include "chrome/browser/extensions/chrome_extension_function.h"
18 #include "chrome/browser/extensions/event_router.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser_list_observer.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "content/public/browser/web_contents_observer.h"
25 #include "content/public/browser/web_contents_user_data.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   virtual ~WebNavigationTabObserver();
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   virtual void Observe(int type,
51                        const content::NotificationSource& source,
52                        const content::NotificationDetails& details) OVERRIDE;
53
54   // content::WebContentsObserver implementation.
55   virtual void RenderViewDeleted(
56       content::RenderViewHost* render_view_host) OVERRIDE;
57   virtual void AboutToNavigateRenderView(
58       content::RenderViewHost* render_view_host) OVERRIDE;
59   virtual void DidStartProvisionalLoadForFrame(
60       int64 frame_num,
61       int64 parent_frame_num,
62       bool is_main_frame,
63       const GURL& validated_url,
64       bool is_error_page,
65       bool is_iframe_srcdoc,
66       content::RenderViewHost* render_view_host) OVERRIDE;
67   virtual void DidCommitProvisionalLoadForFrame(
68       int64 frame_num,
69       const string16& frame_unique_name,
70       bool is_main_frame,
71       const GURL& url,
72       content::PageTransition transition_type,
73       content::RenderViewHost* render_view_host) OVERRIDE;
74   virtual void DidFailProvisionalLoad(
75       int64 frame_num,
76       const string16& frame_unique_name,
77       bool is_main_frame,
78       const GURL& validated_url,
79       int error_code,
80       const string16& error_description,
81       content::RenderViewHost* render_view_host) OVERRIDE;
82   virtual void DocumentLoadedInFrame(
83       int64 frame_num,
84       content::RenderViewHost* render_view_host) OVERRIDE;
85   virtual void DidFinishLoad(
86       int64 frame_num,
87       const GURL& validated_url,
88       bool is_main_frame,
89       content::RenderViewHost* render_view_host) OVERRIDE;
90   virtual void DidFailLoad(
91       int64 frame_num,
92       const GURL& validated_url,
93       bool is_main_frame,
94       int error_code,
95       const string16& error_description,
96       content::RenderViewHost* render_view_host) OVERRIDE;
97   virtual void DidOpenRequestedURL(content::WebContents* new_contents,
98                                    const GURL& url,
99                                    const content::Referrer& referrer,
100                                    WindowOpenDisposition disposition,
101                                    content::PageTransition transition,
102                                    int64 source_frame_num) OVERRIDE;
103   virtual void FrameDetached(content::RenderViewHost* render_view_host,
104                              int64 frame_num) OVERRIDE;
105   virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE;
106
107  private:
108   explicit WebNavigationTabObserver(content::WebContents* web_contents);
109   friend class content::WebContentsUserData<WebNavigationTabObserver>;
110
111   // True if the transition and target url correspond to a reference fragment
112   // navigation.
113   bool IsReferenceFragmentNavigation(FrameNavigationState::FrameID frame_id,
114                                      const GURL& url);
115
116   // Creates and sends onErrorOccurred events for all on-going navigations. If
117   // |render_view_host| is non-NULL, only generates events for frames in this
118   // render view host. If |id_to_skip| is given, no events are sent for that
119   // frame.
120   void SendErrorEvents(content::WebContents* web_contents,
121                        content::RenderViewHost* render_view_host,
122                        FrameNavigationState::FrameID id_to_skip);
123
124   // Tracks the state of the frames we are sending events for.
125   FrameNavigationState navigation_state_;
126
127   // Used for tracking registrations to redirect notifications.
128   content::NotificationRegistrar registrar_;
129
130   // The current RenderViewHost of the observed WebContents.
131   content::RenderViewHost* render_view_host_;
132
133   // During a cross site navigation, the WebContents has a second, pending
134   // RenderViewHost.
135   content::RenderViewHost* pending_render_view_host_;
136
137   DISALLOW_COPY_AND_ASSIGN(WebNavigationTabObserver);
138 };
139
140 // Observes navigation notifications and routes them as events to the extension
141 // system.
142 class WebNavigationEventRouter : public TabStripModelObserver,
143                                  public chrome::BrowserListObserver,
144                                  public content::NotificationObserver {
145  public:
146   explicit WebNavigationEventRouter(Profile* profile);
147   virtual ~WebNavigationEventRouter();
148
149  private:
150   // Used to cache the information about newly created WebContents objects.
151   struct PendingWebContents{
152     PendingWebContents();
153     PendingWebContents(content::WebContents* source_web_contents,
154                        int64 source_frame_id,
155                        bool source_frame_is_main_frame,
156                        content::WebContents* target_web_contents,
157                        const GURL& target_url);
158     ~PendingWebContents();
159
160     content::WebContents* source_web_contents;
161     int64 source_frame_id;
162     bool source_frame_is_main_frame;
163     content::WebContents* target_web_contents;
164     GURL target_url;
165   };
166
167   // TabStripModelObserver implementation.
168   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
169                              content::WebContents* old_contents,
170                              content::WebContents* new_contents,
171                              int index) OVERRIDE;
172
173   // chrome::BrowserListObserver implementation.
174   virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
175   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
176
177   // content::NotificationObserver implementation.
178   virtual void Observe(int type,
179                        const content::NotificationSource& source,
180                        const content::NotificationDetails& details) OVERRIDE;
181
182   // Handler for the NOTIFICATION_RETARGETING event. The method takes the
183   // details of such an event and stores them for the later
184   // NOTIFICATION_TAB_ADDED event.
185   void Retargeting(const RetargetingDetails* details);
186
187   // Handler for the NOTIFICATION_TAB_ADDED event. The method takes the details
188   // of such an event and creates a JSON formated extension event from it.
189   void TabAdded(content::WebContents* tab);
190
191   // Handler for NOTIFICATION_WEB_CONTENTS_DESTROYED. If |tab| is in
192   // |pending_web_contents_|, it is removed.
193   void TabDestroyed(content::WebContents* tab);
194
195   // Mapping pointers to WebContents objects to information about how they got
196   // created.
197   std::map<content::WebContents*, PendingWebContents> pending_web_contents_;
198
199   // Used for tracking registrations to navigation notifications.
200   content::NotificationRegistrar registrar_;
201
202   // The profile that owns us via ExtensionService.
203   Profile* profile_;
204
205   DISALLOW_COPY_AND_ASSIGN(WebNavigationEventRouter);
206 };
207
208 // API function that returns the state of a given frame.
209 class WebNavigationGetFrameFunction : public ChromeSyncExtensionFunction {
210   virtual ~WebNavigationGetFrameFunction() {}
211   virtual bool RunImpl() OVERRIDE;
212   DECLARE_EXTENSION_FUNCTION("webNavigation.getFrame", WEBNAVIGATION_GETFRAME)
213 };
214
215 // API function that returns the states of all frames in a given tab.
216 class WebNavigationGetAllFramesFunction : public ChromeSyncExtensionFunction {
217   virtual ~WebNavigationGetAllFramesFunction() {}
218   virtual bool RunImpl() OVERRIDE;
219   DECLARE_EXTENSION_FUNCTION("webNavigation.getAllFrames",
220                              WEBNAVIGATION_GETALLFRAMES)
221 };
222
223 class WebNavigationAPI : public ProfileKeyedAPI,
224                          public extensions::EventRouter::Observer {
225  public:
226   explicit WebNavigationAPI(Profile* profile);
227   virtual ~WebNavigationAPI();
228
229   // BrowserContextKeyedService implementation.
230   virtual void Shutdown() OVERRIDE;
231
232   // ProfileKeyedAPI implementation.
233   static ProfileKeyedAPIFactory<WebNavigationAPI>* GetFactoryInstance();
234
235   // EventRouter::Observer implementation.
236   virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
237       OVERRIDE;
238
239  private:
240   friend class ProfileKeyedAPIFactory<WebNavigationAPI>;
241
242   Profile* profile_;
243
244   // ProfileKeyedAPI implementation.
245   static const char* service_name() {
246     return "WebNavigationAPI";
247   }
248   static const bool kServiceIsNULLWhileTesting = true;
249
250   // Created lazily upon OnListenerAdded.
251   scoped_ptr<WebNavigationEventRouter> web_navigation_event_router_;
252
253   DISALLOW_COPY_AND_ASSIGN(WebNavigationAPI);
254 };
255
256 }  // namespace extensions
257
258 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEB_NAVIGATION_WEB_NAVIGATION_API_H_