| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/observer_list_threadsafe.h" | 12 #include "base/observer_list.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate.h" | 15 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate.h" |
| 16 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate_factory.h" | 16 #include "chrome/browser/extensions/api/passwords_private/passwords_private_dele
gate_factory.h" |
| 17 #include "chrome/browser/extensions/extension_apitest.h" | 17 #include "chrome/browser/extensions/extension_apitest.h" |
| 18 #include "chrome/common/extensions/api/passwords_private.h" | 18 #include "chrome/common/extensions/api/passwords_private.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 22 #include "extensions/common/switches.h" | 22 #include "extensions/common/switches.h" |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 static const size_t kNumMocks = 3; | 28 static const size_t kNumMocks = 3; |
| 29 static const int kNumCharactersInPassword = 10; | 29 static const int kNumCharactersInPassword = 10; |
| 30 static const char kPlaintextPassword[] = "plaintext"; | 30 static const char kPlaintextPassword[] = "plaintext"; |
| 31 | 31 |
| 32 linked_ptr<api::passwords_private::PasswordUiEntry> CreateEntry(size_t num) { | 32 api::passwords_private::PasswordUiEntry CreateEntry(size_t num) { |
| 33 api::passwords_private::PasswordUiEntry* entry = | 33 api::passwords_private::PasswordUiEntry entry; |
| 34 new api::passwords_private::PasswordUiEntry(); | |
| 35 std::stringstream ss; | 34 std::stringstream ss; |
| 36 ss << "http://test" << num << ".com"; | 35 ss << "http://test" << num << ".com"; |
| 37 entry->login_pair.origin_url = ss.str(); | 36 entry.login_pair.origin_url = ss.str(); |
| 38 ss.clear(); | 37 ss.clear(); |
| 39 ss << "testName" << num; | 38 ss << "testName" << num; |
| 40 entry->login_pair.username = ss.str(); | 39 entry.login_pair.username = ss.str(); |
| 41 entry->num_characters_in_password = kNumCharactersInPassword; | 40 entry.num_characters_in_password = kNumCharactersInPassword; |
| 42 return make_linked_ptr(entry); | 41 return entry; |
| 43 } | 42 } |
| 44 | 43 |
| 45 std::string CreateException(size_t num) { | 44 std::string CreateException(size_t num) { |
| 46 std::stringstream ss; | 45 std::stringstream ss; |
| 47 ss << "http://exception" << num << ".com"; | 46 ss << "http://exception" << num << ".com"; |
| 48 return ss.str(); | 47 return ss.str(); |
| 49 } | 48 } |
| 50 | 49 |
| 51 // A test PasswordsPrivateDelegate implementation which uses mock data. | 50 // A test PasswordsPrivateDelegate implementation which uses mock data. |
| 52 // TestDelegate starts out with kNumMocks mocks of each type (saved password | 51 // TestDelegate starts out with kNumMocks mocks of each type (saved password |
| 53 // and password exception) and removes one mock each time RemoveSavedPassword() | 52 // and password exception) and removes one mock each time RemoveSavedPassword() |
| 54 // or RemovePasswordException() is called. | 53 // or RemovePasswordException() is called. |
| 55 class TestDelegate : public PasswordsPrivateDelegate { | 54 class TestDelegate : public PasswordsPrivateDelegate { |
| 56 public: | 55 public: |
| 57 TestDelegate() : observers_(new base::ObserverListThreadSafe<Observer>()) { | 56 TestDelegate() { |
| 58 // Create mock data. | 57 // Create mock data. |
| 59 for (size_t i = 0; i < kNumMocks; i++) { | 58 for (size_t i = 0; i < kNumMocks; i++) { |
| 60 current_entries_.push_back(CreateEntry(i)); | 59 current_entries_.push_back(CreateEntry(i)); |
| 61 current_exceptions_.push_back(CreateException(i)); | 60 current_exceptions_.push_back(CreateException(i)); |
| 62 } | 61 } |
| 63 } | 62 } |
| 64 ~TestDelegate() override {} | 63 ~TestDelegate() override {} |
| 65 | 64 |
| 66 void AddObserver(Observer* observer) override { | 65 void AddObserver(Observer* observer) override { |
| 67 observers_->AddObserver(observer); | 66 observers_.AddObserver(observer); |
| 68 SendSavedPasswordsList(); | 67 SendSavedPasswordsList(); |
| 69 SendPasswordExceptionsList(); | 68 SendPasswordExceptionsList(); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void RemoveObserver(Observer* observer) override { | 71 void RemoveObserver(Observer* observer) override { |
| 73 observers_->RemoveObserver(observer); | 72 observers_.RemoveObserver(observer); |
| 74 } | 73 } |
| 75 | 74 |
| 76 void RemoveSavedPassword( | 75 void RemoveSavedPassword( |
| 77 const std::string& origin_url, const std::string& username) override { | 76 const std::string& origin_url, const std::string& username) override { |
| 78 if (!current_entries_.size()) | 77 if (!current_entries_.size()) |
| 79 return; | 78 return; |
| 80 | 79 |
| 81 // Since this is just mock data, remove the first entry regardless of | 80 // Since this is just mock data, remove the first entry regardless of |
| 82 // the data contained. | 81 // the data contained. |
| 83 current_entries_.erase(current_entries_.begin()); | 82 current_entries_.erase(current_entries_.begin()); |
| 84 SendSavedPasswordsList(); | 83 SendSavedPasswordsList(); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void RemovePasswordException(const std::string& exception_url) override { | 86 void RemovePasswordException(const std::string& exception_url) override { |
| 88 if (!current_exceptions_.size()) | 87 if (!current_exceptions_.size()) |
| 89 return; | 88 return; |
| 90 | 89 |
| 91 // Since this is just mock data, remove the first entry regardless of | 90 // Since this is just mock data, remove the first entry regardless of |
| 92 // the data contained. | 91 // the data contained. |
| 93 current_exceptions_.erase(current_exceptions_.begin()); | 92 current_exceptions_.erase(current_exceptions_.begin()); |
| 94 SendPasswordExceptionsList(); | 93 SendPasswordExceptionsList(); |
| 95 } | 94 } |
| 96 | 95 |
| 97 void RequestShowPassword(const std::string& origin_url, | 96 void RequestShowPassword(const std::string& origin_url, |
| 98 const std::string& username, | 97 const std::string& username, |
| 99 content::WebContents* web_contents) override { | 98 content::WebContents* web_contents) override { |
| 100 // Return a mocked password value. | 99 // Return a mocked password value. |
| 101 std::string plaintext_password(kPlaintextPassword); | 100 std::string plaintext_password(kPlaintextPassword); |
| 102 observers_->Notify( | 101 FOR_EACH_OBSERVER( |
| 103 FROM_HERE, | 102 Observer, observers_, |
| 104 &Observer::OnPlaintextPasswordFetched, | 103 OnPlaintextPasswordFetched(origin_url, username, plaintext_password)); |
| 105 origin_url, | |
| 106 username, | |
| 107 plaintext_password); | |
| 108 } | 104 } |
| 109 | 105 |
| 110 private: | 106 private: |
| 111 void SendSavedPasswordsList() { | 107 void SendSavedPasswordsList() { |
| 112 observers_->Notify( | 108 FOR_EACH_OBSERVER(Observer, observers_, |
| 113 FROM_HERE, | 109 OnSavedPasswordsListChanged(current_entries_)); |
| 114 &Observer::OnSavedPasswordsListChanged, | |
| 115 current_entries_); | |
| 116 } | 110 } |
| 117 | 111 |
| 118 void SendPasswordExceptionsList() { | 112 void SendPasswordExceptionsList() { |
| 119 observers_->Notify( | 113 FOR_EACH_OBSERVER(Observer, observers_, |
| 120 FROM_HERE, | 114 OnPasswordExceptionsListChanged(current_exceptions_)); |
| 121 &Observer::OnPasswordExceptionsListChanged, | |
| 122 current_exceptions_); | |
| 123 } | 115 } |
| 124 | 116 |
| 125 // The current list of entries/exceptions. Cached here so that when new | 117 // The current list of entries/exceptions. Cached here so that when new |
| 126 // observers are added, this delegate can send the current lists without | 118 // observers are added, this delegate can send the current lists without |
| 127 // having to request them from |password_manager_presenter_| again. | 119 // having to request them from |password_manager_presenter_| again. |
| 128 std::vector<linked_ptr<api::passwords_private::PasswordUiEntry>> | 120 std::vector<api::passwords_private::PasswordUiEntry> current_entries_; |
| 129 current_entries_; | |
| 130 std::vector<std::string> current_exceptions_; | 121 std::vector<std::string> current_exceptions_; |
| 131 | 122 |
| 132 // The observers. | 123 // The observers. |
| 133 scoped_refptr<base::ObserverListThreadSafe<Observer>> observers_; | 124 base::ObserverList<Observer> observers_; |
| 134 }; | 125 }; |
| 135 | 126 |
| 136 class PasswordsPrivateApiTest : public ExtensionApiTest { | 127 class PasswordsPrivateApiTest : public ExtensionApiTest { |
| 137 public: | 128 public: |
| 138 PasswordsPrivateApiTest() { | 129 PasswordsPrivateApiTest() { |
| 139 if (!s_test_delegate_) { | 130 if (!s_test_delegate_) { |
| 140 s_test_delegate_ = new TestDelegate(); | 131 s_test_delegate_ = new TestDelegate(); |
| 141 } | 132 } |
| 142 } | 133 } |
| 143 ~PasswordsPrivateApiTest() override {} | 134 ~PasswordsPrivateApiTest() override {} |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 178 |
| 188 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RemovePasswordException) { | 179 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RemovePasswordException) { |
| 189 EXPECT_TRUE(RunPasswordsSubtest("removePasswordException")) << message_; | 180 EXPECT_TRUE(RunPasswordsSubtest("removePasswordException")) << message_; |
| 190 } | 181 } |
| 191 | 182 |
| 192 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RequestPlaintextPassword) { | 183 IN_PROC_BROWSER_TEST_F(PasswordsPrivateApiTest, RequestPlaintextPassword) { |
| 193 EXPECT_TRUE(RunPasswordsSubtest("requestPlaintextPassword")) << message_; | 184 EXPECT_TRUE(RunPasswordsSubtest("requestPlaintextPassword")) << message_; |
| 194 } | 185 } |
| 195 | 186 |
| 196 } // namespace extensions | 187 } // namespace extensions |
| OLD | NEW |