1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/extensions/extension_browser_actions_api.h" 6 7#include <string> 8 9#include "base/values.h" 10#include "chrome/browser/ui/browser_list.h" 11#include "chrome/common/extensions/extension.h" 12#include "chrome/common/render_messages.h" 13#include "content/common/notification_service.h" 14 15namespace { 16// Errors. 17const char kNoBrowserActionError[] = 18 "This extension has no browser action specified."; 19const char kIconIndexOutOfBounds[] = 20 "Browser action icon index out of bounds."; 21} 22 23bool BrowserActionFunction::RunImpl() { 24 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); 25 EXTENSION_FUNCTION_VALIDATE(details_ != NULL); 26 27 if (details_->HasKey("tabId")) 28 EXTENSION_FUNCTION_VALIDATE(details_->GetInteger("tabId", &tab_id_)); 29 30 const Extension* extension = GetExtension(); 31 browser_action_ = extension->browser_action(); 32 if (!browser_action_) { 33 error_ = kNoBrowserActionError; 34 return false; 35 } 36 37 if (!RunBrowserAction()) 38 return false; 39 40 NotificationService::current()->Notify( 41 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, 42 Source<ExtensionAction>(browser_action_), 43 NotificationService::NoDetails()); 44 return true; 45} 46 47bool BrowserActionSetIconFunction::RunBrowserAction() { 48 BinaryValue* binary = NULL; 49 EXTENSION_FUNCTION_VALIDATE(details_->GetBinary("imageData", &binary)); 50 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); 51 void* iter = NULL; 52 SkBitmap bitmap; 53 EXTENSION_FUNCTION_VALIDATE( 54 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap)); 55 browser_action_->SetIcon(tab_id_, bitmap); 56 return true; 57} 58 59bool BrowserActionSetTitleFunction::RunBrowserAction() { 60 std::string title; 61 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title)); 62 browser_action_->SetTitle(tab_id_, title); 63 return true; 64} 65 66bool BrowserActionSetPopupFunction::RunBrowserAction() { 67 std::string popup_string; 68 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string)); 69 70 GURL popup_url; 71 if (!popup_string.empty()) 72 popup_url = GetExtension()->GetResourceURL(popup_string); 73 74 browser_action_->SetPopupUrl(tab_id_, popup_url); 75 return true; 76} 77 78bool BrowserActionSetBadgeTextFunction::RunBrowserAction() { 79 std::string badge_text; 80 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text)); 81 browser_action_->SetBadgeText(tab_id_, badge_text); 82 return true; 83} 84 85bool BrowserActionSetBadgeBackgroundColorFunction::RunBrowserAction() { 86 ListValue* list = NULL; 87 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list)); 88 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); 89 90 int color_array[4] = {0}; 91 for (size_t i = 0; i < arraysize(color_array); ++i) { 92 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); 93 } 94 95 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], 96 color_array[2]); 97 browser_action_->SetBadgeBackgroundColor(tab_id_, color); 98 99 return true; 100} 101