Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login feature for browser #91

Merged
merged 11 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions browser/brave_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "content/public/browser/web_contents.h"
#include "extensions/buildflags/buildflags.h"
#include "media/base/media_switches.h"
#include "ui/views/login/login_screen_view.h"

#if BUILDFLAG(ENABLE_SPEEDREADER)
#include "brave/components/speedreader/speedreader_extended_info_handler.h"
Expand Down Expand Up @@ -81,9 +82,6 @@ ChromeBrowserMainParts::ChromeBrowserMainParts(bool is_integration_test,
ChromeBrowserMainParts::~ChromeBrowserMainParts() = default;

int ChromeBrowserMainParts::PreMainMessageLoopRun() {
brave_component_updater::BraveOnDemandUpdater::GetInstance()
->RegisterOnDemandUpdater(
&g_browser_process->component_updater()->GetOnDemandUpdater());

return ChromeBrowserMainParts_ChromiumImpl::PreMainMessageLoopRun();
}
Expand All @@ -99,6 +97,20 @@ void ChromeBrowserMainParts::PreBrowserStart() {
speedreader::SpeedreaderExtendedInfoHandler::Register();
#endif

brave_component_updater::BraveOnDemandUpdater::GetInstance()
->RegisterOnDemandUpdater(
&g_browser_process->component_updater()->GetOnDemandUpdater());

// views::Widget* login_widget = new views::Widget();
// views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
// params.bounds = gfx::Rect(100, 100, 400, 300);
// login_widget->Init(std::move(params));

// LoginScreenView* login_screen = new LoginScreenView();
// login_widget->SetContentsView(login_screen);

// login_widget->Show();

ChromeBrowserMainParts_ChromiumImpl::PreBrowserStart();
}

Expand Down
11 changes: 11 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "brave/components/tor/buildflags/buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/prefetch/pref_names.h"
#include "components/prefs/pref_service.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/preloading/preloading_prefs.h"
#include "chrome/browser/ui/webui/new_tab_page/ntp_pref_names.h"
Expand Down Expand Up @@ -151,6 +152,8 @@ void RegisterProfilePrefsForMigration(
brave_wallet::RegisterProfilePrefsForMigration(registry);

// Restore "Other Bookmarks" migration
registry->RegisterBooleanPref(kLoginState, false);

registry->RegisterBooleanPref(kOtherBookmarksMigrated, false);

// Added 05/2021
Expand Down Expand Up @@ -499,4 +502,12 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
base::Value(false));
}

bool IsLoggedIn(PrefService* prefs) {
return prefs->GetBoolean(kLoginState);
}

void SetLoggedIn(PrefService* prefs, bool logged_in) {
prefs->SetBoolean(kLoginState, logged_in);
}

} // namespace brave
4 changes: 4 additions & 0 deletions browser/brave_profile_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
#ifndef BRAVE_BROWSER_BRAVE_PROFILE_PREFS_H_
#define BRAVE_BROWSER_BRAVE_PROFILE_PREFS_H_

#include "components/prefs/pref_service.h"

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace brave {

void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
bool IsLoggedIn(PrefService* prefs);
void SetLoggedIn(PrefService* prefs, bool logged_in);

} // namespace brave

Expand Down
3 changes: 3 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ source_set("ui") {
"whats_new/pref_names.h",
"whats_new/whats_new_util.cc",
"whats_new/whats_new_util.h",

]

