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

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: Fixed formatting 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"
11 #include "chrome/browser/guest_view/guest_view_constants.h" 11 #include "chrome/browser/guest_view/guest_view_constants.h"
12 #include "chrome/browser/guest_view/guest_view_manager.h" 12 #include "chrome/browser/guest_view/guest_view_manager.h"
13 #include "chrome/browser/guest_view/web_view/web_view_guest.h" 13 #include "chrome/browser/guest_view/web_view/web_view_guest.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/content_settings.h" 15 #include "chrome/common/content_settings.h"
17 #include "content/public/browser/render_frame_host.h" 16 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_process_host.h" 17 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h" 18 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/url_constants.h" 20 #include "content/public/common/url_constants.h"
22 #include "extensions/browser/event_router.h" 21 #include "extensions/browser/event_router.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h" 22 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 23
(...skipping 18 matching lines...) Expand all
43 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { 42 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() {
44 return args_.Pass(); 43 return args_.Pass();
45 } 44 }
46 45
47 // This observer ensures that the GuestViewBase destroys itself when its 46 // This observer ensures that the GuestViewBase destroys itself when its
48 // embedder goes away. 47 // embedder goes away.
49 class GuestViewBase::EmbedderWebContentsObserver : public WebContentsObserver { 48 class GuestViewBase::EmbedderWebContentsObserver : public WebContentsObserver {
50 public: 49 public:
51 explicit EmbedderWebContentsObserver(GuestViewBase* guest) 50 explicit EmbedderWebContentsObserver(GuestViewBase* guest)
52 : WebContentsObserver(guest->embedder_web_contents()), 51 : WebContentsObserver(guest->embedder_web_contents()),
52 destroyed_(false),
53 guest_(guest) { 53 guest_(guest) {
54 } 54 }
55 55
56 virtual ~EmbedderWebContentsObserver() { 56 virtual ~EmbedderWebContentsObserver() {
57 } 57 }
58 58
59 // WebContentsObserver implementation. 59 // WebContentsObserver implementation.
60 virtual void WebContentsDestroyed() OVERRIDE { 60 virtual void WebContentsDestroyed() OVERRIDE {
61 Destroy();
62 }
63
64 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE {
65 Destroy();
66 }
67
68 private:
69 bool destroyed_;
70 GuestViewBase* guest_;
71
72 void Destroy() {
73 if (destroyed_)
74 return;
75 destroyed_ = true;
61 guest_->embedder_web_contents_ = NULL; 76 guest_->embedder_web_contents_ = NULL;
62 guest_->EmbedderDestroyed(); 77 guest_->EmbedderDestroyed();
63 guest_->Destroy(); 78 guest_->Destroy();
64 } 79 }
65 80
66 private:
67 GuestViewBase* guest_;
68
69 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver); 81 DISALLOW_COPY_AND_ASSIGN(EmbedderWebContentsObserver);
70 }; 82 };
71 83
72 GuestViewBase::GuestViewBase(content::BrowserContext* browser_context, 84 GuestViewBase::GuestViewBase(content::BrowserContext* browser_context,
73 int guest_instance_id) 85 int guest_instance_id)
74 : embedder_web_contents_(NULL), 86 : embedder_web_contents_(NULL),
75 embedder_render_process_id_(0), 87 embedder_render_process_id_(0),
76 browser_context_(browser_context), 88 browser_context_(browser_context),
77 guest_instance_id_(guest_instance_id), 89 guest_instance_id_(guest_instance_id),
78 view_instance_id_(guestview::kInstanceIDNone), 90 view_instance_id_(guestview::kInstanceIDNone),
79 initialized_(false), 91 initialized_(false),
80 weak_ptr_factory_(this) { 92 weak_ptr_factory_(this) {
81 } 93 }
82 94
83 void GuestViewBase::Init( 95 void GuestViewBase::Init(
84 const std::string& embedder_extension_id, 96 const std::string& embedder_extension_id,
85 int embedder_render_process_id, 97 int embedder_render_process_id,
86 const base::DictionaryValue& create_params, 98 const base::DictionaryValue& create_params,
87 const WebContentsCreatedCallback& callback) { 99 const WebContentsCreatedCallback& callback) {
88 if (initialized_) 100 if (initialized_)
89 return; 101 return;
90 initialized_ = true; 102 initialized_ = true;
91 103
92 CreateWebContents(embedder_extension_id, 104 CreateWebContents(embedder_extension_id,
93 embedder_render_process_id, 105 embedder_render_process_id,
94 create_params, 106 create_params,
95 base::Bind(&GuestViewBase::CompleteCreateWebContents, 107 base::Bind(&GuestViewBase::CompleteInit,
96 AsWeakPtr(), 108 AsWeakPtr(),
97 embedder_extension_id, 109 embedder_extension_id,
98 embedder_render_process_id, 110 embedder_render_process_id,
99 callback)); 111 callback));
100 } 112 }
101 113
102 void GuestViewBase::InitWithWebContents( 114 void GuestViewBase::InitWithWebContents(
103 const std::string& embedder_extension_id, 115 const std::string& embedder_extension_id,
104 int embedder_render_process_id, 116 int embedder_render_process_id,
105 content::WebContents* guest_web_contents) { 117 content::WebContents* guest_web_contents) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 CHECK(!attached()); 219 CHECK(!attached());
208 CHECK_EQ(host->GetID(), embedder_render_process_id()); 220 CHECK_EQ(host->GetID(), embedder_render_process_id());
209 221
210 // This code path may be reached if the embedder WebContents is killed for 222 // This code path may be reached if the embedder WebContents is killed for
211 // whatever reason immediately after a called to GuestViewInternal.createGuest 223 // whatever reason immediately after a called to GuestViewInternal.createGuest
212 // and before attaching the new guest to a frame. 224 // and before attaching the new guest to a frame.
213 Destroy(); 225 Destroy();
214 } 226 }
215 227
216 void GuestViewBase::Destroy() { 228 void GuestViewBase::Destroy() {
229 DCHECK(guest_web_contents());
217 content::RenderProcessHost* host = 230 content::RenderProcessHost* host =
218 content::RenderProcessHost::FromID(embedder_render_process_id()); 231 content::RenderProcessHost::FromID(embedder_render_process_id());
219 if (host) 232 if (host)
220 host->RemoveObserver(this); 233 host->RemoveObserver(this);
221 WillDestroy(); 234 WillDestroy();
222 if (!destruction_callback_.is_null()) 235 if (!destruction_callback_.is_null())
223 destruction_callback_.Run(); 236 destruction_callback_.Run();
237
238 webcontents_guestview_map.Get().erase(guest_web_contents());
239 GuestViewManager::FromBrowserContext(browser_context_)->
240 RemoveGuest(guest_instance_id_);
241 pending_events_.clear();
242
224 delete guest_web_contents(); 243 delete guest_web_contents();
225 } 244 }
226 245
227 void GuestViewBase::DidAttach() { 246 void GuestViewBase::DidAttach() {
228 // Give the derived class an opportunity to perform some actions. 247 // Give the derived class an opportunity to perform some actions.
229 DidAttachToEmbedder(); 248 DidAttachToEmbedder();
230 249
231 SendQueuedEvents(); 250 SendQueuedEvents();
232 } 251 }
233 252
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 } 304 }
286 305
287 bool GuestViewBase::PreHandleGestureEvent(content::WebContents* source, 306 bool GuestViewBase::PreHandleGestureEvent(content::WebContents* source,
288 const blink::WebGestureEvent& event) { 307 const blink::WebGestureEvent& event) {
289 return event.type == blink::WebGestureEvent::GesturePinchBegin || 308 return event.type == blink::WebGestureEvent::GesturePinchBegin ||
290 event.type == blink::WebGestureEvent::GesturePinchUpdate || 309 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
291 event.type == blink::WebGestureEvent::GesturePinchEnd; 310 event.type == blink::WebGestureEvent::GesturePinchEnd;
292 } 311 }
293 312
294 GuestViewBase::~GuestViewBase() { 313 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 } 314 }
304 315
305 void GuestViewBase::DispatchEventToEmbedder(Event* event) { 316 void GuestViewBase::DispatchEventToEmbedder(Event* event) {
306 scoped_ptr<Event> event_ptr(event); 317 scoped_ptr<Event> event_ptr(event);
307 if (!in_extension()) { 318 if (!in_extension()) {
308 NOTREACHED(); 319 NOTREACHED();
309 return; 320 return;
310 } 321 }
311 322
312 if (!attached()) { 323 if (!attached()) {
313 pending_events_.push_back(linked_ptr<Event>(event_ptr.release())); 324 pending_events_.push_back(linked_ptr<Event>(event_ptr.release()));
314 return; 325 return;
315 } 326 }
316 327
317 Profile* profile = Profile::FromBrowserContext(browser_context_);
318
319 extensions::EventFilteringInfo info; 328 extensions::EventFilteringInfo info;
320 info.SetInstanceID(view_instance_id_); 329 info.SetInstanceID(view_instance_id_);
321 scoped_ptr<base::ListValue> args(new base::ListValue()); 330 scoped_ptr<base::ListValue> args(new base::ListValue());
322 args->Append(event->GetArguments().release()); 331 args->Append(event->GetArguments().release());
323 332
324 extensions::EventRouter::DispatchEvent( 333 extensions::EventRouter::DispatchEvent(
325 embedder_web_contents_, 334 embedder_web_contents_,
326 profile, 335 browser_context_,
327 embedder_extension_id_, 336 embedder_extension_id_,
328 event->name(), 337 event->name(),
329 args.Pass(), 338 args.Pass(),
330 extensions::EventRouter::USER_GESTURE_UNKNOWN, 339 extensions::EventRouter::USER_GESTURE_UNKNOWN,
331 info); 340 info);
332 } 341 }
333 342
334 void GuestViewBase::SendQueuedEvents() { 343 void GuestViewBase::SendQueuedEvents() {
335 if (!attached()) 344 if (!attached())
336 return; 345 return;
337 while (!pending_events_.empty()) { 346 while (!pending_events_.empty()) {
338 linked_ptr<Event> event_ptr = pending_events_.front(); 347 linked_ptr<Event> event_ptr = pending_events_.front();
339 pending_events_.pop_front(); 348 pending_events_.pop_front();
340 DispatchEventToEmbedder(event_ptr.release()); 349 DispatchEventToEmbedder(event_ptr.release());
341 } 350 }
342 } 351 }
343 352
344 void GuestViewBase::CompleteCreateWebContents( 353 void GuestViewBase::CompleteInit(const std::string& embedder_extension_id,
345 const std::string& embedder_extension_id, 354 int embedder_render_process_id,
346 int embedder_render_process_id, 355 const WebContentsCreatedCallback& callback,
347 const WebContentsCreatedCallback& callback, 356 content::WebContents* guest_web_contents) {
348 content::WebContents* guest_web_contents) {
349 if (!guest_web_contents) { 357 if (!guest_web_contents) {
358 // The derived class did not create a WebContents so this class serves no
359 // purpose. Let's self-destruct.
360 delete this;
350 callback.Run(NULL); 361 callback.Run(NULL);
351 return; 362 return;
352 } 363 }
353 InitWithWebContents(embedder_extension_id, 364 InitWithWebContents(embedder_extension_id,
354 embedder_render_process_id, 365 embedder_render_process_id,
355 guest_web_contents); 366 guest_web_contents);
356 callback.Run(guest_web_contents); 367 callback.Run(guest_web_contents);
357 } 368 }
OLDNEW
« no previous file with comments | « chrome/browser/guest_view/guest_view_base.h ('k') | chrome/browser/guest_view/guest_view_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698