OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/stringprintf.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" |
| 13 #include "chrome/browser/extensions/extension_action.h" |
| 14 #include "chrome/browser/extensions/extension_action_manager.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "content/public/browser/invalidate_type.h" |
| 20 #include "content/public/browser/web_contents.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 namespace keys = declarative_content_constants; |
| 25 |
| 26 namespace { |
| 27 // Error messages. |
| 28 const char kInvalidInstanceTypeError[] = |
| 29 "An action has an invalid instanceType: %s"; |
| 30 |
| 31 #define INPUT_FORMAT_VALIDATE(test) do { \ |
| 32 if (!(test)) { \ |
| 33 *bad_message = true; \ |
| 34 return scoped_ptr<ContentAction>(NULL); \ |
| 35 } \ |
| 36 } while (0) |
| 37 |
| 38 // |
| 39 // The following are concrete actions. |
| 40 // |
| 41 |
| 42 // Action that instructs to show an extension's page action. |
| 43 class ShowPageAction : public ContentAction { |
| 44 public: |
| 45 ShowPageAction() {} |
| 46 virtual ~ShowPageAction() {} |
| 47 |
| 48 // Implementation of ContentAction: |
| 49 virtual Type GetType() const OVERRIDE { return ACTION_SHOW_PAGE_ACTION; } |
| 50 virtual void Apply(const std::string& extension_id, |
| 51 const base::Time& extension_install_time, |
| 52 ApplyInfo* apply_info) const OVERRIDE { |
| 53 GetPageAction(apply_info->profile, extension_id)->DeclarativeShow( |
| 54 ExtensionTabUtil::GetTabId(apply_info->tab)); |
| 55 apply_info->tab->NotifyNavigationStateChanged( |
| 56 content::INVALIDATE_TYPE_PAGE_ACTIONS); |
| 57 } |
| 58 virtual void Revert(const std::string& extension_id, |
| 59 const base::Time& extension_install_time, |
| 60 ApplyInfo* apply_info) const OVERRIDE { |
| 61 GetPageAction(apply_info->profile, extension_id)-> |
| 62 UndoDeclarativeShow(ExtensionTabUtil::GetTabId(apply_info->tab)); |
| 63 apply_info->tab->NotifyNavigationStateChanged( |
| 64 content::INVALIDATE_TYPE_PAGE_ACTIONS); |
| 65 } |
| 66 |
| 67 private: |
| 68 static ExtensionAction* GetPageAction(Profile* profile, |
| 69 const std::string& extension_id) { |
| 70 ExtensionService* service = |
| 71 ExtensionSystem::Get(profile)->extension_service(); |
| 72 const Extension* extension = service->GetInstalledExtension(extension_id); |
| 73 DCHECK(extension); |
| 74 return ExtensionActionManager::Get(profile)->GetPageAction(*extension); |
| 75 } |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(ShowPageAction); |
| 78 }; |
| 79 |
| 80 // Helper function for ContentActions that can be instantiated by just |
| 81 // calling the constructor. |
| 82 template <class T> |
| 83 scoped_ptr<ContentAction> CallConstructorFactoryMethod( |
| 84 const base::DictionaryValue* dict, |
| 85 std::string* error, |
| 86 bool* bad_message) { |
| 87 return scoped_ptr<ContentAction>(new T); |
| 88 } |
| 89 |
| 90 struct ContentActionFactory { |
| 91 // Factory methods for ContentAction instances. |dict| contains the json |
| 92 // dictionary that describes the action. |error| is used to return error |
| 93 // messages in case the extension passed an action that was syntactically |
| 94 // correct but semantically incorrect. |bad_message| is set to true in case |
| 95 // |dict| does not confirm to the validated JSON specification. |
| 96 typedef scoped_ptr<ContentAction> |
| 97 (* FactoryMethod)(const base::DictionaryValue* /* dict */, |
| 98 std::string* /* error */, |
| 99 bool* /* bad_message */); |
| 100 // Maps the name of a declarativeContent action type to the factory |
| 101 // function creating it. |
| 102 std::map<std::string, FactoryMethod> factory_methods; |
| 103 |
| 104 ContentActionFactory() { |
| 105 factory_methods[keys::kShowPageAction] = |
| 106 &CallConstructorFactoryMethod<ShowPageAction>; |
| 107 } |
| 108 }; |
| 109 |
| 110 base::LazyInstance<ContentActionFactory>::Leaky |
| 111 g_content_action_factory = LAZY_INSTANCE_INITIALIZER; |
| 112 |
| 113 } // namespace |
| 114 |
| 115 // |
| 116 // ContentAction |
| 117 // |
| 118 |
| 119 ContentAction::ContentAction() {} |
| 120 |
| 121 ContentAction::~ContentAction() {} |
| 122 |
| 123 // static |
| 124 scoped_ptr<ContentAction> ContentAction::Create( |
| 125 const base::Value& json_action, |
| 126 std::string* error, |
| 127 bool* bad_message) { |
| 128 *error = ""; |
| 129 *bad_message = false; |
| 130 |
| 131 const base::DictionaryValue* action_dict = NULL; |
| 132 INPUT_FORMAT_VALIDATE(json_action.GetAsDictionary(&action_dict)); |
| 133 |
| 134 std::string instance_type; |
| 135 INPUT_FORMAT_VALIDATE( |
| 136 action_dict->GetString(keys::kInstanceType, &instance_type)); |
| 137 |
| 138 ContentActionFactory& factory = g_content_action_factory.Get(); |
| 139 std::map<std::string, ContentActionFactory::FactoryMethod>::iterator |
| 140 factory_method_iter = factory.factory_methods.find(instance_type); |
| 141 if (factory_method_iter != factory.factory_methods.end()) |
| 142 return (*factory_method_iter->second)(action_dict, error, bad_message); |
| 143 |
| 144 *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str()); |
| 145 return scoped_ptr<ContentAction>(); |
| 146 } |
| 147 |
| 148 } // namespace extensions |
OLD | NEW |