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

Side by Side Diff: chrome/browser/extensions/api/extension_action/extension_actions_api.cc

Issue 11361189: Initial skeleton for System Indicator API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Clean up extension action arguments, style, smaller tests. Created 8 years 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) 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"
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 base::StringPiece functionName(name());
not at google - send to devlin 2012/11/26 19:44:54 chromium is unix_style not camelCase. however, na
dewittj 2012/11/27 01:53:55 Done. StartsWithASCII added.
290
291 if (functionName.starts_with("scriptBadge.")) {
292 extension_action_ = manager->GetScriptBadge(*extension);
293 } else if (functionName.starts_with("systemIndicator.")) {
294 extension_action_ = manager->GetSystemIndicator(*extension);
289 } else { 295 } else {
290 extension_action_ = extensions::ExtensionActionManager::Get(profile_)-> 296 extension_action_ = manager->GetBrowserAction(*extension);
291 GetBrowserAction(*GetExtension());
292 if (!extension_action_) { 297 if (!extension_action_) {
293 extension_action_ = extensions::ExtensionActionManager::Get(profile_)-> 298 extension_action_ = manager->GetPageAction(*extension);
294 GetPageAction(*GetExtension());
295 } 299 }
296 } 300 }
297 if (!extension_action_) { 301 if (!extension_action_) {
298 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event 302 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event
299 // exist for extensions that don't have one declared. This should come as 303 // exist for extensions that don't have one declared. This should come as
300 // part of the Feature system. 304 // part of the Feature system.
301 error_ = kNoExtensionActionError; 305 error_ = kNoExtensionActionError;
302 return false; 306 return false;
303 } 307 }
304 308
305 // There may or may not be details (depends on the function). 309 // Populates the tab_id_ and details_ members.
306 // The tabId might appear in details (if it exists) or as the first 310 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 311
345 // Find the WebContents that contains this tab id if one is required. 312 // Find the WebContents that contains this tab id if one is required.
346 if (tab_id_ == ExtensionAction::kDefaultTabId) { 313 if (tab_id_ != ExtensionAction::kDefaultTabId) {
347 EXTENSION_FUNCTION_VALIDATE(
348 extensions::ExtensionActionManager::Get(profile_)->
349 GetBrowserAction(*GetExtension()));
350 } else {
351 ExtensionTabUtil::GetTabById( 314 ExtensionTabUtil::GetTabById(
352 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL); 315 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL);
353 if (!contents_) { 316 if (!contents_) {
354 error_ = extensions::ErrorUtils::FormatErrorMessage( 317 error_ = extensions::ErrorUtils::FormatErrorMessage(
355 kNoTabError, base::IntToString(tab_id_)); 318 kNoTabError, base::IntToString(tab_id_));
356 return false; 319 return false;
357 } 320 }
321 } else {
322 // Only browser actions and system indicators have a default tabId.
323 const extensions::Extension::ActionInfo::Type type_browser =
324 extensions::Extension::ActionInfo::TYPE_BROWSER;
325 const extensions::Extension::ActionInfo::Type type_system_indicator =
326 extensions::Extension::ActionInfo::TYPE_SYSTEM_INDICATOR;
not at google - send to devlin 2012/11/26 19:44:54 this is just making it more verbose?
dewittj 2012/11/27 01:53:55 This is making it fit in the 80 column limit. Rep
327 extensions::Extension::ActionInfo::Type action_type =
328 extension_action_->action_type();
329
330 EXTENSION_FUNCTION_VALIDATE(
331 action_type == type_browser || action_type == type_system_indicator);
332 }
333 return RunExtensionAction();
334 }
335
336 bool ExtensionActionFunction::ExtractDataFromArguments() {
337 // There may or may not be details (depends on the function).
338 // The tabId might appear in details (if it exists), as the first
339 // argument besides the action type (depends on the function), or be omitted
340 // entirely.
341 base::Value* first_arg = NULL;
342 if (!args_->Get(0, &first_arg)) {
343 return true;
358 } 344 }
not at google - send to devlin 2012/11/26 19:44:54 generally leave out {} if it's just a 1 line body.
dewittj 2012/11/27 01:53:55 Done.
359 345
360 return RunExtensionAction(); 346 switch (first_arg->GetType()) {
347 case Value::TYPE_INTEGER:
348 CHECK(first_arg->GetAsInteger(&tab_id_));
349 break;
350
351 case Value::TYPE_DICTIONARY: {
352 // Found the details argument.
353 details_ = static_cast<base::DictionaryValue*>(first_arg);
354 // Still need to check for the tabId within details.
355 base::Value* tab_id_value = NULL;
356 if (details_->Get("tabId", &tab_id_value)) {
357 switch (tab_id_value->GetType()) {
358 case Value::TYPE_NULL:
359 // OK; tabId is optional, leave it default.
360 return true;
361 case Value::TYPE_INTEGER:
362 CHECK(tab_id_value->GetAsInteger(&tab_id_));
363 return true;
364 default:
365 // Boom.
366 return false;
367 }
368 }
369 // Not found; tabId is optional, leave it default.
370 break;
371 }
372
373 case Value::TYPE_NULL:
374 // The tabId might be an optional argument.
375 break;
376
377 default:
378 return false;
379 }
380
381 return true;
361 } 382 }
362 383
363 void ExtensionActionFunction::NotifyChange() { 384 void ExtensionActionFunction::NotifyChange() {
364 switch (extension_action_->action_type()) { 385 switch (extension_action_->action_type()) {
365 case extensions::Extension::ActionInfo::TYPE_BROWSER: 386 case extensions::Extension::ActionInfo::TYPE_BROWSER:
366 case extensions::Extension::ActionInfo::TYPE_PAGE: 387 case extensions::Extension::ActionInfo::TYPE_PAGE:
367 if (extensions::ExtensionActionManager::Get(profile_)-> 388 if (extensions::ExtensionActionManager::Get(profile_)->
368 GetBrowserAction(*extension_)) { 389 GetBrowserAction(*extension_)) {
369 NotifyBrowserActionChange(); 390 NotifyBrowserActionChange();
370 } else if (extensions::ExtensionActionManager::Get(profile_)-> 391 } else if (extensions::ExtensionActionManager::Get(profile_)->
371 GetPageAction(*extension_)) { 392 GetPageAction(*extension_)) {
372 NotifyLocationBarChange(); 393 NotifyLocationBarChange();
373 } 394 }
374 return; 395 return;
375 case extensions::Extension::ActionInfo::TYPE_SCRIPT_BADGE: 396 case extensions::Extension::ActionInfo::TYPE_SCRIPT_BADGE:
376 NotifyLocationBarChange(); 397 NotifyLocationBarChange();
377 return; 398 return;
399 case extensions::Extension::ActionInfo::TYPE_SYSTEM_INDICATOR:
400 NotifyStatusTrayChange();
401 return;
378 } 402 }
379 NOTREACHED(); 403 NOTREACHED();
380 } 404 }
381 405
382 void ExtensionActionFunction::NotifyBrowserActionChange() { 406 void ExtensionActionFunction::NotifyBrowserActionChange() {
383 content::NotificationService::current()->Notify( 407 content::NotificationService::current()->Notify(
384 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 408 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
385 content::Source<ExtensionAction>(extension_action_), 409 content::Source<ExtensionAction>(extension_action_),
386 content::Details<Profile>(profile())); 410 content::Details<Profile>(profile()));
387 } 411 }
388 412
389 void ExtensionActionFunction::NotifyLocationBarChange() { 413 void ExtensionActionFunction::NotifyLocationBarChange() {
390 extensions::TabHelper::FromWebContents(contents_)-> 414 extensions::TabHelper::FromWebContents(contents_)->
391 location_bar_controller()->NotifyChange(); 415 location_bar_controller()->NotifyChange();
392 } 416 }
393 417
418 void ExtensionActionFunction::NotifyStatusTrayChange() {
419 // TODO(dewittj) Implement status tray behavior here.
420 // See http://crbug.com/142450.
421 }
422
394 // static 423 // static
395 bool ExtensionActionFunction::ParseCSSColorString( 424 bool ExtensionActionFunction::ParseCSSColorString(
396 const std::string& color_string, 425 const std::string& color_string,
397 SkColor* result) { 426 SkColor* result) {
398 std::string formatted_color = "#"; 427 std::string formatted_color = "#";
399 // Check the string for incorrect formatting. 428 // Check the string for incorrect formatting.
400 if (color_string[0] != '#') 429 if (color_string[0] != '#')
401 return false; 430 return false;
402 431
403 // Convert the string from #FFF format to #FFFFFF format. 432 // Convert the string from #FFF format to #FFFFFF format.
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { 584 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() {
556 ListValue* list = new ListValue(); 585 ListValue* list = new ListValue();
557 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); 586 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_);
558 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); 587 list->Append(Value::CreateIntegerValue(SkColorGetR(color)));
559 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); 588 list->Append(Value::CreateIntegerValue(SkColorGetG(color)));
560 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); 589 list->Append(Value::CreateIntegerValue(SkColorGetB(color)));
561 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); 590 list->Append(Value::CreateIntegerValue(SkColorGetA(color)));
562 SetResult(list); 591 SetResult(list);
563 return true; 592 return true;
564 } 593 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698