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

Unified Diff: chrome/browser/extensions/web_intent_callbacks.cc

Issue 10828172: Allow platform apps to respond to Web Intents (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Hooked up observing of source WebContents Created 8 years, 4 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
Index: chrome/browser/extensions/web_intent_callbacks.cc
diff --git a/chrome/browser/extensions/web_intent_callbacks.cc b/chrome/browser/extensions/web_intent_callbacks.cc
new file mode 100644
index 0000000000000000000000000000000000000000..01839c8d64b177a1531f97e377732cd2d40adaf6
--- /dev/null
+++ b/chrome/browser/extensions/web_intent_callbacks.cc
@@ -0,0 +1,123 @@
+// 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.
+
+#include "base/bind.h"
+#include "base/stringprintf.h"
+#include "chrome/browser/extensions/web_intent_callbacks.h"
+#include "chrome/browser/profiles/profile_dependency_manager.h"
+#include "chrome/common/extensions/extension.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_observer.h"
+#include "content/public/browser/web_intents_dispatcher.h"
+#include "webkit/glue/web_intent_reply_data.h"
benwells 2012/08/13 08:27:29 Are all these includes needed?
thorogood 2012/08/14 02:59:08 I've removed a couple - thanks.
+
+namespace {
+
+// Get the key for use inside the internal pending WebIntentCallbacks map.
+std::string GetKey(const extensions::Extension* extension, int id) {
+ return StringPrintf("%s/%d", extension->id().c_str(), id);
+}
+
+} // namespace
+
+namespace extensions {
+
+// SourceObserver is a subclass of WebContentsObserver that is instantiated
+// on RegisterCallback to wait for the source WebContents to be destroyed.
+// If it is destroyed, this automatically clears the callback from pending_.
+class WebIntentCallbacks::SourceObserver : content::WebContentsObserver {
+ public:
+ SourceObserver(content::WebContents* web_contents,
+ WebIntentCallbacks* callbacks,
benwells 2012/08/13 08:27:29 Indentation
thorogood 2012/08/14 02:59:08 Done.
+ const std::string key)
+ : content::WebContentsObserver(web_contents),
+ callbacks_(callbacks),
+ key_(key) {}
+ virtual ~SourceObserver() {}
+
+ // Implement WebContentsObserver
+ virtual void WebContentsDestroyed(content::WebContents* web_contents) {
benwells 2012/08/13 08:27:29 Missing OVERRIDE
thorogood 2012/08/14 02:59:08 Done - although I've copied this inner class mostl
+ callbacks_->pending_.erase(key_);
+ delete this;
+ }
+
+ private:
+ WebIntentCallbacks* callbacks_;
+ const std::string key_;
+};
+
+WebIntentCallbacks::WebIntentCallbacks() {}
benwells 2012/08/13 08:27:29 Can you initialize last_id_ to zero here?
thorogood 2012/08/14 02:59:08 Done.
+
+WebIntentCallbacks::~WebIntentCallbacks() {}
+
+// static
+WebIntentCallbacks* WebIntentCallbacks::Get(Profile* profile) {
+ return Factory::GetForProfile(profile);
+}
+
+int WebIntentCallbacks::RegisterCallback(
+ const Extension* extension,
+ content::WebIntentsDispatcher* dispatcher,
+ content::WebContents* source) {
+ int id = last_id_++;
+ std::string key = GetKey(extension, id);
+ pending_[key] = dispatcher;
+
+ // TODO(thorogood): We should also listen for extension suspend events, and
+ // clear the relevant callback (i.e., the handler of this intent)
+
+ // TODO(thorogood): Note that if the caller is itself a platform app, then
benwells 2012/08/13 08:27:29 What is the TODO? Is this behavior comparable to
thorogood 2012/08/14 02:59:08 So, I've rewritten the comment to make it clearer.
+ // we'll clear the callback when the window goes away: not the whole app.
+
+ if (source) {
+ // Listen to when the source WebContent is destroyed. This object is self-
+ // deleting on this event.
+ new SourceObserver(source, this, key);
+ } else {
+ LOG(WARNING) << "No source WebContents for WebIntent; "
benwells 2012/08/13 08:27:29 Can this happen? If it should not be able to happe
thorogood 2012/08/14 02:59:08 I don't believe so. I originally believed it may o
+ << "unresponded intent will leak";
+ }
+ return id;
+}
+
+content::WebIntentsDispatcher* WebIntentCallbacks::RetrieveCallback(
+ const Extension* extension, int id) {
+ std::string key = GetKey(extension, id);
+
+ if (!pending_.count(key))
+ return NULL;
+
+ content::WebIntentsDispatcher* dispatcher = pending_[key];
+ pending_.erase(key);
+ return dispatcher;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Factory boilerplate
+
+// static
+WebIntentCallbacks* WebIntentCallbacks::Factory::GetForProfile(
+ Profile* profile) {
+ return static_cast<WebIntentCallbacks*>(
+ GetInstance()->GetServiceForProfile(profile, true));
+}
+
+WebIntentCallbacks::Factory* WebIntentCallbacks::Factory::GetInstance() {
+ return Singleton<WebIntentCallbacks::Factory>::get();
+}
+
+WebIntentCallbacks::Factory::Factory()
+ : ProfileKeyedServiceFactory("WebIntentCallbacks",
+ ProfileDependencyManager::GetInstance()) {
+}
+
+WebIntentCallbacks::Factory::~Factory() {
+}
+
+ProfileKeyedService* WebIntentCallbacks::Factory::BuildServiceInstanceFor(
+ Profile* profile) const {
+ return new WebIntentCallbacks();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698