chrome_extension_function.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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) 71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/window_controller.h" 81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/window_controller_list.h" 91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser.h" 111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h" 121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h" 131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/render_process_host.h" 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/render_view_host.h" 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/extension_function_dispatcher.h" 161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using content::RenderViewHost; 181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using content::WebContents; 191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 20010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() { 21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 23010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Profile* ChromeUIThreadExtensionFunction::GetProfile() const { 241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return Profile::FromBrowserContext(context_); 251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ChromeUIThreadExtensionFunction::CanOperateOnWindow( 281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const extensions::WindowController* window_controller) const { 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const extensions::Extension* extension = GetExtension(); 301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // |extension| is NULL for unit tests only. 311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (extension != NULL && !window_controller->IsVisibleToExtension(extension)) 321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return false; 331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (GetProfile() == window_controller->profile()) 351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return true; 361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (!include_incognito()) 381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return false; 391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return GetProfile()->HasOffTheRecordProfile() && 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) GetProfile()->GetOffTheRecordProfile() == window_controller->profile(); 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// TODO(stevenjb): Replace this with GetExtensionWindowController(). 45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { 461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // If the delegate has an associated browser, return it. 471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (dispatcher()) { 481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) extensions::WindowController* window_controller = 491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) dispatcher()->delegate()->GetExtensionWindowController(); 501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (window_controller) { 511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Browser* browser = window_controller->GetBrowser(); 521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (browser) 531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return browser; 541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Otherwise, try to default to a reasonable browser. If |include_incognito_| 581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // is true, we will also search browsers in the incognito version of this 591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // profile. Note that the profile may already be incognito, in which case 601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // we will search the incognito version only, regardless of the value of 611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // |include_incognito|. Look only for browsers on the active desktop as it is 621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // preferable to pretend no browser is open then to return a browser on 631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // another desktop. 641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (render_view_host_) { 651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Profile* profile = Profile::FromBrowserContext( 661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) render_view_host_->GetProcess()->GetBrowserContext()); 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Browser* browser = chrome::FindAnyBrowser( 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) profile, include_incognito_, chrome::GetActiveDesktop()); 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (browser) 701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return browser; 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // NOTE(rafaelw): This can return NULL in some circumstances. In particular, 741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // a background_page onload chrome.tabs api call can make it into here 751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // before the browser is sufficiently initialized to return here, or 761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // all of this profile's browser windows may have been closed. 771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // A similar situation may arise during shutdown. 781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // TODO(rafaelw): Delay creation of background_page until the browser 791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // is available. http://code.google.com/p/chromium/issues/detail?id=13284 801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return NULL; 811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)extensions::WindowController* 84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::GetExtensionWindowController() { 851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // If the delegate has an associated window controller, return it. 861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (dispatcher()) { 871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) extensions::WindowController* window_controller = 881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) dispatcher()->delegate()->GetExtensionWindowController(); 891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (window_controller) 901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return window_controller; 911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return extensions::WindowControllerList::GetInstance() 941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ->CurrentWindowForFunction(this); 951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 97010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)content::WebContents* 98010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::GetAssociatedWebContents() { 991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) content::WebContents* web_contents = 1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) UIThreadExtensionFunction::GetAssociatedWebContents(); 1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (web_contents) 1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return web_contents; 1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) Browser* browser = GetCurrentBrowser(); 1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (!browser) 1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return NULL; 1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return browser->tab_strip_model()->GetActiveWebContents(); 1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 110010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() { 111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() { 114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 115010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {} 1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ExtensionFunction::ResponseAction ChromeAsyncExtensionFunction::Run() { 119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) return RunAsync() ? RespondLater() : RespondNow(Error(error_)); 120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static 123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ChromeAsyncExtensionFunction::ValidationFailure( 124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) ChromeAsyncExtensionFunction* function) { 125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) return false; 126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() { 1295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu} 1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {} 132010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 133010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() { 134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_)); 135010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 136010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// static 138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ChromeSyncExtensionFunction::ValidationFailure( 139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) ChromeSyncExtensionFunction* function) { 140010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) return false; 141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 142