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 BrowserActionSetIconFunction::RunImpl() { | 20 bool BrowserActionSetIconFunction::RunImpl() { |
21 // setIcon can take a variant argument: either a canvas ImageData, or an | 21 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
22 // icon index. | 22 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); |
23 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_BINARY) || | |
24 args_->IsType(Value::TYPE_DICTIONARY)); | |
25 | 23 |
26 Extension* extension = dispatcher()->GetExtension(); | 24 Extension* extension = dispatcher()->GetExtension(); |
27 if (!extension->browser_action()) { | 25 if (!extension->browser_action()) { |
28 error_ = kNoBrowserActionError; | 26 error_ = kNoBrowserActionError; |
29 return false; | 27 return false; |
30 } | 28 } |
31 | 29 |
32 if (args_->IsType(Value::TYPE_BINARY)) { | 30 // setIcon can take a variant argument: either a canvas ImageData, or an |
33 BinaryValue* binary = static_cast<BinaryValue*>(args_); | 31 // icon index. |
| 32 BinaryValue* binary; |
| 33 int icon_index; |
| 34 if (args->GetBinary(L"imageData", &binary)) { |
34 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); | 35 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); |
35 void* iter = NULL; | 36 void* iter = NULL; |
36 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | 37 scoped_ptr<SkBitmap> bitmap(new SkBitmap); |
37 EXTENSION_FUNCTION_VALIDATE( | 38 EXTENSION_FUNCTION_VALIDATE( |
38 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); | 39 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); |
39 extension->browser_action_state()->set_icon(bitmap.release()); | 40 extension->browser_action_state()->set_icon(bitmap.release()); |
40 } else { | 41 } else if (args->GetInteger(L"iconIndex", &icon_index)) { |
41 int icon_index = -1; | 42 if (icon_index < 0 || static_cast<size_t>(icon_index) >= |
42 EXTENSION_FUNCTION_VALIDATE( | |
43 static_cast<DictionaryValue*>(args_)->GetInteger( | |
44 L"iconIndex", &icon_index)); | |
45 | 43 |
46 if (icon_index < 0 || | |
47 static_cast<size_t>(icon_index) >= | |
48 extension->browser_action()->icon_paths().size()) { | 44 extension->browser_action()->icon_paths().size()) { |
49 error_ = kIconIndexOutOfBounds; | 45 error_ = kIconIndexOutOfBounds; |
50 return false; | 46 return false; |
51 } | 47 } |
52 extension->browser_action_state()->set_icon_index(icon_index); | 48 extension->browser_action_state()->set_icon_index(icon_index); |
53 extension->browser_action_state()->set_icon(NULL); | 49 extension->browser_action_state()->set_icon(NULL); |
| 50 } else { |
| 51 EXTENSION_FUNCTION_VALIDATE(false); |
54 } | 52 } |
55 | 53 |
56 NotificationService::current()->Notify( | 54 NotificationService::current()->Notify( |
57 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 55 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
58 Source<ExtensionAction>(extension->browser_action()), | 56 Source<ExtensionAction>(extension->browser_action()), |
59 Details<ExtensionActionState>(extension->browser_action_state())); | 57 Details<ExtensionActionState>(extension->browser_action_state())); |
60 return true; | 58 return true; |
61 } | 59 } |
62 | 60 |
63 bool BrowserActionSetTitleFunction::RunImpl() { | 61 bool BrowserActionSetTitleFunction::RunImpl() { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 } | 125 } |
128 | 126 |
129 extension->browser_action_state()->set_badge_background_color(color); | 127 extension->browser_action_state()->set_badge_background_color(color); |
130 | 128 |
131 NotificationService::current()->Notify( | 129 NotificationService::current()->Notify( |
132 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 130 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
133 Source<ExtensionAction>(extension->browser_action()), | 131 Source<ExtensionAction>(extension->browser_action()), |
134 Details<ExtensionActionState>(extension->browser_action_state())); | 132 Details<ExtensionActionState>(extension->browser_action_state())); |
135 return true; | 133 return true; |
136 } | 134 } |
OLD | NEW |