Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: chrome/browser/extensions/extension_webnavigation_api.cc

Issue 3561008: Implement the frame id required for the web navigation api. (Closed)
Patch Set: updates Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_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 "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 21
22 namespace keys = extension_webnavigation_api_constants; 22 namespace keys = extension_webnavigation_api_constants;
23 23
24 namespace {
25
26 // Returns 0 if the navigation happens in the main frame, or the frame ID
27 // modulo 32 bits otherwise.
28 int GetFrameId(ProvisionalLoadDetails* details) {
29 return details->main_frame() ? 0 : static_cast<int>(details->frame_id());
30 }
31
32 // Returns |time| as milliseconds since the epoch.
33 double MilliSecondsFromTime(const base::Time& time) {
34 return 1000 * time.ToDoubleT();
35 }
36
37 } // namespace
38
24 // static 39 // static
25 ExtensionWebNavigationEventRouter* 40 ExtensionWebNavigationEventRouter*
26 ExtensionWebNavigationEventRouter::GetInstance() { 41 ExtensionWebNavigationEventRouter::GetInstance() {
27 return Singleton<ExtensionWebNavigationEventRouter>::get(); 42 return Singleton<ExtensionWebNavigationEventRouter>::get();
28 } 43 }
29 44
30 void ExtensionWebNavigationEventRouter::Init() { 45 void ExtensionWebNavigationEventRouter::Init() {
31 if (registrar_.IsEmpty()) { 46 if (registrar_.IsEmpty()) {
32 registrar_.Add(this, 47 registrar_.Add(this,
33 NotificationType::FRAME_PROVISIONAL_LOAD_START, 48 NotificationType::FRAME_PROVISIONAL_LOAD_START,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 } 83 }
69 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( 84 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart(
70 NavigationController* controller, 85 NavigationController* controller,
71 ProvisionalLoadDetails* details) { 86 ProvisionalLoadDetails* details) {
72 ListValue args; 87 ListValue args;
73 DictionaryValue* dict = new DictionaryValue(); 88 DictionaryValue* dict = new DictionaryValue();
74 dict->SetInteger(keys::kTabIdKey, 89 dict->SetInteger(keys::kTabIdKey,
75 ExtensionTabUtil::GetTabId(controller->tab_contents())); 90 ExtensionTabUtil::GetTabId(controller->tab_contents()));
76 dict->SetString(keys::kUrlKey, 91 dict->SetString(keys::kUrlKey,
77 details->url().spec()); 92 details->url().spec());
78 dict->SetInteger(keys::kFrameIdKey, 0); 93 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details));
79 dict->SetInteger(keys::kRequestIdKey, 0); 94 dict->SetInteger(keys::kRequestIdKey, 0);
80 dict->SetReal(keys::kTimeStampKey, 95 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
81 static_cast<double>(
82 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()));
83 args.Append(dict); 96 args.Append(dict);
84 97
85 std::string json_args; 98 std::string json_args;
86 base::JSONWriter::Write(&args, false, &json_args); 99 base::JSONWriter::Write(&args, false, &json_args);
87 DispatchEvent(controller->profile(), keys::kOnBeforeNavigate, json_args); 100 DispatchEvent(controller->profile(), keys::kOnBeforeNavigate, json_args);
88 } 101 }
89 102
90 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadCommitted( 103 void ExtensionWebNavigationEventRouter::FrameProvisionalLoadCommitted(
91 NavigationController* controller, 104 NavigationController* controller,
92 ProvisionalLoadDetails* details) { 105 ProvisionalLoadDetails* details) {
93 ListValue args; 106 ListValue args;
94 DictionaryValue* dict = new DictionaryValue(); 107 DictionaryValue* dict = new DictionaryValue();
95 dict->SetInteger(keys::kTabIdKey, 108 dict->SetInteger(keys::kTabIdKey,
96 ExtensionTabUtil::GetTabId(controller->tab_contents())); 109 ExtensionTabUtil::GetTabId(controller->tab_contents()));
97 dict->SetString(keys::kUrlKey, 110 dict->SetString(keys::kUrlKey,
98 details->url().spec()); 111 details->url().spec());
99 dict->SetInteger(keys::kFrameIdKey, 0); 112 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details));
100 dict->SetString(keys::kTransitionTypeKey, 113 dict->SetString(keys::kTransitionTypeKey,
101 PageTransition::CoreTransitionString( 114 PageTransition::CoreTransitionString(
102 details->transition_type())); 115 details->transition_type()));
103 dict->SetString(keys::kTransitionQualifiersKey, 116 dict->SetString(keys::kTransitionQualifiersKey,
104 PageTransition::QualifierString( 117 PageTransition::QualifierString(
105 details->transition_type())); 118 details->transition_type()));
106 dict->SetReal(keys::kTimeStampKey, 119 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
107 static_cast<double>(
108 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()));
109 args.Append(dict); 120 args.Append(dict);
110 121
111 std::string json_args; 122 std::string json_args;
112 base::JSONWriter::Write(&args, false, &json_args); 123 base::JSONWriter::Write(&args, false, &json_args);
113 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); 124 DispatchEvent(controller->profile(), keys::kOnCommitted, json_args);
114 } 125 }
115 126
116 void ExtensionWebNavigationEventRouter::FailProvisionalLoadWithError( 127 void ExtensionWebNavigationEventRouter::FailProvisionalLoadWithError(
117 NavigationController* controller, 128 NavigationController* controller,
118 ProvisionalLoadDetails* details) { 129 ProvisionalLoadDetails* details) {
119 ListValue args; 130 ListValue args;
120 DictionaryValue* dict = new DictionaryValue(); 131 DictionaryValue* dict = new DictionaryValue();
121 dict->SetInteger(keys::kTabIdKey, 132 dict->SetInteger(keys::kTabIdKey,
122 ExtensionTabUtil::GetTabId(controller->tab_contents())); 133 ExtensionTabUtil::GetTabId(controller->tab_contents()));
123 dict->SetString(keys::kUrlKey, 134 dict->SetString(keys::kUrlKey,
124 details->url().spec()); 135 details->url().spec());
125 dict->SetInteger(keys::kFrameIdKey, 0); 136 dict->SetInteger(keys::kFrameIdKey, GetFrameId(details));
126 dict->SetString(keys::kErrorKey, 137 dict->SetString(keys::kErrorKey,
127 std::string(net::ErrorToString(details->error_code()))); 138 std::string(net::ErrorToString(details->error_code())));
128 dict->SetReal(keys::kTimeStampKey, 139 dict->SetReal(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now()));
129 static_cast<double>(
130 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()));
131 args.Append(dict); 140 args.Append(dict);
132 141
133 std::string json_args; 142 std::string json_args;
134 base::JSONWriter::Write(&args, false, &json_args); 143 base::JSONWriter::Write(&args, false, &json_args);
135 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); 144 DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args);
136 } 145 }
137 146
138 void ExtensionWebNavigationEventRouter::DispatchEvent( 147 void ExtensionWebNavigationEventRouter::DispatchEvent(
139 Profile* profile, 148 Profile* profile,
140 const char* event_name, 149 const char* event_name,
141 const std::string& json_args) { 150 const std::string& json_args) {
142 if (profile && profile->GetExtensionMessageService()) { 151 if (profile && profile->GetExtensionMessageService()) {
143 profile->GetExtensionMessageService()->DispatchEventToRenderers( 152 profile->GetExtensionMessageService()->DispatchEventToRenderers(
144 event_name, json_args, profile, GURL()); 153 event_name, json_args, profile, GURL());
145 } 154 }
146 } 155 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_webnavigation_api.h ('k') | chrome/browser/renderer_host/render_view_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698