Commit e35c0a03 authored by Scott Wu's avatar Scott Wu Committed by Commit Bot

Move password form serialization logic into components folder

The purpose is to share this logic with both ios chrome and ios web view

Bug: 865114
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: Id6f16a05eb3408238c2772b420ec408e499ed2e8
Reviewed-on: https://chromium-review.googlesource.com/1151181Reviewed-by: default avatarVadym Doroshenko <dvadym@chromium.org>
Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarJohn Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Commit-Queue: Scott Wu <scottwu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581180}
parent 971b7ef9
......@@ -190,6 +190,7 @@ test("components_unittests") {
"//components/autofill/ios/browser:unit_tests",
"//components/autofill/ios/form_util:unit_tests",
"//components/image_fetcher/ios:unit_tests",
"//components/password_manager/ios:unit_tests",
"//components/signin/ios/browser:unit_tests",
"//components/translate/ios/browser:unit_tests",
]
......
......@@ -7,11 +7,46 @@ component("ios") {
deps = [
"//base",
"//components/autofill/core/common",
"//ios/web/public",
"//url",
]
sources = [
"account_select_fill_data.cc",
"account_select_fill_data.h",
"js_password_manager.h",
"js_password_manager.mm",
]
}
static_library("test_support") {
testonly = true
sources = [
"test_helpers.cc",
"test_helpers.h",
]
deps = [
"//base",
"//components/autofill/core/common",
"//url",
]
}
source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"account_select_fill_data_unittest.cc",
]
deps = [
":ios",
":test_support",
"//base",
"//base/test:test_support",
"//components/autofill/core/common",
"//testing/gmock",
"//testing/gtest",
]
}
include_rules = [
"+components/autofill/core/common",
"+ios/web/public",
]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/passwords/account_select_fill_data.h"
#include "components/password_manager/ios/account_select_fill_data.h"
#include "base/strings/string_util.h"
#include "components/autofill/core/common/password_form_fill_data.h"
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_PASSWORDS_ACCOUNT_SELECT_FILL_DATA_H_
#define IOS_CHROME_BROWSER_PASSWORDS_ACCOUNT_SELECT_FILL_DATA_H_
#ifndef COMPONENTS_PASSWORD_MANAGER_IOS_ACCOUNT_SELECT_FILL_DATA_H_
#define COMPONENTS_PASSWORD_MANAGER_IOS_ACCOUNT_SELECT_FILL_DATA_H_
#include <map>
#include <memory>
......@@ -127,4 +127,4 @@ class AccountSelectFillData {
} // namespace password_manager
#endif // IOS_CHROME_BROWSER_PASSWORDS_ACCOUNT_SELECT_FILL_DATA_H_
#endif // COMPONENTS_PASSWORD_MANAGER_IOS_ACCOUNT_SELECT_FILL_DATA_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/passwords/account_select_fill_data.h"
#include "components/password_manager/ios/account_select_fill_data.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "ios/chrome/browser/passwords/test_helpers.h"
#include "components/password_manager/ios/test_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
......@@ -31,7 +32,7 @@ const char* kAdditionalPasswords[] = {"secret", nullptr};
class AccountSelectFillDataTest : public PlatformTest {
public:
AccountSelectFillDataTest() {
for (size_t i = 0; i < arraysize(form_data_); ++i) {
for (size_t i = 0; i < base::size(form_data_); ++i) {
SetPasswordFormFillData(form_data_[i], kUrl, kUrl, kUsernameElements[i],
kUsernames[i], kPasswordElements[i],
kPasswords[i], kAdditionalUsernames[i],
......@@ -166,7 +167,7 @@ TEST_F(AccountSelectFillDataTest, GetFillData) {
account_select_fill_data.Add(form_data_[1]);
for (bool is_password_field : {false, true}) {
for (size_t form_i = 0; form_i < arraysize(form_data_); ++form_i) {
for (size_t form_i = 0; form_i < base::size(form_data_); ++form_i) {
const auto& form_data = form_data_[form_i];
// Suggestions should be shown on any password field on the form. So in
// case of clicking on a password field it is taken an id different from
......
......@@ -8,6 +8,25 @@
#include "base/ios/block_types.h"
#import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
namespace autofill {
struct PasswordFormFillData;
} // namespace autofill
namespace password_manager {
struct FillData;
// Serializes |fillData| into a JSON string that can be used by the JS side
// of PasswordController.
NSString* SerializeFillData(const password_manager::FillData& fillData);
// Serializes |formData| into a JSON string that can be used by the JS side
// of PasswordController.
NSString* SerializePasswordFormFillData(
const autofill::PasswordFormFillData& formData);
} // namespace password_manager
// Loads the JavaScript file, password_controller.js, which contains password
// form parsing and autofill functions. It will be evaluated on a page that
// is known to have at least one password form (see hasPasswordField_ in
......
......@@ -4,15 +4,66 @@
#import "components/password_manager/ios/js_password_manager.h"
#include "base/json/json_writer.h"
#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "components/password_manager/ios/account_select_fill_data.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace password_manager {
NSString* SerializeFillData(const GURL& origin,
const GURL& action,
const base::string16& username_element,
const base::string16& username_value,
const base::string16& password_element,
const base::string16& password_value) {
base::DictionaryValue rootDict;
rootDict.SetString("origin", origin.spec());
rootDict.SetString("action", action.spec());
auto fieldList = std::make_unique<base::ListValue>();
auto usernameField = std::make_unique<base::DictionaryValue>();
usernameField->SetString("name", username_element);
usernameField->SetString("value", username_value);
fieldList->Append(std::move(usernameField));
auto passwordField = std::make_unique<base::DictionaryValue>();
passwordField->SetString("name", password_element);
passwordField->SetString("value", password_value);
fieldList->Append(std::move(passwordField));
rootDict.Set("fields", std::move(fieldList));
std::string jsonString;
base::JSONWriter::Write(rootDict, &jsonString);
return base::SysUTF8ToNSString(jsonString);
}
NSString* SerializePasswordFormFillData(
const autofill::PasswordFormFillData& formData) {
return SerializeFillData(
formData.origin, formData.action, formData.username_field.name,
formData.username_field.value, formData.password_field.name,
formData.password_field.value);
}
NSString* SerializeFillData(const password_manager::FillData& fillData) {
return SerializeFillData(fillData.origin, fillData.action,
fillData.username_element, fillData.username_value,
fillData.password_element, fillData.password_value);
}
} // namespace password_manager
namespace {
// Sanitizes |JSONString| and wraps it in quotes so it can be injected safely in
// JavaScript.
......
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Copyright (c) 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/passwords/test_helpers.h"
#include "components/password_manager/ios/test_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/password_form_fill_data.h"
......
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Copyright (c) 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_PASSWORDS_TEST_HELPERS_H_
#define IOS_CHROME_BROWSER_PASSWORDS_TEST_HELPERS_H_
#ifndef COMPONENTS_PASSWORD_MANAGER_IOS_TEST_HELPERS_H_
#define COMPONENTS_PASSWORD_MANAGER_IOS_TEST_HELPERS_H_
#include <string>
......@@ -27,4 +27,4 @@ void SetPasswordFormFillData(autofill::PasswordFormFillData& form_data,
} // namespace test_helpers
#endif // IOS_CHROME_BROWSER_PASSWORDS_TEST_HELPERS_H_
#endif // COMPONENTS_PASSWORD_MANAGER_IOS_TEST_HELPERS_H_
......@@ -7,8 +7,6 @@ import("//ios/web/js_compile.gni")
source_set("passwords") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"account_select_fill_data.cc",
"account_select_fill_data.h",
"credential_manager.h",
"credential_manager.mm",
"credential_manager_features.cc",
......@@ -115,14 +113,11 @@ source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"account_select_fill_data_unittest.cc",
"credential_manager_unittest.mm",
"credential_manager_util_unittest.cc",
"js_credential_manager_unittest.mm",
"password_controller_js_unittest.mm",
"password_controller_unittest.mm",
"test_helpers.cc",
"test_helpers.h",
]
deps = [
":passwords",
......@@ -134,6 +129,7 @@ source_set("unit_tests") {
"//components/password_manager/core/browser:test_support",
"//components/password_manager/core/common",
"//components/password_manager/ios",
"//components/password_manager/ios:test_support",
"//components/prefs",
"//components/prefs:test_support",
"//components/security_state/ios",
......
......@@ -13,8 +13,6 @@
#include <vector>
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/mac/foundation_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string16.h"
......@@ -35,11 +33,11 @@
#include "components/password_manager/core/browser/password_manager.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "components/password_manager/core/browser/password_manager_driver.h"
#include "components/password_manager/ios/account_select_fill_data.h"
#import "components/password_manager/ios/js_password_manager.h"
#include "components/sync/driver/sync_service.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#include "ios/chrome/browser/passwords/account_select_fill_data.h"
#include "ios/chrome/browser/passwords/credential_manager.h"
#include "ios/chrome/browser/passwords/credential_manager_features.h"
#import "ios/chrome/browser/passwords/ios_chrome_save_password_infobar_delegate.h"
......@@ -71,6 +69,8 @@ using password_manager::PasswordFormManagerForUI;
using password_manager::PasswordManager;
using password_manager::PasswordManagerClient;
using password_manager::PasswordManagerDriver;
using password_manager::SerializeFillData;
using password_manager::SerializePasswordFormFillData;
typedef void (^PasswordSuggestionsAvailableCompletion)(
const AccountSelectFillData*);
......@@ -230,60 +230,14 @@ NSArray* BuildSuggestions(const AccountSelectFillData& fillData,
return [suggestions copy];
}
// Serializes arguments into a JSON string that can be used by the JS side
// of PasswordController.
NSString* SerializeFillData(const GURL& origin,
const GURL& action,
const base::string16& username_element,
const base::string16& username_value,
const base::string16& password_element,
const base::string16& password_value) {
base::DictionaryValue rootDict;
rootDict.SetString("origin", origin.spec());
rootDict.SetString("action", action.spec());
auto fieldList = std::make_unique<base::ListValue>();
auto usernameField = std::make_unique<base::DictionaryValue>();
usernameField->SetString("name", username_element);
usernameField->SetString("value", username_value);
fieldList->Append(std::move(usernameField));
auto passwordField = std::make_unique<base::DictionaryValue>();
passwordField->SetString("name", password_element);
passwordField->SetString("value", password_value);
fieldList->Append(std::move(passwordField));
rootDict.Set("fields", std::move(fieldList));
std::string jsonString;
base::JSONWriter::Write(rootDict, &jsonString);
return base::SysUTF8ToNSString(jsonString);
}
// Serializes |formData| into a JSON string that can be used by the JS side
// of PasswordController.
NSString* SerializePasswordFormFillData(
const autofill::PasswordFormFillData& formData) {
return SerializeFillData(
formData.origin, formData.action, formData.username_field.name,
formData.username_field.value, formData.password_field.name,
formData.password_field.value);
}
NSString* SerializeFillData(const FillData& fillData) {
return SerializeFillData(fillData.origin, fillData.action,
fillData.username_element, fillData.username_value,
fillData.password_element, fillData.password_value);
}
// Returns true if the trust level for the current page URL of |web_state| is
// kAbsolute. If |page_url| is not null, fills it with the current page URL.
bool GetPageURLAndCheckTrustLevel(web::WebState* web_state, GURL* page_url) {
auto trustLevel = web::URLVerificationTrustLevel::kNone;
GURL dummy;
if (!page_url)
if (!page_url) {
page_url = &dummy;
}
*page_url = web_state->GetCurrentURL(&trustLevel);
return trustLevel == web::URLVerificationTrustLevel::kAbsolute;
}
......
......@@ -23,6 +23,7 @@
#include "components/password_manager/core/browser/stub_password_manager_client.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#import "components/password_manager/ios/js_password_manager.h"
#include "components/password_manager/ios/test_helpers.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/security_state/ios/ssl_status_input_event_data.h"
......@@ -30,7 +31,6 @@
#import "ios/chrome/browser/autofill/form_suggestion_controller.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "ios/chrome/browser/passwords/password_form_filler.h"
#include "ios/chrome/browser/passwords/test_helpers.h"
#include "ios/chrome/browser/web/chrome_web_client.h"
#import "ios/chrome/browser/web/chrome_web_test.h"
#import "ios/web/public/navigation_item.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment