11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file. 41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/chrome_extension_function.h" 61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/extensions/chrome_extension_function_details.h" 81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/window_controller.h" 91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/window_controller_list.h" 101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser.h" 121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h" 131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h" 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/render_process_host.h" 151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/render_view_host.h" 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/extension_function_dispatcher.h" 171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using content::RenderViewHost; 191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using content::WebContents; 201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() { 22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Profile* ChromeUIThreadExtensionFunction::GetProfile() const { 251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return Profile::FromBrowserContext(context_); 261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ChromeUIThreadExtensionFunction::CanOperateOnWindow( 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const extensions::WindowController* window_controller) const { 305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) // |extension()| is NULL for unit tests only. 315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (extension() != NULL && 325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) !window_controller->IsVisibleToExtension(extension())) { 331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return false; 345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (GetProfile() == window_controller->profile()) 371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return true; 381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (!include_incognito()) 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return false; 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return GetProfile()->HasOffTheRecordProfile() && 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) GetProfile()->GetOffTheRecordProfile() == window_controller->profile(); 441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// TODO(stevenjb): Replace this with GetExtensionWindowController(). 47010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { 481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // If the delegate has an associated browser, return it. 491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (dispatcher()) { 501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) extensions::WindowController* window_controller = 511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) dispatcher()->delegate()->GetExtensionWindowController(); 521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (window_controller) { 531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Browser* browser = window_controller->GetBrowser(); 541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (browser) 551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return browser; 561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Otherwise, try to default to a reasonable browser. If |include_incognito_| 601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // is true, we will also search browsers in the incognito version of this 611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // profile. Note that the profile may already be incognito, in which case 621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // we will search the incognito version only, regardless of the value of 631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // |include_incognito|. Look only for browsers on the active desktop as it is 641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // preferable to pretend no browser is open then to return a browser on 651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // another desktop. 661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (render_view_host_) { 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Profile* profile = Profile::FromBrowserContext( 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) render_view_host_->GetProcess()->GetBrowserContext()); 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Browser* browser = chrome::FindAnyBrowser( 701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) profile, include_incognito_, chrome::GetActiveDesktop()); 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (browser) 721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return browser; 731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // NOTE(rafaelw): This can return NULL in some circumstances. In particular, 761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // a background_page onload chrome.tabs api call can make it into here 771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // before the browser is sufficiently initialized to return here, or 781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // all of this profile's browser windows may have been closed. 791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // A similar situation may arise during shutdown. 801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // TODO(rafaelw): Delay creation of background_page until the browser 811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // is available. http://code.google.com/p/chromium/issues/detail?id=13284 821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return NULL; 831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)extensions::WindowController* 86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::GetExtensionWindowController() { 871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // If the delegate has an associated window controller, return it. 881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (dispatcher()) { 891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) extensions::WindowController* window_controller = 901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) dispatcher()->delegate()->GetExtensionWindowController(); 911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (window_controller) 921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return window_controller; 931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return extensions::WindowControllerList::GetInstance() 961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ->CurrentWindowForFunction(ChromeExtensionFunctionDetails(this)); 971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 99010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)content::WebContents* 100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::GetAssociatedWebContents() { 1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) content::WebContents* web_contents = 1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) UIThreadExtensionFunction::GetAssociatedWebContents(); 1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (web_contents) 1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return web_contents; 1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Browser* browser = GetCurrentBrowser(); 1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (!browser) 1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return NULL; 1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return browser->tab_strip_model()->GetActiveWebContents(); 1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() { 113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 115010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() { 116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 117010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {} 1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ExtensionFunction::ResponseAction ChromeAsyncExtensionFunction::Run() { 121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) return RunAsync() ? RespondLater() : RespondNow(Error(error_)); 122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static 125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ChromeAsyncExtensionFunction::ValidationFailure( 126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) ChromeAsyncExtensionFunction* function) { 127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) return false; 128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() { 1315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu} 1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {} 134010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 135010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() { 136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_)); 137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static 140010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ChromeSyncExtensionFunction::ValidationFailure( 141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) ChromeSyncExtensionFunction* function) { 142010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) return false; 143010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 144