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

Side by Side Diff: chrome/browser/guest_view/guest_view_base.cc

Issue 354483004: Implement <appview> (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@app_view_skeleton
Patch Set: Added tests Created 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "chrome/browser/guest_view/guest_view_base.h" 5 #include "chrome/browser/guest_view/guest_view_base.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/guest_view/app_view/app_view_guest.h" 10 #include "chrome/browser/guest_view/app_view/app_view_guest.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { 43 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() {
44 return args_.Pass(); 44 return args_.Pass();
45 } 45 }
46 46
47 // This observer ensures that the GuestViewBase destroys itself when its 47 // This observer ensures that the GuestViewBase destroys itself when its
48 // embedder goes away. 48 // embedder goes away.
49 class GuestViewBase::EmbedderWebContentsObserver : public WebContentsObserver { 49 class GuestViewBase::EmbedderWebContentsObserver : public WebContentsObserver {
50 public: 50 public:
51 explicit EmbedderWebContentsObserver(GuestViewBase* guest) 51 explicit EmbedderWebContentsObserver(GuestViewBase* guest)
52 : WebContentsObserver(guest->embedder_web_contents()), 52 : WebContentsObserver(guest->embedder_web_contents()),
53 destroyed_(false),
53 guest_(guest) { 54 guest_(guest) {
54 } 55 }
55 56
56 virtual ~EmbedderWebContentsObserver() { 57 virtual ~EmbedderWebContentsObserver() {
57 } 58 }
58 59
59 // WebContentsObserver implementation. 60 // WebContentsObserver implementation.
60 virtual void WebContentsDestroyed() OVERRIDE { 61 virtual void WebContentsDestroyed() OVERRIDE {
62 Destroy();
63 }
64
65 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE {
66 Destroy();
67 }
68
69 private:
70 bool destroyed_;
71 GuestViewBase* guest_;
72
73 void Destroy() {
74 if (destroyed_)
75 return;
76 destroyed_ = true;
61 guest_->embedder_web_contents_ = NULL; 77 guest_->embedder_web_contents_ = NULL;
62 guest_->EmbedderDestroyed(); 78 guest_->EmbedderDestroyed();
63 guest_->Destroy(); 79 guest_->Destroy();
64 } 80 }
65 81
66 private:
67 GuestViewBase* guest_;
68
69 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver); 82 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver);
70 }; 83 };
71 84
72 GuestViewBase::GuestViewBase(content::BrowserContext* browser_context, 85 GuestViewBase::GuestViewBase(content::BrowserContext* browser_context,
73 int guest_instance_id) 86 int guest_instance_id)
74 : embedder_web_contents_(NULL), 87 : embedder_web_contents_(NULL),
75 embedder_render_process_id_(0), 88 embedder_render_process_id_(0),
76 browser_context_(browser_context), 89 browser_context_(browser_context),
77 guest_instance_id_(guest_instance_id), 90 guest_instance_id_(guest_instance_id),
78 view_instance_id_(guestview::kInstanceIDNone), 91 view_instance_id_(guestview::kInstanceIDNone),
79 initialized_(false), 92 initialized_(false),
80 weak_ptr_factory_(this) { 93 weak_ptr_factory_(this) {
81 } 94 }
82 95
83 void GuestViewBase::Init( 96 void GuestViewBase::Init(
84 const std::string& embedder_extension_id, 97 const std::string& embedder_extension_id,
85 int embedder_render_process_id, 98 int embedder_render_process_id,
86 const base::DictionaryValue& create_params, 99 const base::DictionaryValue& create_params,
87 const WebContentsCreatedCallback& callback) { 100 const WebContentsCreatedCallback& callback) {
88 if (initialized_) 101 if (initialized_)
89 return; 102 return;
90 initialized_ = true; 103 initialized_ = true;
91 104
92 CreateWebContents(embedder_extension_id, 105 CreateWebContents(embedder_extension_id,
93 embedder_render_process_id, 106 embedder_render_process_id,
94 create_params, 107 create_params,
95 base::Bind(&GuestViewBase::CompleteCreateWebContents, 108 base::Bind(&GuestViewBase::CompleteInit,
96 AsWeakPtr(), 109 AsWeakPtr(),
97 embedder_extension_id, 110 embedder_extension_id,
98 embedder_render_process_id, 111 embedder_render_process_id,
99 callback)); 112 callback));
100 } 113 }
101 114
102 void GuestViewBase::InitWithWebContents( 115 void GuestViewBase::InitWithWebContents(
103 const std::string& embedder_extension_id, 116 const std::string& embedder_extension_id,
104 int embedder_render_process_id, 117 int embedder_render_process_id,
105 content::WebContents* guest_web_contents) { 118 content::WebContents* guest_web_contents) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 CHECK(!attached()); 220 CHECK(!attached());
208 CHECK_EQ(host->GetID(), embedder_render_process_id()); 221 CHECK_EQ(host->GetID(), embedder_render_process_id());
209 222
210 // This code path may be reached if the embedder WebContents is killed for 223 // This code path may be reached if the embedder WebContents is killed for
211 // whatever reason immediately after a called to GuestViewInternal.createGuest 224 // whatever reason immediately after a called to GuestViewInternal.createGuest
212 // and before attaching the new guest to a frame. 225 // and before attaching the new guest to a frame.
213 Destroy(); 226 Destroy();
214 } 227 }
215 228
216 void GuestViewBase::Destroy() { 229 void GuestViewBase::Destroy() {
230 DCHECK(guest_web_contents());
217 content::RenderProcessHost* host = 231 content::RenderProcessHost* host =
218 content::RenderProcessHost::FromID(embedder_render_process_id()); 232 content::RenderProcessHost::FromID(embedder_render_process_id());
219 if (host) 233 if (host)
220 host->RemoveObserver(this); 234 host->RemoveObserver(this);
221 WillDestroy(); 235 WillDestroy();
222 if (!destruction_callback_.is_null()) 236 if (!destruction_callback_.is_null())
223 destruction_callback_.Run(); 237 destruction_callback_.Run();
238
239 webcontents_guestview_map.Get().erase(guest_web_contents());
240 GuestViewManager::FromBrowserContext(browser_context_)->
241 RemoveGuest(guest_instance_id_);
242 pending_events_.clear();
243
224 delete guest_web_contents(); 244 delete guest_web_contents();
225 } 245 }
226 246
227 void GuestViewBase::DidAttach() { 247 void GuestViewBase::DidAttach() {
228 // Give the derived class an opportunity to perform some actions. 248 // Give the derived class an opportunity to perform some actions.
229 DidAttachToEmbedder(); 249 DidAttachToEmbedder();
230 250
231 SendQueuedEvents(); 251 SendQueuedEvents();
232 } 252 }
233 253
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 } 305 }
286 306
287 bool GuestViewBase::PreHandleGestureEvent(content::WebContents* source, 307 bool GuestViewBase::PreHandleGestureEvent(content::WebContents* source,
288 const blink::WebGestureEvent& event) { 308 const blink::WebGestureEvent& event) {
289 return event.type == blink::WebGestureEvent::GesturePinchBegin || 309 return event.type == blink::WebGestureEvent::GesturePinchBegin ||
290 event.type == blink::WebGestureEvent::GesturePinchUpdate || 310 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
291 event.type == blink::WebGestureEvent::GesturePinchEnd; 311 event.type == blink::WebGestureEvent::GesturePinchEnd;
292 } 312 }
293 313
294 GuestViewBase::~GuestViewBase() { 314 GuestViewBase::~GuestViewBase() {
295 std::pair<int, int> key(embedder_render_process_id_, guest_instance_id_);
296
297 webcontents_guestview_map.Get().erase(guest_web_contents());
298
299 GuestViewManager::FromBrowserContext(browser_context_)->
300 RemoveGuest(guest_instance_id_);
301
302 pending_events_.clear();
303 } 315 }
304 316
305 void GuestViewBase::DispatchEvent(Event* event) { 317 void GuestViewBase::DispatchEvent(Event* event) {
306 scoped_ptr<Event> event_ptr(event); 318 scoped_ptr<Event> event_ptr(event);
307 if (!in_extension()) { 319 if (!in_extension()) {
308 NOTREACHED(); 320 NOTREACHED();
309 return; 321 return;
310 } 322 }
311 323
312 if (!attached()) { 324 if (!attached()) {
(...skipping 21 matching lines...) Expand all
334 void GuestViewBase::SendQueuedEvents() { 346 void GuestViewBase::SendQueuedEvents() {
335 if (!attached()) 347 if (!attached())
336 return; 348 return;
337 while (!pending_events_.empty()) { 349 while (!pending_events_.empty()) {
338 linked_ptr<Event> event_ptr = pending_events_.front(); 350 linked_ptr<Event> event_ptr = pending_events_.front();
339 pending_events_.pop_front(); 351 pending_events_.pop_front();
340 DispatchEvent(event_ptr.release()); 352 DispatchEvent(event_ptr.release());
341 } 353 }
342 } 354 }
343 355
344 void GuestViewBase::CompleteCreateWebContents( 356 void GuestViewBase::CompleteInit(const std::string& embedder_extension_id,
345 const std::string& embedder_extension_id, 357 int embedder_render_process_id,
346 int embedder_render_process_id, 358 const WebContentsCreatedCallback& callback,
347 const WebContentsCreatedCallback& callback, 359 content::WebContents* guest_web_contents) {
348 content::WebContents* guest_web_contents) {
349 if (!guest_web_contents) { 360 if (!guest_web_contents) {
361 // The derived class did not create a WebContents so this class serves no
362 // purpose. Let's self-destruct.
363 delete this;
lazyboy 2014/07/07 21:34:44 Can you call callback.Run(this) after "delete this
Fady Samuel 2014/07/08 15:47:08 Yes, this callback is a local variable. We can't a
350 callback.Run(NULL); 364 callback.Run(NULL);
351 return; 365 return;
352 } 366 }
353 InitWithWebContents(embedder_extension_id, 367 InitWithWebContents(embedder_extension_id,
354 embedder_render_process_id, 368 embedder_render_process_id,
355 guest_web_contents); 369 guest_web_contents);
356 callback.Run(guest_web_contents); 370 callback.Run(guest_web_contents);
357 } 371 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698