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