| 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_event_router.h" | 12 #include "chrome/browser/extensions/extension_event_router.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_controller.h" |
| 17 #include "chrome/browser/tab_contents/provisional_load_details.h" | 17 #include "chrome/browser/tab_contents/provisional_load_details.h" |
| 18 #include "chrome/common/notification_type.h" | 18 #include "chrome/common/notification_type.h" |
| 19 #include "chrome/common/notification_service.h" | 19 #include "chrome/common/notification_service.h" |
| 20 #include "chrome/common/url_constants.h" | |
| 21 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 22 | 21 |
| 23 namespace keys = extension_webnavigation_api_constants; | 22 namespace keys = extension_webnavigation_api_constants; |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 // Returns 0 if the navigation happens in the main frame, or the frame ID | 26 // Returns 0 if the navigation happens in the main frame, or the frame ID |
| 28 // modulo 32 bits otherwise. | 27 // modulo 32 bits otherwise. |
| 29 int GetFrameId(ProvisionalLoadDetails* details) { | 28 int GetFrameId(ProvisionalLoadDetails* details) { |
| 30 return details->main_frame() ? 0 : static_cast<int>(details->frame_id()); | 29 return details->main_frame() ? 0 : static_cast<int>(details->frame_id()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 47 bool FrameNavigationState::CanSendEvents(int64 frame_id) const { | 46 bool FrameNavigationState::CanSendEvents(int64 frame_id) const { |
| 48 FrameIdToStateMap::const_iterator frame_state = | 47 FrameIdToStateMap::const_iterator frame_state = |
| 49 frame_state_map_.find(frame_id); | 48 frame_state_map_.find(frame_id); |
| 50 return frame_state != frame_state_map_.end() && | 49 return frame_state != frame_state_map_.end() && |
| 51 !frame_state->second.error_occurred; | 50 !frame_state->second.error_occurred; |
| 52 } | 51 } |
| 53 | 52 |
| 54 void FrameNavigationState::TrackFrame(int64 frame_id, | 53 void FrameNavigationState::TrackFrame(int64 frame_id, |
| 55 const GURL& url, | 54 const GURL& url, |
| 56 bool is_main_frame, | 55 bool is_main_frame, |
| 56 bool is_error_page, |
| 57 const TabContents* tab_contents) { | 57 const TabContents* tab_contents) { |
| 58 if (is_main_frame) | 58 if (is_main_frame) |
| 59 RemoveTabContentsState(tab_contents); | 59 RemoveTabContentsState(tab_contents); |
| 60 tab_contents_map_.insert(std::make_pair(tab_contents, frame_id)); | 60 tab_contents_map_.insert(std::make_pair(tab_contents, frame_id)); |
| 61 FrameState& frame_state = frame_state_map_[frame_id]; | 61 FrameState& frame_state = frame_state_map_[frame_id]; |
| 62 frame_state.error_occurred = (url.spec() == chrome::kUnreachableWebDataURL); | 62 frame_state.error_occurred = is_error_page; |
| 63 frame_state.url = url; | 63 frame_state.url = url; |
| 64 frame_state.is_main_frame = is_main_frame; | 64 frame_state.is_main_frame = is_main_frame; |
| 65 } | 65 } |
| 66 | 66 |
| 67 GURL FrameNavigationState::GetUrl(int64 frame_id) const { | 67 GURL FrameNavigationState::GetUrl(int64 frame_id) const { |
| 68 FrameIdToStateMap::const_iterator frame_state = | 68 FrameIdToStateMap::const_iterator frame_state = |
| 69 frame_state_map_.find(frame_id); | 69 frame_state_map_.find(frame_id); |
| 70 if (frame_state == frame_state_map_.end()) { | 70 if (frame_state == frame_state_map_.end()) { |
| 71 NOTREACHED(); | 71 NOTREACHED(); |
| 72 return GURL(); | 72 return GURL(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 default: | 169 default: |
| 170 NOTREACHED(); | 170 NOTREACHED(); |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( | 173 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( |
| 174 NavigationController* controller, | 174 NavigationController* controller, |
| 175 ProvisionalLoadDetails* details) { | 175 ProvisionalLoadDetails* details) { |
| 176 navigation_state_.TrackFrame(details->frame_id(), | 176 navigation_state_.TrackFrame(details->frame_id(), |
| 177 details->url(), | 177 details->url(), |
| 178 details->main_frame(), | 178 details->main_frame(), |
| 179 details->is_error_page(), |
| 179 controller->tab_contents()); | 180 controller->tab_contents()); |
| 180 if (!navigation_state_.CanSendEvents(details->frame_id())) | 181 if (!navigation_state_.CanSendEvents(details->frame_id())) |
| 181 return; | 182 return; |
| 182 ListValue args; | 183 ListValue args; |
| 183 DictionaryValue* dict = new DictionaryValue(); | 184 DictionaryValue* dict = new DictionaryValue(); |
| 184 dict->SetInteger(keys::kTabIdKey, | 185 dict->SetInteger(keys::kTabIdKey, |
| 185 ExtensionTabUtil::GetTabId(controller->tab_contents())); | 186 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 186 dict->SetString(keys::kUrlKey, details->url().spec()); | 187 dict->SetString(keys::kUrlKey, details->url().spec()); |
| 187 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); | 188 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details)); |
| 188 dict->SetInteger(keys::kRequestIdKey, 0); | 189 dict->SetInteger(keys::kRequestIdKey, 0); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 284 |
| 284 void ExtensionWebNavigationEventRouter::DispatchEvent( | 285 void ExtensionWebNavigationEventRouter::DispatchEvent( |
| 285 Profile* profile, | 286 Profile* profile, |
| 286 const char* event_name, | 287 const char* event_name, |
| 287 const std::string& json_args) { | 288 const std::string& json_args) { |
| 288 if (profile && profile->GetExtensionEventRouter()) { | 289 if (profile && profile->GetExtensionEventRouter()) { |
| 289 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 290 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 290 event_name, json_args, profile, GURL()); | 291 event_name, json_args, profile, GURL()); |
| 291 } | 292 } |
| 292 } | 293 } |
| OLD | NEW |