Chromium Code Reviews| 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 #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 Loading... | |
| 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 Loading... | |
| 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; | |
| 217 const Extension* extension = NULL; | |
| 218 | 225 |
| 219 switch (type) { | 226 switch (type) { |
| 220 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: | 227 case chrome::NOTIFICATION_EXTENSION_VIEW_UNREGISTERED: { |
| 221 event_data.event_type = developer::EVENT_TYPE_INSTALLED; | 228 developer::EventData event_data; |
|
Devlin
2014/06/02 18:41:02
nit: Move this outside switch() to line 225...
limasdf
2014/06/02 18:52:36
Done.
| |
| 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()); |
| 232 BroadcastItemStateChanged( | |
|
Devlin
2014/06/02 18:41:02
... and then put this and the same call on line 24
limasdf
2014/06/02 18:52:36
Done.
| |
| 233 profile, event_data.event_type, event_data.item_id); | |
| 242 break; | 234 break; |
| 243 case chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED: | 235 } |
| 236 case chrome::NOTIFICATION_EXTENSION_VIEW_REGISTERED: { | |
| 237 developer::EventData event_data; | |
| 244 event_data.event_type = developer::EVENT_TYPE_VIEW_REGISTERED; | 238 event_data.event_type = developer::EVENT_TYPE_VIEW_REGISTERED; |
| 245 event_data.item_id = GetExtensionID( | 239 event_data.item_id = GetExtensionID( |
| 246 content::Details<const RenderViewHost>(details).ptr()); | 240 content::Details<const RenderViewHost>(details).ptr()); |
| 241 BroadcastItemStateChanged( | |
| 242 profile, event_data.event_type, event_data.item_id); | |
| 247 break; | 243 break; |
| 244 } | |
| 248 default: | 245 default: |
| 249 NOTREACHED(); | 246 NOTREACHED(); |
| 250 return; | 247 return; |
| 251 } | 248 } |
| 249 } | |
| 252 | 250 |
| 253 if (extension) | 251 void DeveloperPrivateEventRouter::OnExtensionLoaded( |
| 254 event_data.item_id = extension->id(); | 252 content::BrowserContext* browser_context, |
| 253 const Extension* extension) { | |
| 254 CHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context))); | |
|
Devlin
2014/06/02 18:41:02
probably safe to make these DCHECKs. Nothing terr
| |
| 255 BroadcastItemStateChanged( | |
| 256 browser_context, developer::EVENT_TYPE_LOADED, extension->id()); | |
| 257 } | |
| 255 | 258 |
| 256 scoped_ptr<base::ListValue> args(new base::ListValue()); | 259 void DeveloperPrivateEventRouter::OnExtensionUnloaded( |
| 257 args->Append(event_data.ToValue().release()); | 260 content::BrowserContext* browser_context, |
| 261 const Extension* extension, | |
| 262 UnloadedExtensionInfo::Reason reason) { | |
| 263 CHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context))); | |
| 264 BroadcastItemStateChanged( | |
| 265 browser_context, developer::EVENT_TYPE_UNLOADED, extension->id()); | |
| 266 } | |
| 258 | 267 |
| 259 event_name = developer_private::OnItemStateChanged::kEventName; | 268 void DeveloperPrivateEventRouter::OnExtensionWillBeInstalled( |
| 260 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 269 content::BrowserContext* browser_context, |
| 261 EventRouter::Get(profile)->BroadcastEvent(event.Pass()); | 270 const Extension* extension, |
| 271 bool is_update, | |
| 272 bool from_ephemeral, | |
| 273 const std::string& old_name) { | |
| 274 CHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context))); | |
| 275 BroadcastItemStateChanged( | |
| 276 browser_context, developer::EVENT_TYPE_INSTALLED, extension->id()); | |
| 277 } | |
| 278 | |
| 279 void DeveloperPrivateEventRouter::OnExtensionUninstalled( | |
| 280 content::BrowserContext* browser_context, | |
| 281 const Extension* extension) { | |
| 282 CHECK(profile_->IsSameProfile(Profile::FromBrowserContext(browser_context))); | |
| 283 BroadcastItemStateChanged( | |
| 284 browser_context, developer::EVENT_TYPE_UNINSTALLED, extension->id()); | |
| 262 } | 285 } |
| 263 | 286 |
| 264 void DeveloperPrivateEventRouter::OnErrorAdded(const ExtensionError* error) { | 287 void DeveloperPrivateEventRouter::OnErrorAdded(const ExtensionError* error) { |
| 265 // We don't want to handle errors thrown by extensions subscribed to these | 288 // 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 | 289 // events (currently only the Apps Developer Tool), because doing so risks |
| 267 // entering a loop. | 290 // entering a loop. |
| 268 if (extension_ids_.find(error->extension_id()) != extension_ids_.end()) | 291 if (extension_ids_.find(error->extension_id()) != extension_ids_.end()) |
| 269 return; | 292 return; |
| 270 | 293 |
| 271 developer::EventData event_data; | 294 BroadcastItemStateChanged( |
| 272 event_data.event_type = developer::EVENT_TYPE_ERROR_ADDED; | 295 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 } | 296 } |
| 281 | 297 |
| 282 void DeveloperPrivateAPI::SetLastUnpackedDirectory(const base::FilePath& path) { | 298 void DeveloperPrivateAPI::SetLastUnpackedDirectory(const base::FilePath& path) { |
| 283 last_unpacked_directory_ = path; | 299 last_unpacked_directory_ = path; |
| 284 } | 300 } |
| 285 | 301 |
| 286 void DeveloperPrivateAPI::RegisterNotifications() { | 302 void DeveloperPrivateAPI::RegisterNotifications() { |
| 287 EventRouter::Get(profile_)->RegisterObserver( | 303 EventRouter::Get(profile_)->RegisterObserver( |
| 288 this, developer_private::OnItemStateChanged::kEventName); | 304 this, developer_private::OnItemStateChanged::kEventName); |
| 289 } | 305 } |
| (...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1359 } | 1375 } |
| 1360 | 1376 |
| 1361 error_ui_util::HandleOpenDevTools(dict); | 1377 error_ui_util::HandleOpenDevTools(dict); |
| 1362 | 1378 |
| 1363 return true; | 1379 return true; |
| 1364 } | 1380 } |
| 1365 | 1381 |
| 1366 } // namespace api | 1382 } // namespace api |
| 1367 | 1383 |
| 1368 } // namespace extensions | 1384 } // namespace extensions |
| OLD | NEW |