password_manager_ios_util.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 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. #include "components/password_manager/ios/password_manager_ios_util.h"
  5. #include "base/strings/sys_string_conversions.h"
  6. #include "base/values.h"
  7. #import "components/autofill/ios/browser/autofill_util.h"
  8. #include "components/security_state/ios/security_state_utils.h"
  9. #import "ios/web/public/web_state.h"
  10. #include "services/network/public/cpp/is_potentially_trustworthy.h"
  11. #include "url/origin.h"
  12. #if !defined(__has_feature) || !__has_feature(objc_arc)
  13. #error "This file requires ARC support."
  14. #endif
  15. namespace password_manager {
  16. bool WebStateContentIsSecureHtml(const web::WebState* web_state) {
  17. if (!web_state) {
  18. return false;
  19. }
  20. if (!web_state->ContentIsHTML()) {
  21. return false;
  22. }
  23. const GURL last_committed_url = web_state->GetLastCommittedURL();
  24. if (!network::IsUrlPotentiallyTrustworthy(last_committed_url)) {
  25. return false;
  26. }
  27. // If scheme is not cryptographic, the origin must be either localhost or a
  28. // file.
  29. if (!security_state::IsSchemeCryptographic(last_committed_url)) {
  30. return security_state::IsOriginLocalhostOrFile(last_committed_url);
  31. }
  32. // If scheme is cryptographic, valid SSL certificate is required.
  33. security_state::SecurityLevel security_level =
  34. security_state::GetSecurityLevelForWebState(web_state);
  35. return security_state::IsSslCertificateValid(security_level);
  36. }
  37. bool JsonStringToFormData(NSString* json_string,
  38. autofill::FormData* form_data,
  39. GURL page_url) {
  40. std::unique_ptr<base::Value> formValue = autofill::ParseJson(json_string);
  41. if (!formValue)
  42. return false;
  43. return autofill::ExtractFormData(
  44. *formValue, false, std::u16string(), page_url,
  45. page_url.DeprecatedGetOriginAsURL(), form_data);
  46. }
  47. } // namespace password_manager