Commit 602894ff authored by Mark Cogan's avatar Mark Cogan Committed by Commit Bot

[iOS] Factor UrlLoadingNotifier into a BrowserAgent

Changes the URL loading notifier keyed service into a browser agent.

This removes the notifier service as a direct dependency on the URL
loading service; instead the loading service fetches the notifier
when it is assigned a Browser.

Bug: 1046375
Change-Id: I302858f854d310749660922c994ae8b9238a24b9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2093441
Commit-Queue: Mark Cogan <marq@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749657}
parent c1e00312
...@@ -52,6 +52,7 @@ source_set("main") { ...@@ -52,6 +52,7 @@ source_set("main") {
"//ios/chrome/browser/sessions:session_service", "//ios/chrome/browser/sessions:session_service",
"//ios/chrome/browser/tabs", "//ios/chrome/browser/tabs",
"//ios/chrome/browser/ui/commands", "//ios/chrome/browser/ui/commands",
"//ios/chrome/browser/url_loading",
"//ios/chrome/browser/web_state_list", "//ios/chrome/browser/web_state_list",
"//ios/chrome/browser/web_state_list:agents", "//ios/chrome/browser/web_state_list:agents",
"//ios/chrome/browser/web_state_list/web_usage_enabler", "//ios/chrome/browser/web_state_list/web_usage_enabler",
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#import "ios/chrome/browser/sessions/session_restoration_browser_agent.h" #import "ios/chrome/browser/sessions/session_restoration_browser_agent.h"
#import "ios/chrome/browser/sessions/session_service_ios.h" #import "ios/chrome/browser/sessions/session_service_ios.h"
#include "ios/chrome/browser/tabs/synced_window_delegate_browser_agent.h" #include "ios/chrome/browser/tabs/synced_window_delegate_browser_agent.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#import "ios/chrome/browser/web_state_list/tab_insertion_browser_agent.h" #import "ios/chrome/browser/web_state_list/tab_insertion_browser_agent.h"
#import "ios/chrome/browser/web_state_list/web_usage_enabler/web_usage_enabler_browser_agent.h" #import "ios/chrome/browser/web_state_list/web_usage_enabler/web_usage_enabler_browser_agent.h"
#include "ios/public/provider/chrome/browser/chrome_browser_provider.h" #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
...@@ -31,6 +32,7 @@ void AttachBrowserAgents(Browser* browser) { ...@@ -31,6 +32,7 @@ void AttachBrowserAgents(Browser* browser) {
AttachInfobarOverlayBrowserAgent(browser); AttachInfobarOverlayBrowserAgent(browser);
SyncedWindowDelegateBrowserAgent::CreateForBrowser(browser); SyncedWindowDelegateBrowserAgent::CreateForBrowser(browser);
WebUsageEnablerBrowserAgent::CreateForBrowser(browser); WebUsageEnablerBrowserAgent::CreateForBrowser(browser);
UrlLoadingNotifierBrowserAgent::CreateForBrowser(browser);
// SessionRestorartionAgent requires WebUsageEnablerBrowserAgent. // SessionRestorartionAgent requires WebUsageEnablerBrowserAgent.
SessionRestorationBrowserAgent::CreateForBrowser( SessionRestorationBrowserAgent::CreateForBrowser(
......
...@@ -232,6 +232,7 @@ source_set("unit_tests") { ...@@ -232,6 +232,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/ui/toolbar/public", "//ios/chrome/browser/ui/toolbar/public",
"//ios/chrome/browser/ui/toolbar/test", "//ios/chrome/browser/ui/toolbar/test",
"//ios/chrome/browser/ui/util", "//ios/chrome/browser/ui/util",
"//ios/chrome/browser/url_loading:url_loading",
"//ios/chrome/browser/web", "//ios/chrome/browser/web",
"//ios/chrome/browser/web_state_list", "//ios/chrome/browser/web_state_list",
"//ios/chrome/browser/web_state_list:test_support", "//ios/chrome/browser/web_state_list:test_support",
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#import "ios/chrome/browser/ui/commands/browser_coordinator_commands.h" #import "ios/chrome/browser/ui/commands/browser_coordinator_commands.h"
#import "ios/chrome/browser/ui/commands/command_dispatcher.h" #import "ios/chrome/browser/ui/commands/command_dispatcher.h"
#import "ios/chrome/browser/ui/download/features.h" #import "ios/chrome/browser/ui/download/features.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#include "ios/web/public/test/web_task_environment.h" #include "ios/web/public/test/web_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h" #include "testing/gtest_mac.h"
...@@ -27,17 +28,20 @@ ...@@ -27,17 +28,20 @@
class BrowserCoordinatorTest : public PlatformTest { class BrowserCoordinatorTest : public PlatformTest {
protected: protected:
BrowserCoordinatorTest() BrowserCoordinatorTest()
: base_view_controller_([[UIViewController alloc] init]) {} : base_view_controller_([[UIViewController alloc] init]),
browser_(std::make_unique<TestBrowser>()) {
UrlLoadingNotifierBrowserAgent::CreateForBrowser(browser_.get());
}
BrowserCoordinator* GetBrowserCoordinator() { BrowserCoordinator* GetBrowserCoordinator() {
return [[BrowserCoordinator alloc] return [[BrowserCoordinator alloc]
initWithBaseViewController:base_view_controller_ initWithBaseViewController:base_view_controller_
browser:&browser_]; browser:browser_.get()];
} }
web::WebTaskEnvironment task_environment_; web::WebTaskEnvironment task_environment_;
UIViewController* base_view_controller_; UIViewController* base_view_controller_;
TestBrowser browser_; std::unique_ptr<TestBrowser> browser_;
}; };
// Tests if the URL to open the downlads directory from files.app is valid. // Tests if the URL to open the downlads directory from files.app is valid.
...@@ -64,7 +68,7 @@ TEST_F(BrowserCoordinatorTest, ShowDownloadsFolder) { ...@@ -64,7 +68,7 @@ TEST_F(BrowserCoordinatorTest, ShowDownloadsFolder) {
[browser_coordinator start]; [browser_coordinator start];
CommandDispatcher* dispatcher = browser_.GetCommandDispatcher(); CommandDispatcher* dispatcher = browser_->GetCommandDispatcher();
id<BrowserCoordinatorCommands> handler = id<BrowserCoordinatorCommands> handler =
HandlerForProtocol(dispatcher, BrowserCoordinatorCommands); HandlerForProtocol(dispatcher, BrowserCoordinatorCommands);
[handler showDownloadsFolder]; [handler showDownloadsFolder];
......
...@@ -150,8 +150,7 @@ ...@@ -150,8 +150,7 @@
#import "ios/chrome/browser/ui/voice/text_to_speech_playback_controller_factory.h" #import "ios/chrome/browser/ui/voice/text_to_speech_playback_controller_factory.h"
#include "ios/chrome/browser/upgrade/upgrade_center.h" #include "ios/chrome/browser/upgrade/upgrade_center.h"
#import "ios/chrome/browser/url_loading/image_search_param_generator.h" #import "ios/chrome/browser/url_loading/image_search_param_generator.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier.h" #import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier_factory.h"
#import "ios/chrome/browser/url_loading/url_loading_observer_bridge.h" #import "ios/chrome/browser/url_loading/url_loading_observer_bridge.h"
#import "ios/chrome/browser/url_loading/url_loading_params.h" #import "ios/chrome/browser/url_loading/url_loading_params.h"
#import "ios/chrome/browser/url_loading/url_loading_service.h" #import "ios/chrome/browser/url_loading/url_loading_service.h"
...@@ -1357,10 +1356,10 @@ NSString* const kBrowserViewControllerSnackbarCategory = ...@@ -1357,10 +1356,10 @@ NSString* const kBrowserViewControllerSnackbarCategory =
if (controller) if (controller)
controller->SetWebStateList(nullptr); controller->SetWebStateList(nullptr);
UrlLoadingNotifier* urlLoadingNotifier = UrlLoadingNotifierBrowserAgent* notifier =
UrlLoadingNotifierFactory::GetForBrowserState(self.browserState); UrlLoadingNotifierBrowserAgent::FromBrowser(self.browser);
if (urlLoadingNotifier) if (notifier)
urlLoadingNotifier->RemoveObserver(_URLLoadingObserverBridge.get()); notifier->RemoveObserver(_URLLoadingObserverBridge.get());
} }
// Uninstall delegates so that any delegate callbacks triggered by subsequent // Uninstall delegates so that any delegate callbacks triggered by subsequent
...@@ -1870,9 +1869,8 @@ NSString* const kBrowserViewControllerSnackbarCategory = ...@@ -1870,9 +1869,8 @@ NSString* const kBrowserViewControllerSnackbarCategory =
_webStateListObserver = std::make_unique<WebStateListObserverBridge>(self); _webStateListObserver = std::make_unique<WebStateListObserverBridge>(self);
self.browser->GetWebStateList()->AddObserver(_webStateListObserver.get()); self.browser->GetWebStateList()->AddObserver(_webStateListObserver.get());
_URLLoadingObserverBridge = std::make_unique<UrlLoadingObserverBridge>(self); _URLLoadingObserverBridge = std::make_unique<UrlLoadingObserverBridge>(self);
UrlLoadingNotifier* urlLoadingNotifier = UrlLoadingNotifierBrowserAgent::FromBrowser(self.browser)
UrlLoadingNotifierFactory::GetForBrowserState(self.browserState); ->AddObserver(_URLLoadingObserverBridge.get());
urlLoadingNotifier->AddObserver(_URLLoadingObserverBridge.get());
WebStateList* webStateList = self.browser->GetWebStateList(); WebStateList* webStateList = self.browser->GetWebStateList();
for (int index = 0; index < webStateList->count(); ++index) for (int index = 0; index < webStateList->count(); ++index)
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#import "ios/chrome/browser/ui/commands/page_info_commands.h" #import "ios/chrome/browser/ui/commands/page_info_commands.h"
#import "ios/chrome/browser/ui/commands/text_zoom_commands.h" #import "ios/chrome/browser/ui/commands/text_zoom_commands.h"
#include "ios/chrome/browser/ui/util/ui_util.h" #include "ios/chrome/browser/ui/util/ui_util.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#include "ios/chrome/browser/web_state_list/fake_web_state_list_delegate.h" #include "ios/chrome/browser/web_state_list/fake_web_state_list_delegate.h"
#include "ios/chrome/browser/web_state_list/web_state_list.h" #include "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/browser/web_state_list/web_state_opener.h" #import "ios/chrome/browser/web_state_list/web_state_opener.h"
...@@ -105,6 +106,7 @@ class BrowserViewControllerTest : public BlockCleanupTest { ...@@ -105,6 +106,7 @@ class BrowserViewControllerTest : public BlockCleanupTest {
browser_ = std::make_unique<TestBrowser>(chrome_browser_state_.get()); browser_ = std::make_unique<TestBrowser>(chrome_browser_state_.get());
WebUsageEnablerBrowserAgent::CreateForBrowser(browser_.get()); WebUsageEnablerBrowserAgent::CreateForBrowser(browser_.get());
UrlLoadingNotifierBrowserAgent::CreateForBrowser(browser_.get());
WebUsageEnablerBrowserAgent::FromBrowser(browser_.get()) WebUsageEnablerBrowserAgent::FromBrowser(browser_.get())
->SetWebUsageEnabled(true); ->SetWebUsageEnabled(true);
......
...@@ -13,10 +13,8 @@ source_set("url_loading") { ...@@ -13,10 +13,8 @@ source_set("url_loading") {
"test_app_url_loading_service.mm", "test_app_url_loading_service.mm",
"test_url_loading_service.h", "test_url_loading_service.h",
"test_url_loading_service.mm", "test_url_loading_service.mm",
"url_loading_notifier.h", "url_loading_notifier_browser_agent.h",
"url_loading_notifier.mm", "url_loading_notifier_browser_agent.mm",
"url_loading_notifier_factory.cc",
"url_loading_notifier_factory.h",
"url_loading_observer_bridge.h", "url_loading_observer_bridge.h",
"url_loading_observer_bridge.mm", "url_loading_observer_bridge.mm",
"url_loading_params.h", "url_loading_params.h",
...@@ -37,7 +35,7 @@ source_set("url_loading") { ...@@ -37,7 +35,7 @@ source_set("url_loading") {
"//ios/chrome/browser", "//ios/chrome/browser",
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/feature_engagement", "//ios/chrome/browser/feature_engagement",
"//ios/chrome/browser/main", "//ios/chrome/browser/main:public",
"//ios/chrome/browser/prerender", "//ios/chrome/browser/prerender",
"//ios/chrome/browser/sessions", "//ios/chrome/browser/sessions",
"//ios/chrome/browser/snapshots", "//ios/chrome/browser/snapshots",
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
class TestUrlLoadingService : public UrlLoadingService { class TestUrlLoadingService : public UrlLoadingService {
public: public:
TestUrlLoadingService(UrlLoadingNotifier* notifier); TestUrlLoadingService();
// These are the last parameters passed to |OpenUrl|. // These are the last parameters passed to |OpenUrl|.
UrlLoadParams last_params; UrlLoadParams last_params;
......
...@@ -5,14 +5,13 @@ ...@@ -5,14 +5,13 @@
#import "ios/chrome/browser/url_loading/test_url_loading_service.h" #import "ios/chrome/browser/url_loading/test_url_loading_service.h"
#import "ios/chrome/browser/ui/commands/open_new_tab_command.h" #import "ios/chrome/browser/ui/commands/open_new_tab_command.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier.h" #import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
TestUrlLoadingService::TestUrlLoadingService(UrlLoadingNotifier* notifier) TestUrlLoadingService::TestUrlLoadingService() {}
: UrlLoadingService(notifier) {}
void TestUrlLoadingService::LoadUrlInCurrentTab(const UrlLoadParams& params) { void TestUrlLoadingService::LoadUrlInCurrentTab(const UrlLoadParams& params) {
last_params = params; last_params = params;
......
...@@ -2,13 +2,11 @@ ...@@ -2,13 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_H_ #ifndef IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_BROWSER_AGENT_H_
#define IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_H_ #define IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_BROWSER_AGENT_H_
#include "base/lazy_instance.h"
#include "base/memory/ptr_util.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "components/keyed_service/core/keyed_service.h" #include "ios/chrome/browser/main/browser_user_data.h"
#include "ui/base/page_transition_types.h" #include "ui/base/page_transition_types.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -16,11 +14,15 @@ class UrlLoadingObserverBridge; ...@@ -16,11 +14,15 @@ class UrlLoadingObserverBridge;
// A class containing static functions to notify observers of url loading // A class containing static functions to notify observers of url loading
// state change. // state change.
class UrlLoadingNotifier : public KeyedService { class UrlLoadingNotifierBrowserAgent
: public BrowserUserData<UrlLoadingNotifierBrowserAgent> {
public: public:
// Creates a UrlLoadingNotifier. // Not copyable or moveable
explicit UrlLoadingNotifier(); UrlLoadingNotifierBrowserAgent(const UrlLoadingNotifierBrowserAgent&) =
~UrlLoadingNotifier() override; delete;
UrlLoadingNotifierBrowserAgent& operator=(
const UrlLoadingNotifierBrowserAgent&) = delete;
~UrlLoadingNotifierBrowserAgent() override;
// Adds |observer| to the list of observers. // Adds |observer| to the list of observers.
void AddObserver(UrlLoadingObserverBridge* observer); void AddObserver(UrlLoadingObserverBridge* observer);
...@@ -64,9 +66,11 @@ class UrlLoadingNotifier : public KeyedService { ...@@ -64,9 +66,11 @@ class UrlLoadingNotifier : public KeyedService {
void DidSwitchToTabWithUrl(const GURL& url, int new_web_state_index); void DidSwitchToTabWithUrl(const GURL& url, int new_web_state_index);
private: private:
base::ObserverList<UrlLoadingObserverBridge>::Unchecked observers_; explicit UrlLoadingNotifierBrowserAgent(Browser* browser);
friend class BrowserUserData<UrlLoadingNotifierBrowserAgent>;
BROWSER_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(UrlLoadingNotifier); base::ObserverList<UrlLoadingObserverBridge>::Unchecked observers_;
}; };
#endif // IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_H_ #endif // IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_BROWSER_AGENT_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import "ios/chrome/browser/url_loading/url_loading_notifier.h" #import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#import "ios/chrome/browser/url_loading/url_loading_observer_bridge.h" #import "ios/chrome/browser/url_loading/url_loading_observer_bridge.h"
...@@ -10,69 +10,80 @@ ...@@ -10,69 +10,80 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
UrlLoadingNotifier::UrlLoadingNotifier() {} BROWSER_USER_DATA_KEY_IMPL(UrlLoadingNotifierBrowserAgent)
UrlLoadingNotifier::~UrlLoadingNotifier() {}
void UrlLoadingNotifier::AddObserver(UrlLoadingObserverBridge* observer) { UrlLoadingNotifierBrowserAgent::UrlLoadingNotifierBrowserAgent(
Browser* browser) {}
UrlLoadingNotifierBrowserAgent::~UrlLoadingNotifierBrowserAgent() {}
void UrlLoadingNotifierBrowserAgent::AddObserver(
UrlLoadingObserverBridge* observer) {
observers_.AddObserver(observer); observers_.AddObserver(observer);
} }
void UrlLoadingNotifier::RemoveObserver(UrlLoadingObserverBridge* observer) { void UrlLoadingNotifierBrowserAgent::RemoveObserver(
UrlLoadingObserverBridge* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
void UrlLoadingNotifier::TabWillLoadUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::TabWillLoadUrl(
ui::PageTransition transition_type) { const GURL& url,
ui::PageTransition transition_type) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.TabWillLoadUrl(url, transition_type); observer.TabWillLoadUrl(url, transition_type);
} }
void UrlLoadingNotifier::TabFailedToLoadUrl( void UrlLoadingNotifierBrowserAgent::TabFailedToLoadUrl(
const GURL& url, const GURL& url,
ui::PageTransition transition_type) { ui::PageTransition transition_type) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.TabFailedToLoadUrl(url, transition_type); observer.TabFailedToLoadUrl(url, transition_type);
} }
void UrlLoadingNotifier::TabDidPrerenderUrl( void UrlLoadingNotifierBrowserAgent::TabDidPrerenderUrl(
const GURL& url, const GURL& url,
ui::PageTransition transition_type) { ui::PageTransition transition_type) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.TabDidPrerenderUrl(url, transition_type); observer.TabDidPrerenderUrl(url, transition_type);
} }
void UrlLoadingNotifier::TabDidReloadUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::TabDidReloadUrl(
ui::PageTransition transition_type) { const GURL& url,
ui::PageTransition transition_type) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.TabDidReloadUrl(url, transition_type); observer.TabDidReloadUrl(url, transition_type);
} }
void UrlLoadingNotifier::TabDidLoadUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::TabDidLoadUrl(
ui::PageTransition transition_type) { const GURL& url,
ui::PageTransition transition_type) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.TabDidLoadUrl(url, transition_type); observer.TabDidLoadUrl(url, transition_type);
} }
void UrlLoadingNotifier::NewTabWillLoadUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::NewTabWillLoadUrl(const GURL& url,
bool user_initiated) { bool user_initiated) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.NewTabWillLoadUrl(url, user_initiated); observer.NewTabWillLoadUrl(url, user_initiated);
} }
void UrlLoadingNotifier::NewTabDidLoadUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::NewTabDidLoadUrl(const GURL& url,
bool user_initiated) { bool user_initiated) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.NewTabDidLoadUrl(url, user_initiated); observer.NewTabDidLoadUrl(url, user_initiated);
} }
void UrlLoadingNotifier::WillSwitchToTabWithUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::WillSwitchToTabWithUrl(
int new_web_state_index) { const GURL& url,
int new_web_state_index) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.WillSwitchToTabWithUrl(url, new_web_state_index); observer.WillSwitchToTabWithUrl(url, new_web_state_index);
} }
void UrlLoadingNotifier::DidSwitchToTabWithUrl(const GURL& url, void UrlLoadingNotifierBrowserAgent::DidSwitchToTabWithUrl(
int new_web_state_index) { const GURL& url,
int new_web_state_index) {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.DidSwitchToTabWithUrl(url, new_web_state_index); observer.DidSwitchToTabWithUrl(url, new_web_state_index);
} }
// Copyright 2019 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 "ios/chrome/browser/url_loading/url_loading_notifier_factory.h"
#include "base/no_destructor.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/url_loading/url_loading_notifier.h"
// static
UrlLoadingNotifier* UrlLoadingNotifierFactory::GetForBrowserState(
ChromeBrowserState* browser_state) {
return static_cast<UrlLoadingNotifier*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
// static
UrlLoadingNotifierFactory* UrlLoadingNotifierFactory::GetInstance() {
static base::NoDestructor<UrlLoadingNotifierFactory> instance;
return instance.get();
}
UrlLoadingNotifierFactory::UrlLoadingNotifierFactory()
: BrowserStateKeyedServiceFactory(
"UrlLoadingNotifier",
BrowserStateDependencyManager::GetInstance()) {}
UrlLoadingNotifierFactory::~UrlLoadingNotifierFactory() {}
std::unique_ptr<KeyedService>
UrlLoadingNotifierFactory::BuildServiceInstanceFor(
web::BrowserState* context) const {
return std::make_unique<UrlLoadingNotifier>();
}
web::BrowserState* UrlLoadingNotifierFactory::GetBrowserStateToUse(
web::BrowserState* context) const {
return GetBrowserStateOwnInstanceInIncognito(context);
}
// Copyright 2019 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 IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_FACTORY_H_
#define IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_FACTORY_H_
#include <memory>
#include "base/macros.h"
#include "base/no_destructor.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
class ChromeBrowserState;
class UrlLoadingNotifier;
// Singleton that owns all UrlLoadingNotifiers and associates them with
// ChromeBrowserState.
class UrlLoadingNotifierFactory : public BrowserStateKeyedServiceFactory {
public:
static UrlLoadingNotifier* GetForBrowserState(
ChromeBrowserState* browser_state);
static UrlLoadingNotifierFactory* GetInstance();
private:
friend class base::NoDestructor<UrlLoadingNotifierFactory>;
UrlLoadingNotifierFactory();
~UrlLoadingNotifierFactory() override;
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
web::BrowserState* context) const override;
web::BrowserState* GetBrowserStateToUse(
web::BrowserState* context) const override;
DISALLOW_COPY_AND_ASSIGN(UrlLoadingNotifierFactory);
};
#endif // IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_NOTIFIER_FACTORY_H_
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
class AppUrlLoadingService; class AppUrlLoadingService;
class Browser; class Browser;
class UrlLoadingNotifier; class UrlLoadingNotifierBrowserAgent;
struct UrlLoadParams; struct UrlLoadParams;
// Objective-C delegate for UrlLoadingService. // Objective-C delegate for UrlLoadingService.
...@@ -31,7 +31,7 @@ struct UrlLoadParams; ...@@ -31,7 +31,7 @@ struct UrlLoadParams;
// Service used to load url in current or new tab. // Service used to load url in current or new tab.
class UrlLoadingService : public KeyedService { class UrlLoadingService : public KeyedService {
public: public:
UrlLoadingService(UrlLoadingNotifier* notifier); UrlLoadingService();
void SetAppService(AppUrlLoadingService* app_service); void SetAppService(AppUrlLoadingService* app_service);
void SetDelegate(id<URLLoadingServiceDelegate> delegate); void SetDelegate(id<URLLoadingServiceDelegate> delegate);
...@@ -56,7 +56,7 @@ class UrlLoadingService : public KeyedService { ...@@ -56,7 +56,7 @@ class UrlLoadingService : public KeyedService {
__weak id<URLLoadingServiceDelegate> delegate_; __weak id<URLLoadingServiceDelegate> delegate_;
AppUrlLoadingService* app_service_; AppUrlLoadingService* app_service_;
Browser* browser_; Browser* browser_;
UrlLoadingNotifier* notifier_; UrlLoadingNotifierBrowserAgent* notifier_;
}; };
#endif // IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_SERVICE_H_ #endif // IOS_CHROME_BROWSER_URL_LOADING_URL_LOADING_SERVICE_H_
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#import "ios/chrome/browser/ui/commands/open_new_tab_command.h" #import "ios/chrome/browser/ui/commands/open_new_tab_command.h"
#import "ios/chrome/browser/ui/ntp/ntp_util.h" #import "ios/chrome/browser/ui/ntp/ntp_util.h"
#import "ios/chrome/browser/url_loading/app_url_loading_service.h" #import "ios/chrome/browser/url_loading/app_url_loading_service.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier.h" #import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#import "ios/chrome/browser/url_loading/url_loading_params.h" #import "ios/chrome/browser/url_loading/url_loading_params.h"
#import "ios/chrome/browser/url_loading/url_loading_service_factory.h" #import "ios/chrome/browser/url_loading/url_loading_service_factory.h"
#import "ios/chrome/browser/url_loading/url_loading_util.h" #import "ios/chrome/browser/url_loading/url_loading_util.h"
...@@ -60,8 +60,7 @@ void InduceBrowserCrash(const GURL& url) { ...@@ -60,8 +60,7 @@ void InduceBrowserCrash(const GURL& url) {
} }
} }
UrlLoadingService::UrlLoadingService(UrlLoadingNotifier* notifier) UrlLoadingService::UrlLoadingService() {}
: notifier_(notifier) {}
void UrlLoadingService::SetAppService(AppUrlLoadingService* app_service) { void UrlLoadingService::SetAppService(AppUrlLoadingService* app_service) {
app_service_ = app_service; app_service_ = app_service;
...@@ -73,6 +72,8 @@ void UrlLoadingService::SetDelegate(id<URLLoadingServiceDelegate> delegate) { ...@@ -73,6 +72,8 @@ void UrlLoadingService::SetDelegate(id<URLLoadingServiceDelegate> delegate) {
void UrlLoadingService::SetBrowser(Browser* browser) { void UrlLoadingService::SetBrowser(Browser* browser) {
browser_ = browser; browser_ = browser;
if (browser_)
notifier_ = UrlLoadingNotifierBrowserAgent::FromBrowser(browser_);
} }
void UrlLoadingService::Load(const UrlLoadParams& params) { void UrlLoadingService::Load(const UrlLoadParams& params) {
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "ios/chrome/browser/browser_state/browser_state_otr_helper.h" #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h" #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/url_loading/test_url_loading_service.h" #include "ios/chrome/browser/url_loading/test_url_loading_service.h"
#include "ios/chrome/browser/url_loading/url_loading_notifier_factory.h"
#include "ios/chrome/browser/url_loading/url_loading_service.h" #include "ios/chrome/browser/url_loading/url_loading_service.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -31,10 +30,7 @@ UrlLoadingServiceFactory* UrlLoadingServiceFactory::GetInstance() { ...@@ -31,10 +30,7 @@ UrlLoadingServiceFactory* UrlLoadingServiceFactory::GetInstance() {
std::unique_ptr<KeyedService> BuildTestUrlLoadingService( std::unique_ptr<KeyedService> BuildTestUrlLoadingService(
web::BrowserState* context) { web::BrowserState* context) {
ChromeBrowserState* browser_state = return std::make_unique<TestUrlLoadingService>();
ChromeBrowserState::FromBrowserState(context);
return std::make_unique<TestUrlLoadingService>(
UrlLoadingNotifierFactory::GetForBrowserState(browser_state));
} }
// static // static
...@@ -46,18 +42,13 @@ UrlLoadingServiceFactory::GetDefaultFactory() { ...@@ -46,18 +42,13 @@ UrlLoadingServiceFactory::GetDefaultFactory() {
UrlLoadingServiceFactory::UrlLoadingServiceFactory() UrlLoadingServiceFactory::UrlLoadingServiceFactory()
: BrowserStateKeyedServiceFactory( : BrowserStateKeyedServiceFactory(
"UrlLoadingService", "UrlLoadingService",
BrowserStateDependencyManager::GetInstance()) { BrowserStateDependencyManager::GetInstance()) {}
DependsOn(UrlLoadingNotifierFactory::GetInstance());
}
UrlLoadingServiceFactory::~UrlLoadingServiceFactory() {} UrlLoadingServiceFactory::~UrlLoadingServiceFactory() {}
std::unique_ptr<KeyedService> UrlLoadingServiceFactory::BuildServiceInstanceFor( std::unique_ptr<KeyedService> UrlLoadingServiceFactory::BuildServiceInstanceFor(
web::BrowserState* context) const { web::BrowserState* context) const {
ChromeBrowserState* browser_state = return std::make_unique<UrlLoadingService>();
ChromeBrowserState::FromBrowserState(context);
return std::make_unique<UrlLoadingService>(
UrlLoadingNotifierFactory::GetForBrowserState(browser_state));
} }
web::BrowserState* UrlLoadingServiceFactory::GetBrowserStateToUse( web::BrowserState* UrlLoadingServiceFactory::GetBrowserStateToUse(
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#import "ios/chrome/browser/tabs/tab_model.h" #import "ios/chrome/browser/tabs/tab_model.h"
#import "ios/chrome/browser/url_loading/app_url_loading_service.h" #import "ios/chrome/browser/url_loading/app_url_loading_service.h"
#import "ios/chrome/browser/url_loading/test_app_url_loading_service.h" #import "ios/chrome/browser/url_loading/test_app_url_loading_service.h"
#import "ios/chrome/browser/url_loading/url_loading_notifier_browser_agent.h"
#import "ios/chrome/browser/url_loading/url_loading_params.h" #import "ios/chrome/browser/url_loading/url_loading_params.h"
#import "ios/chrome/browser/url_loading/url_loading_service_factory.h" #import "ios/chrome/browser/url_loading/url_loading_service_factory.h"
#include "ios/chrome/browser/web_state_list/fake_web_state_list_delegate.h" #include "ios/chrome/browser/web_state_list/fake_web_state_list_delegate.h"
...@@ -81,13 +82,15 @@ class URLLoadingServiceTest : public BlockCleanupTest { ...@@ -81,13 +82,15 @@ class URLLoadingServiceTest : public BlockCleanupTest {
WebUsageEnablerBrowserAgent::FromBrowser(otr_browser_.get()); WebUsageEnablerBrowserAgent::FromBrowser(otr_browser_.get());
otr_enabler->SetWebUsageEnabled(false); otr_enabler->SetWebUsageEnabled(false);
// Create insertion agents and configure services. // Create insertion and notifier agents and configure services.
TabInsertionBrowserAgent::CreateForBrowser(browser_.get()); TabInsertionBrowserAgent::CreateForBrowser(browser_.get());
UrlLoadingNotifierBrowserAgent::CreateForBrowser(browser_.get());
service_->SetDelegate(url_loading_delegate_); service_->SetDelegate(url_loading_delegate_);
service_->SetBrowser(browser_.get()); service_->SetBrowser(browser_.get());
service_->SetAppService(app_service_.get()); service_->SetAppService(app_service_.get());
TabInsertionBrowserAgent::CreateForBrowser(otr_browser_.get()); TabInsertionBrowserAgent::CreateForBrowser(otr_browser_.get());
UrlLoadingNotifierBrowserAgent::CreateForBrowser(otr_browser_.get());
otr_service_->SetDelegate(url_loading_delegate_); otr_service_->SetDelegate(url_loading_delegate_);
otr_service_->SetBrowser(otr_browser_.get()); otr_service_->SetBrowser(otr_browser_.get());
otr_service_->SetAppService(app_service_.get()); otr_service_->SetAppService(app_service_.get());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment