| OLD | NEW |
| 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_page_actions_
api.h" | 5 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 namespace { | 27 namespace { |
| 28 // Errors. | 28 // Errors. |
| 29 const char kNoTabError[] = "No tab with id: *."; | 29 const char kNoTabError[] = "No tab with id: *."; |
| 30 const char kNoPageActionError[] = | 30 const char kNoPageActionError[] = |
| 31 "This extension has no page action specified."; | 31 "This extension has no page action specified."; |
| 32 const char kUrlNotActiveError[] = "This url is no longer active: *."; | 32 const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| 33 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; | 33 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; |
| 34 const char kNoIconSpecified[] = "Page action has no icons to show."; | 34 const char kNoIconSpecified[] = "Page action has no icons to show."; |
| 35 } | 35 } |
| 36 | 36 |
| 37 PageActionsFunction::PageActionsFunction() { |
| 38 } |
| 39 |
| 40 PageActionsFunction::~PageActionsFunction() { |
| 41 } |
| 42 |
| 37 bool PageActionFunction::RunImpl() { | 43 bool PageActionFunction::RunImpl() { |
| 38 ExtensionActionFunction::RunImpl(); | 44 ExtensionActionFunction::RunImpl(); |
| 39 | 45 |
| 40 // If the first argument is an integer, it is the tabId. | 46 // If the first argument is an integer, it is the tabId. |
| 41 base::Value* arg = NULL; | 47 base::Value* arg = NULL; |
| 42 args_->Get(0, &arg); | 48 args_->Get(0, &arg); |
| 43 if (arg->IsType(base::Value::TYPE_INTEGER)) | 49 if (arg->IsType(base::Value::TYPE_INTEGER)) |
| 44 arg->GetAsInteger(&tab_id_); | 50 arg->GetAsInteger(&tab_id_); |
| 45 | 51 |
| 46 extension_action_ = GetExtension()->page_action(); | 52 extension_action_ = GetExtension()->page_action(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 60 return false; | 66 return false; |
| 61 } | 67 } |
| 62 contents_ = wrapper; | 68 contents_ = wrapper; |
| 63 | 69 |
| 64 if (!RunExtensionAction()) | 70 if (!RunExtensionAction()) |
| 65 return false; | 71 return false; |
| 66 | 72 |
| 67 return true; | 73 return true; |
| 68 } | 74 } |
| 69 | 75 |
| 70 bool PageActionFunction::SetPageActionEnabled(bool enable) { | 76 bool PageActionsFunction::SetPageActionEnabled(bool enable) { |
| 71 std::string extension_action_id; | 77 std::string extension_action_id; |
| 72 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); | 78 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); |
| 73 DictionaryValue* action = NULL; | 79 DictionaryValue* action = NULL; |
| 74 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); | 80 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); |
| 75 | 81 |
| 82 int tab_id; |
| 83 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 76 std::string url; | 84 std::string url; |
| 77 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | 85 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| 78 | 86 |
| 79 std::string title; | 87 std::string title; |
| 80 int icon_id = 0; | 88 int icon_id = 0; |
| 81 if (enable) { | 89 if (enable) { |
| 82 // Both of those are optional. | 90 // Both of those are optional. |
| 83 if (action->HasKey(keys::kTitleKey)) | 91 if (action->HasKey(keys::kTitleKey)) |
| 84 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); | 92 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); |
| 85 if (action->HasKey(keys::kIconIdKey)) { | 93 if (action->HasKey(keys::kIconIdKey)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 96 | 104 |
| 97 if (icon_id < 0 || | 105 if (icon_id < 0 || |
| 98 static_cast<size_t>(icon_id) >= page_action->icon_paths()->size()) { | 106 static_cast<size_t>(icon_id) >= page_action->icon_paths()->size()) { |
| 99 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds; | 107 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds; |
| 100 return false; | 108 return false; |
| 101 } | 109 } |
| 102 | 110 |
| 103 // Find the TabContents that contains this tab id. | 111 // Find the TabContents that contains this tab id. |
| 104 TabContentsWrapper* contents = NULL; | 112 TabContentsWrapper* contents = NULL; |
| 105 bool result = ExtensionTabUtil::GetTabById( | 113 bool result = ExtensionTabUtil::GetTabById( |
| 106 tab_id_, profile(), include_incognito(), NULL, NULL, &contents, NULL); | 114 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); |
| 107 if (!result || !contents) { | 115 if (!result || !contents) { |
| 108 error_ = ExtensionErrorUtils::FormatErrorMessage( | 116 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 109 kNoTabError, base::IntToString(tab_id_)); | 117 kNoTabError, base::IntToString(tab_id)); |
| 110 return false; | 118 return false; |
| 111 } | 119 } |
| 112 | 120 |
| 113 // Make sure the URL hasn't changed. | 121 // Make sure the URL hasn't changed. |
| 114 NavigationEntry* entry = | 122 NavigationEntry* entry = |
| 115 contents->web_contents()->GetController().GetActiveEntry(); | 123 contents->web_contents()->GetController().GetActiveEntry(); |
| 116 if (!entry || url != entry->GetURL().spec()) { | 124 if (!entry || url != entry->GetURL().spec()) { |
| 117 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); | 125 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); |
| 118 return false; | 126 return false; |
| 119 } | 127 } |
| 120 | 128 |
| 121 // Set visibility and broadcast notifications that the UI should be updated. | 129 // Set visibility and broadcast notifications that the UI should be updated. |
| 122 page_action->SetIsVisible(tab_id_, enable); | 130 page_action->SetIsVisible(tab_id, enable); |
| 123 page_action->SetTitle(tab_id_, title); | 131 page_action->SetTitle(tab_id, title); |
| 124 page_action->SetIconIndex(tab_id_, icon_id); | 132 page_action->SetIconIndex(tab_id, icon_id); |
| 125 contents->extension_tab_helper()->PageActionStateChanged(); | 133 contents->extension_tab_helper()->PageActionStateChanged(); |
| 126 | 134 |
| 127 return true; | 135 return true; |
| 128 } | 136 } |
| 129 | 137 |
| 130 bool PageActionFunction::SetVisible(bool visible) { | 138 bool PageActionFunction::SetVisible(bool visible) { |
| 131 extension_action_->SetIsVisible(tab_id_, visible); | 139 extension_action_->SetIsVisible(tab_id_, visible); |
| 132 contents_->extension_tab_helper()->PageActionStateChanged(); | 140 contents_->extension_tab_helper()->PageActionStateChanged(); |
| 133 return true; | 141 return true; |
| 134 } | 142 } |
| 135 | 143 |
| 136 bool EnablePageActionFunction::RunExtensionAction() { | 144 bool EnablePageActionsFunction::RunImpl() { |
| 137 return SetPageActionEnabled(true); | 145 return SetPageActionEnabled(true); |
| 138 } | 146 } |
| 139 | 147 |
| 140 bool DisablePageActionFunction::RunExtensionAction() { | 148 bool DisablePageActionsFunction::RunImpl() { |
| 141 return SetPageActionEnabled(false); | 149 return SetPageActionEnabled(false); |
| 142 } | 150 } |
| 143 | 151 |
| 144 bool PageActionShowFunction::RunExtensionAction() { | 152 bool PageActionShowFunction::RunExtensionAction() { |
| 145 return SetVisible(true); | 153 return SetVisible(true); |
| 146 } | 154 } |
| 147 | 155 |
| 148 bool PageActionHideFunction::RunExtensionAction() { | 156 bool PageActionHideFunction::RunExtensionAction() { |
| 149 return SetVisible(false); | 157 return SetVisible(false); |
| 150 } | 158 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 // extension_function_dispatcher. | 243 // extension_function_dispatcher. |
| 236 bool PageActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | 244 bool PageActionGetBadgeBackgroundColorFunction::RunExtensionAction() { |
| 237 return GetBadgeBackgroundColor(); | 245 return GetBadgeBackgroundColor(); |
| 238 } | 246 } |
| 239 | 247 |
| 240 // Not currently exposed to extensions. To re-enable, add mapping in | 248 // Not currently exposed to extensions. To re-enable, add mapping in |
| 241 // extension_function_dispatcher. | 249 // extension_function_dispatcher. |
| 242 bool PageActionGetBadgeTextFunction::RunExtensionAction() { | 250 bool PageActionGetBadgeTextFunction::RunExtensionAction() { |
| 243 return GetBadgeText(); | 251 return GetBadgeText(); |
| 244 } | 252 } |
| OLD | NEW |