Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Matt Lilek <webkit (at) mattlilek.com>
      4  * Copyright (C) 2012 Google Inc. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1.  Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  * 2.  Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     16  *     its contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/inspector/InjectedScriptManager.h"
     33 
     34 #include "bindings/core/v8/ScriptValue.h"
     35 #include "core/inspector/InjectedScript.h"
     36 #include "core/inspector/InjectedScriptHost.h"
     37 #include "core/inspector/JSONParser.h"
     38 #include "platform/JSONValues.h"
     39 #include "public/platform/Platform.h"
     40 #include "public/platform/WebData.h"
     41 #include "wtf/PassOwnPtr.h"
     42 
     43 namespace blink {
     44 
     45 #if ENABLE(OILPAN)
     46 InjectedScriptManager::CallbackData::~CallbackData()
     47 {
     48     WrapperPersistent<InjectedScriptHost>::destroy(hostPtr);
     49 }
     50 #endif
     51 
     52 PassOwnPtrWillBeRawPtr<InjectedScriptManager> InjectedScriptManager::createForPage()
     53 {
     54     return adoptPtrWillBeNoop(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWindow));
     55 }
     56 
     57 PassOwnPtrWillBeRawPtr<InjectedScriptManager> InjectedScriptManager::createForWorker()
     58 {
     59     return adoptPtrWillBeNoop(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWorkerGlobalScope));
     60 }
     61 
     62 InjectedScriptManager::InjectedScriptManager(InspectedStateAccessCheck accessCheck)
     63     : m_nextInjectedScriptId(1)
     64     , m_injectedScriptHost(InjectedScriptHost::create())
     65     , m_inspectedStateAccessCheck(accessCheck)
     66 {
     67 }
     68 
     69 InjectedScriptManager::~InjectedScriptManager()
     70 {
     71 }
     72 
     73 void InjectedScriptManager::trace(Visitor* visitor)
     74 {
     75     visitor->trace(m_injectedScriptHost);
     76 }
     77 
     78 void InjectedScriptManager::disconnect()
     79 {
     80     m_injectedScriptHost->disconnect();
     81     m_injectedScriptHost.clear();
     82 }
     83 
     84 InjectedScriptHost* InjectedScriptManager::injectedScriptHost()
     85 {
     86     return m_injectedScriptHost.get();
     87 }
     88 
     89 InjectedScript InjectedScriptManager::injectedScriptForId(int id)
     90 {
     91     IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id);
     92     if (it != m_idToInjectedScript.end())
     93         return it->value;
     94     for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
     95         if (it->value == id)
     96             return injectedScriptFor(it->key.get());
     97     }
     98     return InjectedScript();
     99 }
    100 
    101 int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState)
    102 {
    103     ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState);
    104     if (it != m_scriptStateToId.end())
    105         return it->value;
    106     int id = m_nextInjectedScriptId++;
    107     m_scriptStateToId.set(scriptState, id);
    108     return id;
    109 }
    110 
    111 InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId)
    112 {
    113     RefPtr<JSONValue> parsedObjectId = parseJSON(objectId);
    114     if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) {
    115         long injectedScriptId = 0;
    116         bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId);
    117         if (success)
    118             return m_idToInjectedScript.get(injectedScriptId);
    119     }
    120     return InjectedScript();
    121 }
    122 
    123 void InjectedScriptManager::discardInjectedScripts()
    124 {
    125     m_idToInjectedScript.clear();
    126     m_scriptStateToId.clear();
    127 }
    128 
    129 void InjectedScriptManager::discardInjectedScriptsFor(LocalDOMWindow* window)
    130 {
    131     if (m_scriptStateToId.isEmpty())
    132         return;
    133 
    134     Vector<long> idsToRemove;
    135     IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end();
    136     for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != end; ++it) {
    137         ScriptState* scriptState = it->value.scriptState();
    138         if (window != scriptState->domWindow())
    139             continue;
    140         m_scriptStateToId.remove(scriptState);
    141         idsToRemove.append(it->key);
    142     }
    143     m_idToInjectedScript.removeAll(idsToRemove);
    144 
    145     // Now remove script states that have id but no injected script.
    146     Vector<ScriptState*> scriptStatesToRemove;
    147     for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
    148         ScriptState* scriptState = it->key.get();
    149         if (window == scriptState->domWindow())
    150             scriptStatesToRemove.append(scriptState);
    151     }
    152     m_scriptStateToId.removeAll(scriptStatesToRemove);
    153 }
    154 
    155 bool InjectedScriptManager::canAccessInspectedWorkerGlobalScope(ScriptState*)
    156 {
    157     return true;
    158 }
    159 
    160 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup)
    161 {
    162     Vector<int> keys;
    163     keys.appendRange(m_idToInjectedScript.keys().begin(), m_idToInjectedScript.keys().end());
    164     for (Vector<int>::iterator k = keys.begin(); k != keys.end(); ++k) {
    165         IdToInjectedScriptMap::iterator s = m_idToInjectedScript.find(*k);
    166         if (s != m_idToInjectedScript.end())
    167             s->value.releaseObjectGroup(objectGroup); // m_idToInjectedScript may change here.
    168     }
    169 }
    170 
    171 String InjectedScriptManager::injectedScriptSource()
    172 {
    173     const blink::WebData& injectedScriptSourceResource = blink::Platform::current()->loadResource("InjectedScriptSource.js");
    174     return String(injectedScriptSourceResource.data(), injectedScriptSourceResource.size());
    175 }
    176 
    177 InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedScriptState)
    178 {
    179     ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState);
    180     if (it != m_scriptStateToId.end()) {
    181         IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->value);
    182         if (it1 != m_idToInjectedScript.end())
    183             return it1->value;
    184     }
    185 
    186     if (!m_inspectedStateAccessCheck(inspectedScriptState))
    187         return InjectedScript();
    188 
    189     int id = injectedScriptIdFor(inspectedScriptState);
    190     ScriptValue injectedScriptValue = createInjectedScript(injectedScriptSource(), inspectedScriptState, id);
    191     InjectedScript result(injectedScriptValue, m_inspectedStateAccessCheck);
    192     m_idToInjectedScript.set(id, result);
    193     return result;
    194 }
    195 
    196 } // namespace blink
    197 
    198