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

gmail_fetcher webcontent setup #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ source_set("ui") {
"tabs/shared_pinned_tab_service.h",
"tabs/shared_pinned_tab_service_factory.cc",
"tabs/shared_pinned_tab_service_factory.h",
"tabs/gmail_fetcher.cc",
"tabs/gmail_fetcher.h",
"tabs/gmail_fetcher_factory.cc",
"tabs/gmail_fetcher_factory.h",
"tabs/split_view_browser_data.cc",
"tabs/split_view_browser_data.h",
"tabs/split_view_browser_data_observer.h",
Expand Down
78 changes: 78 additions & 0 deletions browser/ui/tabs/gmail_fetcher.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "gmail_fetcher.h"

#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) {
LOG(INFO) << "GmailFetcher created with profile: " << profile;
}

// Destructor
GmailFetcher::~GmailFetcher() {
// 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;

gmail_contents_ = content::WebContents::Create(create_params);
LOG(INFO) << "Dummy WebContents created: " << gmail_contents_.get();

// Start observing WebContents for navigation events
Observe(gmail_contents_.get());

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();
LOG(INFO) << "Has committed: " << navigation_handle->HasCommitted();
LOG(INFO) << "Is same document: " << navigation_handle->IsSameDocument();
LOG(INFO) << "Is renderer initiated: " << navigation_handle->IsRendererInitiated();
LOG(INFO) << "Is main frame: " << navigation_handle->IsInMainFrame();

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.";
}
}


// Destroy Gmail WebContents
void GmailFetcher::DestroyGmailContents() {
if (gmail_contents_) {
LOG(INFO) << "Destroying WebContents: " << gmail_contents_.get();
Observe(nullptr); // Stop observing
gmail_contents_->Close();
gmail_contents_.reset();
LOG(INFO) << "WebContents destroyed";
}
}
41 changes: 41 additions & 0 deletions browser/ui/tabs/gmail_fetcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef BRAVE_BROWSER_UI_TABS_GMAIL_FETCHER_H_
#define BRAVE_BROWSER_UI_TABS_GMAIL_FETCHER_H_

#include "base/memory/weak_ptr.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"

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
};

#endif // BRAVE_BROWSER_UI_TABS_GMAIL_FETCHER_H_
44 changes: 44 additions & 0 deletions browser/ui/tabs/gmail_fetcher_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) 2023 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/tabs/gmail_fetcher_factory.h"

#include <memory>

#include "base/no_destructor.h"
#include "brave/browser/ui/tabs/gmail_fetcher.h"
#include "chrome/browser/profiles/profile.h"

// static
GmailFetcher* GmailFetcherFactory::GetForProfile(Profile* profile) {
return static_cast<GmailFetcher*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}

GmailFetcherFactory* GmailFetcherFactory::GetInstance() {
static base::NoDestructor<GmailFetcherFactory> instance;
return instance.get();
}

GmailFetcherFactory::GmailFetcherFactory()
: ProfileKeyedServiceFactory(
"GmailFetcher",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOwnInstance)
.WithGuest(ProfileSelection::kOwnInstance)
.Build()) {}

GmailFetcherFactory::~GmailFetcherFactory() {}

std::unique_ptr<KeyedService>
GmailFetcherFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
return std::make_unique<GmailFetcher>(
Profile::FromBrowserContext(context));
}

bool GmailFetcherFactory::ServiceIsCreatedWithBrowserContext() const {
return true;
}
38 changes: 38 additions & 0 deletions browser/ui/tabs/gmail_fetcher_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright (c) 2023 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_TABS_GMAIL_FETCHER_FACTORY_H_
#define BRAVE_BROWSER_UI_TABS_GMAIL_FETCHER_FACTORY_H_

#include <memory>

#include "chrome/browser/profiles/profile_keyed_service_factory.h"

namespace base {
template <typename T>
class NoDestructor;
} // namespace base

class GmailFetcher;

class GmailFetcherFactory : public ProfileKeyedServiceFactory {
public:
static GmailFetcher* GetForProfile(Profile* profile);

static GmailFetcherFactory* GetInstance();

private:
friend base::NoDestructor<GmailFetcherFactory>;

GmailFetcherFactory();
~GmailFetcherFactory() override;

// BrowserContextKeyedServiceFactory:
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override;
};

#endif // BRAVE_BROWSER_UI_TABS_GMAIL_FETCHER_FACTORY_H_
35 changes: 35 additions & 0 deletions browser/ui/views/toolbar/brave_toolbar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include "ui/base/window_open_disposition_utils.h"
#include "ui/events/event.h"
#include "ui/views/window/hit_test_utils.h"
#include "base/values.h"
#include "components/prefs/pref_service.h"
#include <vector>
#include "brave/browser/ui/tabs/gmail_fetcher.h"

#if BUILDFLAG(ENABLE_AI_CHAT)
#include "brave/browser/ai_chat/ai_chat_utils.h"
Expand Down Expand Up @@ -244,6 +248,17 @@ void BraveToolbarView::Init() {

UpdateWalletButtonVisibility();

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

custom_button_->SetTriggerableEventFlags(ui::EF_LEFT_MOUSE_BUTTON);
custom_button_->SetAccessibleName(u"Open Popup");
custom_button_->SetVisible(true);


#if BUILDFLAG(ENABLE_AI_CHAT)
// Don't check policy status since we're going to
// setup a watcher for policy pref.
Expand Down Expand Up @@ -521,5 +536,25 @@ void BraveToolbarView::UpdateWalletButtonVisibility() {
wallet_->SetVisible(false);
}

void BraveToolbarView::FetchGmailOnClick() {

Profile* profile = browser()->profile();

// Create an instance of GmailFetcher directly as a raw pointer
auto* gmail_fetcher = new GmailFetcher(base::raw_ptr<Profile>(profile));

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();
}

BEGIN_METADATA(BraveToolbarView)
END_METADATA
10 changes: 10 additions & 0 deletions browser/ui/views/toolbar/brave_toolbar_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "components/prefs/pref_member.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include <vector>
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/controls/textfield/textfield.h"
#include "components/prefs/pref_service.h"

#if BUILDFLAG(ENABLE_AI_CHAT)
class AIChatButton;
Expand Down Expand Up @@ -68,6 +72,12 @@ class BraveToolbarView : public ToolbarView,
void ResetLocationBarBounds();
void ResetButtonBounds();
void UpdateBookmarkVisibility();
std::u16string note_input_;
std::vector<std::u16string> notes_;
raw_ptr<views::Textfield> text_field_ = nullptr;
raw_ptr<PrefService> pref_service_ = nullptr;
raw_ptr<views::Button> custom_button_ = nullptr;
void FetchGmailOnClick();

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