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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "base/bind.h"
6 #include "base/stringprintf.h"
7 #include "chrome/browser/extensions/web_intent_callbacks.h"
8 #include "chrome/browser/profiles/profile_dependency_manager.h"
9 #include "chrome/common/extensions/extension.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/browser/web_intents_dispatcher.h"
13 #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.
14
15 namespace {
16
17 // Get the key for use inside the internal pending WebIntentCallbacks map.
18 std::string GetKey(const extensions::Extension* extension, int id) {
19 return StringPrintf("%s/%d", extension->id().c_str(), id);
20 }
21
22 } // namespace
23
24 namespace extensions {
25
26 // SourceObserver is a subclass of WebContentsObserver that is instantiated
27 // on RegisterCallback to wait for the source WebContents to be destroyed.
28 // If it is destroyed, this automatically clears the callback from pending_.
29 class WebIntentCallbacks::SourceObserver : content::WebContentsObserver {
30 public:
31 SourceObserver(content::WebContents* web_contents,
32 WebIntentCallbacks* callbacks,
benwells 2012/08/13 08:27:29 Indentation
thorogood 2012/08/14 02:59:08 Done.
33 const std::string key)
34 : content::WebContentsObserver(web_contents),
35 callbacks_(callbacks),
36 key_(key) {}
37 virtual ~SourceObserver() {}
38
39 // Implement WebContentsObserver
40 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
41 callbacks_->pending_.erase(key_);
42 delete this;
43 }
44
45 private:
46 WebIntentCallbacks* callbacks_;
47 const std::string key_;
48 };
49
50 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.
51
52 WebIntentCallbacks::~WebIntentCallbacks() {}
53
54 // static
55 WebIntentCallbacks* WebIntentCallbacks::Get(Profile* profile) {
56 return Factory::GetForProfile(profile);
57 }
58
59 int WebIntentCallbacks::RegisterCallback(
60 const Extension* extension,
61 content::WebIntentsDispatcher* dispatcher,
62 content::WebContents* source) {
63 int id = last_id_++;
64 std::string key = GetKey(extension, id);
65 pending_[key] = dispatcher;
66
67 // TODO(thorogood): We should also listen for extension suspend events, and
68 // clear the relevant callback (i.e., the handler of this intent)
69
70 // 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.
71 // we'll clear the callback when the window goes away: not the whole app.
72
73 if (source) {
74 // Listen to when the source WebContent is destroyed. This object is self-
75 // deleting on this event.
76 new SourceObserver(source, this, key);
77 } else {
78 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
79 << "unresponded intent will leak";
80 }
81 return id;
82 }
83
84 content::WebIntentsDispatcher* WebIntentCallbacks::RetrieveCallback(
85 const Extension* extension, int id) {
86 std::string key = GetKey(extension, id);
87
88 if (!pending_.count(key))
89 return NULL;
90
91 content::WebIntentsDispatcher* dispatcher = pending_[key];
92 pending_.erase(key);
93 return dispatcher;
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////
97 // Factory boilerplate
98
99 // static
100 WebIntentCallbacks* WebIntentCallbacks::Factory::GetForProfile(
101 Profile* profile) {
102 return static_cast<WebIntentCallbacks*>(
103 GetInstance()->GetServiceForProfile(profile, true));
104 }
105
106 WebIntentCallbacks::Factory* WebIntentCallbacks::Factory::GetInstance() {
107 return Singleton<WebIntentCallbacks::Factory>::get();
108 }
109
110 WebIntentCallbacks::Factory::Factory()
111 : ProfileKeyedServiceFactory("WebIntentCallbacks",
112 ProfileDependencyManager::GetInstance()) {
113 }
114
115 WebIntentCallbacks::Factory::~Factory() {
116 }
117
118 ProfileKeyedService* WebIntentCallbacks::Factory::BuildServiceInstanceFor(
119 Profile* profile) const {
120 return new WebIntentCallbacks();
121 }
122
123 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698