OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Defines the Chrome Extensions WebNavigation API functions for observing and | 5 // Defines the Chrome Extensions WebNavigation API functions for observing and |
6 // intercepting navigation events, as specified in | 6 // intercepting navigation events, as specified in |
7 // chrome/common/extensions/api/extension_api.json. | 7 // chrome/common/extensions/api/extension_api.json. |
8 | 8 |
9 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ | 9 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ |
10 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ | 10 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ |
11 #pragma once | 11 #pragma once |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 | 14 |
15 #include "base/singleton.h" | 15 #include "base/singleton.h" |
16 #include "chrome/browser/extensions/extension_function.h" | 16 #include "chrome/browser/extensions/extension_function.h" |
17 #include "chrome/common/notification_observer.h" | 17 #include "chrome/common/notification_observer.h" |
18 #include "chrome/common/notification_registrar.h" | 18 #include "chrome/common/notification_registrar.h" |
19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
20 | 20 |
21 class NavigationController; | 21 class NavigationController; |
22 class ProvisionalLoadDetails; | 22 class ProvisionalLoadDetails; |
23 class TabContents; | 23 class TabContents; |
24 | 24 |
25 // Tracks which frames are in an error state, and no navigation events should | 25 // Tracks the navigation state of all frames currently known to the |
26 // be sent for. | 26 // webNavigation API. It is mainly used to track in which frames an error |
| 27 // occurred so no further events for this frame are being sent. |
27 class FrameNavigationState { | 28 class FrameNavigationState { |
28 public: | 29 public: |
29 FrameNavigationState(); | 30 FrameNavigationState(); |
30 ~FrameNavigationState(); | 31 ~FrameNavigationState(); |
31 | 32 |
32 // True if navigation events for the given frame can be sent. | 33 // True if navigation events for the given frame can be sent. |
33 bool CanSendEvents(long long frame_id) const; | 34 bool CanSendEvents(int64 frame_id) const; |
34 | 35 |
35 // Starts to track a frame given by its |frame_id| showing the URL |url| in | 36 // Starts to track a frame given by its |frame_id| showing the URL |url| in |
36 // a |tab_contents|. | 37 // a |tab_contents|. |
37 void TrackFrame(long long frame_id, | 38 void TrackFrame(int64 frame_id, |
38 const GURL& url, | 39 const GURL& url, |
39 bool is_main_frame, | 40 bool is_main_frame, |
40 const TabContents* tab_contents); | 41 const TabContents* tab_contents); |
41 | 42 |
42 // Returns the URL corresponding to a tracked frame given by its |frame_id|. | 43 // Returns the URL corresponding to a tracked frame given by its |frame_id|. |
43 GURL GetUrl(long long frame_id) const; | 44 GURL GetUrl(int64 frame_id) const; |
44 | 45 |
45 // True if the frame given by its |frame_id| is the main frame of its tab. | 46 // True if the frame given by its |frame_id| is the main frame of its tab. |
46 bool IsMainFrame(long long frame_id) const; | 47 bool IsMainFrame(int64 frame_id) const; |
47 | 48 |
48 // Marks a frame as in an error state. | 49 // Marks a frame as in an error state. |
49 void ErrorOccurredInFrame(long long frame_id); | 50 void ErrorOccurredInFrame(int64 frame_id); |
50 | 51 |
51 // Removes state associated with this tab contents and all of its frames. | 52 // Removes state associated with this tab contents and all of its frames. |
52 void RemoveTabContentsState(const TabContents* tab_contents); | 53 void RemoveTabContentsState(const TabContents* tab_contents); |
53 | 54 |
54 private: | 55 private: |
55 typedef std::multimap<const TabContents*, long long> TabContentsToFrameIdMap; | 56 typedef std::multimap<const TabContents*, int64> TabContentsToFrameIdMap; |
56 struct FrameState { | 57 struct FrameState { |
57 bool error_occurred; // True if an error has occurred in this frame. | 58 bool error_occurred; // True if an error has occurred in this frame. |
58 bool is_main_frame; // True if this is a main frame. | 59 bool is_main_frame; // True if this is a main frame. |
59 GURL url; // URL of this frame. | 60 GURL url; // URL of this frame. |
60 }; | 61 }; |
61 typedef std::map<long long, FrameState> FrameIdToStateMap; | 62 typedef std::map<int64, FrameState> FrameIdToStateMap; |
62 | 63 |
63 // Tracks which frames belong to a given tab contents object. | 64 // Tracks which frames belong to a given tab contents object. |
64 TabContentsToFrameIdMap tab_contents_map_; | 65 TabContentsToFrameIdMap tab_contents_map_; |
65 | 66 |
66 // Tracks the state of known frames. | 67 // Tracks the state of known frames. |
67 FrameIdToStateMap frame_state_map_; | 68 FrameIdToStateMap frame_state_map_; |
68 | 69 |
69 DISALLOW_COPY_AND_ASSIGN(FrameNavigationState); | 70 DISALLOW_COPY_AND_ASSIGN(FrameNavigationState); |
70 }; | 71 }; |
71 | 72 |
| 73 |
72 // Observes navigation notifications and routes them as events to the extension | 74 // Observes navigation notifications and routes them as events to the extension |
73 // system. | 75 // system. |
74 class ExtensionWebNavigationEventRouter : public NotificationObserver { | 76 class ExtensionWebNavigationEventRouter : public NotificationObserver { |
75 public: | 77 public: |
76 // Single instance of the event router. | 78 // Returns the singleton instance of the event router. |
77 static ExtensionWebNavigationEventRouter* GetInstance(); | 79 static ExtensionWebNavigationEventRouter* GetInstance(); |
78 | 80 |
| 81 // Invoked by the extensions service once the extension system is fully set |
| 82 // up and can start dispatching events to extensions. |
79 void Init(); | 83 void Init(); |
80 | 84 |
81 private: | 85 private: |
82 friend struct DefaultSingletonTraits<ExtensionWebNavigationEventRouter>; | 86 friend struct DefaultSingletonTraits<ExtensionWebNavigationEventRouter>; |
83 | 87 |
84 ExtensionWebNavigationEventRouter() {} | 88 ExtensionWebNavigationEventRouter() {} |
85 virtual ~ExtensionWebNavigationEventRouter() {} | 89 virtual ~ExtensionWebNavigationEventRouter() {} |
86 | 90 |
87 // NotificationObserver implementation. | 91 // NotificationObserver implementation. |
88 virtual void Observe(NotificationType type, | 92 virtual void Observe(NotificationType type, |
89 const NotificationSource& source, | 93 const NotificationSource& source, |
90 const NotificationDetails& details); | 94 const NotificationDetails& details); |
91 | 95 |
92 // Handler for the FRAME_PROVISIONAL_LOAD_START event. The method takes the | 96 // Handler for the FRAME_PROVISIONAL_LOAD_START event. The method takes the |
93 // details of such an event and constructs a suitable JSON formatted extension | 97 // details of such an event and constructs a suitable JSON formatted extension |
94 // event from it. | 98 // event from it. |
95 void FrameProvisionalLoadStart(NavigationController* controller, | 99 void FrameProvisionalLoadStart(NavigationController* controller, |
96 ProvisionalLoadDetails* details); | 100 ProvisionalLoadDetails* details); |
97 | 101 |
98 // Handler for the FRAME_PROVISIONAL_LOAD_COMMITTED event. The method takes | 102 // Handler for the FRAME_PROVISIONAL_LOAD_COMMITTED event. The method takes |
99 // the details of such an event and constructs a suitable JSON formatted | 103 // the details of such an event and constructs a suitable JSON formatted |
100 // extension event from it. | 104 // extension event from it. |
101 void FrameProvisionalLoadCommitted(NavigationController* controller, | 105 void FrameProvisionalLoadCommitted(NavigationController* controller, |
102 ProvisionalLoadDetails* details); | 106 ProvisionalLoadDetails* details); |
103 | 107 |
104 // Handler for the FRAME_DOM_CONTENT_LOADED event. The method takes the frame | 108 // Handler for the FRAME_DOM_CONTENT_LOADED event. The method takes the frame |
105 // ID and constructs a suitable JSON formatted extension event from it. | 109 // ID and constructs a suitable JSON formatted extension event from it. |
106 void FrameDomContentLoaded(NavigationController* controller, | 110 void FrameDomContentLoaded(NavigationController* controller, |
107 long long frame_id); | 111 int64 frame_id); |
108 | 112 |
109 // Handler for the FRAME_DID_FINISH_LOAD event. The method takes the frame | 113 // Handler for the FRAME_DID_FINISH_LOAD event. The method takes the frame |
110 // ID and constructs a suitable JSON formatted extension event from it. | 114 // ID and constructs a suitable JSON formatted extension event from it. |
111 void FrameDidFinishLoad(NavigationController* controller, long long frame_id); | 115 void FrameDidFinishLoad(NavigationController* controller, int64 frame_id); |
112 | 116 |
113 // Handler for the FAIL_PROVISIONAL_LOAD_WITH_ERROR event. The method takes | 117 // Handler for the FAIL_PROVISIONAL_LOAD_WITH_ERROR event. The method takes |
114 // the details of such an event and constructs a suitable JSON formatted | 118 // the details of such an event and constructs a suitable JSON formatted |
115 // extension event from it. | 119 // extension event from it. |
116 void FailProvisionalLoadWithError(NavigationController* controller, | 120 void FailProvisionalLoadWithError(NavigationController* controller, |
117 ProvisionalLoadDetails* details); | 121 ProvisionalLoadDetails* details); |
118 | 122 |
119 // This method dispatches events to the extension message service. | 123 // Dispatches events to the extension message service. |
120 void DispatchEvent(Profile* context, | 124 void DispatchEvent(Profile* context, |
121 const char* event_name, | 125 const char* event_name, |
122 const std::string& json_args); | 126 const std::string& json_args); |
123 | 127 |
124 // Tracks the state of the frames we are sending events for. | 128 // Tracks the state of the frames we are sending events for. |
125 FrameNavigationState navigation_state_; | 129 FrameNavigationState navigation_state_; |
126 | 130 |
127 // Used for tracking registrations to navigation notifications. | 131 // Used for tracking registrations to navigation notifications. |
128 NotificationRegistrar registrar_; | 132 NotificationRegistrar registrar_; |
129 | 133 |
130 DISALLOW_COPY_AND_ASSIGN(ExtensionWebNavigationEventRouter); | 134 DISALLOW_COPY_AND_ASSIGN(ExtensionWebNavigationEventRouter); |
131 }; | 135 }; |
132 | 136 |
133 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ | 137 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ |
OLD | NEW |