Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / api / media_galleries_private / media_galleries_handler.cc
bloba973036440e71fb54407b4367a33314b735abf62
1 // Copyright 2013 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.
5 #include "chrome/common/extensions/api/media_galleries_private/media_galleries_handler.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/common/extensions/extension_constants.h"
13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/manifest.h"
15 #include "extensions/common/manifest_constants.h"
17 namespace keys = extensions::manifest_keys;
18 namespace errors = extensions::manifest_errors;
20 namespace {
22 // Stored on the Extension.
23 struct MediaGalleriesHandlerInfo : public extensions::Extension::ManifestData {
24 MediaGalleriesHandler::List media_galleries_handlers;
26 MediaGalleriesHandlerInfo();
27 virtual ~MediaGalleriesHandlerInfo();
30 MediaGalleriesHandlerInfo::MediaGalleriesHandlerInfo() {
33 MediaGalleriesHandlerInfo::~MediaGalleriesHandlerInfo() {
36 MediaGalleriesHandler* LoadMediaGalleriesHandler(
37 const std::string& extension_id,
38 const base::DictionaryValue* media_galleries_handler,
39 base::string16* error) {
40 scoped_ptr<MediaGalleriesHandler> result(new MediaGalleriesHandler());
41 result->set_extension_id(extension_id);
43 std::string handler_id;
44 // Read the file action |id| (mandatory).
45 if (!media_galleries_handler->HasKey(keys::kPageActionId) ||
46 !media_galleries_handler->GetString(keys::kPageActionId, &handler_id)) {
47 *error = base::ASCIIToUTF16(errors::kInvalidPageActionId);
48 return NULL;
50 result->set_id(handler_id);
52 // Read the page action title from |default_title| (mandatory).
53 std::string title;
54 if (!media_galleries_handler->HasKey(keys::kPageActionDefaultTitle) ||
55 !media_galleries_handler->GetString(keys::kPageActionDefaultTitle,
56 &title)) {
57 *error = base::ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle);
58 return NULL;
60 result->set_title(title);
62 std::string default_icon;
63 // Read the media galleries action |default_icon| (optional).
64 if (media_galleries_handler->HasKey(keys::kPageActionDefaultIcon)) {
65 if (!media_galleries_handler->GetString(
66 keys::kPageActionDefaultIcon, &default_icon) ||
67 default_icon.empty()) {
68 *error = base::ASCIIToUTF16(errors::kInvalidPageActionIconPath);
69 return NULL;
71 result->set_icon_path(default_icon);
74 return result.release();
77 // Loads MediaGalleriesHandlers from |extension_actions| into a list in
78 // |result|.
79 bool LoadMediaGalleriesHandlers(
80 const std::string& extension_id,
81 const base::ListValue* extension_actions,
82 MediaGalleriesHandler::List* result,
83 base::string16* error) {
84 for (base::ListValue::const_iterator iter = extension_actions->begin();
85 iter != extension_actions->end();
86 ++iter) {
87 if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY)) {
88 *error = base::ASCIIToUTF16(errors::kInvalidMediaGalleriesHandler);
89 return false;
91 scoped_ptr<MediaGalleriesHandler> action(
92 LoadMediaGalleriesHandler(
93 extension_id,
94 reinterpret_cast<base::DictionaryValue*>(*iter), error));
95 if (!action)
96 return false; // Failed to parse media galleries action definition.
97 result->push_back(linked_ptr<MediaGalleriesHandler>(action.release()));
99 return true;
102 } // namespace
104 MediaGalleriesHandler::MediaGalleriesHandler() {
107 MediaGalleriesHandler::~MediaGalleriesHandler() {
110 // static
111 MediaGalleriesHandler::List*
112 MediaGalleriesHandler::GetHandlers(const extensions::Extension* extension) {
113 MediaGalleriesHandlerInfo* info = static_cast<MediaGalleriesHandlerInfo*>(
114 extension->GetManifestData(keys::kMediaGalleriesHandlers));
115 if (info)
116 return &info->media_galleries_handlers;
117 return NULL;
120 MediaGalleriesHandlerParser::MediaGalleriesHandlerParser() {
123 MediaGalleriesHandlerParser::~MediaGalleriesHandlerParser() {
126 bool MediaGalleriesHandlerParser::Parse(extensions::Extension* extension,
127 base::string16* error) {
128 const base::ListValue* media_galleries_handlers_value = NULL;
129 if (!extension->manifest()->GetList(keys::kMediaGalleriesHandlers,
130 &media_galleries_handlers_value)) {
131 *error = base::ASCIIToUTF16(errors::kInvalidMediaGalleriesHandler);
132 return false;
134 scoped_ptr<MediaGalleriesHandlerInfo> info(new MediaGalleriesHandlerInfo);
135 if (!LoadMediaGalleriesHandlers(extension->id(),
136 media_galleries_handlers_value,
137 &info->media_galleries_handlers,
138 error)) {
139 return false; // Failed to parse media galleries actions definition.
142 extension->SetManifestData(keys::kMediaGalleriesHandlers, info.release());
143 return true;
146 const std::vector<std::string> MediaGalleriesHandlerParser::Keys() const {
147 return SingleKey(keys::kMediaGalleriesHandlers);