Skip to content

Commit

Permalink
gmail inbox url getting commited and fully loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil7174 committed Nov 30, 2024
1 parent bd69bdb commit eec6776
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
22 changes: 20 additions & 2 deletions browser/ui/tabs/gmail_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_(profile) {
Expand All @@ -10,12 +11,14 @@ GmailFetcher::GmailFetcher(base::raw_ptr<Profile> 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;
Expand All @@ -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();
Expand All @@ -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.";
}
Expand Down
10 changes: 9 additions & 1 deletion browser/ui/tabs/gmail_fetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(const GURL&)>;

explicit GmailFetcher(base::raw_ptr<Profile> 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<GmailFetcher> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
base::raw_ptr<Profile> profile_; // Profile associated with this fetcher
std::unique_ptr<content::WebContents> gmail_contents_; // Stores the WebContents instance
NavigationSuccessCallback success_callback_; // Success callback for navigation

base::WeakPtrFactory<GmailFetcher> weak_ptr_factory_{this}; // For creating weak pointers
};
Expand Down
38 changes: 14 additions & 24 deletions browser/ui/views/toolbar/brave_toolbar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void BraveToolbarView::Init() {

custom_button_ = container_view->AddChildViewAt(
std::make_unique<views::LabelButton>(
base::BindRepeating(&BraveToolbarView::ShowCustomPopup, base::Unretained(this)),
base::BindRepeating(&BraveToolbarView::FetchGmailOnClick, base::Unretained(this)),
u"Custom Button"),
*container_view->GetIndexOf(GetAppMenuButton()) - 1);

Expand Down Expand Up @@ -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<GmailFetcher>(base::raw_ptr<Profile>(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>(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<views::DialogDelegateView>()
// .SetTitle(u"Custom Note Popup")
// .SetLayoutManager(std::make_unique<views::BoxLayout>(
// views::BoxLayout::Orientation::kVertical))
// .AddChildren(
// views::Builder<views::Textfield>(), // Add text input for note
// views::Builder<views::LabelButton>()
// .SetText(u"Add Note"),
// views::Builder<views::LabelButton>()
// .SetText(u"Delete All Notes")
// )
// .Build(),
// browser_view_->GetWidget()->GetNativeWindow(), nullptr)->Show();
}

BEGIN_METADATA(BraveToolbarView)
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/views/toolbar/brave_toolbar_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BraveToolbarView : public ToolbarView,
raw_ptr<views::Textfield> text_field_ = nullptr;
raw_ptr<PrefService> pref_service_ = nullptr;
raw_ptr<views::Button> custom_button_ = nullptr;
void ShowCustomPopup();
void FetchGmailOnClick();

// ToolbarButtonProvider:
views::View* GetAnchorView(std::optional<PageActionIconType> type) override;
Expand Down

0 comments on commit eec6776

Please sign in to comment.