Skip to content

Commit

Permalink
Move ActiveStateManagerImpl::Observer to ActiveStateManager.
Browse files Browse the repository at this point in the history
The observer is thus exposed in ios/web/public and can notably be used
in various components.
Observing the active state allows classes that create and manipulate
WKWebViews to stop and release them when the browser state becomes
inactive and to resume their operation when it becomes active again.

BUG=

Review URL: https://codereview.chromium.org/1310193002

Cr-Commit-Position: refs/heads/master@{#345091}
  • Loading branch information
bzanotti authored and Commit bot committed Aug 24, 2015
1 parent c3c872b commit 76b3d6c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
19 changes: 2 additions & 17 deletions ios/web/active_state_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,8 @@ class ActiveStateManagerImpl : public ActiveStateManager,
// ActiveStateManager methods.
void SetActive(bool active) override;
bool IsActive() override;

// Observer that is notified when a ActiveStateManager becomes active,
// inactive or destroyed.
class Observer {
public:
// Called when the ActiveStateManager becomes active.
virtual void OnActive() = 0;
// Called when the ActiveStateManager becomes inactive.
virtual void OnInactive() = 0;
// Called just before the ActiveStateManager is destroyed.
virtual void WillBeDestroyed() = 0;
};
// Adds an observer for this class. An observer should not be added more
// than once. The caller retains the ownership of the observer object.
void AddObserver(Observer* obs);
// Removes an observer.
void RemoveObserver(Observer* obs);
void AddObserver(ActiveStateManager::Observer* observer) override;
void RemoveObserver(ActiveStateManager::Observer* observer) override;

private:
BrowserState* browser_state_; // weak, owns this object.
Expand Down
4 changes: 2 additions & 2 deletions ios/web/active_state_manager_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
return active_;
}

void ActiveStateManagerImpl::AddObserver(Observer* obs) {
void ActiveStateManagerImpl::AddObserver(ActiveStateManager::Observer* obs) {
DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
observer_list_.AddObserver(obs);
}

void ActiveStateManagerImpl::RemoveObserver(Observer* obs) {
void ActiveStateManagerImpl::RemoveObserver(ActiveStateManager::Observer* obs) {
DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
observer_list_.RemoveObserver(obs);
}
Expand Down
20 changes: 9 additions & 11 deletions ios/web/active_state_manager_impl_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
// A test fixture to test ActiveStateManagerImpl.
typedef WebTest ActiveStateManagerImplTest;

// An ActiveStateManagerImpl::Observer used for testing purposes.
class ActiveStateManagerImplObserver : public ActiveStateManagerImpl::Observer {
// An ActiveStateManager::Observer used for testing purposes.
class ActiveStateManagerObserver : public ActiveStateManager::Observer {
public:
ActiveStateManagerImplObserver() {}
virtual ~ActiveStateManagerImplObserver() {}
ActiveStateManagerObserver() {}
virtual ~ActiveStateManagerObserver() {}

// ActiveStateManagerImpl::Observer implementation.
// ActiveStateManager::Observer implementation.
MOCK_METHOD0(OnActive, void());
MOCK_METHOD0(OnInactive, void());
MOCK_METHOD0(WillBeDestroyed, void());
Expand Down Expand Up @@ -55,17 +55,15 @@
EXPECT_FALSE(active_state_manager->IsActive());
}

// Tests that ActiveStateManagerImpl::Observer are notified correctly.
// Tests that ActiveStateManager::Observer are notified correctly.
TEST_F(ActiveStateManagerImplTest, ObserverMethod) {
// |GetBrowserState()| already has its ActiveStateManager be active.
BrowserState::GetActiveStateManager(GetBrowserState())->SetActive(false);

ActiveStateManagerImplObserver observer;
ActiveStateManagerObserver observer;
TestBrowserState browser_state;
ActiveStateManagerImpl* active_state_manager =
static_cast<ActiveStateManagerImpl*>(
BrowserState::GetActiveStateManager(&browser_state));

ActiveStateManager* active_state_manager =
BrowserState::GetActiveStateManager(&browser_state);

active_state_manager->AddObserver(&observer);

Expand Down
11 changes: 5 additions & 6 deletions ios/web/browsing_data_partition_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@
#import "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "base/supports_user_data.h"
#include "ios/web/active_state_manager_impl.h"
#include "ios/web/public/active_state_manager.h"
#include "ios/web/public/browsing_data_partition.h"

@class CRWBrowsingDataStore;

namespace web {

class ActiveStateManagerImpl;
class BrowserState;

// Concrete subclass of web::BrowsingDataPartition. Observes
// ActiveStateManagerImpl::Observer methods to trigger stash/restore operations
// ActiveStateManager::Observer methods to trigger stash/restore operations
// on the underlying CRWBrowsingDataStore.
class BrowsingDataPartitionImpl : public BrowsingDataPartition,
public base::SupportsUserData::Data,
public web::ActiveStateManagerImpl::Observer {
public web::ActiveStateManager::Observer {
public:
explicit BrowsingDataPartitionImpl(BrowserState* browser_state);
~BrowsingDataPartitionImpl() override;

// BrowsingDataPartition implementation.
CRWBrowsingDataStore* GetBrowsingDataStore() override;

// ActiveStateManagerImpl::Observer implementation.
// ActiveStateManager::Observer implementation.
void OnActive() override;
void OnInactive() override;
void WillBeDestroyed() override;
Expand All @@ -41,7 +40,7 @@ class BrowsingDataPartitionImpl : public BrowsingDataPartition,
// The browsing data store backing this object.
base::scoped_nsobject<CRWBrowsingDataStore> browsing_data_store_;
// Weak pointer to the associated active state manager.
ActiveStateManagerImpl* active_state_manager_;
ActiveStateManager* active_state_manager_;

DISALLOW_COPY_AND_ASSIGN(BrowsingDataPartitionImpl);
};
Expand Down
3 changes: 1 addition & 2 deletions ios/web/browsing_data_partition_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ - (void)observeValueForKeyPath:(NSString*)keyPath
DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
DCHECK(browser_state);

active_state_manager_ = static_cast<ActiveStateManagerImpl*>(
BrowserState::GetActiveStateManager(browser_state));
active_state_manager_ = BrowserState::GetActiveStateManager(browser_state);
DCHECK(active_state_manager_);
active_state_manager_->AddObserver(this);
}
Expand Down
17 changes: 17 additions & 0 deletions ios/web/public/active_state_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ class ActiveStateManager {
// Returns true if the BrowserState is active.
virtual bool IsActive() = 0;

// Observer that is notified when a ActiveStateManager becomes active,
// inactive or destroyed.
class Observer {
public:
// Called when the ActiveStateManager becomes active.
virtual void OnActive() {}
// Called when the ActiveStateManager becomes inactive.
virtual void OnInactive() {}
// Called just before the ActiveStateManager is destroyed.
virtual void WillBeDestroyed() {}
};
// Adds an observer for this class. An observer should not be added more
// than once. The caller retains the ownership of the observer object.
virtual void AddObserver(Observer* observer) = 0;
// Removes an observer.
virtual void RemoveObserver(Observer* observer) = 0;

protected:
virtual ~ActiveStateManager(){};
};
Expand Down

0 comments on commit 76b3d6c

Please sign in to comment.