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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api.cc

Issue 306053013: Use ExtensionRegistry in developer private api (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review comments Created 6 years, 6 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
« no previous file with comments | « chrome/browser/extensions/api/developer_private/developer_private_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h" 5 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h"
6 6
7 #include "apps/app_load_service.h" 7 #include "apps/app_load_service.h"
8 #include "apps/app_restore_service.h" 8 #include "apps/app_restore_service.h"
9 #include "apps/app_window.h" 9 #include "apps/app_window.h"
10 #include "apps/app_window_registry.h" 10 #include "apps/app_window_registry.h"
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 return GetImageURLFromData(contents); 134 return GetImageURLFromData(contents);
135 } 135 }
136 136
137 std::string GetExtensionID(const RenderViewHost* render_view_host) { 137 std::string GetExtensionID(const RenderViewHost* render_view_host) {
138 if (!render_view_host->GetSiteInstance()) 138 if (!render_view_host->GetSiteInstance())
139 return std::string(); 139 return std::string();
140 140
141 return render_view_host->GetSiteInstance()->GetSiteURL().host(); 141 return render_view_host->GetSiteInstance()->GetSiteURL().host();
142 } 142 }
143 143
144 void BroadcastItemStateChanged(content::BrowserContext* browser_context,
145 developer::EventType event_type,
146 const std::string& item_id) {
147 developer::EventData event_data;
148 event_data.event_type = event_type;
149 event_data.item_id = item_id;
150
151 scoped_ptr<base::ListValue> args(new base::ListValue());
152 args->Append(event_data.ToValue().release());
153 scoped_ptr<Event> event(new Event(
154 developer_private::OnItemStateChanged::kEventName, args.Pass()));
155 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass());
156 }
157
144 } // namespace 158 } // namespace
145 159
146 namespace AllowFileAccess = api::developer_private::AllowFileAccess; 160 namespace AllowFileAccess = api::developer_private::AllowFileAccess;
147 namespace AllowIncognito = api::developer_private::AllowIncognito; 161 namespace AllowIncognito = api::developer_private::AllowIncognito;
148 namespace ChoosePath = api::developer_private::ChoosePath; 162 namespace ChoosePath = api::developer_private::ChoosePath;
149 namespace Enable = api::developer_private::Enable; 163 namespace Enable = api::developer_private::Enable;
150 namespace GetItemsInfo = api::developer_private::GetItemsInfo; 164 namespace GetItemsInfo = api::developer_private::GetItemsInfo;
151 namespace Inspect = api::developer_private::Inspect; 165 namespace Inspect = api::developer_private::Inspect;
152 namespace PackDirectory = api::developer_private::PackDirectory; 166 namespace PackDirectory = api::developer_private::PackDirectory;
153 namespace Reload = api::developer_private::Reload; 167 namespace Reload = api::developer_private::Reload;
(...skipping 12 matching lines...) Expand all
166 content::BrowserContext* context) { 180 content::BrowserContext* context) {
167 return GetFactoryInstance()->Get(context); 181 return GetFactoryInstance()->Get(context);
168 } 182 }
169 183
170 DeveloperPrivateAPI::DeveloperPrivateAPI(content::BrowserContext* context) 184 DeveloperPrivateAPI::DeveloperPrivateAPI(content::BrowserContext* context)
171 : profile_(Profile::FromBrowserContext(context)) { 185 : profile_(Profile::FromBrowserContext(context)) {
172 RegisterNotifications(); 186 RegisterNotifications();
173 } 187 }
174 188
175 DeveloperPrivateEventRouter::DeveloperPrivateEventRouter(Profile* profile) 189 DeveloperPrivateEventRouter::DeveloperPrivateEventRouter(Profile* profile)
176 : profile_(profile) { 190 : extension_registry_observer_(this), profile_(profile) {
177 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, 191 registrar_.Add(this,
178 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
179 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
180 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
181 chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED, 192 chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED,
182 chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED}; 193 content::Source<Profile>(profile_));
194 registrar_.Add(this,
195 chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED,
196 content::Source<Profile>(profile_));
183 197
184 CHECK(registrar_.IsEmpty()); 198 // TODO(limasdf): Use scoped_observer instead.
185 for (size_t i = 0; i < arraysize(types); ++i) { 199 ErrorConsole::Get(profile)->AddObserver(this);
186 registrar_.Add(this,
187 types[i],
188 content::Source<Profile>(profile_));
189 }
190 200
191 ErrorConsole::Get(profile)->AddObserver(this); 201 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
192 } 202 }
193 203
194 DeveloperPrivateEventRouter::~DeveloperPrivateEventRouter() { 204 DeveloperPrivateEventRouter::~DeveloperPrivateEventRouter() {
195 ErrorConsole::Get(profile_)->RemoveObserver(this); 205 ErrorConsole::Get(profile_)->RemoveObserver(this);
196 } 206 }
197 207
198 void DeveloperPrivateEventRouter::AddExtensionId( 208 void DeveloperPrivateEventRouter::AddExtensionId(
199 const std::string& extension_id) { 209 const std::string& extension_id) {
200 extension_ids_.insert(extension_id); 210 extension_ids_.insert(extension_id);
201 } 211 }
202 212
203 void DeveloperPrivateEventRouter::RemoveExtensionId( 213 void DeveloperPrivateEventRouter::RemoveExtensionId(
204 const std::string& extension_id) { 214 const std::string& extension_id) {
205 extension_ids_.erase(extension_id); 215 extension_ids_.erase(extension_id);
206 } 216 }
207 217
208 void DeveloperPrivateEventRouter::Observe( 218 void DeveloperPrivateEventRouter::Observe(
209 int type, 219 int type,
210 const content::NotificationSource& source, 220 const content::NotificationSource& source,
211 const content::NotificationDetails& details) { 221 const content::NotificationDetails& details) {
212 const char* event_name = NULL;
213 Profile* profile = content::Source<Profile>(source).ptr(); 222 Profile* profile = content::Source<Profile>(source).ptr();
214 CHECK(profile); 223 CHECK(profile);
215 CHECK(profile_->IsSameProfile(profile)); 224 CHECK(profile_->IsSameProfile(profile));
216 developer::EventData event_data; 225 developer::EventData event_data;
217 const Extension* extension = NULL;
218 226
219 switch (type) { 227 switch (type) {
220 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: 228 case chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED: {
221 event_data.event_type = developer::EVENT_TYPE_INSTALLED;
222 extension =
223 content::Details<const InstalledExtensionInfo>(details)->extension;
224 break;
225 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED:
226 event_data.event_type = developer::EVENT_TYPE_UNINSTALLED;
227 extension = content::Details<const Extension>(details).ptr();
228 break;
229 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
230 event_data.event_type = developer::EVENT_TYPE_LOADED;
231 extension = content::Details<const Extension>(details).ptr();
232 break;
233 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED:
234 event_data.event_type = developer::EVENT_TYPE_UNLOADED;
235 extension =
236 content::Details<const UnloadedExtensionInfo>(details)->extension;
237 break;
238 case chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED:
239 event_data.event_type = developer::EVENT_TYPE_VIEW_UNREGISTERED; 229 event_data.event_type = developer::EVENT_TYPE_VIEW_UNREGISTERED;
240 event_data.item_id = GetExtensionID( 230 event_data.item_id = GetExtensionID(
241 content::Details<const RenderViewHost>(details).ptr()); 231 content::Details<const RenderViewHost>(details).ptr());
242 break; 232 break;
243 case chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED: 233 }
234 case chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED: {
244 event_data.event_type = developer::EVENT_TYPE_VIEW_REGISTERED; 235 event_data.event_type = developer::EVENT_TYPE_VIEW_REGISTERED;
245 event_data.item_id = GetExtensionID( 236 event_data.item_id = GetExtensionID(
246 content::Details<const RenderViewHost>(details).ptr()); 237 content::Details<const RenderViewHost>(details).ptr());
247 break; 238 break;
239 }
248 default: 240 default:
249 NOTREACHED(); 241 NOTREACHED();
250 return; 242 return;
251 } 243 }
252 244
253 if (extension) 245 BroadcastItemStateChanged(profile, event_data.event_type, event_data.item_id);
254 event_data.item_id = extension->id(); 246 }
255 247
256 scoped_ptr<base::ListValue> args(new base::ListValue()); 248 void DeveloperPrivateEventRouter::OnExtensionLoaded(
257 args->Append(event_data.ToValue().release()); 249 content::BrowserContext* browser_context,
250 const Extension* extension) {
251 DCHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context)));
252 BroadcastItemStateChanged(
253 browser_context, developer::EVENT_TYPE_LOADED, extension->id());
254 }
258 255
259 event_name = developer_private::OnItemStateChanged::kEventName; 256 void DeveloperPrivateEventRouter::OnExtensionUnloaded(
260 scoped_ptr<Event> event(new Event(event_name, args.Pass())); 257 content::BrowserContext* browser_context,
261 EventRouter::Get(profile)->BroadcastEvent(event.Pass()); 258 const Extension* extension,
259 UnloadedExtensionInfo::Reason reason) {
260 DCHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context)));
261 BroadcastItemStateChanged(
262 browser_context, developer::EVENT_TYPE_UNLOADED, extension->id());
263 }
264
265 void DeveloperPrivateEventRouter::OnExtensionWillBeInstalled(
266 content::BrowserContext* browser_context,
267 const Extension* extension,
268 bool is_update,
269 bool from_ephemeral,
270 const std::string& old_name) {
271 DCHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context)));
272 BroadcastItemStateChanged(
273 browser_context, developer::EVENT_TYPE_INSTALLED, extension->id());
274 }
275
276 void DeveloperPrivateEventRouter::OnExtensionUninstalled(
277 content::BrowserContext* browser_context,
278 const Extension* extension) {
279 DCHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context)));
280 BroadcastItemStateChanged(
281 browser_context, developer::EVENT_TYPE_UNINSTALLED, extension->id());
262 } 282 }
263 283
264 void DeveloperPrivateEventRouter::OnErrorAdded(const ExtensionError* error) { 284 void DeveloperPrivateEventRouter::OnErrorAdded(const ExtensionError* error) {
265 // We don't want to handle errors thrown by extensions subscribed to these 285 // We don't want to handle errors thrown by extensions subscribed to these
266 // events (currently only the Apps Developer Tool), because doing so risks 286 // events (currently only the Apps Developer Tool), because doing so risks
267 // entering a loop. 287 // entering a loop.
268 if (extension_ids_.find(error->extension_id()) != extension_ids_.end()) 288 if (extension_ids_.find(error->extension_id()) != extension_ids_.end())
269 return; 289 return;
270 290
271 developer::EventData event_data; 291 BroadcastItemStateChanged(
272 event_data.event_type = developer::EVENT_TYPE_ERROR_ADDED; 292 profile_, developer::EVENT_TYPE_ERROR_ADDED, error->extension_id());
273 event_data.item_id = error->extension_id();
274
275 scoped_ptr<base::ListValue> args(new base::ListValue);
276 args->Append(event_data.ToValue().release());
277
278 EventRouter::Get(profile_)->BroadcastEvent(scoped_ptr<Event>(new Event(
279 developer_private::OnItemStateChanged::kEventName, args.Pass())));
280 } 293 }
281 294
282 void DeveloperPrivateAPI::SetLastUnpackedDirectory(const base::FilePath& path) { 295 void DeveloperPrivateAPI::SetLastUnpackedDirectory(const base::FilePath& path) {
283 last_unpacked_directory_ = path; 296 last_unpacked_directory_ = path;
284 } 297 }
285 298
286 void DeveloperPrivateAPI::RegisterNotifications() { 299 void DeveloperPrivateAPI::RegisterNotifications() {
287 EventRouter::Get(profile_)->RegisterObserver( 300 EventRouter::Get(profile_)->RegisterObserver(
288 this, developer_private::OnItemStateChanged::kEventName); 301 this, developer_private::OnItemStateChanged::kEventName);
289 } 302 }
(...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after
1359 } 1372 }
1360 1373
1361 error_ui_util::HandleOpenDevTools(dict); 1374 error_ui_util::HandleOpenDevTools(dict);
1362 1375
1363 return true; 1376 return true;
1364 } 1377 }
1365 1378
1366 } // namespace api 1379 } // namespace api
1367 1380
1368 } // namespace extensions 1381 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/developer_private/developer_private_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698