OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Implements the Chrome Extensions WebNavigation API. |
| 6 |
| 7 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
| 8 |
| 9 #include "base/json/json_writer.h" |
| 10 #include "base/time.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_message_service.h" |
| 13 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 14 #include "chrome/browser/extensions/extension_webnavigation_api_constants.h" |
| 15 #include "chrome/browser/profile.h" |
| 16 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 17 #include "chrome/common/notification_type.h" |
| 18 #include "chrome/common/notification_service.h" |
| 19 |
| 20 namespace keys = extension_webnavigation_api_constants; |
| 21 |
| 22 // static |
| 23 ExtensionWebNavigationEventRouter* |
| 24 ExtensionWebNavigationEventRouter::GetInstance() { |
| 25 return Singleton<ExtensionWebNavigationEventRouter>::get(); |
| 26 } |
| 27 |
| 28 void ExtensionWebNavigationEventRouter::Init() { |
| 29 if (registrar_.IsEmpty()) { |
| 30 registrar_.Add(this, |
| 31 NotificationType::NAV_ENTRY_COMMITTED, |
| 32 NotificationService::AllSources()); |
| 33 } |
| 34 } |
| 35 |
| 36 void ExtensionWebNavigationEventRouter::Observe( |
| 37 NotificationType type, |
| 38 const NotificationSource& source, |
| 39 const NotificationDetails& details) { |
| 40 switch (type.value) { |
| 41 case NotificationType::NAV_ENTRY_COMMITTED: |
| 42 NavEntryCommitted( |
| 43 Source<NavigationController>(source).ptr(), |
| 44 Details<NavigationController::LoadCommittedDetails>(details).ptr()); |
| 45 break; |
| 46 |
| 47 default: |
| 48 NOTREACHED(); |
| 49 } |
| 50 } |
| 51 |
| 52 void ExtensionWebNavigationEventRouter::NavEntryCommitted( |
| 53 NavigationController* controller, |
| 54 NavigationController::LoadCommittedDetails* details) { |
| 55 ListValue args; |
| 56 DictionaryValue* dict = new DictionaryValue(); |
| 57 dict->SetInteger(keys::kTabIdKey, |
| 58 ExtensionTabUtil::GetTabId(controller->tab_contents())); |
| 59 dict->SetString(keys::kUrlKey, |
| 60 details->entry->url().spec()); |
| 61 dict->SetInteger(keys::kFrameIdKey, |
| 62 details->is_main_frame ? 0 : details->entry->page_id()); |
| 63 dict->SetString(keys::kTransitionTypeKey, |
| 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()); |
| 70 args.Append(dict); |
| 71 |
| 72 std::string json_args; |
| 73 base::JSONWriter::Write(&args, false, &json_args); |
| 74 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); |
| 75 } |
| 76 |
| 77 void ExtensionWebNavigationEventRouter::DispatchEvent( |
| 78 Profile* profile, |
| 79 const char* event_name, |
| 80 const std::string& json_args) { |
| 81 if (profile && profile->GetExtensionMessageService()) { |
| 82 profile->GetExtensionMessageService()->DispatchEventToRenderers( |
| 83 event_name, json_args, profile, GURL()); |
| 84 } |
| 85 } |
OLD | NEW |