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

Side by Side Diff: chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_api.cc

Issue 9796012: Revert 127833 - Re-land alexbost's experimental offscreenTabs API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
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 "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_api.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/hash_tables.h"
11 #include "base/json/json_writer.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/lazy_instance.h"
15 #include "base/string_number_conversions.h"
16 #include "base/string_util.h"
17 #include "base/values.h"
18 #include "chrome/browser/extensions/api/offscreen_tabs/offscreen_tabs_constants. h"
19 #include "chrome/browser/extensions/extension_event_names.h"
20 #include "chrome/browser/extensions/extension_event_router.h"
21 #include "chrome/browser/extensions/extension_function_dispatcher.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/extensions/extension_tab_util.h"
24 #include "chrome/browser/extensions/extension_tabs_module.h"
25 #include "chrome/browser/extensions/extension_tabs_module_constants.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/browser_window.h"
29 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
30 #include "chrome/browser/ui/window_sizer.h"
31 #include "chrome/common/chrome_notification_types.h"
32 #include "chrome/common/extensions/extension.h"
33 #include "chrome/common/extensions/extension_error_utils.h"
34 #include "chrome/common/extensions/extension_messages.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h"
37 #include "content/public/browser/navigation_controller.h"
38 #include "content/public/browser/notification_details.h"
39 #include "content/public/browser/notification_source.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/browser/web_contents_view.h"
43
44 using content::NavigationController;
45 using content::NotificationDetails;
46 using content::NotificationSource;
47 using content::WebContents;
48 using WebKit::WebInputEvent;
49
50 namespace keys = extensions::offscreen_tabs_constants;
51 namespace tabs_keys = extension_tabs_module_constants;
52 namespace events = extension_event_names;
53
54 namespace {
55
56 class ParentTab;
57
58 // This class is responsible for the life cycle of an offscreen tab.
59 class OffscreenTab : public content::NotificationObserver {
60 public:
61 OffscreenTab();
62 virtual ~OffscreenTab();
63 void Init(const GURL& url,
64 const int width,
65 const int height,
66 Profile* profile,
67 ParentTab* parent_tab);
68
69 TabContentsWrapper* tab_contents() const {
70 return tab_contents_wrapper_.get();
71 }
72 WebContents* web_contents() const {
73 return tab_contents()->web_contents();
74 }
75 int GetID() const { return ExtensionTabUtil::GetTabId(web_contents()); }
76 ParentTab* parent_tab() { return parent_tab_; }
77
78 // Creates a representation of this OffscreenTab for use by the API methods.
79 // Passes ownership to the caller.
80 DictionaryValue* CreateValue() const;
81
82 // Navigates the tab to the |url|.
83 void NavigateToURL(const GURL& url) {
84 web_contents()->GetController().LoadURL(
85 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
86 }
87
88 // Adjusts the tab's dimensions to the specified |width| and |height|.
89 void SetSize(int width, int height) {
90 // TODO(jstritar): this doesn't seem to work on ChromeOS.
91 web_contents()->GetView()->SizeContents(gfx::Size(width, height));
92 }
93
94 private:
95 virtual void Observe(int type,
96 const NotificationSource& source,
97 const NotificationDetails& details) OVERRIDE;
98
99 content::NotificationRegistrar registrar_;
100 scoped_ptr<TabContentsWrapper> tab_contents_wrapper_;
101 ParentTab* parent_tab_;
102
103 DISALLOW_COPY_AND_ASSIGN(OffscreenTab);
104 };
105
106 typedef ScopedVector<OffscreenTab> OffscreenTabs;
107
108 // Holds info about a tab that has spawned at least one offscreen tab.
109 // Each ParentTab keeps track of its child offscreen tabs. The ParentTab is also
110 // responsible for killing its children when it navigates away or gets closed.
111 class ParentTab : public content::NotificationObserver {
112 public:
113 ParentTab();
114 virtual ~ParentTab();
115 void Init(WebContents* web_contents,
116 const std::string& extension_id);
117
118 TabContentsWrapper* tab_contents() { return tab_contents_wrapper_; }
119 int GetID() { return ExtensionTabUtil::GetTabId(web_contents()); }
120 WebContents* web_contents() {
121 return tab_contents()->web_contents();
122 }
123
124 // Returns the offscreen tabs spawned by this tab.
125 const OffscreenTabs& offscreen_tabs() { return offscreen_tabs_; }
126 const std::string& extension_id() const { return extension_id_; }
127
128 // Tab takes ownership of OffscreenTab.
129 void AddOffscreenTab(OffscreenTab *tab);
130
131 // Removes the offscreen |tab| and returns true if this parent has no more
132 // offscreen tabs.
133 bool RemoveOffscreenTab(OffscreenTab *tab);
134
135 private:
136 virtual void Observe(int type,
137 const NotificationSource& source,
138 const NotificationDetails& details) OVERRIDE;
139
140 content::NotificationRegistrar registrar_;
141
142 TabContentsWrapper* tab_contents_wrapper_;
143 OffscreenTabs offscreen_tabs_;
144 std::string extension_id_;
145
146 DISALLOW_COPY_AND_ASSIGN(ParentTab);
147 };
148
149 // This map keeps track of all tabs that are happy parents of offscreen tabs.
150 class OffscreenTabMap {
151 public:
152 OffscreenTabMap();
153 ~OffscreenTabMap();
154
155 // Returns true if this map tracks |parent_tab|.
156 bool ContainsTab(ParentTab* parent_tab);
157
158 // Gets an offscreen tab by ID.
159 bool GetOffscreenTab(const int offscreen_tab_id,
160 UIThreadExtensionFunction* function,
161 OffscreenTab** offscreen_tab,
162 std::string* error);
163
164 // Gets a parent tab from its contents for the given extension id.
165 // Returns NULL if no such tab exists.
166 ParentTab* GetParentTab(WebContents* parent_contents,
167 const std::string& extension_id);
168
169 // Creates a new offscreen tab and a mapping between the |parent_tab| and
170 // the offscreen tab. Takes ownership of |parent_tab|, if it does not already
171 // have a mapping for it.
172 const OffscreenTab& CreateOffscreenTab(ParentTab* parent_tab,
173 const GURL& url,
174 const int width,
175 const int height,
176 const std::string& extension_id);
177
178 // Removes the mapping between a parent tab and an offscreen tab.
179 // May cause the OffscreenTab object associated with the parent to be deleted.
180 bool RemoveOffscreenTab(const int offscreen_tab_id,
181 UIThreadExtensionFunction* function,
182 std::string* error);
183
184 // Removes and deletes |parent_tab| and all it's offscreen tabs.
185 void RemoveParentTab(ParentTab* parent_tab);
186
187 private:
188 typedef base::hash_map<int, linked_ptr<ParentTab> > TabMap;
189 TabMap map_;
190
191 DISALLOW_COPY_AND_ASSIGN(OffscreenTabMap);
192 };
193
194 static base::LazyInstance<OffscreenTabMap> g_offscreen_tab_map =
195 LAZY_INSTANCE_INITIALIZER;
196
197 // Gets the map of parent tabs to offscreen tabs.
198 OffscreenTabMap* GetMap() {
199 return &g_offscreen_tab_map.Get();
200 }
201
202 // Gets the WebContents of the tab that instantiated the extension API call.
203 // Returns NULL if there was an error.
204 // Note that you can't create offscreen tabs from background pages, since they
205 // don't have an associated WebContents. The lifetime of offscreen tabs is tied
206 // to their creating tab, so requiring visible tabs as the parent helps prevent
207 // offscreen tab leaking.
208 WebContents* GetCurrentWebContents(UIThreadExtensionFunction* function,
209 std::string* error) {
210 WebContents* web_contents =
211 function->dispatcher()->delegate()->GetAssociatedWebContents();
212 if (web_contents)
213 return web_contents;
214
215 *error = keys::kCurrentTabNotFound;
216 return NULL;
217 }
218
219 OffscreenTab::OffscreenTab() : parent_tab_(NULL) {}
220 OffscreenTab::~OffscreenTab() {}
221
222 void OffscreenTab::Init(const GURL& url,
223 const int width,
224 const int height,
225 Profile* profile,
226 ParentTab* parent_tab) {
227 // Create the offscreen tab.
228 WebContents* web_contents = WebContents::Create(
229 profile, NULL, MSG_ROUTING_NONE, NULL, NULL);
230 tab_contents_wrapper_.reset(new TabContentsWrapper(web_contents));
231
232 // Setting the size starts the renderer.
233 SetSize(width, height);
234 NavigateToURL(url);
235
236 parent_tab_ = parent_tab;
237
238 // Register for tab notifications.
239 registrar_.Add(
240 this,
241 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
242 content::Source<NavigationController>(&(web_contents->GetController())));
243 }
244
245 DictionaryValue* OffscreenTab::CreateValue() const {
246 DictionaryValue* result = new DictionaryValue();
247 result->SetInteger(
248 tabs_keys::kIdKey, ExtensionTabUtil::GetTabId(web_contents()));
249 result->SetString(tabs_keys::kUrlKey, web_contents()->GetURL().spec());
250 result->SetInteger(tabs_keys::kWidthKey,
251 web_contents()->GetView()->GetContainerSize().width());
252 result->SetInteger(tabs_keys::kHeightKey,
253 web_contents()->GetView()->GetContainerSize().height());
254 return result;
255 }
256
257 void OffscreenTab::Observe(int type,
258 const NotificationSource& source,
259 const NotificationDetails& details) {
260 CHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type);
261
262 DictionaryValue* changed_properties = new DictionaryValue();
263 changed_properties->SetString(
264 tabs_keys::kUrlKey, web_contents()->GetURL().spec());
265
266 ListValue args;
267 args.Append(Value::CreateIntegerValue(
268 ExtensionTabUtil::GetTabId(web_contents())));
269 args.Append(changed_properties);
270 args.Append(CreateValue());
271 std::string json_args;
272 base::JSONWriter::Write(&args, &json_args);
273
274 // The event router only dispatches the event to renderers listening for the
275 // event.
276 Profile* profile = parent_tab_->tab_contents()->profile();
277 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
278 events::kOnOffscreenTabUpdated, json_args, profile, GURL());
279 }
280
281 ParentTab::ParentTab() : tab_contents_wrapper_(NULL) {}
282 ParentTab::~ParentTab() {}
283
284 void ParentTab::Init(WebContents* web_contents,
285 const std::string& extension_id) {
286 CHECK(web_contents);
287
288 extension_id_ = extension_id;
289 tab_contents_wrapper_ =
290 TabContentsWrapper::GetCurrentWrapperForContents(web_contents);
291
292 CHECK(tab_contents_wrapper_);
293
294 // Register for tab notifications.
295 registrar_.Add(
296 this,
297 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
298 content::Source<NavigationController>(&(web_contents->GetController())));
299 registrar_.Add(
300 this,
301 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
302 content::Source<WebContents>(web_contents));
303 }
304
305 void ParentTab::AddOffscreenTab(OffscreenTab *offscreen_tab) {
306 offscreen_tabs_.push_back(offscreen_tab);
307 }
308
309 bool ParentTab::RemoveOffscreenTab(OffscreenTab *offscreen_tab) {
310 OffscreenTabs::iterator it_tab = std::find(
311 offscreen_tabs_.begin(), offscreen_tabs_.end(), offscreen_tab);
312 offscreen_tabs_.erase(it_tab);
313 return offscreen_tabs_.empty();
314 }
315
316 void ParentTab::Observe(int type,
317 const NotificationSource& source,
318 const NotificationDetails& details) {
319 CHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED ||
320 type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED);
321 GetMap()->RemoveParentTab(this);
322 }
323
324 OffscreenTabMap::OffscreenTabMap() {}
325 OffscreenTabMap::~OffscreenTabMap() {}
326
327 bool OffscreenTabMap::ContainsTab(ParentTab* parent_tab) {
328 return map_.find(parent_tab->GetID()) != map_.end();
329 }
330
331 bool OffscreenTabMap::GetOffscreenTab(const int offscreen_tab_id,
332 UIThreadExtensionFunction* function,
333 OffscreenTab** offscreen_tab,
334 std::string* error) {
335 // Ensure that the current tab is the parent of the offscreen tab.
336 WebContents* web_contents = GetCurrentWebContents(function, error);
337 if (!web_contents)
338 return false;
339
340 ParentTab* parent_tab = GetMap()->GetParentTab(
341 web_contents, function->extension_id());
342 if (parent_tab) {
343 const OffscreenTabs& tabs = parent_tab->offscreen_tabs();
344 for (OffscreenTabs::const_iterator i = tabs.begin(); i != tabs.end(); ++i) {
345 if ((*i)->GetID() == offscreen_tab_id) {
346 *offscreen_tab = *i;
347 return true;
348 }
349 }
350 }
351
352 *error = ExtensionErrorUtils::FormatErrorMessage(
353 keys::kOffscreenTabNotFoundError, base::IntToString(offscreen_tab_id));
354 return false;
355 }
356
357 ParentTab* OffscreenTabMap::GetParentTab(WebContents* parent_contents,
358 const std::string& extension_id) {
359 CHECK(parent_contents);
360
361 int parent_tab_id = ExtensionTabUtil::GetTabId(parent_contents);
362 if (map_.find(parent_tab_id) == map_.end())
363 return NULL;
364
365 return map_[parent_tab_id].get();
366 }
367
368 const OffscreenTab& OffscreenTabMap::CreateOffscreenTab(
369 ParentTab* parent_tab,
370 const GURL& url,
371 const int width,
372 const int height,
373 const std::string& ext_id) {
374 CHECK(parent_tab);
375
376 // Assume ownership of |parent_tab| if we haven't already.
377 if (!ContainsTab(parent_tab)) {
378 map_[ExtensionTabUtil::GetTabId(parent_tab->web_contents())].reset(
379 parent_tab);
380 }
381
382 OffscreenTab* offscreen_tab = new OffscreenTab();
383 offscreen_tab->Init(
384 url, width, height, parent_tab->tab_contents()->profile(), parent_tab);
385 parent_tab->AddOffscreenTab(offscreen_tab);
386
387 return *offscreen_tab;
388 }
389
390 bool OffscreenTabMap::RemoveOffscreenTab(
391 const int offscreen_tab_id,
392 UIThreadExtensionFunction* function,
393 std::string* error) {
394 OffscreenTab* offscreen_tab = NULL;
395 if (!GetOffscreenTab(offscreen_tab_id, function, &offscreen_tab, error))
396 return false;
397
398 // Tell the parent tab to remove the offscreen tab, and then remove the
399 // parent tab if there are no more children.
400 ParentTab* parent_tab = offscreen_tab->parent_tab();
401 if (parent_tab->RemoveOffscreenTab(offscreen_tab))
402 RemoveParentTab(parent_tab);
403
404 return true;
405 }
406
407 void OffscreenTabMap::RemoveParentTab(ParentTab* parent_tab) {
408 CHECK(parent_tab);
409 CHECK(ContainsTab(parent_tab));
410
411 map_.erase(parent_tab->GetID());
412 }
413
414 bool CopyModifiers(const DictionaryValue* js_event,
415 WebInputEvent* event) {
416 bool alt_key = false;
417 if (js_event->HasKey(keys::kEventAltKeyKey)) {
418 if (!js_event->GetBoolean(keys::kEventAltKeyKey, &alt_key))
419 return false;
420 }
421 if (alt_key)
422 event->modifiers |= WebInputEvent::AltKey;
423
424 bool ctrl_key = false;
425 if (js_event->HasKey(keys::kEventCtrlKeyKey)) {
426 if (!js_event->GetBoolean(keys::kEventCtrlKeyKey, &ctrl_key))
427 return false;
428 }
429 if (ctrl_key)
430 event->modifiers |= WebInputEvent::ControlKey;
431
432 bool meta_key = false;
433 if (js_event->HasKey(keys::kEventMetaKeyKey)) {
434 if (!js_event->GetBoolean(keys::kEventMetaKeyKey, &meta_key))
435 return false;
436 }
437 if (meta_key)
438 event->modifiers |= WebInputEvent::MetaKey;
439
440 bool shift_key;
441 if (js_event->HasKey(keys::kEventShiftKeyKey)) {
442 if (!js_event->GetBoolean(keys::kEventShiftKeyKey, &shift_key))
443 return false;
444 }
445 if (shift_key)
446 event->modifiers |= WebInputEvent::ShiftKey;
447 return true;
448 }
449
450 } // namespace
451
452 CreateOffscreenTabFunction::CreateOffscreenTabFunction() {}
453 CreateOffscreenTabFunction::~CreateOffscreenTabFunction() {}
454
455 bool CreateOffscreenTabFunction::RunImpl() {
456 DictionaryValue* create_props;
457 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &create_props));
458
459 std::string url_string;
460 EXTENSION_FUNCTION_VALIDATE(create_props->GetString(
461 tabs_keys::kUrlKey, &url_string));
462
463 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL(
464 url_string, GetExtension());
465 if (!url.is_valid()) {
466 error_ = ExtensionErrorUtils::FormatErrorMessage(
467 tabs_keys::kInvalidUrlError, url_string);
468 return false;
469 }
470
471 if (ExtensionTabUtil::IsCrashURL(url)) {
472 error_ = tabs_keys::kNoCrashBrowserError;
473 return false;
474 }
475
476 gfx::Rect window_bounds;
477 Browser* browser = GetCurrentBrowser();
478 if (!browser) {
479 error_ = tabs_keys::kNoCurrentWindowError;
480 return false;
481 }
482
483 WindowSizer::GetBrowserWindowBounds(
484 std::string(), gfx::Rect(), browser, &window_bounds);
485
486 int width = window_bounds.width();
487 if (create_props->HasKey(tabs_keys::kWidthKey))
488 EXTENSION_FUNCTION_VALIDATE(
489 create_props->GetInteger(tabs_keys::kWidthKey, &width));
490
491 int height = window_bounds.height();
492 if (create_props->HasKey(tabs_keys::kHeightKey))
493 EXTENSION_FUNCTION_VALIDATE(
494 create_props->GetInteger(tabs_keys::kHeightKey, &height));
495
496
497 // Add the offscreen tab to the map so we don't lose track of it.
498 WebContents* web_contents = GetCurrentWebContents(this, &error_);
499 if (!web_contents)
500 return false;
501
502 ParentTab* parent_tab = GetMap()->GetParentTab(web_contents, extension_id());
503 if (!parent_tab) {
504 // Ownership is passed to the OffscreenMap in CreateOffscreenTab.
505 parent_tab = new ParentTab();
506 parent_tab->Init(web_contents, extension_id());
507 }
508
509 const OffscreenTab& offscreen_tab = GetMap()->CreateOffscreenTab(
510 parent_tab, url, width, height, extension_id());
511
512 // TODO(alexbost): Maybe the callback is called too soon. It should probably
513 // be called once we have navigated to the url.
514 if (has_callback()) {
515 result_.reset(offscreen_tab.CreateValue());
516 SendResponse(true);
517 }
518
519 return true;
520 }
521
522 GetOffscreenTabFunction::GetOffscreenTabFunction() {}
523 GetOffscreenTabFunction::~GetOffscreenTabFunction() {}
524
525 bool GetOffscreenTabFunction::RunImpl() {
526 int offscreen_tab_id;
527 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id));
528
529 OffscreenTab* offscreen_tab = NULL;
530 if (!GetMap()->GetOffscreenTab(
531 offscreen_tab_id, this, &offscreen_tab, &error_)) {
532 error_ = ExtensionErrorUtils::FormatErrorMessage(
533 keys::kOffscreenTabNotFoundError, base::IntToString(offscreen_tab_id));
534 return false;
535 }
536
537 result_.reset(offscreen_tab->CreateValue());
538 return true;
539 }
540
541 GetAllOffscreenTabFunction::GetAllOffscreenTabFunction() {}
542 GetAllOffscreenTabFunction::~GetAllOffscreenTabFunction() {}
543
544 bool GetAllOffscreenTabFunction::RunImpl() {
545 WebContents* web_contents = GetCurrentWebContents(this, &error_);
546 if (!web_contents)
547 return NULL;
548
549 ParentTab* parent_tab = GetMap()->GetParentTab(web_contents, extension_id());
550 ListValue* tab_list = new ListValue();
551 if (parent_tab) {
552 for (OffscreenTabs::const_iterator i = parent_tab->offscreen_tabs().begin();
553 i != parent_tab->offscreen_tabs().end(); ++i)
554 tab_list->Append((*i)->CreateValue());
555 }
556
557 result_.reset(tab_list);
558 return true;
559 }
560
561 RemoveOffscreenTabFunction::RemoveOffscreenTabFunction() {}
562 RemoveOffscreenTabFunction::~RemoveOffscreenTabFunction() {}
563
564 bool RemoveOffscreenTabFunction::RunImpl() {
565 int offscreen_tab_id;
566 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id));
567
568 OffscreenTab* offscreen_tab = NULL;
569 if (!GetMap()->GetOffscreenTab(
570 offscreen_tab_id, this, &offscreen_tab, &error_))
571 return false;
572
573 if (!GetMap()->RemoveOffscreenTab(offscreen_tab_id, this, &error_))
574 return false;
575
576 return true;
577 }
578
579 SendKeyboardEventOffscreenTabFunction::
580 SendKeyboardEventOffscreenTabFunction() {}
581 SendKeyboardEventOffscreenTabFunction::
582 ~SendKeyboardEventOffscreenTabFunction() {}
583
584 bool SendKeyboardEventOffscreenTabFunction::RunImpl() {
585 int offscreen_tab_id;
586 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id));
587
588 OffscreenTab* offscreen_tab = NULL;
589 if (!GetMap()->GetOffscreenTab(
590 offscreen_tab_id, this, &offscreen_tab, &error_))
591 return false;
592
593 // JavaScript KeyboardEvent.
594 DictionaryValue* js_keyboard_event = NULL;
595 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &js_keyboard_event));
596
597 NativeWebKeyboardEvent keyboard_event;
598
599 std::string type;
600 if (js_keyboard_event->HasKey(keys::kEventTypeKey)) {
601 EXTENSION_FUNCTION_VALIDATE(
602 js_keyboard_event->GetString(keys::kEventTypeKey, &type));
603 } else {
604 error_ = keys::kInvalidKeyboardEventObjectError;
605 return false;
606 }
607
608 if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) {
609 keyboard_event.type = WebInputEvent::Char;
610 } else if (type.compare(keys::kKeyboardEventTypeValueKeydown) == 0) {
611 keyboard_event.type = WebInputEvent::KeyDown;
612 } else if (type.compare(keys::kKeyboardEventTypeValueKeyup) == 0) {
613 keyboard_event.type = WebInputEvent::KeyUp;
614 } else {
615 error_ = keys::kInvalidKeyboardEventObjectError;
616 return false;
617 }
618
619 int key_code;
620 if (js_keyboard_event->HasKey(keys::kKeyboardEventKeyCodeKey)) {
621 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event->
622 GetInteger(keys::kKeyboardEventKeyCodeKey, &key_code));
623 keyboard_event.nativeKeyCode = key_code;
624 keyboard_event.windowsKeyCode = key_code;
625 keyboard_event.setKeyIdentifierFromWindowsKeyCode();
626 }
627
628 // Keypress = type character
629 if (type.compare(keys::kKeyboardEventTypeValueKeypress) == 0) {
630 int char_code;
631 if (js_keyboard_event->HasKey(keys::kKeyboardEventCharCodeKey)) {
632 EXTENSION_FUNCTION_VALIDATE(js_keyboard_event->
633 GetInteger(keys::kKeyboardEventCharCodeKey, &char_code));
634 keyboard_event.text[0] = char_code;
635 keyboard_event.unmodifiedText[0] = char_code;
636 } else {
637 error_ = keys::kInvalidKeyboardEventObjectError;
638 return false;
639 }
640 }
641
642 EXTENSION_FUNCTION_VALIDATE(
643 CopyModifiers(js_keyboard_event, &keyboard_event));
644
645 // Forward the event.
646 offscreen_tab->web_contents()->GetRenderViewHost()->
647 ForwardKeyboardEvent(keyboard_event);
648
649 return true;
650 }
651
652 SendMouseEventOffscreenTabFunction::SendMouseEventOffscreenTabFunction() {}
653 SendMouseEventOffscreenTabFunction::~SendMouseEventOffscreenTabFunction() {}
654
655 bool SendMouseEventOffscreenTabFunction::RunImpl() {
656 int offscreen_tab_id;
657 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id));
658
659 OffscreenTab* offscreen_tab = NULL;
660 if (!GetMap()->GetOffscreenTab(
661 offscreen_tab_id, this, &offscreen_tab, &error_))
662 return false;
663
664 // JavaScript MouseEvent.
665 DictionaryValue* js_mouse_event = NULL;
666 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &js_mouse_event));
667
668 std::string type;
669 if (js_mouse_event->HasKey(keys::kEventTypeKey)) {
670 EXTENSION_FUNCTION_VALIDATE(
671 js_mouse_event->GetString(keys::kEventTypeKey, &type));
672 } else {
673 error_ = keys::kInvalidMouseEventObjectError;
674 return false;
675 }
676
677 if (type.compare(keys::kMouseEventTypeValueMousewheel) == 0) {
678 WebKit::WebMouseWheelEvent wheel_event;
679
680 wheel_event.type = WebInputEvent::MouseWheel;
681
682 if (js_mouse_event->HasKey(keys::kMouseEventWheelDeltaXKey) &&
683 js_mouse_event->HasKey(keys::kMouseEventWheelDeltaYKey)) {
684 int delta_x, delta_y;
685 EXTENSION_FUNCTION_VALIDATE(js_mouse_event->
686 GetInteger(keys::kMouseEventWheelDeltaXKey, &delta_x));
687 EXTENSION_FUNCTION_VALIDATE(js_mouse_event->
688 GetInteger(keys::kMouseEventWheelDeltaYKey, &delta_y));
689 wheel_event.deltaX = delta_x;
690 wheel_event.deltaY = delta_y;
691 } else {
692 error_ = keys::kInvalidMouseEventObjectError;
693 return false;
694 }
695
696 // Forward the event.
697 offscreen_tab->web_contents()->GetRenderViewHost()->
698 ForwardWheelEvent(wheel_event);
699 } else {
700 WebKit::WebMouseEvent mouse_event;
701
702 if (type.compare(keys::kMouseEventTypeValueMousedown) == 0 ||
703 type.compare(keys::kMouseEventTypeValueClick) == 0) {
704 mouse_event.type = WebInputEvent::MouseDown;
705 } else if (type.compare(keys::kMouseEventTypeValueMouseup) == 0) {
706 mouse_event.type = WebInputEvent::MouseUp;
707 } else if (type.compare(keys::kMouseEventTypeValueMousemove) == 0) {
708 mouse_event.type = WebInputEvent::MouseMove;
709 } else {
710 error_ = keys::kInvalidMouseEventObjectError;
711 return false;
712 }
713
714 int button;
715 if (js_mouse_event->HasKey(keys::kMouseEventButtonKey)) {
716 EXTENSION_FUNCTION_VALIDATE(
717 js_mouse_event->GetInteger(keys::kMouseEventButtonKey, &button));
718 } else {
719 error_ = keys::kInvalidMouseEventObjectError;
720 return false;
721 }
722
723 if (button == keys::kMouseEventButtonValueLeft) {
724 mouse_event.button = WebKit::WebMouseEvent::ButtonLeft;
725 } else if (button == keys::kMouseEventButtonValueMiddle) {
726 mouse_event.button = WebKit::WebMouseEvent::ButtonMiddle;
727 } else if (button == keys::kMouseEventButtonValueRight) {
728 mouse_event.button = WebKit::WebMouseEvent::ButtonRight;
729 } else {
730 error_ = keys::kInvalidMouseEventObjectError;
731 return false;
732 }
733
734 if (HasOptionalArgument(2) && HasOptionalArgument(3)) {
735 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &mouse_event.x));
736 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &mouse_event.y));
737 } else {
738 error_ = keys::kNoMouseCoordinatesError;
739 return false;
740 }
741
742 EXTENSION_FUNCTION_VALIDATE(CopyModifiers(js_mouse_event, &mouse_event));
743
744 mouse_event.clickCount = 1;
745
746 // Forward the event.
747 offscreen_tab->web_contents()->GetRenderViewHost()->
748 ForwardMouseEvent(mouse_event);
749
750 // If the event is a click,
751 // fire a mouseup event in addition to the mousedown.
752 if (type.compare(keys::kMouseEventTypeValueClick) == 0) {
753 mouse_event.type = WebInputEvent::MouseUp;
754 offscreen_tab->web_contents()->GetRenderViewHost()->
755 ForwardMouseEvent(mouse_event);
756 }
757 }
758
759 return true;
760 }
761
762 ToDataUrlOffscreenTabFunction::ToDataUrlOffscreenTabFunction() {}
763 ToDataUrlOffscreenTabFunction::~ToDataUrlOffscreenTabFunction() {}
764
765 bool ToDataUrlOffscreenTabFunction::GetTabToCapture(
766 WebContents** web_contents, TabContentsWrapper** wrapper) {
767 int offscreen_tab_id;
768 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id));
769
770 // TODO(alexbost): We want to optimize this function in order to get more
771 // image updates on the browser side. One improvement would be to implement
772 // another hash map in order to get offscreen tabs in O(1).
773 OffscreenTab* offscreen_tab = NULL;
774 if (!GetMap()->GetOffscreenTab(
775 offscreen_tab_id, this, &offscreen_tab, &error_))
776 return false;
777
778 *web_contents = offscreen_tab->web_contents();
779 *wrapper = offscreen_tab->tab_contents();
780 return true;
781 }
782
783 UpdateOffscreenTabFunction::UpdateOffscreenTabFunction() {}
784 UpdateOffscreenTabFunction::~UpdateOffscreenTabFunction() {}
785
786 bool UpdateOffscreenTabFunction::RunImpl() {
787 int offscreen_tab_id;
788 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &offscreen_tab_id));
789
790 OffscreenTab* offscreen_tab = NULL;
791 if (!GetMap()->GetOffscreenTab(
792 offscreen_tab_id, this, &offscreen_tab, &error_))
793 return false;
794
795 DictionaryValue* update_props;
796 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props));
797
798 web_contents_ = offscreen_tab->web_contents();
799 bool is_async = false;
800 if (!UpdateURLIfPresent(update_props, &is_async))
801 return false;
802
803 // Update the width and height, if specified.
804 if (update_props->HasKey(tabs_keys::kWidthKey) ||
805 update_props->HasKey(tabs_keys::kHeightKey)) {
806 int width;
807 if (update_props->HasKey(tabs_keys::kWidthKey))
808 EXTENSION_FUNCTION_VALIDATE(
809 update_props->GetInteger(tabs_keys::kWidthKey, &width));
810 else
811 web_contents_->GetView()->GetContainerSize().width();
812
813 int height;
814 if (update_props->HasKey(tabs_keys::kHeightKey))
815 EXTENSION_FUNCTION_VALIDATE(
816 update_props->GetInteger(tabs_keys::kHeightKey, &height));
817 else
818 web_contents_->GetView()->GetContainerSize().height();
819
820 offscreen_tab->SetSize(width, height);
821 }
822
823 // The response is sent from UpdateTabFunction::OnExecuteCodeFinish in the
824 // async case (when a "javascript": URL is sent to a tab).
825 if (!is_async)
826 SendResponse(true);
827
828 return true;
829 }
830
831 void UpdateOffscreenTabFunction::PopulateResult() {
832 // There's no result associated with this callback.
833 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698