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_actions_api.h " | 5 #include "chrome/browser/extensions/api/extension_action/extension_actions_api.h " |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_piece.h" | 11 #include "base/string_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h" |
14 #include "chrome/browser/extensions/extension_action.h" | 14 #include "chrome/browser/extensions/extension_action.h" |
15 #include "chrome/browser/extensions/extension_action_manager.h" | 15 #include "chrome/browser/extensions/extension_action_manager.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/extension_system.h" | 17 #include "chrome/browser/extensions/extension_system.h" |
18 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
19 #include "chrome/browser/extensions/location_bar_controller.h" | 19 #include "chrome/browser/extensions/location_bar_controller.h" |
20 #include "chrome/browser/extensions/state_store.h" | 20 #include "chrome/browser/extensions/state_store.h" |
21 #include "chrome/browser/extensions/tab_helper.h" | 21 #include "chrome/browser/extensions/tab_helper.h" |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 : details_(NULL), | 276 : details_(NULL), |
277 tab_id_(ExtensionAction::kDefaultTabId), | 277 tab_id_(ExtensionAction::kDefaultTabId), |
278 contents_(NULL), | 278 contents_(NULL), |
279 extension_action_(NULL) { | 279 extension_action_(NULL) { |
280 } | 280 } |
281 | 281 |
282 ExtensionActionFunction::~ExtensionActionFunction() { | 282 ExtensionActionFunction::~ExtensionActionFunction() { |
283 } | 283 } |
284 | 284 |
285 bool ExtensionActionFunction::RunImpl() { | 285 bool ExtensionActionFunction::RunImpl() { |
286 if (base::StringPiece(name()).starts_with("scriptBadge.")) { | 286 extensions::ExtensionActionManager* manager = |
287 extension_action_ = extensions::ExtensionActionManager::Get(profile_)-> | 287 extensions::ExtensionActionManager::Get(profile_); |
288 GetScriptBadge(*GetExtension()); | 288 const extensions::Extension* extension = GetExtension(); |
289 if (StartsWithASCII(name(), "scriptBadge.", false)) { | |
290 extension_action_ = manager->GetScriptBadge(*extension); | |
291 } else if (StartsWithASCII(name(), "systemIndicator.", false)) { | |
Nico
2015/07/06 17:39:21
Isn't javascript case-sensitive? Why is this passi
dewittj
2015/07/06 17:44:52
Looks like it should be case sensitive.
| |
292 extension_action_ = manager->GetSystemIndicator(*extension); | |
289 } else { | 293 } else { |
290 extension_action_ = extensions::ExtensionActionManager::Get(profile_)-> | 294 extension_action_ = manager->GetBrowserAction(*extension); |
291 GetBrowserAction(*GetExtension()); | |
292 if (!extension_action_) { | 295 if (!extension_action_) { |
293 extension_action_ = extensions::ExtensionActionManager::Get(profile_)-> | 296 extension_action_ = manager->GetPageAction(*extension); |
294 GetPageAction(*GetExtension()); | |
295 } | 297 } |
296 } | 298 } |
297 if (!extension_action_) { | 299 if (!extension_action_) { |
298 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event | 300 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event |
299 // exist for extensions that don't have one declared. This should come as | 301 // exist for extensions that don't have one declared. This should come as |
300 // part of the Feature system. | 302 // part of the Feature system. |
301 error_ = kNoExtensionActionError; | 303 error_ = kNoExtensionActionError; |
302 return false; | 304 return false; |
303 } | 305 } |
304 | 306 |
305 // There may or may not be details (depends on the function). | 307 // Populates the tab_id_ and details_ members. |
306 // The tabId might appear in details (if it exists) or as the first | 308 EXTENSION_FUNCTION_VALIDATE(ExtractDataFromArguments()); |
307 // argument besides the action type (depends on the function). | |
308 { | |
309 base::Value* first_arg = NULL; | |
310 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &first_arg)); | |
311 | |
312 switch (first_arg->GetType()) { | |
313 case Value::TYPE_INTEGER: | |
314 CHECK(first_arg->GetAsInteger(&tab_id_)); | |
315 break; | |
316 | |
317 case Value::TYPE_DICTIONARY: { | |
318 details_ = static_cast<base::DictionaryValue*>(first_arg); | |
319 base::Value* tab_id_value = NULL; | |
320 if (details_->Get("tabId", &tab_id_value)) { | |
321 switch (tab_id_value->GetType()) { | |
322 case Value::TYPE_NULL: | |
323 // Fine, equivalent to it being not-there, and tabId is optional. | |
324 break; | |
325 case Value::TYPE_INTEGER: | |
326 CHECK(tab_id_value->GetAsInteger(&tab_id_)); | |
327 break; | |
328 default: | |
329 // Boom. | |
330 EXTENSION_FUNCTION_VALIDATE(false); | |
331 } | |
332 } | |
333 break; | |
334 } | |
335 | |
336 case Value::TYPE_NULL: | |
337 // The tabId might be an optional argument. | |
338 break; | |
339 | |
340 default: | |
341 EXTENSION_FUNCTION_VALIDATE(false); | |
342 } | |
343 } | |
344 | 309 |
345 // Find the WebContents that contains this tab id if one is required. | 310 // Find the WebContents that contains this tab id if one is required. |
346 if (tab_id_ == ExtensionAction::kDefaultTabId) { | 311 if (tab_id_ != ExtensionAction::kDefaultTabId) { |
347 EXTENSION_FUNCTION_VALIDATE( | |
348 extensions::ExtensionActionManager::Get(profile_)-> | |
349 GetBrowserAction(*GetExtension())); | |
350 } else { | |
351 ExtensionTabUtil::GetTabById( | 312 ExtensionTabUtil::GetTabById( |
352 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL); | 313 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL); |
353 if (!contents_) { | 314 if (!contents_) { |
354 error_ = extensions::ErrorUtils::FormatErrorMessage( | 315 error_ = extensions::ErrorUtils::FormatErrorMessage( |
355 kNoTabError, base::IntToString(tab_id_)); | 316 kNoTabError, base::IntToString(tab_id_)); |
356 return false; | 317 return false; |
357 } | 318 } |
319 } else { | |
320 // Only browser actions and system indicators have a default tabId. | |
321 typedef extensions::Extension::ActionInfo ActionInfo; | |
322 ActionInfo::Type action_type = extension_action_->action_type(); | |
323 EXTENSION_FUNCTION_VALIDATE( | |
324 action_type == ActionInfo::TYPE_BROWSER || | |
325 action_type == ActionInfo::TYPE_SYSTEM_INDICATOR); | |
326 } | |
327 return RunExtensionAction(); | |
328 } | |
329 | |
330 bool ExtensionActionFunction::ExtractDataFromArguments() { | |
331 // There may or may not be details (depends on the function). | |
332 // The tabId might appear in details (if it exists), as the first | |
333 // argument besides the action type (depends on the function), or be omitted | |
334 // entirely. | |
335 base::Value* first_arg = NULL; | |
336 if (!args_->Get(0, &first_arg)) | |
337 return true; | |
338 | |
339 switch (first_arg->GetType()) { | |
340 case Value::TYPE_INTEGER: | |
341 CHECK(first_arg->GetAsInteger(&tab_id_)); | |
342 break; | |
343 | |
344 case Value::TYPE_DICTIONARY: { | |
345 // Found the details argument. | |
346 details_ = static_cast<base::DictionaryValue*>(first_arg); | |
347 // Still need to check for the tabId within details. | |
348 base::Value* tab_id_value = NULL; | |
349 if (details_->Get("tabId", &tab_id_value)) { | |
350 switch (tab_id_value->GetType()) { | |
351 case Value::TYPE_NULL: | |
352 // OK; tabId is optional, leave it default. | |
353 return true; | |
354 case Value::TYPE_INTEGER: | |
355 CHECK(tab_id_value->GetAsInteger(&tab_id_)); | |
356 return true; | |
357 default: | |
358 // Boom. | |
359 return false; | |
360 } | |
361 } | |
362 // Not found; tabId is optional, leave it default. | |
363 break; | |
364 } | |
365 | |
366 case Value::TYPE_NULL: | |
367 // The tabId might be an optional argument. | |
368 break; | |
369 | |
370 default: | |
371 return false; | |
358 } | 372 } |
359 | 373 |
360 return RunExtensionAction(); | 374 return true; |
361 } | 375 } |
362 | 376 |
363 void ExtensionActionFunction::NotifyChange() { | 377 void ExtensionActionFunction::NotifyChange() { |
364 switch (extension_action_->action_type()) { | 378 switch (extension_action_->action_type()) { |
365 case extensions::Extension::ActionInfo::TYPE_BROWSER: | 379 case extensions::Extension::ActionInfo::TYPE_BROWSER: |
366 case extensions::Extension::ActionInfo::TYPE_PAGE: | 380 case extensions::Extension::ActionInfo::TYPE_PAGE: |
367 if (extensions::ExtensionActionManager::Get(profile_)-> | 381 if (extensions::ExtensionActionManager::Get(profile_)-> |
368 GetBrowserAction(*extension_)) { | 382 GetBrowserAction(*extension_)) { |
369 NotifyBrowserActionChange(); | 383 NotifyBrowserActionChange(); |
370 } else if (extensions::ExtensionActionManager::Get(profile_)-> | 384 } else if (extensions::ExtensionActionManager::Get(profile_)-> |
371 GetPageAction(*extension_)) { | 385 GetPageAction(*extension_)) { |
372 NotifyLocationBarChange(); | 386 NotifyLocationBarChange(); |
373 } | 387 } |
374 return; | 388 return; |
375 case extensions::Extension::ActionInfo::TYPE_SCRIPT_BADGE: | 389 case extensions::Extension::ActionInfo::TYPE_SCRIPT_BADGE: |
376 NotifyLocationBarChange(); | 390 NotifyLocationBarChange(); |
377 return; | 391 return; |
392 case extensions::Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: | |
393 NotifyStatusTrayChange(); | |
394 return; | |
378 } | 395 } |
379 NOTREACHED(); | 396 NOTREACHED(); |
380 } | 397 } |
381 | 398 |
382 void ExtensionActionFunction::NotifyBrowserActionChange() { | 399 void ExtensionActionFunction::NotifyBrowserActionChange() { |
383 content::NotificationService::current()->Notify( | 400 content::NotificationService::current()->Notify( |
384 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 401 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
385 content::Source<ExtensionAction>(extension_action_), | 402 content::Source<ExtensionAction>(extension_action_), |
386 content::Details<Profile>(profile())); | 403 content::Details<Profile>(profile())); |
387 } | 404 } |
388 | 405 |
389 void ExtensionActionFunction::NotifyLocationBarChange() { | 406 void ExtensionActionFunction::NotifyLocationBarChange() { |
390 extensions::TabHelper::FromWebContents(contents_)-> | 407 extensions::TabHelper::FromWebContents(contents_)-> |
391 location_bar_controller()->NotifyChange(); | 408 location_bar_controller()->NotifyChange(); |
392 } | 409 } |
393 | 410 |
411 void ExtensionActionFunction::NotifyStatusTrayChange() { | |
412 // TODO(dewittj) Implement status tray behavior here. | |
413 // See http://crbug.com/142450. | |
414 } | |
415 | |
394 // static | 416 // static |
395 bool ExtensionActionFunction::ParseCSSColorString( | 417 bool ExtensionActionFunction::ParseCSSColorString( |
396 const std::string& color_string, | 418 const std::string& color_string, |
397 SkColor* result) { | 419 SkColor* result) { |
398 std::string formatted_color = "#"; | 420 std::string formatted_color = "#"; |
399 // Check the string for incorrect formatting. | 421 // Check the string for incorrect formatting. |
400 if (color_string[0] != '#') | 422 if (color_string[0] != '#') |
401 return false; | 423 return false; |
402 | 424 |
403 // Convert the string from #FFF format to #FFFFFF format. | 425 // Convert the string from #FFF format to #FFFFFF format. |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
555 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | 577 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { |
556 ListValue* list = new ListValue(); | 578 ListValue* list = new ListValue(); |
557 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); | 579 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); |
558 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); | 580 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); |
559 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); | 581 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); |
560 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); | 582 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); |
561 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); | 583 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); |
562 SetResult(list); | 584 SetResult(list); |
563 return true; | 585 return true; |
564 } | 586 } |
OLD | NEW |