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 // Implements the Chrome Extensions WebNavigation API. | 5 // Implements the Chrome Extensions WebNavigation API. |
6 | 6 |
7 #include "chrome/browser/extensions/extension_webnavigation_api.h" | 7 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/extension_message_service.h" | 12 #include "chrome/browser/extensions/extension_message_service.h" |
13 #include "chrome/browser/extensions/extension_tabs_module.h" | 13 #include "chrome/browser/extensions/extension_tabs_module.h" |
14 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" | 14 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" |
15 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
16 #include "chrome/browser/tab_contents/navigation_controller.h" | 16 #include "chrome/browser/tab_contents/navigation_entry.h" |
17 #include "chrome/browser/tab_contents/provisional_load_details.h" | |
18 #include "chrome/common/notification_type.h" | 17 #include "chrome/common/notification_type.h" |
19 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
20 #include "net/base/net_errors.h" | |
21 | 19 |
22 namespace keys = extension_webnavigation_api_constants; | 20 namespace keys = extension_webnavigation_api_constants; |
23 | 21 |
24 // static | 22 // static |
25 ExtensionWebNavigationEventRouter* | 23 ExtensionWebNavigationEventRouter* |
26 ExtensionWebNavigationEventRouter::GetInstance() { | 24 ExtensionWebNavigationEventRouter::GetInstance() { |
27 return Singleton<ExtensionWebNavigationEventRouter>::get(); | 25 return Singleton<ExtensionWebNavigationEventRouter>::get(); |
28 } | 26 } |
29 | 27 |
30 void ExtensionWebNavigationEventRouter::Init() { | 28 void ExtensionWebNavigationEventRouter::Init() { |
31 if (registrar_.IsEmpty()) { | 29 if (registrar_.IsEmpty()) { |
32 registrar_.Add(this, | 30 registrar_.Add(this, |
33 NotificationType::FRAME_PROVISIONAL_LOAD_START, | 31 NotificationType::NAV_ENTRY_COMMITTED, |
34 NotificationService::AllSources()); | |
35 registrar_.Add(this, | |
36 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED, | |
37 NotificationService::AllSources()); | |
38 registrar_.Add(this, | |
39 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR, | |
40 NotificationService::AllSources()); | 32 NotificationService::AllSources()); |
41 } | 33 } |
42 } | 34 } |
43 | 35 |
44 void ExtensionWebNavigationEventRouter::Observe( | 36 void ExtensionWebNavigationEventRouter::Observe( |
45 NotificationType type, | 37 NotificationType type, |
46 const NotificationSource& source, | 38 const NotificationSource& source, |
47 const NotificationDetails& details) { | 39 const NotificationDetails& details) { |
48 switch (type.value) { | 40 switch (type.value) { |
49 case NotificationType::FRAME_PROVISIONAL_LOAD_START: | 41 case NotificationType::NAV_ENTRY_COMMITTED: |
50 FrameProvisionalLoadStart( | 42 NavEntryCommitted( |
51 Source<NavigationController>(source).ptr(), | 43 Source<NavigationController>(source).ptr(), |
52 Details<ProvisionalLoadDetails>(details).ptr()); | 44 Details<NavigationController::LoadCommittedDetails>(details).ptr()); |
53 break; | |
54 case NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED: | |
55 FrameProvisionalLoadCommitted( | |
56 Source<NavigationController>(source).ptr(), | |
57 Details<ProvisionalLoadDetails>(details).ptr()); | |
58 break; | |
59 case NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR: | |
60 FailProvisionalLoadWithError( | |
61 Source<NavigationController>(source).ptr(), | |
62 Details<ProvisionalLoadDetails>(details).ptr()); | |
63 break; | 45 break; |
64 | 46 |
65 default: | 47 default: |
66 NOTREACHED(); | 48 NOTREACHED(); |
67 } | 49 } |
68 } | 50 } |
69 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( | 51 |
| 52 void ExtensionWebNavigationEventRouter::NavEntryCommitted( |
70 NavigationController* controller, | 53 NavigationController* controller, |
71 ProvisionalLoadDetails* details) { | 54 NavigationController::LoadCommittedDetails* details) { |
72 ListValue args; | 55 ListValue args; |
73 DictionaryValue* dict = new DictionaryValue(); | 56 DictionaryValue* dict = new DictionaryValue(); |
74 dict->SetInteger(keys::kTabIdKey, | 57 dict->SetInteger(keys::kTabIdKey, |
75 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 58 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
76 dict->SetString(keys::kUrlKey, | 59 dict->SetString(keys::kUrlKey, |
77 details->url().spec()); | 60 details->entry->url().spec()); |
78 dict->SetInteger(keys::kFrameIdKey, 0); | 61 dict->SetInteger(keys::kFrameIdKey, |
79 dict->SetInteger(keys::kRequestIdKey, 0); | 62 details->is_main_frame ? 0 : details->entry->page_id()); |
80 dict->SetReal(keys::kTimeStampKey, | 63 dict->SetString(keys::kTransitionTypeKey, |
81 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); | 64 PageTransition::CoreTransitionString( |
| 65 details->entry->transition_type())); |
| 66 dict->SetString(keys::kTransitionQualifiersKey, |
| 67 PageTransition::QualifierString( |
| 68 details->entry->transition_type())); |
| 69 dict->SetReal(keys::kTimeStampKey, base::Time::Now().ToDoubleT()); |
82 args.Append(dict); | 70 args.Append(dict); |
83 | 71 |
84 std::string json_args; | 72 std::string json_args; |
85 base::JSONWriter::Write(&args, false, &json_args); | |
86 DispatchEvent(controller->profile(), keys::kOnBeforeNavigate, json_args); | |
87 } | |
88 | |
89 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadCommitted( | |
90 NavigationController* controller, | |
91 ProvisionalLoadDetails* details) { | |
92 ListValue args; | |
93 DictionaryValue* dict = new DictionaryValue(); | |
94 dict->SetInteger(keys::kTabIdKey, | |
95 ExtensionTabUtil::GetTabId(controller->tab_contents())); | |
96 dict->SetString(keys::kUrlKey, | |
97 details->url().spec()); | |
98 dict->SetInteger(keys::kFrameIdKey, 0); | |
99 dict->SetString(keys::kTransitionTypeKey, | |
100 PageTransition::CoreTransitionString( | |
101 details->transition_type())); | |
102 dict->SetString(keys::kTransitionQualifiersKey, | |
103 PageTransition::QualifierString( | |
104 details->transition_type())); | |
105 dict->SetReal(keys::kTimeStampKey, | |
106 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); | |
107 args.Append(dict); | |
108 | |
109 std::string json_args; | |
110 base::JSONWriter::Write(&args, false, &json_args); | 73 base::JSONWriter::Write(&args, false, &json_args); |
111 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); | 74 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); |
112 } | 75 } |
113 | 76 |
114 void ExtensionWebNavigationEventRouter::FailProvisionalLoadWithError( | |
115 NavigationController* controller, | |
116 ProvisionalLoadDetails* details) { | |
117 ListValue args; | |
118 DictionaryValue* dict = new DictionaryValue(); | |
119 dict->SetInteger(keys::kTabIdKey, | |
120 ExtensionTabUtil::GetTabId(controller->tab_contents())); | |
121 dict->SetString(keys::kUrlKey, | |
122 details->url().spec()); | |
123 dict->SetInteger(keys::kFrameIdKey, 0); | |
124 dict->SetString(keys::kErrorKey, | |
125 std::string(net::ErrorToString(details->error_code()))); | |
126 dict->SetReal(keys::kTimeStampKey, | |
127 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); | |
128 args.Append(dict); | |
129 | |
130 std::string json_args; | |
131 base::JSONWriter::Write(&args, false, &json_args); | |
132 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); | |
133 } | |
134 | |
135 void ExtensionWebNavigationEventRouter::DispatchEvent( | 77 void ExtensionWebNavigationEventRouter::DispatchEvent( |
136 Profile* profile, | 78 Profile* profile, |
137 const char* event_name, | 79 const char* event_name, |
138 const std::string& json_args) { | 80 const std::string& json_args) { |
139 if (profile && profile->GetExtensionMessageService()) { | 81 if (profile && profile->GetExtensionMessageService()) { |
140 profile->GetExtensionMessageService()->DispatchEventToRenderers( | 82 profile->GetExtensionMessageService()->DispatchEventToRenderers( |
141 event_name, json_args, profile, GURL()); | 83 event_name, json_args, profile, GURL()); |
142 } | 84 } |
143 } | 85 } |
OLD | NEW |