OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/browser/dom_distiller/web_state_dispatcher_impl.h" |
| 6 |
| 7 #include "components/favicon/ios/web_favicon_driver.h" |
| 8 #include "components/keyed_service/core/service_access_type.h" |
| 9 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 11 #include "ios/chrome/browser/favicon/favicon_service_factory.h" |
| 12 #include "ios/chrome/browser/history/history_service_factory.h" |
| 13 #import "ios/web/public/web_state/web_state.h" |
| 14 |
| 15 namespace { |
| 16 // Delay to download the favicon when the WebState is handed back. |
| 17 const int kMaxDelayFavicon = 10; |
| 18 } |
| 19 |
| 20 namespace dom_distiller { |
| 21 |
| 22 WebStateDispatcherImpl::WebStateDispatcherImpl(web::BrowserState* browser_state) |
| 23 : browser_state_(browser_state), weak_ptr_factory_(this) {} |
| 24 WebStateDispatcherImpl::~WebStateDispatcherImpl() {} |
| 25 |
| 26 web::WebState* WebStateDispatcherImpl::RequestWebState() { |
| 27 const web::WebState::CreateParams web_state_create_params(browser_state_); |
| 28 std::unique_ptr<web::WebState> web_state_unique = |
| 29 web::WebState::Create(web_state_create_params); |
| 30 web::WebState* web_state = web_state_unique.get(); |
| 31 |
| 32 web_states_[web_state] = std::move(web_state_unique); |
| 33 |
| 34 ios::ChromeBrowserState* original_browser_state = |
| 35 ios::ChromeBrowserState::FromBrowserState(browser_state_); |
| 36 |
| 37 favicon::WebFaviconDriver::CreateForWebState( |
| 38 web_state, |
| 39 ios::FaviconServiceFactory::GetForBrowserState( |
| 40 original_browser_state, ServiceAccessType::EXPLICIT_ACCESS), |
| 41 ios::HistoryServiceFactory::GetForBrowserState( |
| 42 original_browser_state, ServiceAccessType::EXPLICIT_ACCESS), |
| 43 ios::BookmarkModelFactory::GetForBrowserState(original_browser_state)); |
| 44 |
| 45 return web_state; |
| 46 } |
| 47 |
| 48 void WebStateDispatcherImpl::ReturnWebState(web::WebState* web_state) { |
| 49 base::WeakPtr<WebStateDispatcherImpl> weak_this = |
| 50 weak_ptr_factory_.GetWeakPtr(); |
| 51 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, |
| 52 (int64_t)(kMaxDelayFavicon * NSEC_PER_SEC)), |
| 53 dispatch_get_main_queue(), ^{ |
| 54 WebStateDispatcherImpl* web_state_dispatcher = |
| 55 weak_this.get(); |
| 56 if (web_state_dispatcher) |
| 57 web_state_dispatcher->web_states_.erase(web_state); |
| 58 }); |
| 59 } |
| 60 |
| 61 } // namespace dom_distiller |
OLD | NEW |