Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Unified Diff: chrome/common/extensions/file_loading_delegate.h

Issue 10833069: ResourceLoadingDelegate class to allow common code to load files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/extensions/file_loading_delegate.h
diff --git a/chrome/common/extensions/file_loading_delegate.h b/chrome/common/extensions/file_loading_delegate.h
new file mode 100644
index 0000000000000000000000000000000000000000..75733097cbc4cbb666116d6b4421bb8606289ff7
--- /dev/null
+++ b/chrome/common/extensions/file_loading_delegate.h
@@ -0,0 +1,87 @@
+// 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.
+
+#ifndef CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_
+#define CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_
+
+#include "base/callback.h"
+
+class ExtensionResource;
+
+namespace extensions {
+
+// Use this in common code to load extension resources asynchronously. In
+// process types that can't load resources, pointers to this type may be NULL.
+class ResourceLoadingDelegate {
+ public:
+ virtual ~FileLoadingDelegate() {}
+
+ // Loads the file represented by |resource|, parses/transforms it using
+ // |transform_on_bg_thread|, and notifies the caller when everything's done
+ // using |on_loaded| or |on_failure|. Must be called on the UI thread.
+ //
+ // |transform_on_bg_thread| must be thread-safe since it may be called on a
+ // non-UI thread. Usually this means it should be stateless.
+ //
+ // |on_loaded| and |on_failure| should generally be bound to a WeakPtr to deal
+ // with loads that finish after the caller is destroyed.
+ //
+ // |on_failure| may be null, in which case failure will be ignored.
+ template<typename IntermediateType>
+ StartLoad(const ExtensionResource& resource,
+ base::Callback<IntermediateType(const std::string&)>
+ transform_on_bg_thread,
+ base::Callback<void(const IntermediateType&)> on_loaded,
+ base::Callback<void()> on_failure);
+
+ protected:
+ class LoadInfo {
+ public:
+ virtual ~LoadInfo() {}
+ // Call this on a background thread
+ virtual void TransformOnBgThread(const std::string& file_data) = 0;
+ virtual void OnLoaded() = 0;
+ };
+
+ private:
+ // Subclass implementations must load the file referred to by |resource| as a
+ // string of bytes, call load_info->TransformOnBgThread(file_contents), and
+ // then call load_info->OnLoaded(). TransformOnBgThread should be called on a
+ // thread that can afford long-running operations, and OnLoaded must be called
+ // on the UI thread.
+ virtual StartLoadImpl(const ExtensionResource& resource,
+ scoped_ptr<LoadInfo> load_info) = 0;
+};
+
+template<typename IntermediateType>
+ResourceLoadingDelegate::StartLoad(
+ const ExtensionResource& resource,
+ base::Callback<IntermediateType(const std::string&)>
+ transform_on_bg_thread,
+ base::Callback<void(const IntermediateType&)> on_loaded) {
+ // Implement LoadInfo for this particular intermediate type:
+ class LoadInfoImpl : public LoadInfo {
+ public:
+ LoadInfoImpl(base::Callback<IntermediateType(const std::string&)>
+ transform_on_bg_thread,
+ base::Callback<void(const IntermediateType&)> on_loaded)
+ : transform_on_bg_thread_(transform_on_bg_thread),
+ on_loaded_(on_loaded) {
+ }
+ virtual void TransformOnBgThread(const std::string& file_data) {
+ intermediate_data_ = transform_on_bg_thread_(file_data);
+ }
+ virtual void OnLoaded() {
+ on_loaded_(intermediate_data_);
+ }
+ };
+
+ // Call the subclass's implementation function.
+ StartLoadImpl(resource, scoped_ptr<LoadInfo>(
+ new LoadInfoImpl(transform_on_bg_thread, on_loaded)));
+}
+
+} // namespace extensions
+
+#endif // CHROME_COMMON_EXTENSIONS_FILE_LOADING_DELEGATE_H_
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698