diff --git a/browser/ui/tabs/gmail_fetcher.cc b/browser/ui/tabs/gmail_fetcher.cc index 1b8a98cd8ac1..0b792b63dbf0 100644 --- a/browser/ui/tabs/gmail_fetcher.cc +++ b/browser/ui/tabs/gmail_fetcher.cc @@ -2,6 +2,7 @@ #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_handle.h" +#include "content/browser/web_contents/web_contents_impl.h" // Constructor GmailFetcher::GmailFetcher(base::raw_ptr profile) : profile_(profile) { @@ -10,12 +11,14 @@ GmailFetcher::GmailFetcher(base::raw_ptr profile) : profile_(profile) { // Destructor GmailFetcher::~GmailFetcher() { - DestroyGmailContents(); + // DestroyGmailContents(); LOG(INFO) << "GmailFetcher destroyed"; + LOG(INFO) << gmail_contents_.get(); } // Fetch Gmail void GmailFetcher::FetchGmail() { + LOG(INFO) << "webcontents Profile: " << profile_; content::WebContents::CreateParams create_params(profile_); create_params.initially_hidden = true; // WebContents is hidden create_params.desired_renderer_state = content::WebContents::CreateParams::kNoRendererProcess; @@ -26,12 +29,22 @@ void GmailFetcher::FetchGmail() { // Start observing WebContents for navigation events Observe(gmail_contents_.get()); - gmail_contents_->GetController().LoadURL(GURL("https://www.google.com"), content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); + gmail_contents_->GetController().LoadURL(GURL("https://mail.google.com/mail/u/0/"), content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); LOG(INFO) << "Loading Gmail URL: " << gmail_contents_->GetURL().spec(); } +void GmailFetcher::SetNavigationSuccessCallback(NavigationSuccessCallback callback) { + success_callback_ = std::move(callback); +} + + // Handle navigation completion void GmailFetcher::DidFinishNavigation(content::NavigationHandle* navigation_handle) { + + if (!gmail_contents_) { + LOG(INFO) << "Navigation handle received but WebContents is already destroyed."; + return; + } LOG(INFO) << "Navigation completed for URL: " << navigation_handle->GetURL().spec(); LOG(INFO) << "Net error code: " << navigation_handle->GetNetErrorCode(); LOG(INFO) << "Is error page: " << navigation_handle->IsErrorPage(); @@ -42,6 +55,11 @@ void GmailFetcher::DidFinishNavigation(content::NavigationHandle* navigation_han if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) { LOG(INFO) << "Navigation finished. Committed URL: " << navigation_handle->GetURL().spec(); + + // Invoke the success callback if it's set + if (success_callback_) { + success_callback_(navigation_handle->GetURL()); + } } else { LOG(INFO) << "Navigation did not commit successfully."; } diff --git a/browser/ui/tabs/gmail_fetcher.h b/browser/ui/tabs/gmail_fetcher.h index c1e310e38ab5..7413373449c8 100644 --- a/browser/ui/tabs/gmail_fetcher.h +++ b/browser/ui/tabs/gmail_fetcher.h @@ -8,24 +8,32 @@ #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_observer.h" -// GmailFetcher class declaration class GmailFetcher : public KeyedService, public content::WebContentsObserver { public: + using NavigationSuccessCallback = std::function; + explicit GmailFetcher(base::raw_ptr profile); ~GmailFetcher() override; // Initiates fetching Gmail void FetchGmail(); + // Sets the success callback + void SetNavigationSuccessCallback(NavigationSuccessCallback callback); + // Destroys the WebContents instance void DestroyGmailContents(); // content::WebContentsObserver overrides void DidFinishNavigation(content::NavigationHandle* navigation_handle) override; + base::WeakPtr GetWeakPtr() { + return weak_ptr_factory_.GetWeakPtr(); + } private: base::raw_ptr profile_; // Profile associated with this fetcher std::unique_ptr gmail_contents_; // Stores the WebContents instance + NavigationSuccessCallback success_callback_; // Success callback for navigation base::WeakPtrFactory weak_ptr_factory_{this}; // For creating weak pointers }; diff --git a/browser/ui/views/toolbar/brave_toolbar_view.cc b/browser/ui/views/toolbar/brave_toolbar_view.cc index b7b44fac5334..852da40ca0f6 100644 --- a/browser/ui/views/toolbar/brave_toolbar_view.cc +++ b/browser/ui/views/toolbar/brave_toolbar_view.cc @@ -250,7 +250,7 @@ void BraveToolbarView::Init() { custom_button_ = container_view->AddChildViewAt( std::make_unique( - base::BindRepeating(&BraveToolbarView::ShowCustomPopup, base::Unretained(this)), + base::BindRepeating(&BraveToolbarView::FetchGmailOnClick, base::Unretained(this)), u"Custom Button"), *container_view->GetIndexOf(GetAppMenuButton()) - 1); @@ -536,34 +536,24 @@ void BraveToolbarView::UpdateWalletButtonVisibility() { wallet_->SetVisible(false); } -void BraveToolbarView::ShowCustomPopup() { +void BraveToolbarView::FetchGmailOnClick() { - // Get the current profile Profile* profile = browser()->profile(); - // LOG(INFO) << "---*profile*--- "<< profile << " --------"; - // Create an instance of GmailFetcher - auto gmail_fetcher = std::make_unique(base::raw_ptr(profile)); - // LOG(INFO) << "---*gmail_fetcher*--- "<< gmail_fetcher << " --------"; + // Create an instance of GmailFetcher directly as a raw pointer + auto* gmail_fetcher = new GmailFetcher(base::raw_ptr(profile)); - // Call FetchGmail to load Gmail URL + LOG(INFO) << "Created GmailFetcher with profile: " << profile; + + // Set navigation success callback + gmail_fetcher->SetNavigationSuccessCallback([gmail_fetcher](const GURL& url) { + LOG(INFO) << "GmailFetcher navigation succeeded for URL: " << url.spec(); + gmail_fetcher->DestroyGmailContents(); + delete gmail_fetcher; + }); + + // Trigger navigation gmail_fetcher->FetchGmail(); - // Create a simple popup dialog (for demonstration purposes). - - // views::DialogDelegate::CreateDialogWidget( - // views::Builder() - // .SetTitle(u"Custom Note Popup") - // .SetLayoutManager(std::make_unique( - // views::BoxLayout::Orientation::kVertical)) - // .AddChildren( - // views::Builder(), // Add text input for note - // views::Builder() - // .SetText(u"Add Note"), - // views::Builder() - // .SetText(u"Delete All Notes") - // ) - // .Build(), - // browser_view_->GetWidget()->GetNativeWindow(), nullptr)->Show(); } BEGIN_METADATA(BraveToolbarView) diff --git a/browser/ui/views/toolbar/brave_toolbar_view.h b/browser/ui/views/toolbar/brave_toolbar_view.h index 1029826d0819..434b11d56efb 100644 --- a/browser/ui/views/toolbar/brave_toolbar_view.h +++ b/browser/ui/views/toolbar/brave_toolbar_view.h @@ -77,7 +77,7 @@ class BraveToolbarView : public ToolbarView, raw_ptr text_field_ = nullptr; raw_ptr pref_service_ = nullptr; raw_ptr custom_button_ = nullptr; - void ShowCustomPopup(); + void FetchGmailOnClick(); // ToolbarButtonProvider: views::View* GetAnchorView(std::optional type) override;