Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 "chrome/browser/adview/adview_guest.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "chrome/browser/adview/adview_constants.h" | |
| 9 #include "chrome/browser/extensions/event_router.h" | |
| 10 #include "chrome/browser/extensions/extension_system.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/render_process_host.h" | |
| 14 #include "content/public/browser/site_instance.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 | |
| 17 using content::WebContents; | |
| 18 | |
| 19 namespace keys = adview_constants; | |
| 20 | |
| 21 namespace chrome { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 typedef std::map<std::pair<int, int>, AdViewGuest*> AdViewGuestMap; | |
| 26 static base::LazyInstance<AdViewGuestMap> adview_guest_map = | |
|
sky
2013/06/25 16:16:52
don't need static here.
Fady Samuel
2013/06/25 16:54:42
Done.
| |
| 27 LAZY_INSTANCE_INITIALIZER; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 AdViewGuest::AdViewGuest(WebContents* guest_web_contents, | |
| 32 WebContents* embedder_web_contents, | |
| 33 const std::string& extension_id, | |
| 34 int adview_instance_id, | |
| 35 const base::DictionaryValue& args) | |
| 36 : WebContentsObserver(guest_web_contents), | |
| 37 embedder_web_contents_(embedder_web_contents), | |
| 38 extension_id_(extension_id), | |
| 39 embedder_render_process_id_( | |
| 40 embedder_web_contents->GetRenderProcessHost()->GetID()), | |
| 41 profile_(guest_web_contents->GetBrowserContext()), | |
| 42 guest_instance_id_(guest_web_contents->GetEmbeddedInstanceID()), | |
| 43 adview_instance_id_(adview_instance_id) { | |
| 44 std::string api_name; | |
|
sky
2013/06/25 16:16:52
You may get a warning with this code on a release
Fady Samuel
2013/06/25 16:54:42
This check is redundant anyway. Removing.
| |
| 45 DCHECK(args.GetString(keys::kAttributeApi, &api_name)); | |
| 46 DCHECK_EQ("adview", api_name); | |
| 47 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | |
| 48 adview_guest_map.Get().insert(std::make_pair(key, this)); | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 AdViewGuest* AdViewGuest::From(int embedder_process_id, | |
| 53 int guest_instance_id) { | |
|
sky
2013/06/25 16:16:52
nit: indenting is off.
Fady Samuel
2013/06/25 16:54:42
Done.
| |
| 54 AdViewGuestMap* guest_map = adview_guest_map.Pointer(); | |
| 55 AdViewGuestMap::iterator it = guest_map->find( | |
| 56 std::make_pair(embedder_process_id, guest_instance_id)); | |
| 57 return it == guest_map->end() ? NULL : it->second; | |
| 58 } | |
| 59 | |
| 60 AdViewGuest::~AdViewGuest() { | |
| 61 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_); | |
| 62 adview_guest_map.Get().erase(key); | |
| 63 } | |
| 64 | |
| 65 void AdViewGuest::DispatchEvent(const std::string& event_name, | |
| 66 scoped_ptr<DictionaryValue> event) { | |
| 67 Profile* profile = Profile::FromBrowserContext( | |
| 68 web_contents()->GetBrowserContext()); | |
| 69 | |
| 70 extensions::EventFilteringInfo info; | |
| 71 info.SetURL(GURL()); | |
| 72 info.SetInstanceID(guest_instance_id_); | |
| 73 scoped_ptr<ListValue> args(new ListValue()); | |
| 74 args->Append(event.release()); | |
| 75 | |
| 76 extensions::EventRouter::DispatchEvent( | |
| 77 embedder_web_contents_, profile, extension_id_, | |
| 78 event_name, args.Pass(), | |
| 79 extensions::EventRouter::USER_GESTURE_UNKNOWN, info); | |
| 80 } | |
| 81 | |
| 82 void AdViewGuest::DidCommitProvisionalLoadForFrame( | |
| 83 int64 frame_id, | |
| 84 bool is_main_frame, | |
| 85 const GURL& url, | |
| 86 content::PageTransition transition_type, | |
| 87 content::RenderViewHost* render_view_host) { | |
| 88 scoped_ptr<DictionaryValue> event(new DictionaryValue()); | |
| 89 event->SetString(keys::kUrl, url.spec()); | |
| 90 event->SetBoolean(keys::kIsTopLevel, is_main_frame); | |
| 91 DispatchEvent(keys::kEventLoadCommit, event.Pass()); | |
| 92 } | |
| 93 | |
| 94 void AdViewGuest::WebContentsDestroyed(WebContents* web_contents) { | |
| 95 delete this; | |
| 96 } | |
| 97 | |
| 98 } // namespace chrome | |
| OLD | NEW |