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/extension_action/extension_action_api.h" | 5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 212 |
213 } // namespace | 213 } // namespace |
214 | 214 |
215 // | 215 // |
216 // ExtensionActionAPI | 216 // ExtensionActionAPI |
217 // | 217 // |
218 | 218 |
219 static base::LazyInstance<ProfileKeyedAPIFactory<ExtensionActionAPI> > | 219 static base::LazyInstance<ProfileKeyedAPIFactory<ExtensionActionAPI> > |
220 g_factory = LAZY_INSTANCE_INITIALIZER; | 220 g_factory = LAZY_INSTANCE_INITIALIZER; |
221 | 221 |
222 ExtensionActionAPI::ExtensionActionAPI(Profile* profile) { | 222 ExtensionActionAPI::ExtensionActionAPI(content::BrowserContext* context) { |
223 ExtensionFunctionRegistry* registry = | 223 ExtensionFunctionRegistry* registry = |
224 ExtensionFunctionRegistry::GetInstance(); | 224 ExtensionFunctionRegistry::GetInstance(); |
225 | 225 |
226 // Browser Actions | 226 // Browser Actions |
227 registry->RegisterFunction<BrowserActionSetIconFunction>(); | 227 registry->RegisterFunction<BrowserActionSetIconFunction>(); |
228 registry->RegisterFunction<BrowserActionSetTitleFunction>(); | 228 registry->RegisterFunction<BrowserActionSetTitleFunction>(); |
229 registry->RegisterFunction<BrowserActionSetBadgeTextFunction>(); | 229 registry->RegisterFunction<BrowserActionSetBadgeTextFunction>(); |
230 registry->RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); | 230 registry->RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); |
231 registry->RegisterFunction<BrowserActionSetPopupFunction>(); | 231 registry->RegisterFunction<BrowserActionSetPopupFunction>(); |
232 registry->RegisterFunction<BrowserActionGetTitleFunction>(); | 232 registry->RegisterFunction<BrowserActionGetTitleFunction>(); |
(...skipping 19 matching lines...) Expand all Loading... |
252 ExtensionActionAPI::~ExtensionActionAPI() { | 252 ExtensionActionAPI::~ExtensionActionAPI() { |
253 } | 253 } |
254 | 254 |
255 // static | 255 // static |
256 ProfileKeyedAPIFactory<ExtensionActionAPI>* | 256 ProfileKeyedAPIFactory<ExtensionActionAPI>* |
257 ExtensionActionAPI::GetFactoryInstance() { | 257 ExtensionActionAPI::GetFactoryInstance() { |
258 return g_factory.Pointer(); | 258 return g_factory.Pointer(); |
259 } | 259 } |
260 | 260 |
261 // static | 261 // static |
262 ExtensionActionAPI* ExtensionActionAPI::Get(Profile* profile) { | 262 ExtensionActionAPI* ExtensionActionAPI::Get(content::BrowserContext* context) { |
263 return ProfileKeyedAPIFactory<ExtensionActionAPI>::GetForProfile(profile); | 263 return ProfileKeyedAPIFactory<ExtensionActionAPI>::GetForProfile(context); |
264 } | 264 } |
265 | 265 |
266 // static | 266 // static |
267 bool ExtensionActionAPI::GetBrowserActionVisibility( | 267 bool ExtensionActionAPI::GetBrowserActionVisibility( |
268 const ExtensionPrefs* prefs, | 268 const ExtensionPrefs* prefs, |
269 const std::string& extension_id) { | 269 const std::string& extension_id) { |
270 bool visible = false; | 270 bool visible = false; |
271 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, | 271 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, |
272 kBrowserActionVisible, | 272 kBrowserActionVisible, |
273 &visible)) { | 273 &visible)) { |
(...skipping 14 matching lines...) Expand all Loading... |
288 kBrowserActionVisible, | 288 kBrowserActionVisible, |
289 new base::FundamentalValue(visible)); | 289 new base::FundamentalValue(visible)); |
290 content::NotificationService::current()->Notify( | 290 content::NotificationService::current()->Notify( |
291 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, | 291 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, |
292 content::Source<ExtensionPrefs>(prefs), | 292 content::Source<ExtensionPrefs>(prefs), |
293 content::Details<const std::string>(&extension_id)); | 293 content::Details<const std::string>(&extension_id)); |
294 } | 294 } |
295 | 295 |
296 // static | 296 // static |
297 void ExtensionActionAPI::BrowserActionExecuted( | 297 void ExtensionActionAPI::BrowserActionExecuted( |
298 Profile* profile, | 298 content::BrowserContext* context, |
299 const ExtensionAction& browser_action, | 299 const ExtensionAction& browser_action, |
300 WebContents* web_contents) { | 300 WebContents* web_contents) { |
301 ExtensionActionExecuted(profile, browser_action, web_contents); | 301 ExtensionActionExecuted(context, browser_action, web_contents); |
302 } | 302 } |
303 | 303 |
304 // static | 304 // static |
305 void ExtensionActionAPI::PageActionExecuted(Profile* profile, | 305 void ExtensionActionAPI::PageActionExecuted(content::BrowserContext* context, |
306 const ExtensionAction& page_action, | 306 const ExtensionAction& page_action, |
307 int tab_id, | 307 int tab_id, |
308 const std::string& url, | 308 const std::string& url, |
309 int button) { | 309 int button) { |
310 DispatchOldPageActionEvent(profile, page_action.extension_id(), | 310 DispatchOldPageActionEvent(context, |
311 page_action.id(), tab_id, url, button); | 311 page_action.extension_id(), |
| 312 page_action.id(), |
| 313 tab_id, |
| 314 url, |
| 315 button); |
312 WebContents* web_contents = NULL; | 316 WebContents* web_contents = NULL; |
313 if (!extensions::ExtensionTabUtil::GetTabById( | 317 if (!extensions::ExtensionTabUtil::GetTabById( |
314 tab_id, profile, profile->IsOffTheRecord(), | 318 tab_id, |
315 NULL, NULL, &web_contents, NULL)) { | 319 Profile::FromBrowserContext(context), |
| 320 context->IsOffTheRecord(), |
| 321 NULL, |
| 322 NULL, |
| 323 &web_contents, |
| 324 NULL)) { |
316 return; | 325 return; |
317 } | 326 } |
318 ExtensionActionExecuted(profile, page_action, web_contents); | 327 ExtensionActionExecuted(context, page_action, web_contents); |
319 } | 328 } |
320 | 329 |
321 // static | 330 // static |
322 void ExtensionActionAPI::DispatchEventToExtension( | 331 void ExtensionActionAPI::DispatchEventToExtension( |
323 Profile* profile, | 332 content::BrowserContext* context, |
324 const std::string& extension_id, | 333 const std::string& extension_id, |
325 const std::string& event_name, | 334 const std::string& event_name, |
326 scoped_ptr<base::ListValue> event_args) { | 335 scoped_ptr<base::ListValue> event_args) { |
327 if (!extensions::ExtensionSystem::Get(profile)->event_router()) | 336 if (!extensions::ExtensionSystem::Get(context)->event_router()) |
328 return; | 337 return; |
329 | 338 |
330 scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); | 339 scoped_ptr<Event> event(new Event(event_name, event_args.Pass())); |
331 event->restrict_to_browser_context = profile; | 340 event->restrict_to_browser_context = context; |
332 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; | 341 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; |
333 ExtensionSystem::Get(profile)->event_router()-> | 342 ExtensionSystem::Get(context)->event_router()->DispatchEventToExtension( |
334 DispatchEventToExtension(extension_id, event.Pass()); | 343 extension_id, event.Pass()); |
335 } | 344 } |
336 | 345 |
337 // static | 346 // static |
338 void ExtensionActionAPI::DispatchOldPageActionEvent( | 347 void ExtensionActionAPI::DispatchOldPageActionEvent( |
339 Profile* profile, | 348 content::BrowserContext* context, |
340 const std::string& extension_id, | 349 const std::string& extension_id, |
341 const std::string& page_action_id, | 350 const std::string& page_action_id, |
342 int tab_id, | 351 int tab_id, |
343 const std::string& url, | 352 const std::string& url, |
344 int button) { | 353 int button) { |
345 scoped_ptr<base::ListValue> args(new base::ListValue()); | 354 scoped_ptr<base::ListValue> args(new base::ListValue()); |
346 args->Append(new base::StringValue(page_action_id)); | 355 args->Append(new base::StringValue(page_action_id)); |
347 | 356 |
348 base::DictionaryValue* data = new base::DictionaryValue(); | 357 base::DictionaryValue* data = new base::DictionaryValue(); |
349 data->Set(page_actions_keys::kTabIdKey, new base::FundamentalValue(tab_id)); | 358 data->Set(page_actions_keys::kTabIdKey, new base::FundamentalValue(tab_id)); |
350 data->Set(page_actions_keys::kTabUrlKey, new base::StringValue(url)); | 359 data->Set(page_actions_keys::kTabUrlKey, new base::StringValue(url)); |
351 data->Set(page_actions_keys::kButtonKey, | 360 data->Set(page_actions_keys::kButtonKey, |
352 new base::FundamentalValue(button)); | 361 new base::FundamentalValue(button)); |
353 args->Append(data); | 362 args->Append(data); |
354 | 363 |
355 DispatchEventToExtension(profile, extension_id, "pageActions", args.Pass()); | 364 DispatchEventToExtension(context, extension_id, "pageActions", args.Pass()); |
356 } | 365 } |
357 | 366 |
358 // static | 367 // static |
359 void ExtensionActionAPI::ExtensionActionExecuted( | 368 void ExtensionActionAPI::ExtensionActionExecuted( |
360 Profile* profile, | 369 content::BrowserContext* context, |
361 const ExtensionAction& extension_action, | 370 const ExtensionAction& extension_action, |
362 WebContents* web_contents) { | 371 WebContents* web_contents) { |
363 const char* event_name = NULL; | 372 const char* event_name = NULL; |
364 switch (extension_action.action_type()) { | 373 switch (extension_action.action_type()) { |
365 case ActionInfo::TYPE_BROWSER: | 374 case ActionInfo::TYPE_BROWSER: |
366 event_name = "browserAction.onClicked"; | 375 event_name = "browserAction.onClicked"; |
367 break; | 376 break; |
368 case ActionInfo::TYPE_PAGE: | 377 case ActionInfo::TYPE_PAGE: |
369 event_name = "pageAction.onClicked"; | 378 event_name = "pageAction.onClicked"; |
370 break; | 379 break; |
371 case ActionInfo::TYPE_SYSTEM_INDICATOR: | 380 case ActionInfo::TYPE_SYSTEM_INDICATOR: |
372 // The System Indicator handles its own clicks. | 381 // The System Indicator handles its own clicks. |
373 break; | 382 break; |
374 } | 383 } |
375 | 384 |
376 if (event_name) { | 385 if (event_name) { |
377 scoped_ptr<base::ListValue> args(new base::ListValue()); | 386 scoped_ptr<base::ListValue> args(new base::ListValue()); |
378 base::DictionaryValue* tab_value = | 387 base::DictionaryValue* tab_value = |
379 extensions::ExtensionTabUtil::CreateTabValue(web_contents); | 388 extensions::ExtensionTabUtil::CreateTabValue(web_contents); |
380 args->Append(tab_value); | 389 args->Append(tab_value); |
381 | 390 |
382 DispatchEventToExtension(profile, | 391 DispatchEventToExtension( |
383 extension_action.extension_id(), | 392 context, extension_action.extension_id(), event_name, args.Pass()); |
384 event_name, | |
385 args.Pass()); | |
386 } | 393 } |
387 } | 394 } |
388 | 395 |
389 // | 396 // |
390 // ExtensionActionStorageManager | 397 // ExtensionActionStorageManager |
391 // | 398 // |
392 | 399 |
393 ExtensionActionStorageManager::ExtensionActionStorageManager(Profile* profile) | 400 ExtensionActionStorageManager::ExtensionActionStorageManager(Profile* profile) |
394 : profile_(profile) { | 401 : profile_(profile) { |
395 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 402 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 return true; | 943 return true; |
937 } | 944 } |
938 | 945 |
939 bool EnablePageActionsFunction::RunImpl() { | 946 bool EnablePageActionsFunction::RunImpl() { |
940 return SetPageActionEnabled(true); | 947 return SetPageActionEnabled(true); |
941 } | 948 } |
942 | 949 |
943 bool DisablePageActionsFunction::RunImpl() { | 950 bool DisablePageActionsFunction::RunImpl() { |
944 return SetPageActionEnabled(false); | 951 return SetPageActionEnabled(false); |
945 } | 952 } |
OLD | NEW |