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_entry.h" | 16 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 17 #include "chrome/browser/tab_contents/provisional_load_details.h" |
17 #include "chrome/common/notification_type.h" | 18 #include "chrome/common/notification_type.h" |
18 #include "chrome/common/notification_service.h" | 19 #include "chrome/common/notification_service.h" |
| 20 #include "net/base/net_errors.h" |
19 | 21 |
20 namespace keys = extension_webnavigation_api_constants; | 22 namespace keys = extension_webnavigation_api_constants; |
21 | 23 |
22 // static | 24 // static |
23 ExtensionWebNavigationEventRouter* | 25 ExtensionWebNavigationEventRouter* |
24 ExtensionWebNavigationEventRouter::GetInstance() { | 26 ExtensionWebNavigationEventRouter::GetInstance() { |
25 return Singleton<ExtensionWebNavigationEventRouter>::get(); | 27 return Singleton<ExtensionWebNavigationEventRouter>::get(); |
26 } | 28 } |
27 | 29 |
28 void ExtensionWebNavigationEventRouter::Init() { | 30 void ExtensionWebNavigationEventRouter::Init() { |
29 if (registrar_.IsEmpty()) { | 31 if (registrar_.IsEmpty()) { |
30 registrar_.Add(this, | 32 registrar_.Add(this, |
31 NotificationType::NAV_ENTRY_COMMITTED, | 33 NotificationType::FRAME_PROVISIONAL_LOAD_START, |
| 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, |
32 NotificationService::AllSources()); | 40 NotificationService::AllSources()); |
33 } | 41 } |
34 } | 42 } |
35 | 43 |
36 void ExtensionWebNavigationEventRouter::Observe( | 44 void ExtensionWebNavigationEventRouter::Observe( |
37 NotificationType type, | 45 NotificationType type, |
38 const NotificationSource& source, | 46 const NotificationSource& source, |
39 const NotificationDetails& details) { | 47 const NotificationDetails& details) { |
40 switch (type.value) { | 48 switch (type.value) { |
41 case NotificationType::NAV_ENTRY_COMMITTED: | 49 case NotificationType::FRAME_PROVISIONAL_LOAD_START: |
42 NavEntryCommitted( | 50 FrameProvisionalLoadStart( |
43 Source<NavigationController>(source).ptr(), | 51 Source<NavigationController>(source).ptr(), |
44 Details<NavigationController::LoadCommittedDetails>(details).ptr()); | 52 Details<ProvisionalLoadDetails>(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()); |
45 break; | 63 break; |
46 | 64 |
47 default: | 65 default: |
48 NOTREACHED(); | 66 NOTREACHED(); |
49 } | 67 } |
50 } | 68 } |
51 | 69 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( |
52 void ExtensionWebNavigationEventRouter::NavEntryCommitted( | |
53 NavigationController* controller, | 70 NavigationController* controller, |
54 NavigationController::LoadCommittedDetails* details) { | 71 ProvisionalLoadDetails* details) { |
55 ListValue args; | 72 ListValue args; |
56 DictionaryValue* dict = new DictionaryValue(); | 73 DictionaryValue* dict = new DictionaryValue(); |
57 dict->SetInteger(keys::kTabIdKey, | 74 dict->SetInteger(keys::kTabIdKey, |
58 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 75 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
59 dict->SetString(keys::kUrlKey, | 76 dict->SetString(keys::kUrlKey, |
60 details->entry->url().spec()); | 77 details->url().spec()); |
61 dict->SetInteger(keys::kFrameIdKey, | 78 dict->SetInteger(keys::kFrameIdKey, 0); |
62 details->is_main_frame ? 0 : details->entry->page_id()); | 79 dict->SetInteger(keys::kRequestIdKey, 0); |
63 dict->SetString(keys::kTransitionTypeKey, | 80 dict->SetReal(keys::kTimeStampKey, |
64 PageTransition::CoreTransitionString( | 81 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); |
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()); | |
70 args.Append(dict); | 82 args.Append(dict); |
71 | 83 |
72 std::string json_args; | 84 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; |
73 base::JSONWriter::Write(&args, false, &json_args); | 110 base::JSONWriter::Write(&args, false, &json_args); |
74 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); | 111 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); |
75 } | 112 } |
76 | 113 |
| 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 |
77 void ExtensionWebNavigationEventRouter::DispatchEvent( | 135 void ExtensionWebNavigationEventRouter::DispatchEvent( |
78 Profile* profile, | 136 Profile* profile, |
79 const char* event_name, | 137 const char* event_name, |
80 const std::string& json_args) { | 138 const std::string& json_args) { |
81 if (profile && profile->GetExtensionMessageService()) { | 139 if (profile && profile->GetExtensionMessageService()) { |
82 profile->GetExtensionMessageService()->DispatchEventToRenderers( | 140 profile->GetExtensionMessageService()->DispatchEventToRenderers( |
83 event_name, json_args, profile, GURL()); | 141 event_name, json_args, profile, GURL()); |
84 } | 142 } |
85 } | 143 } |
OLD | NEW |