OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/common/features/simple_feature.h" | 5 #include "extensions/common/features/simple_feature.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 return "dev"; | 203 return "dev"; |
204 case version_info::Channel::BETA: | 204 case version_info::Channel::BETA: |
205 return "beta"; | 205 return "beta"; |
206 case version_info::Channel::STABLE: | 206 case version_info::Channel::STABLE: |
207 return "stable"; | 207 return "stable"; |
208 } | 208 } |
209 NOTREACHED(); | 209 NOTREACHED(); |
210 return ""; | 210 return ""; |
211 } | 211 } |
212 | 212 |
| 213 std::string GetSessionTypeDisplayName(Feature::SessionType session_type) { |
| 214 switch (session_type) { |
| 215 case Feature::SESSION_TYPE_UNSPECIFIED: |
| 216 return "unspecified"; |
| 217 case Feature::SESSION_TYPE_KIOSK: |
| 218 return "kiosk app"; |
| 219 case Feature::SESSION_TYPE_REGULAR: |
| 220 return "regular user"; |
| 221 default: |
| 222 return ""; |
| 223 } |
| 224 } |
| 225 |
213 // Gets a human-readable list of the display names (pluralized, comma separated | 226 // Gets a human-readable list of the display names (pluralized, comma separated |
214 // with the "and" in the correct place) for each of |enum_types|. | 227 // with the "and" in the correct place) for each of |enum_types|. |
215 template <typename EnumType> | 228 template <typename EnumType> |
216 std::string ListDisplayNames(const std::vector<EnumType>& enum_types) { | 229 std::string ListDisplayNames(const std::vector<EnumType>& enum_types) { |
217 std::string display_name_list; | 230 std::string display_name_list; |
218 for (size_t i = 0; i < enum_types.size(); ++i) { | 231 for (size_t i = 0; i < enum_types.size(); ++i) { |
219 // Pluralize type name. | 232 // Pluralize type name. |
220 display_name_list += GetDisplayName(enum_types[i]) + "s"; | 233 display_name_list += GetDisplayName(enum_types[i]) + "s"; |
221 // Comma-separate entries, with an Oxford comma if there is more than 2 | 234 // Comma-separate entries, with an Oxford comma if there is more than 2 |
222 // total entries. | 235 // total entries. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 platforms["chromeos"] = Feature::CHROMEOS_PLATFORM; | 306 platforms["chromeos"] = Feature::CHROMEOS_PLATFORM; |
294 platforms["linux"] = Feature::LINUX_PLATFORM; | 307 platforms["linux"] = Feature::LINUX_PLATFORM; |
295 platforms["mac"] = Feature::MACOSX_PLATFORM; | 308 platforms["mac"] = Feature::MACOSX_PLATFORM; |
296 platforms["win"] = Feature::WIN_PLATFORM; | 309 platforms["win"] = Feature::WIN_PLATFORM; |
297 | 310 |
298 channels["trunk"] = version_info::Channel::UNKNOWN; | 311 channels["trunk"] = version_info::Channel::UNKNOWN; |
299 channels["canary"] = version_info::Channel::CANARY; | 312 channels["canary"] = version_info::Channel::CANARY; |
300 channels["dev"] = version_info::Channel::DEV; | 313 channels["dev"] = version_info::Channel::DEV; |
301 channels["beta"] = version_info::Channel::BETA; | 314 channels["beta"] = version_info::Channel::BETA; |
302 channels["stable"] = version_info::Channel::STABLE; | 315 channels["stable"] = version_info::Channel::STABLE; |
| 316 |
| 317 session_types["regular"] = Feature::SESSION_TYPE_REGULAR; |
| 318 session_types["kiosk"] = Feature::SESSION_TYPE_KIOSK; |
303 } | 319 } |
304 | 320 |
305 std::map<std::string, Manifest::Type> extension_types; | 321 std::map<std::string, Manifest::Type> extension_types; |
306 std::map<std::string, Feature::Context> contexts; | 322 std::map<std::string, Feature::Context> contexts; |
307 std::map<std::string, SimpleFeature::Location> locations; | 323 std::map<std::string, SimpleFeature::Location> locations; |
308 std::map<std::string, Feature::Platform> platforms; | 324 std::map<std::string, Feature::Platform> platforms; |
309 std::map<std::string, version_info::Channel> channels; | 325 std::map<std::string, version_info::Channel> channels; |
| 326 std::map<std::string, Feature::SessionType> session_types; |
310 }; | 327 }; |
311 | 328 |
312 SimpleFeature::SimpleFeature() | 329 SimpleFeature::SimpleFeature() |
313 : location_(UNSPECIFIED_LOCATION), | 330 : location_(UNSPECIFIED_LOCATION), |
314 min_manifest_version_(0), | 331 min_manifest_version_(0), |
315 max_manifest_version_(0), | 332 max_manifest_version_(0), |
316 component_extensions_auto_granted_(true), | 333 component_extensions_auto_granted_(true), |
317 is_internal_(false) {} | 334 is_internal_(false) {} |
318 | 335 |
319 SimpleFeature::~SimpleFeature() {} | 336 SimpleFeature::~SimpleFeature() {} |
(...skipping 15 matching lines...) Expand all Loading... |
335 } else if (key == "whitelist") { | 352 } else if (key == "whitelist") { |
336 ParseVector(value, &whitelist_); | 353 ParseVector(value, &whitelist_); |
337 } else if (key == "dependencies") { | 354 } else if (key == "dependencies") { |
338 ParseVector(value, &dependencies_); | 355 ParseVector(value, &dependencies_); |
339 } else if (key == "extension_types") { | 356 } else if (key == "extension_types") { |
340 ParseEnumVector<Manifest::Type>(value, &extension_types_, | 357 ParseEnumVector<Manifest::Type>(value, &extension_types_, |
341 mappings.Get().extension_types); | 358 mappings.Get().extension_types); |
342 } else if (key == "contexts") { | 359 } else if (key == "contexts") { |
343 ParseEnumVector<Context>(value, &contexts_, | 360 ParseEnumVector<Context>(value, &contexts_, |
344 mappings.Get().contexts); | 361 mappings.Get().contexts); |
| 362 } else if (key == "session_types") { |
| 363 ParseEnumVector<SessionType>(value, &session_types_, |
| 364 mappings.Get().session_types); |
345 } else if (key == "location") { | 365 } else if (key == "location") { |
346 ParseEnum<Location>(value, &location_, mappings.Get().locations); | 366 ParseEnum<Location>(value, &location_, mappings.Get().locations); |
347 } else if (key == "platforms") { | 367 } else if (key == "platforms") { |
348 ParseEnumVector<Platform>(value, &platforms_, | 368 ParseEnumVector<Platform>(value, &platforms_, |
349 mappings.Get().platforms); | 369 mappings.Get().platforms); |
350 } else if (key == "min_manifest_version") { | 370 } else if (key == "min_manifest_version") { |
351 dictionary->GetInteger("min_manifest_version", &min_manifest_version_); | 371 dictionary->GetInteger("min_manifest_version", &min_manifest_version_); |
352 } else if (key == "max_manifest_version") { | 372 } else if (key == "max_manifest_version") { |
353 dictionary->GetInteger("max_manifest_version", &max_manifest_version_); | 373 dictionary->GetInteger("max_manifest_version", &max_manifest_version_); |
354 } else if (key == "noparent") { | 374 } else if (key == "noparent") { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 return CreateAvailability(INVALID_CONTEXT, context); | 493 return CreateAvailability(INVALID_CONTEXT, context); |
474 | 494 |
475 // TODO(kalman): Consider checking |matches_| regardless of context type. | 495 // TODO(kalman): Consider checking |matches_| regardless of context type. |
476 // Fewer surprises, and if the feature configuration wants to isolate | 496 // Fewer surprises, and if the feature configuration wants to isolate |
477 // "matches" from say "blessed_extension" then they can use complex features. | 497 // "matches" from say "blessed_extension" then they can use complex features. |
478 if ((context == WEB_PAGE_CONTEXT || context == WEBUI_CONTEXT) && | 498 if ((context == WEB_PAGE_CONTEXT || context == WEBUI_CONTEXT) && |
479 !matches_.MatchesURL(url)) { | 499 !matches_.MatchesURL(url)) { |
480 return CreateAvailability(INVALID_URL, url); | 500 return CreateAvailability(INVALID_URL, url); |
481 } | 501 } |
482 | 502 |
| 503 if (!session_types_.empty() && |
| 504 !base::ContainsValue(session_types_, session_type)) { |
| 505 return CreateAvailability(INVALID_SESSION_TYPE, session_type); |
| 506 } |
| 507 |
483 // TODO(kalman): Assert that if the context was a webpage or WebUI context | 508 // TODO(kalman): Assert that if the context was a webpage or WebUI context |
484 // then at some point a "matches" restriction was checked. | 509 // then at some point a "matches" restriction was checked. |
485 return CheckDependencies(base::Bind(&IsAvailableToContextForBind, extension, | 510 return CheckDependencies(base::Bind(&IsAvailableToContextForBind, extension, |
486 context, session_type, url, platform)); | 511 context, session_type, url, platform)); |
487 } | 512 } |
488 | 513 |
489 std::string SimpleFeature::GetAvailabilityMessage( | 514 std::string SimpleFeature::GetAvailabilityMessage( |
490 AvailabilityResult result, | 515 AvailabilityResult result, |
491 Manifest::Type type, | 516 Manifest::Type type, |
492 const GURL& url, | 517 const GURL& url, |
493 Context context, | 518 Context context, |
494 version_info::Channel channel) const { | 519 version_info::Channel channel, |
| 520 SessionType session_type) const { |
495 switch (result) { | 521 switch (result) { |
496 case IS_AVAILABLE: | 522 case IS_AVAILABLE: |
497 return std::string(); | 523 return std::string(); |
498 case NOT_FOUND_IN_WHITELIST: | 524 case NOT_FOUND_IN_WHITELIST: |
499 case FOUND_IN_BLACKLIST: | 525 case FOUND_IN_BLACKLIST: |
500 return base::StringPrintf( | 526 return base::StringPrintf( |
501 "'%s' is not allowed for specified extension ID.", | 527 "'%s' is not allowed for specified extension ID.", |
502 name().c_str()); | 528 name().c_str()); |
503 case INVALID_URL: | 529 case INVALID_URL: |
504 return base::StringPrintf("'%s' is not allowed on %s.", | 530 return base::StringPrintf("'%s' is not allowed on %s.", |
(...skipping 23 matching lines...) Expand all Loading... |
528 case INVALID_MIN_MANIFEST_VERSION: | 554 case INVALID_MIN_MANIFEST_VERSION: |
529 return base::StringPrintf( | 555 return base::StringPrintf( |
530 "'%s' requires manifest version of at least %d.", | 556 "'%s' requires manifest version of at least %d.", |
531 name().c_str(), | 557 name().c_str(), |
532 min_manifest_version_); | 558 min_manifest_version_); |
533 case INVALID_MAX_MANIFEST_VERSION: | 559 case INVALID_MAX_MANIFEST_VERSION: |
534 return base::StringPrintf( | 560 return base::StringPrintf( |
535 "'%s' requires manifest version of %d or lower.", | 561 "'%s' requires manifest version of %d or lower.", |
536 name().c_str(), | 562 name().c_str(), |
537 max_manifest_version_); | 563 max_manifest_version_); |
| 564 case INVALID_SESSION_TYPE: |
| 565 return base::StringPrintf( |
| 566 "'%s' is not allowed in %s session", name().c_str(), |
| 567 GetSessionTypeDisplayName(session_type).c_str()); |
538 case NOT_PRESENT: | 568 case NOT_PRESENT: |
539 return base::StringPrintf( | 569 return base::StringPrintf( |
540 "'%s' requires a different Feature that is not present.", | 570 "'%s' requires a different Feature that is not present.", |
541 name().c_str()); | 571 name().c_str()); |
542 case UNSUPPORTED_CHANNEL: | 572 case UNSUPPORTED_CHANNEL: |
543 return base::StringPrintf( | 573 return base::StringPrintf( |
544 "'%s' requires %s channel or newer, but this is the %s channel.", | 574 "'%s' requires %s channel or newer, but this is the %s channel.", |
545 name().c_str(), GetDisplayName(channel).c_str(), | 575 name().c_str(), GetDisplayName(channel).c_str(), |
546 GetDisplayName(GetCurrentChannel()).c_str()); | 576 GetDisplayName(GetCurrentChannel()).c_str()); |
547 case MISSING_COMMAND_LINE_SWITCH: | 577 case MISSING_COMMAND_LINE_SWITCH: |
548 return base::StringPrintf( | 578 return base::StringPrintf( |
549 "'%s' requires the '%s' command line switch to be enabled.", | 579 "'%s' requires the '%s' command line switch to be enabled.", |
550 name().c_str(), command_line_switch_.c_str()); | 580 name().c_str(), command_line_switch_.c_str()); |
551 } | 581 } |
552 | 582 |
553 NOTREACHED(); | 583 NOTREACHED(); |
554 return std::string(); | 584 return std::string(); |
555 } | 585 } |
556 | 586 |
557 Feature::Availability SimpleFeature::CreateAvailability( | 587 Feature::Availability SimpleFeature::CreateAvailability( |
558 AvailabilityResult result) const { | 588 AvailabilityResult result) const { |
559 return Availability( | 589 return Availability( |
560 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN, GURL(), | 590 result, GetAvailabilityMessage( |
561 UNSPECIFIED_CONTEXT, | 591 result, Manifest::TYPE_UNKNOWN, GURL(), UNSPECIFIED_CONTEXT, |
562 version_info::Channel::UNKNOWN)); | 592 version_info::Channel::UNKNOWN, SESSION_TYPE_UNSPECIFIED)); |
563 } | 593 } |
564 | 594 |
565 Feature::Availability SimpleFeature::CreateAvailability( | 595 Feature::Availability SimpleFeature::CreateAvailability( |
566 AvailabilityResult result, Manifest::Type type) const { | 596 AvailabilityResult result, Manifest::Type type) const { |
567 return Availability( | 597 return Availability( |
568 result, GetAvailabilityMessage(result, type, GURL(), UNSPECIFIED_CONTEXT, | 598 result, GetAvailabilityMessage(result, type, GURL(), UNSPECIFIED_CONTEXT, |
569 version_info::Channel::UNKNOWN)); | 599 version_info::Channel::UNKNOWN, |
| 600 SESSION_TYPE_UNSPECIFIED)); |
570 } | 601 } |
571 | 602 |
572 Feature::Availability SimpleFeature::CreateAvailability( | 603 Feature::Availability SimpleFeature::CreateAvailability( |
573 AvailabilityResult result, | 604 AvailabilityResult result, |
574 const GURL& url) const { | 605 const GURL& url) const { |
575 return Availability( | 606 return Availability( |
576 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN, url, | 607 result, GetAvailabilityMessage( |
577 UNSPECIFIED_CONTEXT, | 608 result, Manifest::TYPE_UNKNOWN, url, UNSPECIFIED_CONTEXT, |
578 version_info::Channel::UNKNOWN)); | 609 version_info::Channel::UNKNOWN, SESSION_TYPE_UNSPECIFIED)); |
579 } | 610 } |
580 | 611 |
581 Feature::Availability SimpleFeature::CreateAvailability( | 612 Feature::Availability SimpleFeature::CreateAvailability( |
582 AvailabilityResult result, | 613 AvailabilityResult result, |
583 Context context) const { | 614 Context context) const { |
584 return Availability( | 615 return Availability( |
585 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN, GURL(), | 616 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN, GURL(), |
586 context, version_info::Channel::UNKNOWN)); | 617 context, version_info::Channel::UNKNOWN, |
| 618 SESSION_TYPE_UNSPECIFIED)); |
587 } | 619 } |
588 | 620 |
589 Feature::Availability SimpleFeature::CreateAvailability( | 621 Feature::Availability SimpleFeature::CreateAvailability( |
590 AvailabilityResult result, | 622 AvailabilityResult result, |
591 version_info::Channel channel) const { | 623 version_info::Channel channel) const { |
592 return Availability( | 624 return Availability( |
593 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN, GURL(), | 625 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN, GURL(), |
594 UNSPECIFIED_CONTEXT, channel)); | 626 UNSPECIFIED_CONTEXT, channel, |
| 627 SESSION_TYPE_UNSPECIFIED)); |
| 628 } |
| 629 |
| 630 Feature::Availability SimpleFeature::CreateAvailability( |
| 631 AvailabilityResult result, |
| 632 SessionType session_type) const { |
| 633 return Availability( |
| 634 result, GetAvailabilityMessage( |
| 635 result, Manifest::TYPE_UNKNOWN, GURL(), UNSPECIFIED_CONTEXT, |
| 636 version_info::Channel::UNKNOWN, session_type)); |
595 } | 637 } |
596 | 638 |
597 bool SimpleFeature::IsInternal() const { | 639 bool SimpleFeature::IsInternal() const { |
598 return is_internal_; | 640 return is_internal_; |
599 } | 641 } |
600 | 642 |
601 bool SimpleFeature::IsIdInBlacklist(const std::string& extension_id) const { | 643 bool SimpleFeature::IsIdInBlacklist(const std::string& extension_id) const { |
602 return IsIdInList(extension_id, blacklist_); | 644 return IsIdInList(extension_id, blacklist_); |
603 } | 645 } |
604 | 646 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 void SimpleFeature::set_dependencies( | 730 void SimpleFeature::set_dependencies( |
689 std::initializer_list<const char* const> dependencies) { | 731 std::initializer_list<const char* const> dependencies) { |
690 dependencies_.assign(dependencies.begin(), dependencies.end()); | 732 dependencies_.assign(dependencies.begin(), dependencies.end()); |
691 } | 733 } |
692 | 734 |
693 void SimpleFeature::set_extension_types( | 735 void SimpleFeature::set_extension_types( |
694 std::initializer_list<Manifest::Type> types) { | 736 std::initializer_list<Manifest::Type> types) { |
695 extension_types_ = types; | 737 extension_types_ = types; |
696 } | 738 } |
697 | 739 |
| 740 void SimpleFeature::set_session_types( |
| 741 std::initializer_list<SessionType> types) { |
| 742 session_types_ = types; |
| 743 } |
| 744 |
698 void SimpleFeature::set_matches( | 745 void SimpleFeature::set_matches( |
699 std::initializer_list<const char* const> matches) { | 746 std::initializer_list<const char* const> matches) { |
700 matches_.ClearPatterns(); | 747 matches_.ClearPatterns(); |
701 for (const auto* pattern : matches) | 748 for (const auto* pattern : matches) |
702 matches_.AddPattern(URLPattern(URLPattern::SCHEME_ALL, pattern)); | 749 matches_.AddPattern(URLPattern(URLPattern::SCHEME_ALL, pattern)); |
703 } | 750 } |
704 | 751 |
705 void SimpleFeature::set_platforms(std::initializer_list<Platform> platforms) { | 752 void SimpleFeature::set_platforms(std::initializer_list<Platform> platforms) { |
706 platforms_ = platforms; | 753 platforms_ = platforms; |
707 } | 754 } |
708 | 755 |
709 void SimpleFeature::set_whitelist( | 756 void SimpleFeature::set_whitelist( |
710 std::initializer_list<const char* const> whitelist) { | 757 std::initializer_list<const char* const> whitelist) { |
711 whitelist_.assign(whitelist.begin(), whitelist.end()); | 758 whitelist_.assign(whitelist.begin(), whitelist.end()); |
712 } | 759 } |
713 | 760 |
714 } // namespace extensions | 761 } // namespace extensions |
OLD | NEW |