| 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/declarative_content/content_action.h" | 5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 bool* bad_message) { | 67 bool* bad_message) { |
| 68 // We can't show a page action if the extension doesn't have one. | 68 // We can't show a page action if the extension doesn't have one. |
| 69 if (ActionInfo::GetPageActionInfo(extension) == NULL) { | 69 if (ActionInfo::GetPageActionInfo(extension) == NULL) { |
| 70 *error = kNoPageAction; | 70 *error = kNoPageAction; |
| 71 return scoped_refptr<ContentAction>(); | 71 return scoped_refptr<ContentAction>(); |
| 72 } | 72 } |
| 73 return scoped_refptr<ContentAction>(new ShowPageAction); | 73 return scoped_refptr<ContentAction>(new ShowPageAction); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Implementation of ContentAction: | 76 // Implementation of ContentAction: |
| 77 virtual Type GetType() const override { return ACTION_SHOW_PAGE_ACTION; } | 77 Type GetType() const override { return ACTION_SHOW_PAGE_ACTION; } |
| 78 virtual void Apply(const std::string& extension_id, | 78 void Apply(const std::string& extension_id, |
| 79 const base::Time& extension_install_time, | 79 const base::Time& extension_install_time, |
| 80 ApplyInfo* apply_info) const override { | 80 ApplyInfo* apply_info) const override { |
| 81 ExtensionAction* action = | 81 ExtensionAction* action = |
| 82 GetPageAction(apply_info->browser_context, extension_id); | 82 GetPageAction(apply_info->browser_context, extension_id); |
| 83 action->DeclarativeShow(ExtensionTabUtil::GetTabId(apply_info->tab)); | 83 action->DeclarativeShow(ExtensionTabUtil::GetTabId(apply_info->tab)); |
| 84 ExtensionActionAPI::Get(apply_info->browser_context)->NotifyChange( | 84 ExtensionActionAPI::Get(apply_info->browser_context)->NotifyChange( |
| 85 action, apply_info->tab, apply_info->browser_context); | 85 action, apply_info->tab, apply_info->browser_context); |
| 86 } | 86 } |
| 87 // The page action is already showing, so nothing needs to be done here. | 87 // The page action is already showing, so nothing needs to be done here. |
| 88 virtual void Reapply(const std::string& extension_id, | 88 void Reapply(const std::string& extension_id, |
| 89 const base::Time& extension_install_time, | 89 const base::Time& extension_install_time, |
| 90 ApplyInfo* apply_info) const override {} | 90 ApplyInfo* apply_info) const override {} |
| 91 virtual void Revert(const std::string& extension_id, | 91 void Revert(const std::string& extension_id, |
| 92 const base::Time& extension_install_time, | 92 const base::Time& extension_install_time, |
| 93 ApplyInfo* apply_info) const override { | 93 ApplyInfo* apply_info) const override { |
| 94 if (ExtensionAction* action = | 94 if (ExtensionAction* action = |
| 95 GetPageAction(apply_info->browser_context, extension_id)) { | 95 GetPageAction(apply_info->browser_context, extension_id)) { |
| 96 action->UndoDeclarativeShow(ExtensionTabUtil::GetTabId(apply_info->tab)); | 96 action->UndoDeclarativeShow(ExtensionTabUtil::GetTabId(apply_info->tab)); |
| 97 ExtensionActionAPI::Get(apply_info->browser_context)->NotifyChange( | 97 ExtensionActionAPI::Get(apply_info->browser_context)->NotifyChange( |
| 98 action, apply_info->tab, apply_info->browser_context); | 98 action, apply_info->tab, apply_info->browser_context); |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 static ExtensionAction* GetPageAction( | 103 static ExtensionAction* GetPageAction( |
| 104 content::BrowserContext* browser_context, | 104 content::BrowserContext* browser_context, |
| 105 const std::string& extension_id) { | 105 const std::string& extension_id) { |
| 106 const Extension* extension = | 106 const Extension* extension = |
| 107 ExtensionRegistry::Get(browser_context) | 107 ExtensionRegistry::Get(browser_context) |
| 108 ->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); | 108 ->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); |
| 109 if (!extension) | 109 if (!extension) |
| 110 return NULL; | 110 return NULL; |
| 111 return ExtensionActionManager::Get(browser_context) | 111 return ExtensionActionManager::Get(browser_context) |
| 112 ->GetPageAction(*extension); | 112 ->GetPageAction(*extension); |
| 113 } | 113 } |
| 114 virtual ~ShowPageAction() {} | 114 ~ShowPageAction() override {} |
| 115 | 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(ShowPageAction); | 116 DISALLOW_COPY_AND_ASSIGN(ShowPageAction); |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 // Action that sets an extension's action icon. | 119 // Action that sets an extension's action icon. |
| 120 class SetIcon : public ContentAction { | 120 class SetIcon : public ContentAction { |
| 121 public: | 121 public: |
| 122 SetIcon(const gfx::Image& icon, ActionInfo::Type action_type) | 122 SetIcon(const gfx::Image& icon, ActionInfo::Type action_type) |
| 123 : icon_(icon), action_type_(action_type) {} | 123 : icon_(icon), action_type_(action_type) {} |
| 124 | 124 |
| 125 static scoped_refptr<ContentAction> Create( | 125 static scoped_refptr<ContentAction> Create( |
| 126 content::BrowserContext* browser_context, | 126 content::BrowserContext* browser_context, |
| 127 const Extension* extension, | 127 const Extension* extension, |
| 128 const base::DictionaryValue* dict, | 128 const base::DictionaryValue* dict, |
| 129 std::string* error, | 129 std::string* error, |
| 130 bool* bad_message); | 130 bool* bad_message); |
| 131 | 131 |
| 132 // Implementation of ContentAction: | 132 // Implementation of ContentAction: |
| 133 virtual Type GetType() const override { return ACTION_SET_ICON; } | 133 Type GetType() const override { return ACTION_SET_ICON; } |
| 134 virtual void Apply(const std::string& extension_id, | 134 void Apply(const std::string& extension_id, |
| 135 const base::Time& extension_install_time, | 135 const base::Time& extension_install_time, |
| 136 ApplyInfo* apply_info) const override { | 136 ApplyInfo* apply_info) const override { |
| 137 Profile* profile = Profile::FromBrowserContext(apply_info->browser_context); | 137 Profile* profile = Profile::FromBrowserContext(apply_info->browser_context); |
| 138 ExtensionAction* action = GetExtensionAction(profile, extension_id); | 138 ExtensionAction* action = GetExtensionAction(profile, extension_id); |
| 139 if (action) { | 139 if (action) { |
| 140 action->DeclarativeSetIcon(ExtensionTabUtil::GetTabId(apply_info->tab), | 140 action->DeclarativeSetIcon(ExtensionTabUtil::GetTabId(apply_info->tab), |
| 141 apply_info->priority, | 141 apply_info->priority, |
| 142 icon_); | 142 icon_); |
| 143 ExtensionActionAPI::Get(profile) | 143 ExtensionActionAPI::Get(profile) |
| 144 ->NotifyChange(action, apply_info->tab, profile); | 144 ->NotifyChange(action, apply_info->tab, profile); |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 virtual void Reapply(const std::string& extension_id, | 148 void Reapply(const std::string& extension_id, |
| 149 const base::Time& extension_install_time, | 149 const base::Time& extension_install_time, |
| 150 ApplyInfo* apply_info) const override {} | 150 ApplyInfo* apply_info) const override {} |
| 151 | 151 |
| 152 virtual void Revert(const std::string& extension_id, | 152 void Revert(const std::string& extension_id, |
| 153 const base::Time& extension_install_time, | 153 const base::Time& extension_install_time, |
| 154 ApplyInfo* apply_info) const override { | 154 ApplyInfo* apply_info) const override { |
| 155 Profile* profile = Profile::FromBrowserContext(apply_info->browser_context); | 155 Profile* profile = Profile::FromBrowserContext(apply_info->browser_context); |
| 156 ExtensionAction* action = GetExtensionAction(profile, extension_id); | 156 ExtensionAction* action = GetExtensionAction(profile, extension_id); |
| 157 if (action) { | 157 if (action) { |
| 158 action->UndoDeclarativeSetIcon( | 158 action->UndoDeclarativeSetIcon( |
| 159 ExtensionTabUtil::GetTabId(apply_info->tab), | 159 ExtensionTabUtil::GetTabId(apply_info->tab), |
| 160 apply_info->priority, | 160 apply_info->priority, |
| 161 icon_); | 161 icon_); |
| 162 ExtensionActionAPI::Get(apply_info->browser_context) | 162 ExtensionActionAPI::Get(apply_info->browser_context) |
| 163 ->NotifyChange(action, apply_info->tab, profile); | 163 ->NotifyChange(action, apply_info->tab, profile); |
| 164 } | 164 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 176 case ActionInfo::TYPE_BROWSER: | 176 case ActionInfo::TYPE_BROWSER: |
| 177 return ExtensionActionManager::Get(profile) | 177 return ExtensionActionManager::Get(profile) |
| 178 ->GetBrowserAction(*extension); | 178 ->GetBrowserAction(*extension); |
| 179 case ActionInfo::TYPE_PAGE: | 179 case ActionInfo::TYPE_PAGE: |
| 180 return ExtensionActionManager::Get(profile)->GetPageAction(*extension); | 180 return ExtensionActionManager::Get(profile)->GetPageAction(*extension); |
| 181 default: | 181 default: |
| 182 NOTREACHED(); | 182 NOTREACHED(); |
| 183 } | 183 } |
| 184 return NULL; | 184 return NULL; |
| 185 } | 185 } |
| 186 virtual ~SetIcon() {} | 186 ~SetIcon() override {} |
| 187 | 187 |
| 188 gfx::Image icon_; | 188 gfx::Image icon_; |
| 189 ActionInfo::Type action_type_; | 189 ActionInfo::Type action_type_; |
| 190 | 190 |
| 191 DISALLOW_COPY_AND_ASSIGN(SetIcon); | 191 DISALLOW_COPY_AND_ASSIGN(SetIcon); |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 // Helper for getting JS collections into C++. | 194 // Helper for getting JS collections into C++. |
| 195 static bool AppendJSStringsToCPPStrings(const base::ListValue& append_strings, | 195 static bool AppendJSStringsToCPPStrings(const base::ListValue& append_strings, |
| 196 std::vector<std::string>* append_to) { | 196 std::vector<std::string>* append_to) { |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 bool* bad_message, | 495 bool* bad_message, |
| 496 const base::DictionaryValue** action_dict, | 496 const base::DictionaryValue** action_dict, |
| 497 std::string* instance_type) { | 497 std::string* instance_type) { |
| 498 INPUT_FORMAT_VALIDATE(json_action.GetAsDictionary(action_dict)); | 498 INPUT_FORMAT_VALIDATE(json_action.GetAsDictionary(action_dict)); |
| 499 INPUT_FORMAT_VALIDATE( | 499 INPUT_FORMAT_VALIDATE( |
| 500 (*action_dict)->GetString(keys::kInstanceType, instance_type)); | 500 (*action_dict)->GetString(keys::kInstanceType, instance_type)); |
| 501 return true; | 501 return true; |
| 502 } | 502 } |
| 503 | 503 |
| 504 } // namespace extensions | 504 } // namespace extensions |
| OLD | NEW |