summaryrefslogtreecommitdiffstats
path: root/chrome/browser/appcache
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-27 14:34:12 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-27 14:34:12 +0000
commit0082d7e0d4531edc2a923ec9ca990c74397cdae2 (patch)
treea669316a8a234b44f39ebf085d5b5785d7357ce9 /chrome/browser/appcache
parent1b7e93b7e3ac1f74f61a83d6c6eb1575d427b578 (diff)
downloadchromium_src-0082d7e0d4531edc2a923ec9ca990c74397cdae2.zip
chromium_src-0082d7e0d4531edc2a923ec9ca990c74397cdae2.tar.gz
chromium_src-0082d7e0d4531edc2a923ec9ca990c74397cdae2.tar.bz2
Continue removing bad dependency of chrome/common on chrome/browser
TEST=none BUG=none Review URL: http://codereview.chromium.org/661178 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40224 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/appcache')
-rw-r--r--chrome/browser/appcache/appcache_dispatcher_host.cc195
-rw-r--r--chrome/browser/appcache/appcache_dispatcher_host.h78
-rw-r--r--chrome/browser/appcache/appcache_frontend_proxy.cc22
-rw-r--r--chrome/browser/appcache/appcache_frontend_proxy.h32
-rw-r--r--chrome/browser/appcache/chrome_appcache_service.cc97
-rw-r--r--chrome/browser/appcache/chrome_appcache_service.h54
6 files changed, 478 insertions, 0 deletions
diff --git a/chrome/browser/appcache/appcache_dispatcher_host.cc b/chrome/browser/appcache/appcache_dispatcher_host.cc
new file mode 100644
index 0000000..ee4ba3a
--- /dev/null
+++ b/chrome/browser/appcache/appcache_dispatcher_host.cc
@@ -0,0 +1,195 @@
+// Copyright (c) 2009 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 "chrome/browser/appcache/appcache_dispatcher_host.h"
+
+#include "base/callback.h"
+#include "chrome/browser/appcache/chrome_appcache_service.h"
+#include "chrome/browser/renderer_host/browser_render_process_host.h"
+#include "chrome/browser/net/chrome_url_request_context.h"
+#include "chrome/common/render_messages.h"
+
+AppCacheDispatcherHost::AppCacheDispatcherHost(
+ URLRequestContextGetter* request_context_getter)
+ : request_context_getter_(request_context_getter),
+ process_handle_(0) {
+ DCHECK(request_context_getter_.get());
+}
+
+void AppCacheDispatcherHost::Initialize(IPC::Message::Sender* sender,
+ int process_id, base::ProcessHandle process_handle) {
+ DCHECK(sender);
+ DCHECK(process_handle && !process_handle_);
+ DCHECK(request_context_getter_.get());
+
+ process_handle_ = process_handle;
+
+ // Get the AppCacheService (it can only be accessed from IO thread).
+ URLRequestContext* context = request_context_getter_->GetURLRequestContext();
+ appcache_service_ =
+ static_cast<ChromeURLRequestContext*>(context)->appcache_service();
+ request_context_getter_ = NULL;
+
+ frontend_proxy_.set_sender(sender);
+ if (appcache_service_.get()) {
+ backend_impl_.Initialize(
+ appcache_service_.get(), &frontend_proxy_, process_id);
+ get_status_callback_.reset(
+ NewCallback(this, &AppCacheDispatcherHost::GetStatusCallback));
+ start_update_callback_.reset(
+ NewCallback(this, &AppCacheDispatcherHost::StartUpdateCallback));
+ swap_cache_callback_.reset(
+ NewCallback(this, &AppCacheDispatcherHost::SwapCacheCallback));
+ }
+}
+
+bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& msg,
+ bool *msg_ok) {
+ DCHECK(process_handle_);
+ *msg_ok = true;
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP_EX(AppCacheDispatcherHost, msg, *msg_ok)
+ IPC_MESSAGE_HANDLER(AppCacheMsg_RegisterHost, OnRegisterHost);
+ IPC_MESSAGE_HANDLER(AppCacheMsg_UnregisterHost, OnUnregisterHost);
+ IPC_MESSAGE_HANDLER(AppCacheMsg_SelectCache, OnSelectCache);
+ IPC_MESSAGE_HANDLER(AppCacheMsg_MarkAsForeignEntry, OnMarkAsForeignEntry);
+ IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheMsg_GetStatus, OnGetStatus);
+ IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheMsg_StartUpdate, OnStartUpdate);
+ IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheMsg_SwapCache, OnSwapCache);
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP_EX()
+ return handled;
+}
+
+void AppCacheDispatcherHost::OnRegisterHost(int host_id) {
+ if (appcache_service_.get()) {
+ if (!backend_impl_.RegisterHost(host_id)) {
+ ReceivedBadMessage(AppCacheMsg_RegisterHost::ID);
+ }
+ }
+}
+
+void AppCacheDispatcherHost::OnUnregisterHost(int host_id) {
+ if (appcache_service_.get()) {
+ if (!backend_impl_.UnregisterHost(host_id)) {
+ ReceivedBadMessage(AppCacheMsg_UnregisterHost::ID);
+ }
+ }
+}
+
+void AppCacheDispatcherHost::OnSelectCache(
+ int host_id, const GURL& document_url,
+ int64 cache_document_was_loaded_from,
+ const GURL& opt_manifest_url) {
+ if (appcache_service_.get()) {
+ if (!backend_impl_.SelectCache(host_id, document_url,
+ cache_document_was_loaded_from,
+ opt_manifest_url)) {
+ ReceivedBadMessage(AppCacheMsg_SelectCache::ID);
+ }
+ } else {
+ frontend_proxy_.OnCacheSelected(
+ host_id, appcache::kNoCacheId, appcache::UNCACHED);
+ }
+}
+
+void AppCacheDispatcherHost::OnMarkAsForeignEntry(
+ int host_id, const GURL& document_url,
+ int64 cache_document_was_loaded_from) {
+ if (appcache_service_.get()) {
+ if (!backend_impl_.MarkAsForeignEntry(host_id, document_url,
+ cache_document_was_loaded_from)) {
+ ReceivedBadMessage(AppCacheMsg_MarkAsForeignEntry::ID);
+ }
+ }
+}
+
+void AppCacheDispatcherHost::OnGetStatus(int host_id,
+ IPC::Message* reply_msg) {
+ if (pending_reply_msg_.get()) {
+ ReceivedBadMessage(AppCacheMsg_GetStatus::ID);
+ delete reply_msg;
+ return;
+ }
+
+ pending_reply_msg_.reset(reply_msg);
+ if (appcache_service_.get()) {
+ if (!backend_impl_.GetStatusWithCallback(
+ host_id, get_status_callback_.get(), reply_msg)) {
+ ReceivedBadMessage(AppCacheMsg_GetStatus::ID);
+ }
+ return;
+ }
+
+ GetStatusCallback(appcache::UNCACHED, reply_msg);
+}
+
+void AppCacheDispatcherHost::OnStartUpdate(int host_id,
+ IPC::Message* reply_msg) {
+ if (pending_reply_msg_.get()) {
+ ReceivedBadMessage(AppCacheMsg_StartUpdate::ID);
+ delete reply_msg;
+ return;
+ }
+
+ pending_reply_msg_.reset(reply_msg);
+ if (appcache_service_.get()) {
+ if (!backend_impl_.StartUpdateWithCallback(
+ host_id, start_update_callback_.get(), reply_msg)) {
+ ReceivedBadMessage(AppCacheMsg_StartUpdate::ID);
+ }
+ return;
+ }
+
+ StartUpdateCallback(false, reply_msg);
+}
+
+void AppCacheDispatcherHost::OnSwapCache(int host_id,
+ IPC::Message* reply_msg) {
+ if (pending_reply_msg_.get()) {
+ ReceivedBadMessage(AppCacheMsg_SwapCache::ID);
+ delete reply_msg;
+ return;
+ }
+
+ pending_reply_msg_.reset(reply_msg);
+ if (appcache_service_.get()) {
+ if (!backend_impl_.SwapCacheWithCallback(
+ host_id, swap_cache_callback_.get(), reply_msg)) {
+ ReceivedBadMessage(AppCacheMsg_SwapCache::ID);
+ }
+ return;
+ }
+
+ SwapCacheCallback(false, reply_msg);
+}
+
+void AppCacheDispatcherHost::GetStatusCallback(
+ appcache::Status status, void* param) {
+ IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param);
+ DCHECK(reply_msg == pending_reply_msg_.get());
+ AppCacheMsg_GetStatus::WriteReplyParams(reply_msg, status);
+ frontend_proxy_.sender()->Send(pending_reply_msg_.release());
+}
+
+void AppCacheDispatcherHost::StartUpdateCallback(bool result, void* param) {
+ IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param);
+ DCHECK(reply_msg == pending_reply_msg_.get());
+ AppCacheMsg_StartUpdate::WriteReplyParams(reply_msg, result);
+ frontend_proxy_.sender()->Send(pending_reply_msg_.release());
+}
+
+void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) {
+ IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param);
+ DCHECK(reply_msg == pending_reply_msg_.get());
+ AppCacheMsg_SwapCache::WriteReplyParams(reply_msg, result);
+ frontend_proxy_.sender()->Send(pending_reply_msg_.release());
+}
+
+void AppCacheDispatcherHost::ReceivedBadMessage(uint32 msg_type) {
+ // TODO(michaeln): Consider gathering UMA stats
+ // http://code.google.com/p/chromium/issues/detail?id=24634
+ BrowserRenderProcessHost::BadMessageTerminateProcess(
+ msg_type, process_handle_);
+}
diff --git a/chrome/browser/appcache/appcache_dispatcher_host.h b/chrome/browser/appcache/appcache_dispatcher_host.h
new file mode 100644
index 0000000..ea2fe93
--- /dev/null
+++ b/chrome/browser/appcache/appcache_dispatcher_host.h
@@ -0,0 +1,78 @@
+// Copyright (c) 2009 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.
+
+#ifndef CHROME_BROWSER_APPCACHE_APPCACHE_DISPATCHER_HOST_H_
+#define CHROME_BROWSER_APPCACHE_APPCACHE_DISPATCHER_HOST_H_
+
+#include <vector>
+
+#include "base/process.h"
+#include "base/ref_counted.h"
+#include "base/scoped_ptr.h"
+#include "chrome/browser/appcache/appcache_frontend_proxy.h"
+#include "ipc/ipc_message.h"
+#include "webkit/appcache/appcache_backend_impl.h"
+
+class ChromeAppCacheService;
+class URLRequestContextGetter;
+
+// Handles appcache related messages sent to the main browser process from
+// its child processes. There is a distinct host for each child process.
+// Messages are handled on the IO thread. The ResourceMessageFilter creates
+// an instance and delegates calls to it.
+class AppCacheDispatcherHost {
+ public:
+ explicit AppCacheDispatcherHost(
+ URLRequestContextGetter* request_context_getter);
+
+ void Initialize(IPC::Message::Sender* sender, int process_id,
+ base::ProcessHandle process_handle);
+ bool OnMessageReceived(const IPC::Message& msg, bool* msg_is_ok);
+
+ int process_id() const { return backend_impl_.process_id(); }
+
+ // Note: needed to satisfy ipc message dispatching macros.
+ bool Send(IPC::Message* msg) {
+ return frontend_proxy_.sender()->Send(msg);
+ }
+
+ private:
+ // Ipc message handlers
+ void OnRegisterHost(int host_id);
+ void OnUnregisterHost(int host_id);
+ void OnSelectCache(int host_id, const GURL& document_url,
+ int64 cache_document_was_loaded_from,
+ const GURL& opt_manifest_url);
+ void OnMarkAsForeignEntry(int host_id, const GURL& document_url,
+ int64 cache_document_was_loaded_from);
+ void OnGetStatus(int host_id, IPC::Message* reply_msg);
+ void OnStartUpdate(int host_id, IPC::Message* reply_msg);
+ void OnSwapCache(int host_id, IPC::Message* reply_msg);
+
+ void GetStatusCallback(appcache::Status status, void* param);
+ void StartUpdateCallback(bool result, void* param);
+ void SwapCacheCallback(bool result, void* param);
+
+ void ReceivedBadMessage(uint32 msg_type);
+
+ AppCacheFrontendProxy frontend_proxy_;
+ appcache::AppCacheBackendImpl backend_impl_;
+
+ // Temporary until Initialize() can be called from the IO thread,
+ // which will extract the AppCacheService from the URLRequestContext.
+ scoped_refptr<URLRequestContextGetter> request_context_getter_;
+
+ // This is only valid once Initialize() has been called.
+ scoped_refptr<ChromeAppCacheService> appcache_service_;
+
+ scoped_ptr<appcache::GetStatusCallback> get_status_callback_;
+ scoped_ptr<appcache::StartUpdateCallback> start_update_callback_;
+ scoped_ptr<appcache::SwapCacheCallback> swap_cache_callback_;
+ base::ProcessHandle process_handle_;
+ scoped_ptr<IPC::Message> pending_reply_msg_;
+
+ DISALLOW_COPY_AND_ASSIGN(AppCacheDispatcherHost);
+};
+
+#endif // CHROME_BROWSER_APPCACHE_APPCACHE_DISPATCHER_HOST_H_
diff --git a/chrome/browser/appcache/appcache_frontend_proxy.cc b/chrome/browser/appcache/appcache_frontend_proxy.cc
new file mode 100644
index 0000000..c0448592
--- /dev/null
+++ b/chrome/browser/appcache/appcache_frontend_proxy.cc
@@ -0,0 +1,22 @@
+// Copyright (c) 2009 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 "chrome/browser/appcache/appcache_frontend_proxy.h"
+
+#include "chrome/common/render_messages.h"
+
+void AppCacheFrontendProxy::OnCacheSelected(int host_id, int64 cache_id ,
+ appcache::Status status) {
+ sender_->Send(new AppCacheMsg_CacheSelected(host_id, cache_id, status));
+}
+
+void AppCacheFrontendProxy::OnStatusChanged(const std::vector<int>& host_ids,
+ appcache::Status status) {
+ sender_->Send(new AppCacheMsg_StatusChanged(host_ids, status));
+}
+
+void AppCacheFrontendProxy::OnEventRaised(const std::vector<int>& host_ids,
+ appcache::EventID event_id) {
+ sender_->Send(new AppCacheMsg_EventRaised(host_ids, event_id));
+}
diff --git a/chrome/browser/appcache/appcache_frontend_proxy.h b/chrome/browser/appcache/appcache_frontend_proxy.h
new file mode 100644
index 0000000..78865ab
--- /dev/null
+++ b/chrome/browser/appcache/appcache_frontend_proxy.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2009 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.
+
+#ifndef CHROME_BROWSER_APPCACHE_APPCACHE_FRONTEND_PROXY_H_
+#define CHROME_BROWSER_APPCACHE_APPCACHE_FRONTEND_PROXY_H_
+
+#include <vector>
+
+#include "ipc/ipc_message.h"
+#include "webkit/appcache/appcache_interfaces.h"
+
+// Sends appcache related messages to a child process.
+class AppCacheFrontendProxy : public appcache::AppCacheFrontend {
+ public:
+ AppCacheFrontendProxy() : sender_(NULL) {}
+ void set_sender(IPC::Message::Sender* sender) { sender_ = sender; }
+ IPC::Message::Sender* sender() const { return sender_; }
+
+ // AppCacheFrontend methods
+ virtual void OnCacheSelected(int host_id, int64 cache_id ,
+ appcache::Status);
+ virtual void OnStatusChanged(const std::vector<int>& host_ids,
+ appcache::Status status);
+ virtual void OnEventRaised(const std::vector<int>& host_ids,
+ appcache::EventID event_id);
+
+ private:
+ IPC::Message::Sender* sender_;
+};
+
+#endif // CHROME_BROWSER_APPCACHE_APPCACHE_FRONTEND_PROXY_H_
diff --git a/chrome/browser/appcache/chrome_appcache_service.cc b/chrome/browser/appcache/chrome_appcache_service.cc
new file mode 100644
index 0000000..8a5160a
--- /dev/null
+++ b/chrome/browser/appcache/chrome_appcache_service.cc
@@ -0,0 +1,97 @@
+// Copyright (c) 2010 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 "chrome/browser/appcache/chrome_appcache_service.h"
+
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/net/chrome_url_request_context.h"
+#include "chrome/common/chrome_constants.h"
+#include "chrome/common/notification_service.h"
+#include "net/base/net_errors.h"
+#include "webkit/appcache/appcache_thread.h"
+
+static bool has_initialized_thread_ids;
+
+ChromeAppCacheService::ChromeAppCacheService(
+ const FilePath& profile_path,
+ ChromeURLRequestContext* request_context) {
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
+ DCHECK(request_context);
+
+ if (!has_initialized_thread_ids) {
+ has_initialized_thread_ids = true;
+ appcache::AppCacheThread::InitIDs(ChromeThread::DB, ChromeThread::IO);
+ }
+
+ host_contents_settings_map_ = request_context->host_content_settings_map();
+ registrar_.Add(
+ this, NotificationType::PURGE_MEMORY, NotificationService::AllSources());
+
+ // Init our base class.
+ Initialize(request_context->is_off_the_record() ?
+ FilePath() : profile_path.Append(chrome::kAppCacheDirname));
+ set_request_context(request_context);
+ set_appcache_policy(this);
+}
+
+ChromeAppCacheService::~ChromeAppCacheService() {
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
+}
+
+// static
+void ChromeAppCacheService::ClearLocalState(const FilePath& profile_path) {
+ file_util::Delete(profile_path.Append(chrome::kAppCacheDirname), true);
+}
+
+bool ChromeAppCacheService::CanLoadAppCache(const GURL& manifest_url) {
+ ContentSetting setting = host_contents_settings_map_->GetContentSetting(
+ manifest_url, CONTENT_SETTINGS_TYPE_COOKIES);
+ DCHECK(setting != CONTENT_SETTING_DEFAULT);
+ return setting == CONTENT_SETTING_ALLOW ||
+ setting == CONTENT_SETTING_ASK; // we don't prompt for read access
+}
+
+int ChromeAppCacheService::CanCreateAppCache(
+ const GURL& manifest_url, net::CompletionCallback* callback) {
+ ContentSetting setting = host_contents_settings_map_->GetContentSetting(
+ manifest_url, CONTENT_SETTINGS_TYPE_COOKIES);
+ DCHECK(setting != CONTENT_SETTING_DEFAULT);
+ if (setting == CONTENT_SETTING_ASK) {
+ // TODO(michaeln): prompt the user, for now we block
+ setting = CONTENT_SETTING_BLOCK;
+ }
+ return (setting != CONTENT_SETTING_BLOCK) ? net::OK : net::ERR_ACCESS_DENIED;
+}
+
+void ChromeAppCacheService::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK(type == NotificationType::PURGE_MEMORY);
+ PurgeMemory();
+}
+
+static ChromeThread::ID ToChromeThreadID(int id) {
+ DCHECK(has_initialized_thread_ids);
+ DCHECK(id == ChromeThread::DB || id == ChromeThread::IO);
+ return static_cast<ChromeThread::ID>(id);
+}
+
+namespace appcache {
+
+// An impl of AppCacheThread we need to provide to the appcache lib.
+
+bool AppCacheThread::PostTask(
+ int id,
+ const tracked_objects::Location& from_here,
+ Task* task) {
+ return ChromeThread::PostTask(ToChromeThreadID(id), from_here, task);
+}
+
+bool AppCacheThread::CurrentlyOn(int id) {
+ return ChromeThread::CurrentlyOn(ToChromeThreadID(id));
+}
+
+} // namespace appcache
diff --git a/chrome/browser/appcache/chrome_appcache_service.h b/chrome/browser/appcache/chrome_appcache_service.h
new file mode 100644
index 0000000..848a83f
--- /dev/null
+++ b/chrome/browser/appcache/chrome_appcache_service.h
@@ -0,0 +1,54 @@
+// Copyright (c) 2010 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.
+
+#ifndef CHROME_BROWSER_APPCACHE_CHROME_APPCACHE_SERVICE_H_
+#define CHROME_BROWSER_APPCACHE_CHROME_APPCACHE_SERVICE_H_
+
+#include "base/ref_counted.h"
+#include "chrome/browser/host_content_settings_map.h"
+#include "chrome/common/notification_registrar.h"
+#include "webkit/appcache/appcache_policy.h"
+#include "webkit/appcache/appcache_service.h"
+
+class ChromeURLRequestContext;
+class FilePath;
+
+// An AppCacheService subclass used by the chrome. There is an instance
+// associated with each Profile. This derivation adds refcounting semantics
+// since a profile has multiple URLRequestContexts which refer to the same
+// object, and those URLRequestContexts are refcounted independently of the
+// owning profile.
+//
+// All methods, including the ctor and dtor, are expected to be called on
+// the IO thread.
+class ChromeAppCacheService
+ : public base::RefCounted<ChromeAppCacheService>,
+ public appcache::AppCacheService,
+ public appcache::AppCachePolicy,
+ public NotificationObserver {
+ public:
+ ChromeAppCacheService(const FilePath& profile_path,
+ ChromeURLRequestContext* request_context);
+
+ static void ClearLocalState(const FilePath& profile_path);
+
+ private:
+ friend class base::RefCounted<ChromeAppCacheService>;
+ virtual ~ChromeAppCacheService();
+
+ // AppCachePolicy overrides
+ virtual bool CanLoadAppCache(const GURL& manifest_url);
+ virtual int CanCreateAppCache(const GURL& manifest_url,
+ net::CompletionCallback* callback);
+
+ // NotificationObserver override
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details);
+
+ scoped_refptr<HostContentSettingsMap> host_contents_settings_map_;
+ NotificationRegistrar registrar_;
+};
+
+#endif // CHROME_BROWSER_APPCACHE_CHROME_APPCACHE_SERVICE_H_