-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle results that are loaded from cached tabs
Resolves brave/brave-browser#17355
- Loading branch information
Kevin Kuehler
committed
Aug 5, 2021
1 parent
c73a9df
commit 47b9a4d
Showing
11 changed files
with
226 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* Copyright 2021 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/speedreader/speedreader_extended_info_handler.h" | ||
|
||
#include <memory> | ||
|
||
#include "base/memory/ptr_util.h" | ||
#include "base/supports_user_data.h" | ||
#include "components/sessions/content/content_serialized_navigation_driver.h" | ||
#include "content/public/browser/navigation_entry.h" | ||
|
||
namespace { | ||
|
||
// This is the key we register in the extended info map. We also use it for the | ||
// navigation entry user data. | ||
constexpr char kSpeedreaderKey[] = "speedreader"; | ||
|
||
constexpr char kPageSavedReaderMode[] = "reader-mode"; | ||
constexpr char kPageSavedSpeedreaderMode[] = "speedreader-mode"; | ||
|
||
struct SpeedreaderNavigationData : public base::SupportsUserData::Data { | ||
explicit SpeedreaderNavigationData(const std::string& value) : value(value) {} | ||
std::string value; | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace speedreader { | ||
|
||
// static | ||
void SpeedreaderExtendedInfoHandler::Register() { | ||
auto* handler = new SpeedreaderExtendedInfoHandler(); | ||
sessions::ContentSerializedNavigationDriver::GetInstance() | ||
->RegisterExtendedInfoHandler(kSpeedreaderKey, base::WrapUnique(handler)); | ||
} | ||
|
||
// static | ||
void SpeedreaderExtendedInfoHandler::PersistSpeedreaderMode( | ||
content::NavigationEntry* entry) { | ||
entry->SetUserData( | ||
kSpeedreaderKey, | ||
std::make_unique<SpeedreaderNavigationData>(kPageSavedSpeedreaderMode)); | ||
} | ||
|
||
// static | ||
void SpeedreaderExtendedInfoHandler::PersistReaderMode( | ||
content::NavigationEntry* entry) { | ||
entry->SetUserData( | ||
kSpeedreaderKey, | ||
std::make_unique<SpeedreaderNavigationData>(kPageSavedReaderMode)); | ||
} | ||
|
||
// static | ||
void SpeedreaderExtendedInfoHandler::ClearPersistedData( | ||
content::NavigationEntry* entry) { | ||
entry->RemoveUserData(kSpeedreaderKey); | ||
} | ||
|
||
// static | ||
bool SpeedreaderExtendedInfoHandler::IsCachedSpeedreaderMode( | ||
content::NavigationEntry* entry) { | ||
auto* data = static_cast<SpeedreaderNavigationData*>( | ||
entry->GetUserData(kSpeedreaderKey)); | ||
if (!data) { | ||
return false; | ||
} | ||
return data->value == kPageSavedSpeedreaderMode; | ||
} | ||
|
||
// static | ||
bool SpeedreaderExtendedInfoHandler::IsCachedReaderMode( | ||
content::NavigationEntry* entry) { | ||
auto* data = static_cast<SpeedreaderNavigationData*>( | ||
entry->GetUserData(kSpeedreaderKey)); | ||
if (!data) { | ||
return false; | ||
} | ||
return data->value == kPageSavedReaderMode; | ||
} | ||
|
||
std::string SpeedreaderExtendedInfoHandler::GetExtendedInfo( | ||
content::NavigationEntry* entry) const { | ||
auto* data = static_cast<SpeedreaderNavigationData*>( | ||
entry->GetUserData(kSpeedreaderKey)); | ||
return data ? data->value : std::string(); | ||
} | ||
|
||
void SpeedreaderExtendedInfoHandler::RestoreExtendedInfo( | ||
const std::string& info, | ||
content::NavigationEntry* entry) { | ||
entry->SetUserData(kSpeedreaderKey, | ||
std::make_unique<SpeedreaderNavigationData>(info)); | ||
} | ||
|
||
} // namespace speedreader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright 2021 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_SPEEDREADER_SPEEDREADER_EXTENDED_INFO_HANDLER_H_ | ||
#define BRAVE_BROWSER_SPEEDREADER_SPEEDREADER_EXTENDED_INFO_HANDLER_H_ | ||
|
||
#include <string> | ||
|
||
#include "components/sessions/content/extended_info_handler.h" | ||
|
||
namespace content { | ||
class NavigationEntry; | ||
} // namespace content | ||
|
||
namespace speedreader { | ||
|
||
// This class is meant to persist data to a content::NavigationEntry so that | ||
// distilled pages will be recognized on a restored session. | ||
class SpeedreaderExtendedInfoHandler : public sessions::ExtendedInfoHandler { | ||
public: | ||
// Register the extended info handler. | ||
// Calling this more than once will cause a crash. | ||
static void Register(); | ||
|
||
static void PersistSpeedreaderMode(content::NavigationEntry* entry); | ||
static void PersistReaderMode(content::NavigationEntry* entry); | ||
static void ClearPersistedData(content::NavigationEntry* entry); | ||
|
||
static bool IsCachedSpeedreaderMode(content::NavigationEntry* entry); | ||
static bool IsCachedReaderMode(content::NavigationEntry* entry); | ||
|
||
SpeedreaderExtendedInfoHandler(const SpeedreaderExtendedInfoHandler&) = | ||
delete; | ||
SpeedreaderExtendedInfoHandler& operator=( | ||
const SpeedreaderExtendedInfoHandler&) = delete; | ||
|
||
SpeedreaderExtendedInfoHandler() = default; | ||
~SpeedreaderExtendedInfoHandler() override = default; | ||
|
||
std::string GetExtendedInfo(content::NavigationEntry* entry) const override; | ||
void RestoreExtendedInfo(const std::string& info, | ||
content::NavigationEntry* entry) override; | ||
}; | ||
|
||
} // namespace speedreader | ||
|
||
#endif // BRAVE_BROWSER_SPEEDREADER_SPEEDREADER_EXTENDED_INFO_HANDLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.