diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-14 01:06:31 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-14 01:06:31 +0000 |
commit | df3329492816f35746b0bff443bab94a98ff1dc2 (patch) | |
tree | eca1f89b836f75f2e785e1aafd0ed3186ae47bcf | |
parent | 144a810c11b8b921279dc639c85241048c9804bf (diff) | |
download | chromium_src-df3329492816f35746b0bff443bab94a98ff1dc2.zip chromium_src-df3329492816f35746b0bff443bab94a98ff1dc2.tar.gz chromium_src-df3329492816f35746b0bff443bab94a98ff1dc2.tar.bz2 |
Make OfflineLoadPage not refer directly to OfflineResourceHandler.
Use base::Callback to enable more flexibility in how the consumer
of OfflineLoadPage manages memory.
Review URL: http://codereview.chromium.org/9211006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117745 0039d316-1c4b-4281-b951-d872f2087c98
5 files changed, 52 insertions, 44 deletions
diff --git a/chrome/browser/chromeos/offline/offline_load_page.cc b/chrome/browser/chromeos/offline/offline_load_page.cc index 42c882a..61dce60 100644 --- a/chrome/browser/chromeos/offline/offline_load_page.cc +++ b/chrome/browser/chromeos/offline/offline_load_page.cc @@ -14,7 +14,6 @@ #include "chrome/browser/chromeos/cros/network_library.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/renderer_host/offline_resource_handler.h" #include "chrome/browser/tab_contents/tab_util.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_list.h" @@ -49,9 +48,9 @@ namespace chromeos { OfflineLoadPage::OfflineLoadPage(WebContents* web_contents, const GURL& url, - OfflineResourceHandler* handler) + const CompletionCallback& callback) : ChromeInterstitialPage(web_contents, true, url), - handler_(handler), + callback_(callback), proceeded_(false) { net::NetworkChangeNotifier::AddOnlineStateObserver(this); } @@ -166,7 +165,8 @@ void OfflineLoadPage::CommandReceived(const std::string& cmd) { } void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) { - handler_->OnBlockingPageComplete(proceed); + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed)); } void OfflineLoadPage::Proceed() { diff --git a/chrome/browser/chromeos/offline/offline_load_page.h b/chrome/browser/chromeos/offline/offline_load_page.h index e403949..75bf367 100644 --- a/chrome/browser/chromeos/offline/offline_load_page.h +++ b/chrome/browser/chromeos/offline/offline_load_page.h @@ -8,12 +8,12 @@ #include <string> +#include "base/callback.h" #include "base/compiler_specific.h" #include "chrome/browser/tab_contents/chrome_interstitial_page.h" #include "net/base/network_change_notifier.h" class Extension; -class OfflineResourceHandler; namespace base { class DictionaryValue; @@ -28,9 +28,14 @@ namespace chromeos { class OfflineLoadPage : public ChromeInterstitialPage, public net::NetworkChangeNotifier::OnlineStateObserver { public: - // Create a offline load page for the |web_contents|. + // Passed a boolean indicating whether or not it is OK to proceed with the + // page load. + typedef base::Callback<void(bool /*proceed*/)> CompletionCallback; + + // Create a offline load page for the |web_contents|. The callback will be + // run on the IO thread. OfflineLoadPage(content::WebContents* web_contents, const GURL& url, - OfflineResourceHandler* handler); + const CompletionCallback& callback); protected: virtual ~OfflineLoadPage(); @@ -60,7 +65,7 @@ class OfflineLoadPage : public ChromeInterstitialPage, // has not been activated. bool ShowActivationMessage(); - scoped_refptr<OfflineResourceHandler> handler_; + CompletionCallback callback_; // True if the proceed is chosen. bool proceeded_; diff --git a/chrome/browser/chromeos/offline/offline_load_page_unittest.cc b/chrome/browser/chromeos/offline/offline_load_page_unittest.cc index 3f2c8f6..60472ca 100644 --- a/chrome/browser/chromeos/offline/offline_load_page_unittest.cc +++ b/chrome/browser/chromeos/offline/offline_load_page_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -26,7 +26,7 @@ class TestOfflineLoadPage : public chromeos::OfflineLoadPage { TestOfflineLoadPage(TabContents* tab_contents, const GURL& url, OfflineLoadPageTest* test_page) - : chromeos::OfflineLoadPage(tab_contents, url, NULL), + : chromeos::OfflineLoadPage(tab_contents, url, CompletionCallback()), test_page_(test_page) { } diff --git a/chrome/browser/renderer_host/offline_resource_handler.cc b/chrome/browser/renderer_host/offline_resource_handler.cc index f0c4c18..7e0e635 100644 --- a/chrome/browser/renderer_host/offline_resource_handler.cc +++ b/chrome/browser/renderer_host/offline_resource_handler.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -27,6 +27,34 @@ using content::BrowserThread; using content::WebContents; +namespace { + +void ShowOfflinePage( + int render_process_id, + int render_view_id, + const GURL& url, + const chromeos::OfflineLoadPage::CompletionCallback& callback) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + // Check again on UI thread and proceed if it's connected. + if (!net::NetworkChangeNotifier::IsOffline()) { + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, base::Bind(callback, true)); + } else { + RenderViewHost* render_view_host = + RenderViewHost::FromID(render_process_id, render_view_id); + WebContents* web_contents = render_view_host ? + render_view_host->delegate()->GetAsWebContents() : NULL; + // There is a chance that the tab closed after we decided to show + // the offline page on the IO thread and before we actually show the + // offline page here on the UI thread. + if (web_contents) + (new chromeos::OfflineLoadPage(web_contents, url, callback))->Show(); + } +} + +} // namespace + OfflineResourceHandler::OfflineResourceHandler( ResourceHandler* handler, int host_id, @@ -99,7 +127,12 @@ void OfflineResourceHandler::OnCanHandleOfflineComplete(int rv) { } else { BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, - base::Bind(&OfflineResourceHandler::ShowOfflinePage, this)); + base::Bind(&ShowOfflinePage, + process_host_id_, + render_view_id_, + request_->url(), + base::Bind(&OfflineResourceHandler::OnBlockingPageComplete, + this))); } } @@ -143,14 +176,6 @@ bool OfflineResourceHandler::OnReadCompleted(int request_id, int* bytes_read) { } void OfflineResourceHandler::OnBlockingPageComplete(bool proceed) { - if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::Bind(&OfflineResourceHandler::OnBlockingPageComplete, - this, proceed)); - return; - } - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); if (deferred_request_id_ == -1) { LOG(WARNING) << "OnBlockingPageComplete called after completion: " @@ -198,22 +223,3 @@ void OfflineResourceHandler::Resume() { if (!defer) rdh_->StartDeferredRequest(process_host_id_, request_id); } - -void OfflineResourceHandler::ShowOfflinePage() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (!net::NetworkChangeNotifier::IsOffline()) { - // Check again on UI thread and proceed if it's connected. - OnBlockingPageComplete(true); - } else { - RenderViewHost* render_view_host = - RenderViewHost::FromID(process_host_id_, render_view_id_); - WebContents* web_contents = render_view_host ? - render_view_host->delegate()->GetAsWebContents() : NULL; - // There is a chance that the tab closed after we decided to show - // the offline page on the IO thread and before we actually show the - // offline page here on the UI thread. - if (web_contents) - (new chromeos::OfflineLoadPage(web_contents, deferred_url_, this))-> - Show(); - } -} diff --git a/chrome/browser/renderer_host/offline_resource_handler.h b/chrome/browser/renderer_host/offline_resource_handler.h index bebb24a..b057896 100644 --- a/chrome/browser/renderer_host/offline_resource_handler.h +++ b/chrome/browser/renderer_host/offline_resource_handler.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -48,10 +48,10 @@ class OfflineResourceHandler : public ResourceHandler { const std::string& security_info) OVERRIDE; virtual void OnRequestClosed() OVERRIDE; + private: // OfflineLoadPage callback. void OnBlockingPageComplete(bool proceed); - private: // Erase the state associated with a deferred load request. void ClearRequestInfo(); bool IsRemote(const GURL& url) const; @@ -62,9 +62,6 @@ class OfflineResourceHandler : public ResourceHandler { // True if chrome should show the offline page. bool ShouldShowOfflinePage(const GURL& url) const; - // Shows the offline interstitial page on the UI thread. - void ShowOfflinePage(); - // A callback to tell if an appcache exists. void OnCanHandleOfflineComplete(int rv); |