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/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" | 14 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" |
14 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 15 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
15 #include "chrome/browser/extensions/extension_action.h" | 16 #include "chrome/browser/extensions/extension_action.h" |
16 #include "chrome/browser/extensions/extension_action_manager.h" | 17 #include "chrome/browser/extensions/extension_action_manager.h" |
17 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/sessions/session_tab_helper.h" | 20 #include "chrome/browser/sessions/session_tab_helper.h" |
20 #include "content/public/browser/invalidate_type.h" | 21 #include "content/public/browser/invalidate_type.h" |
(...skipping 26 matching lines...) Expand all Loading... |
47 // | 48 // |
48 // The following are concrete actions. | 49 // The following are concrete actions. |
49 // | 50 // |
50 | 51 |
51 // Action that instructs to show an extension's page action. | 52 // Action that instructs to show an extension's page action. |
52 class ShowPageAction : public ContentAction { | 53 class ShowPageAction : public ContentAction { |
53 public: | 54 public: |
54 ShowPageAction() {} | 55 ShowPageAction() {} |
55 ~ShowPageAction() override {} | 56 ~ShowPageAction() override {} |
56 | 57 |
57 static scoped_ptr<ContentAction> Create( | 58 static std::unique_ptr<ContentAction> Create( |
58 content::BrowserContext* browser_context, | 59 content::BrowserContext* browser_context, |
59 const Extension* extension, | 60 const Extension* extension, |
60 const base::DictionaryValue* dict, | 61 const base::DictionaryValue* dict, |
61 std::string* error) { | 62 std::string* error) { |
62 // We can't show a page action if the extension doesn't have one. | 63 // We can't show a page action if the extension doesn't have one. |
63 if (ActionInfo::GetPageActionInfo(extension) == NULL) { | 64 if (ActionInfo::GetPageActionInfo(extension) == NULL) { |
64 *error = kNoPageAction; | 65 *error = kNoPageAction; |
65 return scoped_ptr<ContentAction>(); | 66 return std::unique_ptr<ContentAction>(); |
66 } | 67 } |
67 return make_scoped_ptr(new ShowPageAction); | 68 return base::WrapUnique(new ShowPageAction); |
68 } | 69 } |
69 | 70 |
70 // Implementation of ContentAction: | 71 // Implementation of ContentAction: |
71 void Apply(const ApplyInfo& apply_info) const override { | 72 void Apply(const ApplyInfo& apply_info) const override { |
72 ExtensionAction* action = | 73 ExtensionAction* action = |
73 GetPageAction(apply_info.browser_context, apply_info.extension); | 74 GetPageAction(apply_info.browser_context, apply_info.extension); |
74 action->DeclarativeShow(ExtensionTabUtil::GetTabId(apply_info.tab)); | 75 action->DeclarativeShow(ExtensionTabUtil::GetTabId(apply_info.tab)); |
75 ExtensionActionAPI::Get(apply_info.browser_context)->NotifyChange( | 76 ExtensionActionAPI::Get(apply_info.browser_context)->NotifyChange( |
76 action, apply_info.tab, apply_info.browser_context); | 77 action, apply_info.tab, apply_info.browser_context); |
77 } | 78 } |
(...skipping 19 matching lines...) Expand all Loading... |
97 DISALLOW_COPY_AND_ASSIGN(ShowPageAction); | 98 DISALLOW_COPY_AND_ASSIGN(ShowPageAction); |
98 }; | 99 }; |
99 | 100 |
100 // Action that sets an extension's action icon. | 101 // Action that sets an extension's action icon. |
101 class SetIcon : public ContentAction { | 102 class SetIcon : public ContentAction { |
102 public: | 103 public: |
103 SetIcon(const gfx::Image& icon, ActionInfo::Type action_type) | 104 SetIcon(const gfx::Image& icon, ActionInfo::Type action_type) |
104 : icon_(icon), action_type_(action_type) {} | 105 : icon_(icon), action_type_(action_type) {} |
105 ~SetIcon() override {} | 106 ~SetIcon() override {} |
106 | 107 |
107 static scoped_ptr<ContentAction> Create( | 108 static std::unique_ptr<ContentAction> Create( |
108 content::BrowserContext* browser_context, | 109 content::BrowserContext* browser_context, |
109 const Extension* extension, | 110 const Extension* extension, |
110 const base::DictionaryValue* dict, | 111 const base::DictionaryValue* dict, |
111 std::string* error); | 112 std::string* error); |
112 | 113 |
113 // Implementation of ContentAction: | 114 // Implementation of ContentAction: |
114 void Apply(const ApplyInfo& apply_info) const override { | 115 void Apply(const ApplyInfo& apply_info) const override { |
115 Profile* profile = Profile::FromBrowserContext(apply_info.browser_context); | 116 Profile* profile = Profile::FromBrowserContext(apply_info.browser_context); |
116 ExtensionAction* action = GetExtensionAction(profile, | 117 ExtensionAction* action = GetExtensionAction(profile, |
117 apply_info.extension); | 118 apply_info.extension); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 176 } |
176 } | 177 } |
177 | 178 |
178 return true; | 179 return true; |
179 } | 180 } |
180 | 181 |
181 struct ContentActionFactory { | 182 struct ContentActionFactory { |
182 // Factory methods for ContentAction instances. |extension| is the extension | 183 // Factory methods for ContentAction instances. |extension| is the extension |
183 // for which the action is being created. |dict| contains the json dictionary | 184 // for which the action is being created. |dict| contains the json dictionary |
184 // that describes the action. |error| is used to return error messages. | 185 // that describes the action. |error| is used to return error messages. |
185 using FactoryMethod = scoped_ptr<ContentAction>(*)( | 186 using FactoryMethod = std::unique_ptr<ContentAction> (*)( |
186 content::BrowserContext* /* browser_context */, | 187 content::BrowserContext* /* browser_context */, |
187 const Extension* /* extension */, | 188 const Extension* /* extension */, |
188 const base::DictionaryValue* /* dict */, | 189 const base::DictionaryValue* /* dict */, |
189 std::string* /* error */); | 190 std::string* /* error */); |
190 // Maps the name of a declarativeContent action type to the factory | 191 // Maps the name of a declarativeContent action type to the factory |
191 // function creating it. | 192 // function creating it. |
192 std::map<std::string, FactoryMethod> factory_methods; | 193 std::map<std::string, FactoryMethod> factory_methods; |
193 | 194 |
194 ContentActionFactory() { | 195 ContentActionFactory() { |
195 factory_methods[keys::kShowPageAction] = | 196 factory_methods[keys::kShowPageAction] = |
(...skipping 23 matching lines...) Expand all Loading... |
219 bool all_frames; | 220 bool all_frames; |
220 bool match_about_blank; | 221 bool match_about_blank; |
221 }; | 222 }; |
222 | 223 |
223 RequestContentScript::ScriptData::ScriptData() | 224 RequestContentScript::ScriptData::ScriptData() |
224 : all_frames(false), | 225 : all_frames(false), |
225 match_about_blank(false) {} | 226 match_about_blank(false) {} |
226 RequestContentScript::ScriptData::~ScriptData() {} | 227 RequestContentScript::ScriptData::~ScriptData() {} |
227 | 228 |
228 // static | 229 // static |
229 scoped_ptr<ContentAction> RequestContentScript::Create( | 230 std::unique_ptr<ContentAction> RequestContentScript::Create( |
230 content::BrowserContext* browser_context, | 231 content::BrowserContext* browser_context, |
231 const Extension* extension, | 232 const Extension* extension, |
232 const base::DictionaryValue* dict, | 233 const base::DictionaryValue* dict, |
233 std::string* error) { | 234 std::string* error) { |
234 ScriptData script_data; | 235 ScriptData script_data; |
235 if (!InitScriptData(dict, error, &script_data)) | 236 if (!InitScriptData(dict, error, &script_data)) |
236 return scoped_ptr<ContentAction>(); | 237 return std::unique_ptr<ContentAction>(); |
237 | 238 |
238 return make_scoped_ptr(new RequestContentScript(browser_context, extension, | 239 return base::WrapUnique( |
239 script_data)); | 240 new RequestContentScript(browser_context, extension, script_data)); |
240 } | 241 } |
241 | 242 |
242 // static | 243 // static |
243 scoped_ptr<ContentAction> RequestContentScript::CreateForTest( | 244 std::unique_ptr<ContentAction> RequestContentScript::CreateForTest( |
244 DeclarativeUserScriptMaster* master, | 245 DeclarativeUserScriptMaster* master, |
245 const Extension* extension, | 246 const Extension* extension, |
246 const base::Value& json_action, | 247 const base::Value& json_action, |
247 std::string* error) { | 248 std::string* error) { |
248 // Simulate ContentAction-level initialization. Check that instance type is | 249 // Simulate ContentAction-level initialization. Check that instance type is |
249 // RequestContentScript. | 250 // RequestContentScript. |
250 error->clear(); | 251 error->clear(); |
251 const base::DictionaryValue* action_dict = NULL; | 252 const base::DictionaryValue* action_dict = NULL; |
252 std::string instance_type; | 253 std::string instance_type; |
253 if (!(json_action.GetAsDictionary(&action_dict) && | 254 if (!(json_action.GetAsDictionary(&action_dict) && |
254 action_dict->GetString(keys::kInstanceType, &instance_type) && | 255 action_dict->GetString(keys::kInstanceType, &instance_type) && |
255 instance_type == std::string(keys::kRequestContentScript))) | 256 instance_type == std::string(keys::kRequestContentScript))) |
256 return scoped_ptr<ContentAction>(); | 257 return std::unique_ptr<ContentAction>(); |
257 | 258 |
258 // Normal RequestContentScript data initialization. | 259 // Normal RequestContentScript data initialization. |
259 ScriptData script_data; | 260 ScriptData script_data; |
260 if (!InitScriptData(action_dict, error, &script_data)) | 261 if (!InitScriptData(action_dict, error, &script_data)) |
261 return scoped_ptr<ContentAction>(); | 262 return std::unique_ptr<ContentAction>(); |
262 | 263 |
263 // Inject provided DeclarativeUserScriptMaster, rather than looking it up | 264 // Inject provided DeclarativeUserScriptMaster, rather than looking it up |
264 // using a BrowserContext. | 265 // using a BrowserContext. |
265 return make_scoped_ptr(new RequestContentScript(master, extension, | 266 return base::WrapUnique( |
266 script_data)); | 267 new RequestContentScript(master, extension, script_data)); |
267 } | 268 } |
268 | 269 |
269 // static | 270 // static |
270 bool RequestContentScript::InitScriptData(const base::DictionaryValue* dict, | 271 bool RequestContentScript::InitScriptData(const base::DictionaryValue* dict, |
271 std::string* error, | 272 std::string* error, |
272 ScriptData* script_data) { | 273 ScriptData* script_data) { |
273 const base::ListValue* list_value = NULL; | 274 const base::ListValue* list_value = NULL; |
274 | 275 |
275 if (!dict->HasKey(keys::kCss) && !dict->HasKey(keys::kJs)) { | 276 if (!dict->HasKey(keys::kCss) && !dict->HasKey(keys::kJs)) { |
276 *error = base::StringPrintf(kMissingParameter, "css or js"); | 277 *error = base::StringPrintf(kMissingParameter, "css or js"); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 content::RenderFrameHost* render_frame_host = contents->GetMainFrame(); | 375 content::RenderFrameHost* render_frame_host = contents->GetMainFrame(); |
375 render_frame_host->Send(new ExtensionMsg_ExecuteDeclarativeScript( | 376 render_frame_host->Send(new ExtensionMsg_ExecuteDeclarativeScript( |
376 render_frame_host->GetRoutingID(), | 377 render_frame_host->GetRoutingID(), |
377 SessionTabHelper::IdForTab(contents), | 378 SessionTabHelper::IdForTab(contents), |
378 extension->id(), | 379 extension->id(), |
379 script_.id(), | 380 script_.id(), |
380 contents->GetLastCommittedURL())); | 381 contents->GetLastCommittedURL())); |
381 } | 382 } |
382 | 383 |
383 // static | 384 // static |
384 scoped_ptr<ContentAction> SetIcon::Create( | 385 std::unique_ptr<ContentAction> SetIcon::Create( |
385 content::BrowserContext* browser_context, | 386 content::BrowserContext* browser_context, |
386 const Extension* extension, | 387 const Extension* extension, |
387 const base::DictionaryValue* dict, | 388 const base::DictionaryValue* dict, |
388 std::string* error) { | 389 std::string* error) { |
389 // We can't set a page or action's icon if the extension doesn't have one. | 390 // We can't set a page or action's icon if the extension doesn't have one. |
390 ActionInfo::Type type; | 391 ActionInfo::Type type; |
391 if (ActionInfo::GetPageActionInfo(extension) != NULL) { | 392 if (ActionInfo::GetPageActionInfo(extension) != NULL) { |
392 type = ActionInfo::TYPE_PAGE; | 393 type = ActionInfo::TYPE_PAGE; |
393 } else if (ActionInfo::GetBrowserActionInfo(extension) != NULL) { | 394 } else if (ActionInfo::GetBrowserActionInfo(extension) != NULL) { |
394 type = ActionInfo::TYPE_BROWSER; | 395 type = ActionInfo::TYPE_BROWSER; |
395 } else { | 396 } else { |
396 *error = kNoPageOrBrowserAction; | 397 *error = kNoPageOrBrowserAction; |
397 return scoped_ptr<ContentAction>(); | 398 return std::unique_ptr<ContentAction>(); |
398 } | 399 } |
399 | 400 |
400 gfx::ImageSkia icon; | 401 gfx::ImageSkia icon; |
401 const base::DictionaryValue* canvas_set = NULL; | 402 const base::DictionaryValue* canvas_set = NULL; |
402 if (dict->GetDictionary("imageData", &canvas_set) && | 403 if (dict->GetDictionary("imageData", &canvas_set) && |
403 !ExtensionAction::ParseIconFromCanvasDictionary(*canvas_set, &icon)) { | 404 !ExtensionAction::ParseIconFromCanvasDictionary(*canvas_set, &icon)) { |
404 *error = kInvalidIconDictionary; | 405 *error = kInvalidIconDictionary; |
405 return scoped_ptr<ContentAction>(); | 406 return std::unique_ptr<ContentAction>(); |
406 } | 407 } |
407 return make_scoped_ptr(new SetIcon(gfx::Image(icon), type)); | 408 return base::WrapUnique(new SetIcon(gfx::Image(icon), type)); |
408 } | 409 } |
409 | 410 |
410 // | 411 // |
411 // ContentAction | 412 // ContentAction |
412 // | 413 // |
413 | 414 |
414 ContentAction::~ContentAction() {} | 415 ContentAction::~ContentAction() {} |
415 | 416 |
416 // static | 417 // static |
417 scoped_ptr<ContentAction> ContentAction::Create( | 418 std::unique_ptr<ContentAction> ContentAction::Create( |
418 content::BrowserContext* browser_context, | 419 content::BrowserContext* browser_context, |
419 const Extension* extension, | 420 const Extension* extension, |
420 const base::Value& json_action, | 421 const base::Value& json_action, |
421 std::string* error) { | 422 std::string* error) { |
422 error->clear(); | 423 error->clear(); |
423 const base::DictionaryValue* action_dict = NULL; | 424 const base::DictionaryValue* action_dict = NULL; |
424 std::string instance_type; | 425 std::string instance_type; |
425 if (!(json_action.GetAsDictionary(&action_dict) && | 426 if (!(json_action.GetAsDictionary(&action_dict) && |
426 action_dict->GetString(keys::kInstanceType, &instance_type))) | 427 action_dict->GetString(keys::kInstanceType, &instance_type))) |
427 return scoped_ptr<ContentAction>(); | 428 return std::unique_ptr<ContentAction>(); |
428 | 429 |
429 ContentActionFactory& factory = g_content_action_factory.Get(); | 430 ContentActionFactory& factory = g_content_action_factory.Get(); |
430 std::map<std::string, ContentActionFactory::FactoryMethod>::iterator | 431 std::map<std::string, ContentActionFactory::FactoryMethod>::iterator |
431 factory_method_iter = factory.factory_methods.find(instance_type); | 432 factory_method_iter = factory.factory_methods.find(instance_type); |
432 if (factory_method_iter != factory.factory_methods.end()) | 433 if (factory_method_iter != factory.factory_methods.end()) |
433 return (*factory_method_iter->second)( | 434 return (*factory_method_iter->second)( |
434 browser_context, extension, action_dict, error); | 435 browser_context, extension, action_dict, error); |
435 | 436 |
436 *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str()); | 437 *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str()); |
437 return scoped_ptr<ContentAction>(); | 438 return std::unique_ptr<ContentAction>(); |
438 } | 439 } |
439 | 440 |
440 ContentAction::ContentAction() {} | 441 ContentAction::ContentAction() {} |
441 | 442 |
442 } // namespace extensions | 443 } // namespace extensions |
OLD | NEW |