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) 2010-2011 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 "InjectedScriptManager.h"
     33 
     34 #if ENABLE(INSPECTOR)
     35 
     36 #include "InjectedScript.h"
     37 #include "InjectedScriptHost.h"
     38 #include "InjectedScriptSource.h"
     39 #include "InspectorValues.h"
     40 #include <wtf/PassOwnPtr.h>
     41 #include <wtf/StdLibExtras.h>
     42 
     43 using namespace std;
     44 
     45 namespace WebCore {
     46 
     47 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForPage()
     48 {
     49     return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWindow));
     50 }
     51 
     52 PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForWorker()
     53 {
     54     return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWorkerContext));
     55 }
     56 
     57 InjectedScriptManager::InjectedScriptManager(InspectedStateAccessCheck accessCheck)
     58     : m_nextInjectedScriptId(1)
     59     , m_injectedScriptHost(InjectedScriptHost::create())
     60     , m_inspectedStateAccessCheck(accessCheck)
     61 {
     62 }
     63 
     64 InjectedScriptManager::~InjectedScriptManager()
     65 {
     66 }
     67 
     68 void InjectedScriptManager::disconnect()
     69 {
     70     m_injectedScriptHost->disconnect();
     71     m_injectedScriptHost.clear();
     72 }
     73 
     74 InjectedScriptHost* InjectedScriptManager::injectedScriptHost()
     75 {
     76     return m_injectedScriptHost.get();
     77 }
     78 
     79 InjectedScript InjectedScriptManager::injectedScriptForId(long id)
     80 {
     81     return m_idToInjectedScript.get(id);
     82 }
     83 
     84 InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId)
     85 {
     86     RefPtr<InspectorValue> parsedObjectId = InspectorValue::parseJSON(objectId);
     87     if (parsedObjectId && parsedObjectId->type() == InspectorValue::TypeObject) {
     88         long injectedScriptId = 0;
     89         bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId);
     90         if (success)
     91             return injectedScriptForId(injectedScriptId);
     92     }
     93     return InjectedScript();
     94 }
     95 
     96 void InjectedScriptManager::discardInjectedScripts()
     97 {
     98     IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end();
     99     for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != end; ++it)
    100         discardInjectedScript(it->second.scriptState());
    101     m_idToInjectedScript.clear();
    102 }
    103 
    104 bool InjectedScriptManager::canAccessInspectedWorkerContext(ScriptState*)
    105 {
    106     return true;
    107 }
    108 
    109 void InjectedScriptManager::releaseObjectGroup(const String& objectGroup)
    110 {
    111     for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != m_idToInjectedScript.end(); ++it)
    112         it->second.releaseObjectGroup(objectGroup);
    113 }
    114 
    115 String InjectedScriptManager::injectedScriptSource()
    116 {
    117     return String(reinterpret_cast<const char*>(InjectedScriptSource_js), sizeof(InjectedScriptSource_js));
    118 }
    119 
    120 pair<long, ScriptObject> InjectedScriptManager::injectScript(const String& source, ScriptState* scriptState)
    121 {
    122     long id = m_nextInjectedScriptId++;
    123     return std::make_pair(id, createInjectedScript(source, scriptState, id));
    124 }
    125 
    126 } // namespace WebCore
    127 
    128 #endif // ENABLE(INSPECTOR)
    129