OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 GURL url = GetResourceURL(WideToUTF8(relative)); | 290 GURL url = GetResourceURL(WideToUTF8(relative)); |
291 ExtensionResource resource = GetResource(WideToUTF8(relative)); | 291 ExtensionResource resource = GetResource(WideToUTF8(relative)); |
292 result->css_scripts().push_back(UserScript::File( | 292 result->css_scripts().push_back(UserScript::File( |
293 resource.extension_root(), resource.relative_path(), url)); | 293 resource.extension_root(), resource.relative_path(), url)); |
294 } | 294 } |
295 } | 295 } |
296 | 296 |
297 return true; | 297 return true; |
298 } | 298 } |
299 | 299 |
300 // Helper method that loads a PageAction or BrowserAction object from a | |
301 // dictionary in the page_actions list or browser_action key of the manifest. | |
302 ExtensionAction* Extension::LoadExtensionActionHelper( | |
303 const DictionaryValue* page_action, std::string* error, | |
304 ExtensionAction::ExtensionActionType action_type) { | |
305 scoped_ptr<ExtensionAction> result(new ExtensionAction()); | |
306 result->set_extension_id(id()); | |
307 result->set_type(action_type); | |
308 | |
309 // TODO(EXTENSIONS_DEPRECATED): icons list is obsolete. | |
310 ListValue* icons = NULL; | |
311 if (page_action->HasKey(keys::kPageActionIcons) && | |
312 page_action->GetList(keys::kPageActionIcons, &icons)) { | |
313 for (ListValue::const_iterator iter = icons->begin(); | |
314 iter != icons->end(); ++iter) { | |
315 std::string path; | |
316 if (!(*iter)->GetAsString(&path) || path.empty()) { | |
317 *error = errors::kInvalidPageActionIconPath; | |
318 return NULL; | |
319 } | |
320 | |
321 result->AddIconPath(path); | |
322 } | |
323 } | |
324 | |
325 // TODO(EXTENSIONS_DEPRECATED): Read the page action |id| (optional). | |
326 std::string id; | |
327 if (action_type == ExtensionAction::PAGE_ACTION) | |
328 page_action->GetString(keys::kPageActionId, &id); | |
329 result->set_id(id); | |
330 | |
331 std::string default_icon; | |
332 // Read the page action |default_icon| (optional). | |
333 if (page_action->HasKey(keys::kPageActionDefaultIcon)) { | |
334 if (!page_action->GetString(keys::kPageActionDefaultIcon, &default_icon) || | |
335 default_icon.empty()) { | |
336 *error = errors::kInvalidPageActionIconPath; | |
337 return NULL; | |
338 } | |
339 // TODO(EXTENSIONS_DEPRECATED): one icon. | |
340 result->AddIconPath(default_icon); | |
341 } | |
342 | |
343 // Read the page action |default_title|. | |
344 std::string title; | |
345 if (!page_action->GetString(keys::kName, &title) && | |
346 !page_action->GetString(keys::kPageActionDefaultTitle, &title)) { | |
347 *error = errors::kInvalidPageActionDefaultTitle; | |
348 return NULL; | |
349 } | |
350 result->set_title(title); | |
351 | |
352 // Read the action's |popup| (optional). | |
353 DictionaryValue* popup = NULL; | |
354 std::string url_str; | |
355 if (page_action->HasKey(keys::kPageActionPopup) && | |
356 !page_action->GetDictionary(keys::kPageActionPopup, &popup) && | |
357 !page_action->GetString(keys::kPageActionPopup, &url_str)) { | |
358 *error = errors::kInvalidPageActionPopup; | |
359 return NULL; | |
360 } | |
361 if (popup) { | |
362 // TODO(EXTENSIONS_DEPRECATED): popup is a string only | |
363 if (!popup->GetString(keys::kPageActionPopupPath, &url_str)) { | |
364 *error = ExtensionErrorUtils::FormatErrorMessage( | |
365 errors::kInvalidPageActionPopupPath, "<missing>"); | |
366 return NULL; | |
367 } | |
368 GURL url = GetResourceURL(url_str); | |
369 if (!url.is_valid()) { | |
370 *error = ExtensionErrorUtils::FormatErrorMessage( | |
371 errors::kInvalidPageActionPopupPath, url_str); | |
372 return NULL; | |
373 } | |
374 result->set_popup_url(url); | |
375 } else if (!url_str.empty()) { | |
376 GURL url = GetResourceURL(url_str); | |
377 if (!url.is_valid()) { | |
378 *error = ExtensionErrorUtils::FormatErrorMessage( | |
379 errors::kInvalidPageActionPopupPath, url_str); | |
380 return NULL; | |
381 } | |
382 result->set_popup_url(url); | |
383 } | |
384 | |
385 return result.release(); | |
386 } | |
387 | |
388 ExtensionAction2* Extension::LoadExtensionAction2Helper( | 300 ExtensionAction2* Extension::LoadExtensionAction2Helper( |
389 const DictionaryValue* extension_action, std::string* error) { | 301 const DictionaryValue* extension_action, std::string* error) { |
390 scoped_ptr<ExtensionAction2> result(new ExtensionAction2()); | 302 scoped_ptr<ExtensionAction2> result(new ExtensionAction2()); |
391 result->set_extension_id(id()); | 303 result->set_extension_id(id()); |
392 | 304 |
| 305 // Page actions are hidden by default, and browser actions ignore |
| 306 // visibility. |
| 307 result->SetIsVisible(ExtensionAction2::kDefaultTabId, false); |
| 308 |
393 // TODO(EXTENSIONS_DEPRECATED): icons list is obsolete. | 309 // TODO(EXTENSIONS_DEPRECATED): icons list is obsolete. |
394 ListValue* icons = NULL; | 310 ListValue* icons = NULL; |
395 if (extension_action->HasKey(keys::kPageActionIcons) && | 311 if (extension_action->HasKey(keys::kPageActionIcons) && |
396 extension_action->GetList(keys::kPageActionIcons, &icons)) { | 312 extension_action->GetList(keys::kPageActionIcons, &icons)) { |
397 for (ListValue::const_iterator iter = icons->begin(); | 313 for (ListValue::const_iterator iter = icons->begin(); |
398 iter != icons->end(); ++iter) { | 314 iter != icons->end(); ++iter) { |
399 std::string path; | 315 std::string path; |
400 if (!(*iter)->GetAsString(&path) || path.empty()) { | 316 if (!(*iter)->GetAsString(&path) || path.empty()) { |
401 *error = errors::kInvalidPageActionIconPath; | 317 *error = errors::kInvalidPageActionIconPath; |
402 return NULL; | 318 return NULL; |
403 } | 319 } |
404 | 320 |
405 result->icon_paths()->push_back(path); | 321 result->icon_paths()->push_back(path); |
406 result->SetDefaultIcon(path); | |
407 } | 322 } |
408 } | 323 } |
409 | 324 |
410 // TODO(EXTENSIONS_DEPRECATED): Read the page action |id| (optional). | 325 // TODO(EXTENSIONS_DEPRECATED): Read the page action |id| (optional). |
411 std::string id; | 326 std::string id; |
412 if (extension_action->HasKey(keys::kPageActionId)) { | 327 if (extension_action->HasKey(keys::kPageActionId)) { |
413 if (!extension_action->GetString(keys::kPageActionId, &id)) { | 328 if (!extension_action->GetString(keys::kPageActionId, &id)) { |
414 *error = errors::kInvalidPageActionId; | 329 *error = errors::kInvalidPageActionId; |
415 return NULL; | 330 return NULL; |
416 } | 331 } |
417 result->set_id(id); | 332 result->set_id(id); |
418 } | 333 } |
419 | 334 |
420 std::string default_icon; | 335 std::string default_icon; |
421 // Read the page action |default_icon| (optional). | 336 // Read the page action |default_icon| (optional). |
422 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) { | 337 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) { |
423 if (!extension_action->GetString(keys::kPageActionDefaultIcon, | 338 if (!extension_action->GetString(keys::kPageActionDefaultIcon, |
424 &default_icon) || | 339 &default_icon) || |
425 default_icon.empty()) { | 340 default_icon.empty()) { |
426 *error = errors::kInvalidPageActionIconPath; | 341 *error = errors::kInvalidPageActionIconPath; |
427 return NULL; | 342 return NULL; |
428 } | 343 } |
429 result->SetDefaultIcon(default_icon); | 344 result->set_default_icon_path(default_icon); |
430 } | 345 } |
431 | 346 |
432 // Read the page action |default_title|. | 347 // Read the page action |default_title|. |
433 std::string title; | 348 std::string title; |
434 if (!extension_action->GetString(keys::kName, &title) && | 349 if (!extension_action->GetString(keys::kName, &title) && |
435 !extension_action->GetString(keys::kPageActionDefaultTitle, &title)) { | 350 !extension_action->GetString(keys::kPageActionDefaultTitle, &title)) { |
436 *error = errors::kInvalidPageActionDefaultTitle; | 351 *error = errors::kInvalidPageActionDefaultTitle; |
437 return NULL; | 352 return NULL; |
438 } | 353 } |
439 result->SetTitle(ExtensionAction2::kDefaultTabId, title); | 354 result->SetTitle(ExtensionAction2::kDefaultTabId, title); |
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 return false; | 939 return false; |
1025 } | 940 } |
1026 | 941 |
1027 DictionaryValue* page_action_value; | 942 DictionaryValue* page_action_value; |
1028 if (!list_value->GetDictionary(0, &page_action_value)) { | 943 if (!list_value->GetDictionary(0, &page_action_value)) { |
1029 *error = errors::kInvalidPageAction; | 944 *error = errors::kInvalidPageAction; |
1030 return false; | 945 return false; |
1031 } | 946 } |
1032 | 947 |
1033 page_action_.reset( | 948 page_action_.reset( |
1034 LoadExtensionActionHelper(page_action_value, error, | 949 LoadExtensionAction2Helper(page_action_value, error)); |
1035 ExtensionAction::PAGE_ACTION)); | |
1036 if (!page_action_.get()) | 950 if (!page_action_.get()) |
1037 return false; // Failed to parse page action definition. | 951 return false; // Failed to parse page action definition. |
1038 } else if (source.HasKey(keys::kPageAction)) { | 952 } else if (source.HasKey(keys::kPageAction)) { |
1039 DictionaryValue* page_action_value; | 953 DictionaryValue* page_action_value; |
1040 if (!source.GetDictionary(keys::kPageAction, &page_action_value)) { | 954 if (!source.GetDictionary(keys::kPageAction, &page_action_value)) { |
1041 *error = errors::kInvalidPageAction; | 955 *error = errors::kInvalidPageAction; |
1042 return false; | 956 return false; |
1043 } | 957 } |
1044 | 958 |
1045 page_action_.reset( | 959 page_action_.reset( |
1046 LoadExtensionActionHelper(page_action_value, error, | 960 LoadExtensionAction2Helper(page_action_value, error)); |
1047 ExtensionAction::PAGE_ACTION)); | |
1048 if (!page_action_.get()) | 961 if (!page_action_.get()) |
1049 return false; // Failed to parse page action definition. | 962 return false; // Failed to parse page action definition. |
1050 } | 963 } |
1051 | 964 |
1052 // Initialize browser action (optional). | 965 // Initialize browser action (optional). |
1053 if (source.HasKey(keys::kBrowserAction)) { | 966 if (source.HasKey(keys::kBrowserAction)) { |
1054 // Restrict extensions to one UI surface. | 967 // Restrict extensions to one UI surface. |
1055 if (source.HasKey(keys::kPageAction) || source.HasKey(keys::kPageActions)) { | 968 if (source.HasKey(keys::kPageAction) || source.HasKey(keys::kPageActions)) { |
1056 *error = errors::kOneUISurfaceOnly; | 969 *error = errors::kOneUISurfaceOnly; |
1057 return false; | 970 return false; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1172 it != theme_images->end_keys(); ++it) { | 1085 it != theme_images->end_keys(); ++it) { |
1173 std::wstring val; | 1086 std::wstring val; |
1174 if (theme_images->GetString(*it, &val)) { | 1087 if (theme_images->GetString(*it, &val)) { |
1175 image_paths.insert(FilePath::FromWStringHack(val)); | 1088 image_paths.insert(FilePath::FromWStringHack(val)); |
1176 } | 1089 } |
1177 } | 1090 } |
1178 } | 1091 } |
1179 | 1092 |
1180 // page action icons | 1093 // page action icons |
1181 if (page_action_.get()) { | 1094 if (page_action_.get()) { |
1182 const std::vector<std::string>& icon_paths = page_action_->icon_paths(); | 1095 std::vector<std::string>* icon_paths = page_action_->icon_paths(); |
1183 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); | 1096 for (std::vector<std::string>::iterator iter = icon_paths->begin(); |
1184 iter != icon_paths.end(); ++iter) { | 1097 iter != icon_paths->end(); ++iter) { |
1185 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); | 1098 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); |
1186 } | 1099 } |
1187 } | 1100 } |
1188 | 1101 |
1189 // browser action icons | 1102 // browser action icons |
1190 if (browser_action_.get()) { | 1103 if (browser_action_.get()) { |
1191 std::vector<std::string>* icon_paths = browser_action_->icon_paths(); | 1104 std::vector<std::string>* icon_paths = browser_action_->icon_paths(); |
1192 for (std::vector<std::string>::iterator iter = icon_paths->begin(); | 1105 for (std::vector<std::string>::iterator iter = icon_paths->begin(); |
1193 iter != icon_paths->end(); ++iter) { | 1106 iter != icon_paths->end(); ++iter) { |
1194 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); | 1107 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1258 UserScript::PatternList::const_iterator pattern = | 1171 UserScript::PatternList::const_iterator pattern = |
1259 content_script->url_patterns().begin(); | 1172 content_script->url_patterns().begin(); |
1260 for (; pattern != content_script->url_patterns().end(); ++pattern) { | 1173 for (; pattern != content_script->url_patterns().end(); ++pattern) { |
1261 if (pattern->match_subdomains() && pattern->host().empty()) | 1174 if (pattern->match_subdomains() && pattern->host().empty()) |
1262 return true; | 1175 return true; |
1263 } | 1176 } |
1264 } | 1177 } |
1265 | 1178 |
1266 return false; | 1179 return false; |
1267 } | 1180 } |
OLD | NEW |