| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_INTENTS_INTENT_INJECTOR_H_ | 5 #ifndef CONTENT_BROWSER_INTENTS_INTENT_INJECTOR_H_ |
| 6 #define CONTENT_BROWSER_INTENTS_INTENT_INJECTOR_H_ | 6 #define CONTENT_BROWSER_INTENTS_INTENT_INJECTOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/gtest_prod_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 11 #include "base/string16.h" | 13 #include "base/string16.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 14 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 15 #include "webkit/glue/web_intent_reply_data.h" | 17 #include "webkit/glue/web_intent_reply_data.h" |
| 16 | 18 |
| 17 namespace content { | 19 namespace content { |
| 18 class WebIntentsDispatcher; | 20 class WebIntentsDispatcher; |
| 19 } | 21 } |
| 20 | 22 |
| 21 namespace webkit_glue { | 23 namespace webkit_glue { |
| 22 struct WebIntentData; | 24 struct WebIntentData; |
| 23 } | 25 } |
| 24 | 26 |
| 25 // Injects an intent into the renderer of a WebContents. The intent dispatch | 27 // Injects an intent into the renderer of a WebContents. The intent dispatch |
| 26 // logic will create one of these to take care of passing intent data down into | 28 // logic will create one of these to take care of passing intent data down into |
| 27 // the context of the service, which will be running in the WebContents on which | 29 // the context of the service, which will be running in the WebContents on which |
| 28 // this class is an observer. Attaches to the service contents and deletes | 30 // this class is an observer. Attaches to the service contents and deletes |
| 29 // itself when that WebContents is closed. | 31 // itself when that WebContents is closed. |
| 30 // | 32 // |
| 31 // This object should be attached to the new WebContents very early: before the | 33 // This object should be attached to the new WebContents very early: before the |
| 32 // RenderView is created. It will then send the intent data down to the renderer | 34 // RenderView is created. It will then send the intent data down to the renderer |
| 33 // on the RenderViewCreated call, so that the intent data is available | 35 // on the RenderViewCreated call, so that the intent data is available |
| 34 // throughout the parsing of the loaded document. | 36 // throughout the parsing of the loaded document. |
| 35 class CONTENT_EXPORT IntentInjector : public content::WebContentsObserver { | 37 class CONTENT_EXPORT IntentInjector : public content::WebContentsObserver { |
| 36 public: | 38 public: |
| 37 // |web_contents| must not be NULL. | 39 // |web_contents| must not be NULL. |
| 38 explicit IntentInjector(content::WebContents* web_contents); | 40 explicit IntentInjector(content::WebContents* web_contents); |
| 39 virtual ~IntentInjector(); | |
| 40 | 41 |
| 41 // content::WebContentsObserver implementation. | 42 // content::WebContentsObserver implementation. |
| 42 virtual void RenderViewCreated( | 43 virtual void RenderViewCreated( |
| 43 content::RenderViewHost* render_view_host) OVERRIDE; | 44 content::RenderViewHost* render_view_host) OVERRIDE; |
| 44 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 45 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 45 virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE; | 46 virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE; |
| 46 | 47 |
| 47 // Used to notify the object that the source contents has been destroyed. | 48 // Used to notify the object that the source contents has been destroyed. |
| 48 void SourceWebContentsDestroyed(content::WebContents* contents); | 49 void SourceWebContentsDestroyed(content::WebContents* contents); |
| 49 | 50 |
| 50 // Sets the intent data to be injected. Call after the user has selected a | 51 // Sets the intent data to be injected. Call after the user has selected a |
| 51 // service to pass the intent data to that service. |intents_dispatcher| is a | 52 // service to pass the intent data to that service. |intents_dispatcher| is a |
| 52 // sender to use to communicate to the source contents. The caller must | 53 // sender to use to communicate to the source contents. The caller must |
| 53 // ensure that SourceWebContentsDestroyed is called when this object becomes | 54 // ensure that SourceWebContentsDestroyed is called when this object becomes |
| 54 // unusable. |intent| is the intent data from the source. | 55 // unusable. |intent| is the intent data from the source. |
| 55 void SetIntent(content::WebIntentsDispatcher* intents_dispatcher, | 56 void SetIntent(content::WebIntentsDispatcher* intents_dispatcher, |
| 56 const webkit_glue::WebIntentData& intent); | 57 const webkit_glue::WebIntentData& intent); |
| 57 | 58 |
| 59 // Abandon this injector. Used when re-delivering an intent to a different |
| 60 // service provider, i.e. when abandoning an inline disposition and picking |
| 61 // a new provider. Self-deletes. |
| 62 void Abandon(); |
| 63 |
| 58 private: | 64 private: |
| 65 FRIEND_TEST_ALL_PREFIXES(IntentInjectorTest, AbandonDeletes); |
| 66 |
| 67 // Private, since the class is self-deleting. |
| 68 virtual ~IntentInjector(); |
| 69 |
| 59 // Handles receiving a reply from the intent delivery page. | 70 // Handles receiving a reply from the intent delivery page. |
| 60 void OnReply(webkit_glue::WebIntentReplyType reply_type, | 71 void OnReply(webkit_glue::WebIntentReplyType reply_type, |
| 61 const string16& data); | 72 const string16& data); |
| 62 | 73 |
| 63 // Gets notification that someone else has replied to the intent call. | 74 // Gets notification that someone else has replied to the intent call. |
| 64 void OnSendReturnMessage(webkit_glue::WebIntentReplyType reply_type); | 75 void OnSendReturnMessage(webkit_glue::WebIntentReplyType reply_type); |
| 65 | 76 |
| 66 // Source intent data provided by caller. | 77 // Source intent data provided by caller. |
| 67 scoped_ptr<webkit_glue::WebIntentData> source_intent_; | 78 scoped_ptr<webkit_glue::WebIntentData> source_intent_; |
| 68 | 79 |
| 69 // Weak pointer to the message forwarder to the contents invoking the intent. | 80 // Weak pointer to the message forwarder to the contents invoking the intent. |
| 70 content::WebIntentsDispatcher* intents_dispatcher_; | 81 content::WebIntentsDispatcher* intents_dispatcher_; |
| 71 | 82 |
| 72 // Remember the initial delivery url for origin restriction. | 83 // Remember the initial delivery url for origin restriction. |
| 73 GURL initial_url_; | 84 GURL initial_url_; |
| 74 | 85 |
| 86 // The injector may be Abandon()ed and deleted before the dispatcher. Use weak |
| 87 // pointers to ensure the notification callback remains valid. |
| 88 base::WeakPtrFactory<IntentInjector> weak_factory_; |
| 89 |
| 75 DISALLOW_COPY_AND_ASSIGN(IntentInjector); | 90 DISALLOW_COPY_AND_ASSIGN(IntentInjector); |
| 76 }; | 91 }; |
| 77 | 92 |
| 78 #endif // CONTENT_BROWSER_INTENTS_INTENT_INJECTOR_H_ | 93 #endif // CONTENT_BROWSER_INTENTS_INTENT_INJECTOR_H_ |
| OLD | NEW |