| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/api/web_navigation/web_navigation_api.h" | 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" | 14 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" |
| 15 #include "chrome/browser/extensions/extension_event_router.h" | 15 #include "chrome/browser/extensions/extension_event_router.h" |
| 16 #include "chrome/browser/extensions/extension_tab_util.h" | 16 #include "chrome/browser/extensions/extension_tab_util.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/tab_contents/retargeting_details.h" | 18 #include "chrome/browser/tab_contents/retargeting_details.h" |
| 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 20 #include "chrome/browser/view_type_utils.h" | 20 #include "chrome/browser/view_type_utils.h" |
| 21 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 22 #include "chrome/common/extensions/event_filtering_info.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 23 #include "content/public/browser/resource_request_details.h" | 24 #include "content/public/browser/resource_request_details.h" |
| 24 #include "content/public/browser/navigation_details.h" | 25 #include "content/public/browser/navigation_details.h" |
| 25 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_types.h" | 27 #include "content/public/browser/notification_types.h" |
| 27 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 29 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 30 | 31 |
| 31 using content::BrowserContext; | 32 using content::BrowserContext; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 } | 64 } |
| 64 | 65 |
| 65 // Returns |time| as milliseconds since the epoch. | 66 // Returns |time| as milliseconds since the epoch. |
| 66 double MilliSecondsFromTime(const base::Time& time) { | 67 double MilliSecondsFromTime(const base::Time& time) { |
| 67 return 1000 * time.ToDoubleT(); | 68 return 1000 * time.ToDoubleT(); |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Dispatches events to the extension message service. | 71 // Dispatches events to the extension message service. |
| 71 void DispatchEvent(BrowserContext* browser_context, | 72 void DispatchEvent(BrowserContext* browser_context, |
| 72 const char* event_name, | 73 const char* event_name, |
| 73 const std::string& json_args) { | 74 const ListValue& args, |
| 75 const GURL& url) { |
| 76 std::string json_args; |
| 77 base::JSONWriter::Write(&args, &json_args); |
| 78 |
| 79 extensions::EventFilteringInfo info; |
| 80 info.SetURL(url); |
| 81 |
| 74 Profile* profile = Profile::FromBrowserContext(browser_context); | 82 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 75 if (profile && profile->GetExtensionEventRouter()) { | 83 if (profile && profile->GetExtensionEventRouter()) { |
| 76 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 84 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 77 event_name, json_args, profile, GURL()); | 85 event_name, json_args, profile, GURL(), info); |
| 78 } | 86 } |
| 79 } | 87 } |
| 80 | 88 |
| 81 // Constructs and dispatches an onBeforeNavigate event. | 89 // Constructs and dispatches an onBeforeNavigate event. |
| 82 void DispatchOnBeforeNavigate(WebContents* web_contents, | 90 void DispatchOnBeforeNavigate(WebContents* web_contents, |
| 83 int64 frame_id, | 91 int64 frame_id, |
| 84 bool is_main_frame, | 92 bool is_main_frame, |
| 85 const GURL& validated_url) { | 93 const GURL& validated_url) { |
| 86 ListValue args; | 94 ListValue args; |
| 87 DictionaryValue* dict = new DictionaryValue(); | 95 DictionaryValue* dict = new DictionaryValue(); |
| 88 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 96 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 89 dict->SetString(keys::kUrlKey, validated_url.spec()); | 97 dict->SetString(keys::kUrlKey, validated_url.spec()); |
| 90 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 98 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 91 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 99 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 92 args.Append(dict); | 100 args.Append(dict); |
| 93 | 101 |
| 94 std::string json_args; | |
| 95 base::JSONWriter::Write(&args, &json_args); | |
| 96 DispatchEvent(web_contents->GetBrowserContext(), | 102 DispatchEvent(web_contents->GetBrowserContext(), |
| 97 keys::kOnBeforeNavigate, | 103 keys::kOnBeforeNavigate, |
| 98 json_args); | 104 args, |
| 105 validated_url); |
| 99 } | 106 } |
| 100 | 107 |
| 101 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated | 108 // Constructs and dispatches an onCommitted or onReferenceFragmentUpdated |
| 102 // event. | 109 // event. |
| 103 void DispatchOnCommitted(const char* event_name, | 110 void DispatchOnCommitted(const char* event_name, |
| 104 WebContents* web_contents, | 111 WebContents* web_contents, |
| 105 int64 frame_id, | 112 int64 frame_id, |
| 106 bool is_main_frame, | 113 bool is_main_frame, |
| 107 const GURL& url, | 114 const GURL& url, |
| 108 content::PageTransition transition_type) { | 115 content::PageTransition transition_type) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 120 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) | 127 if (transition_type & content::PAGE_TRANSITION_SERVER_REDIRECT) |
| 121 qualifiers->Append(Value::CreateStringValue("server_redirect")); | 128 qualifiers->Append(Value::CreateStringValue("server_redirect")); |
| 122 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) | 129 if (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) |
| 123 qualifiers->Append(Value::CreateStringValue("forward_back")); | 130 qualifiers->Append(Value::CreateStringValue("forward_back")); |
| 124 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) | 131 if (transition_type & content::PAGE_TRANSITION_FROM_ADDRESS_BAR) |
| 125 qualifiers->Append(Value::CreateStringValue("from_address_bar")); | 132 qualifiers->Append(Value::CreateStringValue("from_address_bar")); |
| 126 dict->Set(keys::kTransitionQualifiersKey, qualifiers); | 133 dict->Set(keys::kTransitionQualifiersKey, qualifiers); |
| 127 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 134 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 128 args.Append(dict); | 135 args.Append(dict); |
| 129 | 136 |
| 130 std::string json_args; | 137 DispatchEvent(web_contents->GetBrowserContext(), event_name, args, url); |
| 131 base::JSONWriter::Write(&args, &json_args); | |
| 132 DispatchEvent(web_contents->GetBrowserContext(), event_name, json_args); | |
| 133 } | 138 } |
| 134 | 139 |
| 135 // Constructs and dispatches an onDOMContentLoaded event. | 140 // Constructs and dispatches an onDOMContentLoaded event. |
| 136 void DispatchOnDOMContentLoaded(WebContents* web_contents, | 141 void DispatchOnDOMContentLoaded(WebContents* web_contents, |
| 137 const GURL& url, | 142 const GURL& url, |
| 138 bool is_main_frame, | 143 bool is_main_frame, |
| 139 int64 frame_id) { | 144 int64 frame_id) { |
| 140 ListValue args; | 145 ListValue args; |
| 141 DictionaryValue* dict = new DictionaryValue(); | 146 DictionaryValue* dict = new DictionaryValue(); |
| 142 dict->SetInteger(keys::kTabIdKey, | 147 dict->SetInteger(keys::kTabIdKey, |
| 143 ExtensionTabUtil::GetTabId(web_contents)); | 148 ExtensionTabUtil::GetTabId(web_contents)); |
| 144 dict->SetString(keys::kUrlKey, url.spec()); | 149 dict->SetString(keys::kUrlKey, url.spec()); |
| 145 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 150 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 146 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 151 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 147 args.Append(dict); | 152 args.Append(dict); |
| 148 | 153 |
| 149 std::string json_args; | |
| 150 base::JSONWriter::Write(&args, &json_args); | |
| 151 DispatchEvent(web_contents->GetBrowserContext(), | 154 DispatchEvent(web_contents->GetBrowserContext(), |
| 152 keys::kOnDOMContentLoaded, | 155 keys::kOnDOMContentLoaded, |
| 153 json_args); | 156 args, |
| 157 url); |
| 154 } | 158 } |
| 155 | 159 |
| 156 // Constructs and dispatches an onCompleted event. | 160 // Constructs and dispatches an onCompleted event. |
| 157 void DispatchOnCompleted(WebContents* web_contents, | 161 void DispatchOnCompleted(WebContents* web_contents, |
| 158 const GURL& url, | 162 const GURL& url, |
| 159 bool is_main_frame, | 163 bool is_main_frame, |
| 160 int64 frame_id) { | 164 int64 frame_id) { |
| 161 ListValue args; | 165 ListValue args; |
| 162 DictionaryValue* dict = new DictionaryValue(); | 166 DictionaryValue* dict = new DictionaryValue(); |
| 163 dict->SetInteger(keys::kTabIdKey, | 167 dict->SetInteger(keys::kTabIdKey, |
| 164 ExtensionTabUtil::GetTabId(web_contents)); | 168 ExtensionTabUtil::GetTabId(web_contents)); |
| 165 dict->SetString(keys::kUrlKey, url.spec()); | 169 dict->SetString(keys::kUrlKey, url.spec()); |
| 166 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 170 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 167 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 171 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 168 args.Append(dict); | 172 args.Append(dict); |
| 169 | 173 |
| 170 std::string json_args; | 174 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnCompleted, args, |
| 171 base::JSONWriter::Write(&args, &json_args); | 175 url); |
| 172 DispatchEvent(web_contents->GetBrowserContext(), | |
| 173 keys::kOnCompleted, json_args); | |
| 174 } | 176 } |
| 175 | 177 |
| 176 // Constructs and dispatches an onCreatedNavigationTarget event. | 178 // Constructs and dispatches an onCreatedNavigationTarget event. |
| 177 void DispatchOnCreatedNavigationTarget( | 179 void DispatchOnCreatedNavigationTarget( |
| 178 WebContents* web_contents, | 180 WebContents* web_contents, |
| 179 BrowserContext* browser_context, | 181 BrowserContext* browser_context, |
| 180 int64 source_frame_id, | 182 int64 source_frame_id, |
| 181 bool source_frame_is_main_frame, | 183 bool source_frame_is_main_frame, |
| 182 WebContents* target_web_contents, | 184 WebContents* target_web_contents, |
| 183 const GURL& target_url) { | 185 const GURL& target_url) { |
| 184 // Check that the tab is already inserted into a tab strip model. This code | 186 // Check that the tab is already inserted into a tab strip model. This code |
| 185 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. | 187 // path is exercised by ExtensionApiTest.WebNavigationRequestOpenTab. |
| 186 DCHECK(ExtensionTabUtil::GetTabById( | 188 DCHECK(ExtensionTabUtil::GetTabById( |
| 187 ExtensionTabUtil::GetTabId(target_web_contents), | 189 ExtensionTabUtil::GetTabId(target_web_contents), |
| 188 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), | 190 Profile::FromBrowserContext(target_web_contents->GetBrowserContext()), |
| 189 false, NULL, NULL, NULL, NULL)); | 191 false, NULL, NULL, NULL, NULL)); |
| 190 | 192 |
| 191 ListValue args; | 193 ListValue args; |
| 192 DictionaryValue* dict = new DictionaryValue(); | 194 DictionaryValue* dict = new DictionaryValue(); |
| 193 dict->SetInteger(keys::kSourceTabIdKey, | 195 dict->SetInteger(keys::kSourceTabIdKey, |
| 194 ExtensionTabUtil::GetTabId(web_contents)); | 196 ExtensionTabUtil::GetTabId(web_contents)); |
| 195 dict->SetInteger(keys::kSourceFrameIdKey, | 197 dict->SetInteger(keys::kSourceFrameIdKey, |
| 196 GetFrameId(source_frame_is_main_frame, source_frame_id)); | 198 GetFrameId(source_frame_is_main_frame, source_frame_id)); |
| 197 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); | 199 dict->SetString(keys::kUrlKey, target_url.possibly_invalid_spec()); |
| 198 dict->SetInteger(keys::kTabIdKey, | 200 dict->SetInteger(keys::kTabIdKey, |
| 199 ExtensionTabUtil::GetTabId(target_web_contents)); | 201 ExtensionTabUtil::GetTabId(target_web_contents)); |
| 200 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 202 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 201 args.Append(dict); | 203 args.Append(dict); |
| 202 | 204 |
| 203 std::string json_args; | 205 DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args, |
| 204 base::JSONWriter::Write(&args, &json_args); | 206 target_url); |
| 205 DispatchEvent( | |
| 206 browser_context, keys::kOnCreatedNavigationTarget, json_args); | |
| 207 } | 207 } |
| 208 | 208 |
| 209 // Constructs and dispatches an onErrorOccurred event. | 209 // Constructs and dispatches an onErrorOccurred event. |
| 210 void DispatchOnErrorOccurred(WebContents* web_contents, | 210 void DispatchOnErrorOccurred(WebContents* web_contents, |
| 211 const GURL& url, | 211 const GURL& url, |
| 212 int64 frame_id, | 212 int64 frame_id, |
| 213 bool is_main_frame, | 213 bool is_main_frame, |
| 214 int error_code) { | 214 int error_code) { |
| 215 ListValue args; | 215 ListValue args; |
| 216 DictionaryValue* dict = new DictionaryValue(); | 216 DictionaryValue* dict = new DictionaryValue(); |
| 217 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); | 217 dict->SetInteger(keys::kTabIdKey, ExtensionTabUtil::GetTabId(web_contents)); |
| 218 dict->SetString(keys::kUrlKey, url.spec()); | 218 dict->SetString(keys::kUrlKey, url.spec()); |
| 219 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); | 219 dict->SetInteger(keys::kFrameIdKey, GetFrameId(is_main_frame, frame_id)); |
| 220 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); | 220 dict->SetString(keys::kErrorKey, net::ErrorToString(error_code)); |
| 221 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 221 dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); |
| 222 args.Append(dict); | 222 args.Append(dict); |
| 223 | 223 |
| 224 std::string json_args; | 224 DispatchEvent(web_contents->GetBrowserContext(), keys::kOnErrorOccurred, |
| 225 base::JSONWriter::Write(&args, &json_args); | 225 args, url); |
| 226 DispatchEvent(web_contents->GetBrowserContext(), | |
| 227 keys::kOnErrorOccurred, | |
| 228 json_args); | |
| 229 } | 226 } |
| 230 | 227 |
| 231 } // namespace | 228 } // namespace |
| 232 | 229 |
| 233 | 230 |
| 234 // FrameNavigationState ------------------------------------------------------- | 231 // FrameNavigationState ------------------------------------------------------- |
| 235 | 232 |
| 236 // static | 233 // static |
| 237 bool FrameNavigationState::allow_extension_scheme_ = false; | 234 bool FrameNavigationState::allow_extension_scheme_ = false; |
| 238 | 235 |
| (...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 frameDict->SetBoolean( | 800 frameDict->SetBoolean( |
| 804 keys::kErrorOccurredKey, | 801 keys::kErrorOccurredKey, |
| 805 navigation_state.GetErrorOccurredInFrame(*frame)); | 802 navigation_state.GetErrorOccurredInFrame(*frame)); |
| 806 resultList->Append(frameDict); | 803 resultList->Append(frameDict); |
| 807 } | 804 } |
| 808 result_.reset(resultList); | 805 result_.reset(resultList); |
| 809 return true; | 806 return true; |
| 810 } | 807 } |
| 811 | 808 |
| 812 } // namespace extensions | 809 } // namespace extensions |
| OLD | NEW |