Chromium Code Reviews| Index: ios/chrome/browser/dom_distiller/web_state_dispatcher_impl.mm |
| diff --git a/ios/chrome/browser/dom_distiller/web_state_dispatcher_impl.mm b/ios/chrome/browser/dom_distiller/web_state_dispatcher_impl.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..38b948ae9ccee3c557214996bf37aff25c094901 |
| --- /dev/null |
| +++ b/ios/chrome/browser/dom_distiller/web_state_dispatcher_impl.mm |
| @@ -0,0 +1,61 @@ |
| +// Copyright 2016 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/dom_distiller/web_state_dispatcher_impl.h" |
| + |
| +#include "components/favicon/ios/web_favicon_driver.h" |
| +#include "components/keyed_service/core/service_access_type.h" |
| +#include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| +#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| +#include "ios/chrome/browser/favicon/favicon_service_factory.h" |
| +#include "ios/chrome/browser/history/history_service_factory.h" |
| +#import "ios/web/public/web_state/web_state.h" |
| + |
| +namespace { |
| +// Delay to download the favicon when the WebState is handed back. |
| +const int kMaxDelayFavicon = 10; |
| +} |
| + |
| +namespace dom_distiller { |
| + |
| +WebStateDispatcherImpl::WebStateDispatcherImpl(web::BrowserState* browser_state) |
| + : browser_state_(browser_state), weak_ptr_factory_(this) {} |
| +WebStateDispatcherImpl::~WebStateDispatcherImpl() {} |
| + |
| +web::WebState* WebStateDispatcherImpl::RequestWebState() { |
| + const web::WebState::CreateParams web_state_create_params(browser_state_); |
| + std::unique_ptr<web::WebState> web_state_unique = |
| + web::WebState::Create(web_state_create_params); |
| + web::WebState* web_state = web_state_unique.get(); |
| + |
| + web_states_[web_state] = std::move(web_state_unique); |
| + |
| + ios::ChromeBrowserState* original_browser_state = |
| + ios::ChromeBrowserState::FromBrowserState(browser_state_); |
| + |
| + favicon::WebFaviconDriver::CreateForWebState( |
| + web_state, |
| + ios::FaviconServiceFactory::GetForBrowserState( |
| + original_browser_state, ServiceAccessType::EXPLICIT_ACCESS), |
| + ios::HistoryServiceFactory::GetForBrowserState( |
| + original_browser_state, ServiceAccessType::EXPLICIT_ACCESS), |
| + ios::BookmarkModelFactory::GetForBrowserState(original_browser_state)); |
| + |
| + return web_state; |
| +} |
| + |
| +void WebStateDispatcherImpl::ReturnWebState(web::WebState* web_state) { |
| + base::WeakPtr<WebStateDispatcherImpl> weak_this = |
| + weak_ptr_factory_.GetWeakPtr(); |
| + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, |
| + (int64_t)(kMaxDelayFavicon * NSEC_PER_SEC)), |
|
Olivier
2016/12/16 10:31:31
Do we need a cleaner on shutdown?
gambard
2016/12/16 13:49:19
On shutdown the WebStateDispatchers should be dest
Olivier
2016/12/16 14:10:50
If the map is destroyed, the web_state is destroye
|
| + dispatch_get_main_queue(), ^{ |
| + WebStateDispatcherImpl* web_state_dispatcher = |
| + weak_this.get(); |
| + if (web_state_dispatcher) |
| + web_state_dispatcher->web_states_.erase(web_state); |
| + }); |
| +} |
| + |
| +} // namespace dom_distiller |