Skip to content

Commit

Permalink
[Notes] Create UserNotesTabHelper
Browse files Browse the repository at this point in the history
Creates the UserNotesTabHelper class that monitors Primary Page changes
and navigations for the WebContents it's attached to. When the Primary
Page changes, a UserNotesManager is attached to it (via PageUserData).
When a navigation occurs (excluding errors, non-committed, same-document
or outside the primary main frame), the UserNoteService is notified so
it can deal with notes for the new page.

The UserNotesTabHelper is only attached if the User Notes feature is
enabled, if the session isn't off the record, and if the platform is
non-mobile.

Also adds the service method that the tab helper must use to notify
the service of navigations (left unimplemented for now).

Bug: 1303900
Change-Id: Id700fe0c920c4076a3ce76949371508857a25826
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3558830
Reviewed-by: David Bokan <bokan@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Commit-Queue: Guillaume Jenkins <gujen@google.com>
Cr-Commit-Position: refs/heads/main@{#989076}
  • Loading branch information
guillaumejenkins authored and Chromium LUCI CQ committed Apr 5, 2022
1 parent 639a247 commit a288ef2
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 4 deletions.
2 changes: 2 additions & 0 deletions chrome/browser/BUILD.gn
Expand Up @@ -4406,6 +4406,8 @@ static_library("browser") {
"user_notes/user_note_service_delegate_impl.h",
"user_notes/user_note_service_factory.cc",
"user_notes/user_note_service_factory.h",
"user_notes/user_notes_tab_helper.cc",
"user_notes/user_notes_tab_helper.h",
"webauthn/authenticator_list_observer.h",
"webauthn/authenticator_reference.cc",
"webauthn/authenticator_reference.h",
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/ui/BUILD.gn
Expand Up @@ -1777,6 +1777,7 @@ static_library("ui") {
"//components/services/app_service/public/mojom",
"//components/ui_metrics",
"//components/url_formatter",
"//components/user_notes:features",
"//components/vector_icons",
"//components/web_modal",
"//components/webauthn/content/browser",
Expand Down
9 changes: 5 additions & 4 deletions chrome/browser/ui/tab_helpers.cc
Expand Up @@ -96,6 +96,7 @@
#include "chrome/browser/ui/tab_dialogs.h"
#include "chrome/browser/ui/tab_ui_helper.h"
#include "chrome/browser/ui/thumbnails/thumbnail_tab_helper.h"
#include "chrome/browser/user_notes/user_notes_tab_helper.h"
#include "chrome/browser/vr/vr_tab_helper.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_features.h"
Expand Down Expand Up @@ -134,6 +135,7 @@
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/tracing/common/tracing_switches.h"
#include "components/ukm/content/source_url_recorder.h"
#include "components/user_notes/user_notes_features.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "content/public/browser/web_contents.h"
#include "extensions/buildflags/buildflags.h"
Expand Down Expand Up @@ -530,11 +532,10 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
accuracy_tips::features::kAccuracyTipsSurveyFeature)) {
HatsHelper::CreateForWebContents(web_contents);
}
#endif

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
BUILDFLAG(IS_CHROMEOS)
SharedHighlightingPromo::CreateForWebContents(web_contents);
if (user_notes::IsUserNotesEnabled() && !profile->IsOffTheRecord()) {
user_notes::UserNotesTabHelper::CreateForWebContents(web_contents);
}
#endif

#if BUILDFLAG(IS_WIN)
Expand Down
47 changes: 47 additions & 0 deletions chrome/browser/user_notes/user_notes_tab_helper.cc
@@ -0,0 +1,47 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/user_notes/user_notes_tab_helper.h"

#include "chrome/browser/user_notes/user_note_service_factory.h"
#include "components/user_notes/browser/user_note_service.h"
#include "components/user_notes/browser/user_notes_manager.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"

namespace user_notes {

UserNotesTabHelper::UserNotesTabHelper(content::WebContents* web_contents)
: WebContentsObserver(web_contents),
content::WebContentsUserData<UserNotesTabHelper>(*web_contents) {}

UserNotesTabHelper::~UserNotesTabHelper() = default;

void UserNotesTabHelper::PrimaryPageChanged(content::Page& page) {
if (!UserNotesManager::GetForPage(page)) {
UserNotesManager::CreateForPage(page, service()->GetSafeRef());
}
}

void UserNotesTabHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Note: `DidFinishNavigation` is needed in addition to `PrimaryPageChanged`
// because of non-main frame navigations.
if (!navigation_handle->HasCommitted() ||
navigation_handle->IsSameDocument()) {
return;
}
service()->OnFrameNavigated(navigation_handle->GetRenderFrameHost());
}

UserNoteService* UserNotesTabHelper::service() const {
return UserNoteServiceFactory::GetForContext(
web_contents()->GetBrowserContext());
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(UserNotesTabHelper);

} // namespace user_notes
51 changes: 51 additions & 0 deletions chrome/browser/user_notes/user_notes_tab_helper.h
@@ -0,0 +1,51 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_USER_NOTES_USER_NOTES_TAB_HELPER_H_
#define CHROME_BROWSER_USER_NOTES_USER_NOTES_TAB_HELPER_H_

#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

namespace content {
class NavigationHandle;
class Page;
class WebContents;
} // namespace content

namespace user_notes {

class UserNoteService;

// An observer of WebContents that attaches User Notes page data to primary
// pages, and notifies the User Notes service when new URLs are navigated to so
// it can display notes.
class UserNotesTabHelper
: public content::WebContentsObserver,
public content::WebContentsUserData<UserNotesTabHelper> {
public:
UserNotesTabHelper(const UserNotesTabHelper&) = delete;
UserNotesTabHelper& operator=(const UserNotesTabHelper&) = delete;

~UserNotesTabHelper() override;

private:
friend class content::WebContentsUserData<UserNotesTabHelper>;

explicit UserNotesTabHelper(content::WebContents* web_contents);

UserNoteService* service() const;

// WebContentsObserver implementation.
void PrimaryPageChanged(content::Page& page) override;

void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;

WEB_CONTENTS_USER_DATA_KEY_DECL();
};

} // namespace user_notes

#endif // CHROME_BROWSER_USER_NOTES_USER_NOTES_TAB_HELPER_H_
17 changes: 17 additions & 0 deletions components/user_notes/browser/user_note_service.cc
Expand Up @@ -7,6 +7,7 @@
#include "base/notreached.h"
#include "components/user_notes/browser/user_notes_manager.h"
#include "components/user_notes/user_notes_features.h"
#include "content/public/browser/render_frame_host.h"

namespace user_notes {

Expand All @@ -20,6 +21,22 @@ base::SafeRef<UserNoteService> UserNoteService::GetSafeRef() {
return weak_ptr_factory_.GetSafeRef();
}

void UserNoteService::OnFrameNavigated(content::RenderFrameHost* rfh) {
DCHECK(IsUserNotesEnabled());

// For now, Notes are only supported in the main frame.
if (!rfh->IsInPrimaryMainFrame()) {
return;
}

if (rfh->GetPage().GetMainDocument().IsErrorDocument()) {
return;
}

DCHECK(UserNotesManager::GetForPage(rfh->GetPage()));
NOTIMPLEMENTED();
}

void UserNoteService::OnNoteInstanceAddedToPage(
const base::UnguessableToken& id,
UserNotesManager* manager) {
Expand Down
9 changes: 9 additions & 0 deletions components/user_notes/browser/user_note_service.h
Expand Up @@ -20,6 +20,10 @@
#include "components/user_notes/interfaces/user_notes_ui_delegate.h"
#include "components/user_notes/model/user_note.h"

namespace content {
class RenderFrameHost;
} // namespace content

namespace user_notes {

class UserNotesManager;
Expand All @@ -35,6 +39,11 @@ class UserNoteService : public KeyedService, public UserNotesUIDelegate {

base::SafeRef<UserNoteService> GetSafeRef();

// Called by the embedder when a frame navigates to a new URL. Queries the
// storage to find notes associated with that URL, and if there are any, kicks
// off the logic to display them in the page.
void OnFrameNavigated(content::RenderFrameHost* rfh);

// Called by |UserNotesManager| objects when a |UserNoteInstance| is added to
// the page they're attached to. Updates the model map to add a ref to the
// given |UserNotesManager| for the note with the specified ID.
Expand Down
2 changes: 2 additions & 0 deletions components/user_notes/browser/user_notes_manager.cc
Expand Up @@ -67,4 +67,6 @@ void UserNotesManager::AddNoteInstance(std::unique_ptr<UserNoteInstance> note) {
instance_map_.emplace(note->model().id(), std::move(note));
}

PAGE_USER_DATA_KEY_IMPL(UserNotesManager);

} // namespace user_notes

0 comments on commit a288ef2

Please sign in to comment.