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