deps += [
Expand Down Expand Up @@ -534,6 +535,8 @@ source_set("ui") {
"views/omnibox/brave_rounded_omnibox_results_frame.h",
"views/omnibox/brave_search_conversion_promotion_view.cc",
"views/omnibox/brave_search_conversion_promotion_view.h",
"views/login/login_screen_view.cc",
"views/login/login_screen_view.h",
"views/overlay/brave_back_to_tab_label_button.cc",
"views/overlay/brave_back_to_tab_label_button.h",
"views/overlay/brave_video_overlay_window_views.cc",
Expand Down
126 changes: 126 additions & 0 deletions browser/ui/views/login/login_screen_view.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/views/login/login_screen_view.h"

#include "login_screen_view.h"
#include "ui/views/layout/box_layout.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/gfx/geometry/insets.h"

LoginScreenView::LoginScreenView(Profile* profile,
base::OnceCallback<void()> on_login_success)
: profile_(profile->GetWeakPtr()),
username_field_(nullptr),
password_field_(nullptr),
login_button_(nullptr),
on_login_success_(std::move(on_login_success)) {

DCHECK(profile) << "Profile cannot be null";
CreateUI();
}

LoginScreenView::~LoginScreenView() {
LOG(INFO) << "LoginScreenView destroyed";
}

void LoginScreenView::CreateUI() {
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical,
gfx::Insets(20),
10));

auto* title = new views::Label(u"Ping Login");
title->SetHorizontalAlignment(gfx::ALIGN_CENTER);
AddChildView(title);

// Username section
auto* username_label = new views::Label(u"Username:");
AddChildView(username_label);

username_field_ = new views::Textfield();
AddChildView(username_field_);

// Password section
auto* password_label = new views::Label(u"Password:");
AddChildView(password_label);

password_field_ = new views::Textfield();
password_field_->SetTextInputType(ui::TextInputType::TEXT_INPUT_TYPE_PASSWORD);
AddChildView(password_field_);

// Login button
login_button_ = new views::MdTextButton(
base::BindRepeating(&LoginScreenView::OnLoginButtonPressed,
weak_ptr_factory_.GetWeakPtr()),
u"Login");
login_button_->SetIsDefault(true);
AddChildView(login_button_);
}

bool LoginScreenView::Cancel() {
// Closing the login dialog
CloseAndRunCallback();
return true;
}

bool LoginScreenView::Accept() {
// Returning false to prevent the default functionality of OK button
return false;
}

void LoginScreenView::CloseAndRunCallback() {
LOG(INFO) << "Processing login success";

// Taking ownership of the callback before closing the widget
auto callback = std::move(on_login_success_);
auto* widget = GetWidget();

// Posting the task to ensure safe execution order
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
[](base::OnceCallback<void()> callback,
views::Widget* widget,
base::WeakPtr<Profile> profile) {
if (!profile) {
LOG(ERROR) << "Profile no longer valid";
return;
}

LOG(INFO) << "Executing login callback";
std::move(callback).Run();

LOG(INFO) << "Closing login dialog";
if (widget)
widget->CloseNow();
},
std::move(callback),
widget,
profile_));
}

void LoginScreenView::OnLoginButtonPressed() {
if (!profile_) {
LOG(ERROR) << "Profile no longer valid during login";
return;
}

std::u16string username = username_field_->GetText();
std::u16string password = password_field_->GetText();

LOG(INFO) << "Processing login attempt";

// Test authentication
if (username == u"admin" && password == u"password") {
LOG(INFO) << "Login credentials validated";
CloseAndRunCallback();
} else {
LOG(INFO) << "Login validation failed";
// TODO: Adding user feedback
}
}
50 changes: 50 additions & 0 deletions browser/ui/views/login/login_screen_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_VIEWS_LOGIN_SCREEN_VIEW_H_
#define BRAVE_BROWSER_UI_VIEWS_LOGIN_SCREEN_VIEW_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "ui/views/window/dialog_delegate.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/label.h"
#include "chrome/browser/profiles/profile.h"
#include "base/functional/callback.h"
#include "ui/views/window/dialog_delegate.h"

class LoginScreenView : public views::DialogDelegateView {
public:
explicit LoginScreenView(Profile* profile,
base::OnceCallback<void()> on_login_success);
~LoginScreenView() override;

// Prevents copying and assignment of the dialog view
LoginScreenView(const LoginScreenView&) = delete;
LoginScreenView& operator=(const LoginScreenView&) = delete;

bool Accept() override;

bool Cancel() override;

private:
void CreateUI();

void OnLoginButtonPressed();

void CloseAndRunCallback();

base::WeakPtr<Profile> profile_;
raw_ptr<views::Textfield> username_field_;
raw_ptr<views::Textfield> password_field_;
raw_ptr<views::MdTextButton> login_button_;
base::OnceCallback<void()> on_login_success_;

// For safe callback handling on the UI thread after the dialog is closed
base::WeakPtrFactory<LoginScreenView> weak_ptr_factory_{this};
};

#endif // BRAVE_BROWSER_UI_VIEWS_LOGIN_SCREEN_VIEW_H_
Loading
Loading