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 */ , | |
battre
2013/01/18 10:37:29
nit: additional space after */
Jeffrey Yasskin
2013/01/22 08:01:22
Done.
| |
98 std::string* /* error */, | |
99 bool* /* bad_message */); | |
100 std::map<std::string, FactoryMethod> factory_methods; | |
battre
2013/01/18 10:37:29
comment the std::string in the key represents?
Jeffrey Yasskin
2013/01/22 08:01:22
Done. Remember to do the same in webrequest_action
battre
2013/01/22 12:54:52
Done.
| |
101 | |
102 ContentActionFactory() { | |
103 factory_methods[keys::kShowPageAction] = | |
104 &CallConstructorFactoryMethod<ShowPageAction>; | |
105 } | |
106 }; | |
107 | |
108 base::LazyInstance<ContentActionFactory>::Leaky | |
109 g_content_action_factory = LAZY_INSTANCE_INITIALIZER; | |
110 | |
111 } // namespace | |
112 | |
113 // | |
114 // ContentAction | |
115 // | |
116 | |
117 ContentAction::ContentAction() {} | |
118 | |
119 ContentAction::~ContentAction() {} | |
120 | |
121 // static | |
122 scoped_ptr<ContentAction> ContentAction::Create( | |
123 const base::Value& json_action, | |
124 std::string* error, | |
125 bool* bad_message) { | |
126 *error = ""; | |
127 *bad_message = false; | |
128 | |
129 const base::DictionaryValue* action_dict = NULL; | |
130 INPUT_FORMAT_VALIDATE(json_action.GetAsDictionary(&action_dict)); | |
131 | |
132 std::string instance_type; | |
133 INPUT_FORMAT_VALIDATE( | |
134 action_dict->GetString(keys::kInstanceType, &instance_type)); | |
135 | |
136 ContentActionFactory& factory = g_content_action_factory.Get(); | |
137 std::map<std::string, ContentActionFactory::FactoryMethod>::iterator | |
138 factory_method_iter = factory.factory_methods.find(instance_type); | |
139 if (factory_method_iter != factory.factory_methods.end()) | |
140 return (*factory_method_iter->second)(action_dict, error, bad_message); | |
141 | |
142 *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str()); | |
143 return scoped_ptr<ContentAction>(); | |
144 } | |
145 | |
146 } // namespace extensions | |
OLD | NEW |