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/favicon_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 // Default delay to download the favicon when the WebState is handed back. |
| 17 const int64_t kDefaultDelayFaviconSecond = 10; |
| 18 } |
| 19 |
| 20 namespace dom_distiller { |
| 21 |
| 22 FaviconWebStateDispatcherImpl::FaviconWebStateDispatcherImpl( |
| 23 web::BrowserState* browser_state, |
| 24 int64_t keep_alive_second) |
| 25 : FaviconWebStateDispatcher(), |
| 26 browser_state_(browser_state), |
| 27 keep_alive_second_(keep_alive_second), |
| 28 weak_ptr_factory_(this) { |
| 29 if (keep_alive_second_ < 0) |
| 30 keep_alive_second_ = kDefaultDelayFaviconSecond; |
| 31 } |
| 32 |
| 33 FaviconWebStateDispatcherImpl::~FaviconWebStateDispatcherImpl() {} |
| 34 |
| 35 web::WebState* FaviconWebStateDispatcherImpl::RequestWebState() { |
| 36 const web::WebState::CreateParams web_state_create_params(browser_state_); |
| 37 std::unique_ptr<web::WebState> web_state_unique = |
| 38 web::WebState::Create(web_state_create_params); |
| 39 web::WebState* web_state = web_state_unique.get(); |
| 40 |
| 41 web_states_.push_back(std::move(web_state_unique)); |
| 42 |
| 43 ios::ChromeBrowserState* original_browser_state = |
| 44 ios::ChromeBrowserState::FromBrowserState(browser_state_); |
| 45 |
| 46 favicon::WebFaviconDriver::CreateForWebState( |
| 47 web_state, |
| 48 ios::FaviconServiceFactory::GetForBrowserState( |
| 49 original_browser_state, ServiceAccessType::EXPLICIT_ACCESS), |
| 50 ios::HistoryServiceFactory::GetForBrowserState( |
| 51 original_browser_state, ServiceAccessType::EXPLICIT_ACCESS), |
| 52 ios::BookmarkModelFactory::GetForBrowserState(original_browser_state)); |
| 53 |
| 54 return web_state; |
| 55 } |
| 56 |
| 57 void FaviconWebStateDispatcherImpl::ReturnWebState(web::WebState* web_state) { |
| 58 base::WeakPtr<FaviconWebStateDispatcherImpl> weak_this = |
| 59 weak_ptr_factory_.GetWeakPtr(); |
| 60 dispatch_after( |
| 61 dispatch_time(DISPATCH_TIME_NOW, keep_alive_second_ * NSEC_PER_SEC), |
| 62 dispatch_get_main_queue(), ^{ |
| 63 FaviconWebStateDispatcherImpl* web_state_dispatcher = weak_this.get(); |
| 64 if (web_state_dispatcher) { |
| 65 auto it = find_if( |
| 66 web_state_dispatcher->web_states_.begin(), |
| 67 web_state_dispatcher->web_states_.end(), |
| 68 [web_state](std::unique_ptr<web::WebState>& unique_web_state) { |
| 69 return unique_web_state.get() == web_state; |
| 70 }); |
| 71 if (it != web_state_dispatcher->web_states_.end()) |
| 72 web_state_dispatcher->web_states_.erase(it); |
| 73 } |
| 74 }); |
| 75 } |
| 76 |
| 77 } // namespace dom_distiller |
OLD | NEW |