| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/extension_browser_actions_api.h" | 5 #include "chrome/browser/extensions/extension_browser_actions_api.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/browser_list.h" | 8 #include "chrome/browser/browser_list.h" |
| 9 #include "chrome/common/notification_service.h" | 9 #include "chrome/common/notification_service.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 // Errors. | 13 // Errors. |
| 14 const char kNoBrowserActionError[] = | 14 const char kNoBrowserActionError[] = |
| 15 "This extension has no browser action specified."; | 15 "This extension has no browser action specified."; |
| 16 const char kIconIndexOutOfBounds[] = | 16 const char kIconIndexOutOfBounds[] = |
| 17 "Browser action icon index out of bounds."; | 17 "Browser action icon index out of bounds."; |
| 18 } | 18 } |
| 19 | 19 |
| 20 bool BrowserActionSetNameFunction::RunImpl() { | |
| 21 std::string title; | |
| 22 EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&title)); | |
| 23 | |
| 24 Extension* extension = dispatcher()->GetExtension(); | |
| 25 if (!extension->browser_action()) { | |
| 26 error_ = kNoBrowserActionError; | |
| 27 return false; | |
| 28 } | |
| 29 | |
| 30 extension->browser_action_state()->set_title(title); | |
| 31 | |
| 32 NotificationService::current()->Notify( | |
| 33 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | |
| 34 Source<ExtensionAction>(extension->browser_action()), | |
| 35 Details<ExtensionActionState>(extension->browser_action_state())); | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 bool BrowserActionSetIconFunction::RunImpl() { | 20 bool BrowserActionSetIconFunction::RunImpl() { |
| 40 // setIcon can take a variant argument: either a canvas ImageData, or an | 21 // setIcon can take a variant argument: either a canvas ImageData, or an |
| 41 // icon index. | 22 // icon index. |
| 42 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_BINARY) || | 23 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_BINARY) || |
| 43 args_->IsType(Value::TYPE_INTEGER)); | 24 args_->IsType(Value::TYPE_DICTIONARY)); |
| 44 | 25 |
| 45 Extension* extension = dispatcher()->GetExtension(); | 26 Extension* extension = dispatcher()->GetExtension(); |
| 46 if (!extension->browser_action()) { | 27 if (!extension->browser_action()) { |
| 47 error_ = kNoBrowserActionError; | 28 error_ = kNoBrowserActionError; |
| 48 return false; | 29 return false; |
| 49 } | 30 } |
| 50 | 31 |
| 51 if (args_->IsType(Value::TYPE_BINARY)) { | 32 if (args_->IsType(Value::TYPE_BINARY)) { |
| 52 BinaryValue* binary = static_cast<BinaryValue*>(args_); | 33 BinaryValue* binary = static_cast<BinaryValue*>(args_); |
| 53 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); | 34 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); |
| 54 void* iter = NULL; | 35 void* iter = NULL; |
| 55 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | 36 scoped_ptr<SkBitmap> bitmap(new SkBitmap); |
| 56 EXTENSION_FUNCTION_VALIDATE( | 37 EXTENSION_FUNCTION_VALIDATE( |
| 57 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); | 38 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); |
| 58 extension->browser_action_state()->set_icon(bitmap.release()); | 39 extension->browser_action_state()->set_icon(bitmap.release()); |
| 59 } else { | 40 } else { |
| 60 int icon_index = -1; | 41 int icon_index = -1; |
| 61 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&icon_index)); | 42 EXTENSION_FUNCTION_VALIDATE( |
| 62 | 43 static_cast<DictionaryValue*>(args_)->GetInteger( |
| 44 L"iconIndex", &icon_index)); |
| 63 if (icon_index < 0 || | 45 if (icon_index < 0 || |
| 64 static_cast<size_t>(icon_index) >= | 46 static_cast<size_t>(icon_index) >= |
| 65 extension->browser_action()->icon_paths().size()) { | 47 extension->browser_action()->icon_paths().size()) { |
| 66 error_ = kIconIndexOutOfBounds; | 48 error_ = kIconIndexOutOfBounds; |
| 67 return false; | 49 return false; |
| 68 } | 50 } |
| 69 extension->browser_action_state()->set_icon_index(icon_index); | 51 extension->browser_action_state()->set_icon_index(icon_index); |
| 70 extension->browser_action_state()->set_icon(NULL); | 52 extension->browser_action_state()->set_icon(NULL); |
| 71 } | 53 } |
| 72 | 54 |
| 73 NotificationService::current()->Notify( | 55 NotificationService::current()->Notify( |
| 74 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 56 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 75 Source<ExtensionAction>(extension->browser_action()), | 57 Source<ExtensionAction>(extension->browser_action()), |
| 76 Details<ExtensionActionState>(extension->browser_action_state())); | 58 Details<ExtensionActionState>(extension->browser_action_state())); |
| 77 return true; | 59 return true; |
| 78 } | 60 } |
| 79 | 61 |
| 62 bool BrowserActionSetTitleFunction::RunImpl() { |
| 63 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 64 DictionaryValue* details = static_cast<DictionaryValue*>(args_); |
| 65 |
| 66 std::string title; |
| 67 EXTENSION_FUNCTION_VALIDATE(details->GetString(L"title", &title)); |
| 68 |
| 69 Extension* extension = dispatcher()->GetExtension(); |
| 70 if (!extension->browser_action()) { |
| 71 error_ = kNoBrowserActionError; |
| 72 return false; |
| 73 } |
| 74 |
| 75 extension->browser_action_state()->set_title(title); |
| 76 |
| 77 NotificationService::current()->Notify( |
| 78 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 79 Source<ExtensionAction>(extension->browser_action()), |
| 80 Details<ExtensionActionState>(extension->browser_action_state())); |
| 81 return true; |
| 82 } |
| 83 |
| 80 bool BrowserActionSetBadgeTextFunction::RunImpl() { | 84 bool BrowserActionSetBadgeTextFunction::RunImpl() { |
| 85 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 86 DictionaryValue* details = static_cast<DictionaryValue*>(args_); |
| 87 |
| 81 std::string badge_text; | 88 std::string badge_text; |
| 82 EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&badge_text)); | 89 EXTENSION_FUNCTION_VALIDATE(details->GetString(L"text", &badge_text)); |
| 83 | 90 |
| 84 Extension* extension = dispatcher()->GetExtension(); | 91 Extension* extension = dispatcher()->GetExtension(); |
| 85 if (!extension->browser_action()) { | 92 if (!extension->browser_action()) { |
| 86 error_ = kNoBrowserActionError; | 93 error_ = kNoBrowserActionError; |
| 87 return false; | 94 return false; |
| 88 } | 95 } |
| 89 | 96 |
| 90 extension->browser_action_state()->set_badge_text(badge_text); | 97 extension->browser_action_state()->set_badge_text(badge_text); |
| 91 | 98 |
| 92 NotificationService::current()->Notify( | 99 NotificationService::current()->Notify( |
| 93 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 100 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 94 Source<ExtensionAction>(extension->browser_action()), | 101 Source<ExtensionAction>(extension->browser_action()), |
| 95 Details<ExtensionActionState>(extension->browser_action_state())); | 102 Details<ExtensionActionState>(extension->browser_action_state())); |
| 96 return true; | 103 return true; |
| 97 } | 104 } |
| 98 | 105 |
| 99 bool BrowserActionSetBadgeBackgroundColorFunction::RunImpl() { | 106 bool BrowserActionSetBadgeBackgroundColorFunction::RunImpl() { |
| 100 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 107 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 101 ListValue* list = static_cast<ListValue*>(args_); | 108 DictionaryValue* details = static_cast<DictionaryValue*>(args_); |
| 109 |
| 110 ListValue* list = NULL; |
| 111 EXTENSION_FUNCTION_VALIDATE(details->GetList(L"color", &list)); |
| 102 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); | 112 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); |
| 103 | 113 |
| 104 int color_array[4] = {0}; | 114 int color_array[4] = {0}; |
| 105 for (size_t i = 0; i < arraysize(color_array); ++i) { | 115 for (size_t i = 0; i < arraysize(color_array); ++i) { |
| 106 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); | 116 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); |
| 107 } | 117 } |
| 108 | 118 |
| 109 SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], | 119 SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2], |
| 110 color_array[3]); | 120 color_array[3]); |
| 111 | 121 |
| 112 Extension* extension = dispatcher()->GetExtension(); | 122 Extension* extension = dispatcher()->GetExtension(); |
| 113 if (!extension->browser_action()) { | 123 if (!extension->browser_action()) { |
| 114 error_ = kNoBrowserActionError; | 124 error_ = kNoBrowserActionError; |
| 115 return false; | 125 return false; |
| 116 } | 126 } |
| 117 | 127 |
| 118 extension->browser_action_state()->set_badge_background_color(color); | 128 extension->browser_action_state()->set_badge_background_color(color); |
| 119 | 129 |
| 120 NotificationService::current()->Notify( | 130 NotificationService::current()->Notify( |
| 121 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 131 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 122 Source<ExtensionAction>(extension->browser_action()), | 132 Source<ExtensionAction>(extension->browser_action()), |
| 123 Details<ExtensionActionState>(extension->browser_action_state())); | 133 Details<ExtensionActionState>(extension->browser_action_state())); |
| 124 return true; | 134 return true; |
| 125 } | 135 } |
| OLD | NEW |