Commit 01b9e426 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Remove use of NotificationService from android TabModel.

Bug: 268984
Change-Id: Ie9244c091c4852722e5d7a42b3a0882b3938655d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1804825Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701329}
parent a4406936
...@@ -91,7 +91,7 @@ ChromeTracingDelegate::~ChromeTracingDelegate() { ...@@ -91,7 +91,7 @@ ChromeTracingDelegate::~ChromeTracingDelegate() {
void ChromeTracingDelegate::OnTabModelAdded() { void ChromeTracingDelegate::OnTabModelAdded() {
for (TabModelList::const_iterator i = TabModelList::begin(); for (TabModelList::const_iterator i = TabModelList::begin();
i != TabModelList::end(); i++) { i != TabModelList::end(); i++) {
if ((*i)->IsOffTheRecord()) if ((*i)->GetProfile()->IsOffTheRecord())
incognito_launched_ = true; incognito_launched_ = true;
} }
} }
......
...@@ -6,15 +6,11 @@ ...@@ -6,15 +6,11 @@
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/glue/synced_window_delegate_android.h" #include "chrome/browser/sync/glue/synced_window_delegate_android.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h" #include "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory.h" #include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory.h"
#include "components/omnibox/browser/location_bar_model_impl.h" #include "components/omnibox/browser/location_bar_model_impl.h"
#include "content/public/browser/notification_service.h"
using content::NotificationService;
// Keep this in sync with // Keep this in sync with
// chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabList.java // chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabList.java
...@@ -26,36 +22,16 @@ TabModel::TabModel(Profile* profile, bool is_tabbed_activity) ...@@ -26,36 +22,16 @@ TabModel::TabModel(Profile* profile, bool is_tabbed_activity)
synced_window_delegate_( synced_window_delegate_(
new browser_sync::SyncedWindowDelegateAndroid(this, new browser_sync::SyncedWindowDelegateAndroid(this,
is_tabbed_activity)), is_tabbed_activity)),
session_id_(SessionID::NewUnique()) { session_id_(SessionID::NewUnique()) {}
if (profile) {
// A normal Profile creates an OTR profile if it does not exist when
// GetOffTheRecordProfile() is called, so we guard it with
// HasOffTheRecordProfile(). An OTR profile returns itself when you call
// GetOffTheRecordProfile().
is_off_the_record_ = (profile->HasOffTheRecordProfile() &&
profile == profile->GetOffTheRecordProfile());
// A profile can be destroyed, for example in the case of closing all
// incognito tabs. We therefore must listen for when this happens, and
// remove our pointer to the profile accordingly.
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
} else {
is_off_the_record_ = false;
}
}
TabModel::~TabModel() { TabModel::~TabModel() = default;
}
Profile* TabModel::GetProfile() const { Profile* TabModel::GetProfile() const {
return profile_; return profile_;
} }
bool TabModel::IsOffTheRecord() const { bool TabModel::IsOffTheRecord() const {
return is_off_the_record_; return GetProfile()->IsOffTheRecord();
} }
sync_sessions::SyncedWindowDelegate* TabModel::GetSyncedWindowDelegate() const { sync_sessions::SyncedWindowDelegate* TabModel::GetSyncedWindowDelegate() const {
...@@ -78,40 +54,9 @@ content::WebContents* TabModel::GetActiveWebContents() const { ...@@ -78,40 +54,9 @@ content::WebContents* TabModel::GetActiveWebContents() const {
} }
void TabModel::BroadcastSessionRestoreComplete() { void TabModel::BroadcastSessionRestoreComplete() {
if (profile_) { sync_sessions::SyncSessionsWebContentsRouter* router =
sync_sessions::SyncSessionsWebContentsRouter* router = sync_sessions::SyncSessionsWebContentsRouterFactory::GetForProfile(
sync_sessions::SyncSessionsWebContentsRouterFactory::GetForProfile( GetProfile());
profile_); if (router)
if (router) router->NotifySessionRestoreComplete();
router->NotifySessionRestoreComplete();
} else {
// TODO(nyquist): Uncomment this once downstream Android uses new
// constructor that takes a Profile* argument. See crbug.com/159704.
// NOTREACHED();
}
}
void TabModel::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_PROFILE_DESTROYED:
// Our profile just got destroyed, so we delete our pointer to it.
profile_ = NULL;
break;
case chrome::NOTIFICATION_PROFILE_CREATED:
// Our incognito tab model out lives the profile, so we need to recapture
// the pointer if ours was previously deleted.
// NOTIFICATION_PROFILE_DESTROYED is not sent for every destruction, so
// we overwrite the pointer regardless of whether it's NULL.
if (is_off_the_record_) {
Profile* profile = content::Source<Profile>(source).ptr();
if (profile && profile->IsOffTheRecord())
profile_ = profile;
}
break;
default:
NOTREACHED();
}
} }
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
#include "components/omnibox/browser/location_bar_model_delegate.h" #include "components/omnibox/browser/location_bar_model_delegate.h"
#include "components/sessions/core/session_id.h" #include "components/sessions/core/session_id.h"
#include "components/sync_sessions/synced_window_delegate.h" #include "components/sync_sessions/synced_window_delegate.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
struct NavigateParams; struct NavigateParams;
...@@ -37,7 +35,7 @@ class TabModelObserver; ...@@ -37,7 +35,7 @@ class TabModelObserver;
// Abstract representation of a Tab Model for Android. Since Android does // Abstract representation of a Tab Model for Android. Since Android does
// not use Browser/BrowserList, this is required to allow Chrome to interact // not use Browser/BrowserList, this is required to allow Chrome to interact
// with Android's Tabs and Tab Model. // with Android's Tabs and Tab Model.
class TabModel : public content::NotificationObserver { class TabModel {
public: public:
// Various ways tabs can be launched. // Various ways tabs can be launched.
// Values must be numbered from 0 and can't have gaps. // Values must be numbered from 0 and can't have gaps.
...@@ -147,7 +145,7 @@ class TabModel : public content::NotificationObserver { ...@@ -147,7 +145,7 @@ class TabModel : public content::NotificationObserver {
protected: protected:
explicit TabModel(Profile* profile, bool is_tabbed_activity); explicit TabModel(Profile* profile, bool is_tabbed_activity);
~TabModel() override; virtual ~TabModel();
// Instructs the TabModel to broadcast a notification that all tabs are now // Instructs the TabModel to broadcast a notification that all tabs are now
// loaded from storage. // loaded from storage.
...@@ -156,21 +154,12 @@ class TabModel : public content::NotificationObserver { ...@@ -156,21 +154,12 @@ class TabModel : public content::NotificationObserver {
LocationBarModel* GetLocationBarModel(); LocationBarModel* GetLocationBarModel();
private: private:
// Determines how TabModel will interact with the profile.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// The profile associated with this TabModel.
Profile* profile_; Profile* profile_;
// The LiveTabContext associated with TabModel. // The LiveTabContext associated with TabModel.
// Used to restore closed tabs through the TabRestoreService. // Used to restore closed tabs through the TabRestoreService.
std::unique_ptr<AndroidLiveTabContext> live_tab_context_; std::unique_ptr<AndroidLiveTabContext> live_tab_context_;
// Describes if this TabModel contains an off-the-record profile.
bool is_off_the_record_;
// The SyncedWindowDelegate associated with this TabModel. // The SyncedWindowDelegate associated with this TabModel.
std::unique_ptr<browser_sync::SyncedWindowDelegateAndroid> std::unique_ptr<browser_sync::SyncedWindowDelegateAndroid>
synced_window_delegate_; synced_window_delegate_;
...@@ -180,9 +169,6 @@ class TabModel : public content::NotificationObserver { ...@@ -180,9 +169,6 @@ class TabModel : public content::NotificationObserver {
// across sessions. // across sessions.
SessionID session_id_; SessionID session_id_;
// The Registrar used to register TabModel for notifications.
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(TabModel); DISALLOW_COPY_AND_ASSIGN(TabModel);
}; };
......
...@@ -34,11 +34,6 @@ using content::WebContents; ...@@ -34,11 +34,6 @@ using content::WebContents;
namespace { namespace {
static Profile* FindProfile(jboolean is_incognito) { static Profile* FindProfile(jboolean is_incognito) {
if (g_browser_process == NULL ||
g_browser_process->profile_manager() == NULL) {
LOG(ERROR) << "Browser process or profile manager not initialized";
return NULL;
}
Profile* profile = ProfileManager::GetActiveUserProfile(); Profile* profile = ProfileManager::GetActiveUserProfile();
if (is_incognito) if (is_incognito)
return profile->GetOffTheRecordProfile(); return profile->GetOffTheRecordProfile();
......
// Copyright (c) 2012 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/ui/android/tab_model/tab_model.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class TabModelTest : public testing::Test {
content::BrowserTaskEnvironment task_environment_;
};
namespace {
class TabModelAndroidProfileMock : public TestingProfile {
public:
TabModelAndroidProfileMock() {}
virtual ~TabModelAndroidProfileMock() {}
MOCK_METHOD0(GetOffTheRecordProfile, Profile*());
MOCK_METHOD0(HasOffTheRecordProfile, bool());
};
class TestTabModel : public TabModel {
public:
explicit TestTabModel(Profile* profile) : TabModel(profile, false) {}
int GetTabCount() const override { return 0; }
int GetActiveIndex() const override { return 0; }
content::WebContents* GetWebContentsAt(int index) const override {
return NULL;
}
void CreateTab(TabAndroid* parent,
content::WebContents* web_contents) override {}
void HandlePopupNavigation(TabAndroid* parent,
NavigateParams* params) override {}
content::WebContents* CreateNewTabForDevTools(const GURL& url) override {
return NULL;
}
bool IsSessionRestoreInProgress() const override { return false; }
bool IsCurrentModel() const override { return false; }
TabAndroid* GetTabAt(int index) const override { return NULL; }
void SetActiveIndex(int index) override {}
void CloseTabAt(int index) override {}
void AddObserver(TabModelObserver* observer) override {}
void RemoveObserver(TabModelObserver* observer) override {}
};
} // namespace
TEST_F(TabModelTest, TestProfileHandling) {
// Construct TabModel with standard Profile.
TestingProfile testing_profile;
TestTabModel tab_model(&testing_profile);
// Verify TabModel has the correct profile and profile type.
EXPECT_EQ(&testing_profile, tab_model.GetProfile());
EXPECT_FALSE(tab_model.IsOffTheRecord());
// Notify profile is being destroyed and verify pointer is cleared.
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(&testing_profile),
content::NotificationService::NoDetails());
EXPECT_EQ(NULL, tab_model.GetProfile());
}
TEST_F(TabModelTest, TestProfileHandlingOffTheRecord) {
// Construct TabModel with off-the-record Profile.
TabModelAndroidProfileMock testing_profile;
EXPECT_CALL(testing_profile, HasOffTheRecordProfile())
.WillOnce(testing::Return(true));
EXPECT_CALL(testing_profile, GetOffTheRecordProfile())
.WillOnce(testing::Return(&testing_profile));
TestTabModel tab_model(&testing_profile);
// Verify TabModel has the correct profile and profile type.
EXPECT_EQ(&testing_profile, tab_model.GetProfile());
EXPECT_TRUE(tab_model.IsOffTheRecord());
// Notify profile is being destroyed and verify pointer is cleared.
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(&testing_profile),
content::NotificationService::NoDetails());
EXPECT_EQ(NULL, tab_model.GetProfile());
}
...@@ -3273,7 +3273,6 @@ test("unit_tests") { ...@@ -3273,7 +3273,6 @@ test("unit_tests") {
"../browser/translate/translate_fake_page.cc", "../browser/translate/translate_fake_page.cc",
"../browser/translate/translate_service_unittest.cc", "../browser/translate/translate_service_unittest.cc",
"../browser/ui/android/tab_model/tab_model_list_unittest.cc", "../browser/ui/android/tab_model/tab_model_list_unittest.cc",
"../browser/ui/android/tab_model/tab_model_unittest.cc",
"../browser/ui/android/toolbar/location_bar_model_android_unittest.cc", "../browser/ui/android/toolbar/location_bar_model_android_unittest.cc",
"../browser/ui/autofill/autofill_popup_layout_model_unittest.cc", "../browser/ui/autofill/autofill_popup_layout_model_unittest.cc",
"../browser/ui/autofill/popup_view_common_unittest.cc", "../browser/ui/autofill/popup_view_common_unittest.cc",
......
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